JM_KA/assets/Script/Manager.ts
2022-09-04 13:13:06 +08:00

248 lines
7.3 KiB
TypeScript

/*
node version_generator.js -v 1.0.0 -u https://jianmiau.tk/Resources/App/JMKA/update/remote-assets/ -s build/jsb-default/remote-assets -d remote-assets
*/
import { CoroutineV2 } from "./Engine/CatanEngine/CoroutineV2/CoroutineV2";
import { System_Eevent } from "./Engine/CatanEngine/CSharp/System/System_Eevent";
import LocalStorageData from "./Engine/Data/LocalStorageData";
import { Enum_HUDM } from "./HUD/Enum_HUDM";
import HUDM from "./HUD/HUDM";
import NativeClass from "./NativeClass";
import UpdatePanel from "./UpdatePanel";
import BusinessTypeSetting from "./_BusinessTypeSetting/BusinessTypeSetting";
const { ccclass, property } = cc._decorator;
@ccclass
export default class Manager extends cc.Component {
//#region 外調參數
@property({ type: cc.WebView })
public webview: cc.WebView = null;
@property({ type: cc.Node })
public BG: cc.Node = null;
@property({ type: cc.Node })
public UpdatePanel: cc.Node = null;
@property({ type: cc.Label })
public Version: cc.Label = null;
//#endregion
//#region Lifecycle
protected onLoad(): void {
CoroutineV2.Single(this._init()).Start();
}
private *_init(): IterableIterator<any> {
new LocalStorageData();
let isNewBundle: boolean = yield* this.CheckBundleVersion();
console.log(`BUNDLE VERSION: ${LocalStorageData.Instance.BundleVersion}`);
console.log(`COMPILE VERSION: ${BusinessTypeSetting.COMPILE_VERSION}`);
this.Version.string = `Ver ${BusinessTypeSetting.COMPILE_VERSION}`;
BusinessTypeSetting.UsePatch = `https://jianmiau.tk/Resources/App/JMKA/update/remote-assets/${BusinessTypeSetting.MajorVersion}.${BusinessTypeSetting.MinorVersion}`;
cc.debug.setDisplayStats(false);
new NativeClass(this.webview);
if (cc.sys.isNative && isNewBundle) {
new HUDM(this.UpdatePanel.getComponentInChildren(UpdatePanel), isNewBundle);
let needUpdateData: Enum_HUDM.NeedUpdateDataObj = yield* HUDM.Instance.CheckUpdate();
if (needUpdateData.IsNeedUpdate) {
this.UpdatePanel.active = true;
return;
} else {
this.UpdatePanel.active = false;
}
}
let self: this = this;
// this._text_to_Speech = new Text_to_Speech();
let href: string = window.location.href;
// let url: string = `http://220.134.195.1/public/bonus_casino/html5/jianmiau/Test/?host=${href}&v=${Date.now()}`;
// let url: string = `https://karolchang.github.io/jm-expense-vue-ts/?host=${href}&ignore=${Date.now()}`;
// let url: string = `http://karol.jianmiau.cf/jm-expense-vue-ts/?v=${Date.now()}`;
let url: string = `https://jm-expense-2022.firebaseapp.com/login?host=${href}&v=${Date.now()}`;
this.webview.url = url;
this.webview.node.active = true;
cc.view.setResizeCallback(this._resize.bind(this));
this._resize();
// Set EventListener
window.addEventListener("message", function (e: MessageEvent<any>): void {
let data: any = e.data;
let method: string = data.method;
let value: any = data.value;
if (method) {
self.Birdge(method, ...value);
}
}, false);
if (!window["Bridge"]) {
window["Bridge"] = function (method: string = "", value: string = ""): void {
self.Birdge(method, value);
};
}
let scheme: string = "jmka";
this.webview.setJavascriptInterfaceScheme(scheme);
this.webview.setOnJSCallback((sender: any, url: any) => {
let content: string = decodeURI(url.split(`${scheme}://?data=`)[1]);
try {
let data: JSON = JSON.parse(content);
let method: any = data["method"];
let value: any = data["value"];
if (method) {
self.Birdge(method, ...value);
}
} catch (error) {
console.error(error);
}
});
}
/** 跨版本溝通
* @example
* CallParent('Speak', '我愛豬涵')
*/
public Birdge(method: string, value: string = ""): void {
let self: this = this;
if (method && self[method]) {
if (value) {
self[method](value);
return;
} else if (self[method]) {
self[method]();
return;
}
}
console.log(`not function: ${method}, value: ${value}`);
}
public CloseBG(): void {
this.BG.destroy();
}
public Log(msg: string): void {
console.log(msg);
}
public Speak(msg: string): void {
// this._text_to_Speech.speak(msg);
NativeClass.Instance.TTS_Play(msg);
}
public onLoadOK(): void {
CoroutineV2.Single(this.GetFCMToken()).Start();
}
public *GetFCMToken(): IterableIterator<any> {
const FCMToken: string = NativeClass.Instance.GetFCMToken();
if (cc.sys.os === cc.sys.OS_IOS && !FCMToken) {
yield CoroutineV2.WaitTime(1);
yield this.GetFCMToken();
return;
}
console.log(`FCMToken ${FCMToken}`);
NativeClass.Instance.CocosBridge(System_Eevent.SetFCMToken, `"${FCMToken}"`);
}
public Alert(msg: string): void {
alert(msg);
}
private _resize(): void {
let Canvas: cc.Canvas = cc.Canvas.instance;
let rect: DOMRect = cc.game.canvas.getBoundingClientRect();
/** 判断是否是横屏 */
let landscape: boolean = false;
if (rect.width > rect.height) {
landscape = true;
}
// 根据横竖屏调整节点的位置适配等
let frameSize: cc.Size = cc.view.getFrameSize();
if (landscape) {
// 横屏
cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
if (frameSize.height > frameSize.width) {
cc.view.setFrameSize(frameSize.height, frameSize.width);
}
Canvas.designResolution = cc.size(1920, 1080);
} else {
// 竖屏
cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
if (frameSize.width > frameSize.height) {
cc.view.setFrameSize(frameSize.height, frameSize.width);
}
Canvas.designResolution = cc.size(1080, 1920);
}
}
/** 判斷更改Bundle版號.清除BUNDLE記錄 */
public *CheckBundleVersion(): IterableIterator<any> {
let isNewBundle: boolean = false;
let remote_version: string = "0";
let versionLoadEnd: boolean = false;
let fileName: string = "BundleVersion";
let fileFormat: string = ".txt";
let fileVersion: string = `?v=${Date.now()}`;
let fileUrl: string = `https://jianmiau.tk/Resources/App/JMKA/update/remote-assets/${fileName}${fileFormat}${fileVersion}`;
cc.assetManager.loadRemote(fileUrl, (err: Error, res: cc.TextAsset) => {
if (!err) {
remote_version = res.text;
versionLoadEnd = true;
console.log(`${fileName}.txt loaded`);
} else {
console.error(`[Error] ${fileName}載入失敗`);
}
});
while (!versionLoadEnd) {
yield null;
}
let oldBundleVersion: string = LocalStorageData.Instance.BundleVersion || "0.0.0";
let newBundleVersion: string = remote_version;
if (oldBundleVersion) {
let IsUpdate: boolean = this.VersionCompareHandle(oldBundleVersion, newBundleVersion) < 0 ? true : false;
if (IsUpdate) {
console.warn(`change bundle version. ${oldBundleVersion} -> ${newBundleVersion}`);
isNewBundle = true;
}
}
LocalStorageData.Instance.BundleVersion = newBundleVersion;
return isNewBundle;
}
public VersionCompareHandle(versionA: string, versionB: string): number {
// console.log("Ver A " + versionA + "VerB " + versionB);
let vA: string[] = versionA.split(".");
let vB: string[] = versionB.split(".");
// 長度不相等,則進行更新
if (vA.length !== vB.length) {
return -1;
}
for (let i: number = 0; i < vA.length; ++i) {
let a: number = +vA[i];
let b: number = +vB[i] || 0;
if (a === b) {
// 數字相同,則跳過
continue;
} else {
// 數字不同,則進行更新
return -1;
}
}
// 長度相等且數字相等,則不更新
return 0;
}
//#endregion
}