From 468b47e3394ffe9d65deac7d1d589f2103919f03 Mon Sep 17 00:00:00 2001 From: koreanlancer Date: Sun, 15 Nov 2020 23:04:50 +0700 Subject: [PATCH] release version 1.0.1 --- .../framework/entity/X9AutoReduceStyle.js | 71 +++++++----- runtime_module/framework/entity/X9Helper.js | 23 ++-- .../framework/entity/X9LocalData.js | 8 +- .../framework/entity/X9NetworkCommand.js | 44 -------- .../framework/entity/X9NetworkData.js | 103 ++++++++++++++++++ .../framework/entity/X9SaveAndShareData.js | 5 +- runtime_module/hotupdate/HotupdateCommand.js | 7 +- .../hotupdate/HotupdateComponent.js | 48 ++++---- 8 files changed, 195 insertions(+), 114 deletions(-) delete mode 100644 runtime_module/framework/entity/X9NetworkCommand.js create mode 100644 runtime_module/framework/entity/X9NetworkData.js diff --git a/runtime_module/framework/entity/X9AutoReduceStyle.js b/runtime_module/framework/entity/X9AutoReduceStyle.js index d5b8e06..bfcaeed 100644 --- a/runtime_module/framework/entity/X9AutoReduceStyle.js +++ b/runtime_module/framework/entity/X9AutoReduceStyle.js @@ -34,8 +34,7 @@ var X9AutoReduceStyle = cc.Class({ mixins: [X9OrientedCommand], statics: { - TYPE_ARG: "__type__", - CLASS_ARG: "__class__", + STATE_ID_ARG: "__state_id__" }, ctor(){ @@ -75,12 +74,17 @@ var X9AutoReduceStyle = cc.Class({ getStateType(){ let state = this.getState(); - return state[X9OrientedCommand.TYPE_ARG] ? state[X9OrientedCommand.TYPE_ARG] : 'default'; + return state && state[X9OrientedCommand.TYPE_ARG] ? state[X9OrientedCommand.TYPE_ARG] : 'default'; + }, + + getStateId(){ + let state = this.getState(); + return state && state[X9AutoReduceStyle.STATE_ID_ARG] ? state[X9AutoReduceStyle.STATE_ID_ARG] : 1; }, getError(){ let state = this.getState(); - return state[X9OrientedCommand.ERROR_ARG] ? state[X9OrientedCommand.ERROR_ARG] : null; + return state && state[X9OrientedCommand.ERROR_ARG] ? state[X9OrientedCommand.ERROR_ARG] : null; }, export(){ @@ -125,6 +129,13 @@ var X9AutoReduceStyle = cc.Class({ }else{ newState = Object.assign(Object.create(null), state, mergedPayload); } + // + // STATE_ID_ARG + let stateId = this.getStateId(); + stateId++; + stateId = (stateId == Number.MAX_SAFE_INTEGER) ? 1 : stateId; + newState[X9AutoReduceStyle.STATE_ID_ARG] = stateId; + // }else{ newState = state; } @@ -135,7 +146,7 @@ var X9AutoReduceStyle = cc.Class({ return newState; } }else{ - throw new Error(this.__className + "::reduce(state, payload) : " +" Chỉ gọi trong Subclass của X9Com") + throw new Error(this.__className + "::reduce(state, payload) : " +" Chỉ gọi trong Subclass của X9Com"); } return state; }, @@ -152,9 +163,9 @@ var X9AutoReduceStyle = cc.Class({ let stateType = newState[X9OrientedCommand.TYPE_ARG]; this._exportData = this.onUpdateState(this._prepareUpdatingState(newState)); if( this._asyncViewCmds && this._asyncViewCmds.indexOf(stateType) == -1){ - this._excuteViewTasks(stateType, ()=>{ + this._excuteViewTasks(stateType, (stateId)=>{ // ket thuc xu ly view - this.clearThenEndUp(); + this.clearThenEndUp(stateId); CC_DEBUG && cc.log('End Task View ' + (this.__className ? this.__className : this.constructor.name)); }); } @@ -200,17 +211,27 @@ var X9AutoReduceStyle = cc.Class({ } }, + + clearThenEndUp(stateId){ + let currentStateId = this.getStateId(); + let state = this.getState(); + if(stateId == currentStateId){ + this._clearPrivateArgs(state); + } + }, + //---------------------------------- // PRIVATE FUNCTION //---------------------------------- //----------private command -------------------- - clearThenEndUp(){ - let state = this.getState(); - delete state[X9OrientedCommand.TYPE_ARG]; - delete state[X9OrientedCommand.CLASS_ARG]; - delete state[X9OrientedCommand.ERROR_ARG]; + _clearPrivateArgs(state){ + if(state){ + delete state[X9OrientedCommand.TYPE_ARG]; + delete state[X9OrientedCommand.CLASS_ARG]; + delete state[X9OrientedCommand.ERROR_ARG]; + } }, /** @@ -266,15 +287,17 @@ var X9AutoReduceStyle = cc.Class({ } asyncTasks.push(endTask); asyncTasks.reduce( (accumulatorPromise, nextID) => { - return accumulatorPromise.then(() => { + return accumulatorPromise.then(() => { return ((x9CompName)=>{ const x9Comp = (x9CompName === this) ? x9CompName : ( (typeof(x9CompName) !== 'function') ? this.use(x9CompName) : null ); + const x9CompStateId = x9Comp ? x9Comp.getStateId() : null; return new Promise((resolve, reject) => { if( x9Comp && x9Comp.onUpdateView && (x9Comp.getError() == null) ){ // Có lỗi không vào view nữa. x9Comp.onUpdateView(resolve); + x9Comp.clearThenEndUp(x9CompStateId); }else{ - x9Comp.clearThenEndUp(); + // endTask resolve(); } }); @@ -282,10 +305,13 @@ var X9AutoReduceStyle = cc.Class({ }); }, Promise.resolve()); }else{ + const lastStateId = this.getStateId(); if(this.getError() == null){ - this.onUpdateView(endTask); + this.onUpdateView(()=>{ + endTask(lastStateId); + }); }else{ - endTask(); + endTask(lastStateId); } } // @@ -346,19 +372,6 @@ var X9AutoReduceStyle = cc.Class({ this._asyncViewTasks[cmdType] = taskView; }, - - - // methodThatReturnsAPromise(x9CompName) { - // let x9Comp = this.use(x9CompName); - // return new Promise((resolve, reject) => { - // if(x9Comp.onUpdateView){ - // x9Comp.onUpdateView(resolve); - // }else{ - // resolve(); - // } - // }); - // }, - //--------------------------------------------\ /** diff --git a/runtime_module/framework/entity/X9Helper.js b/runtime_module/framework/entity/X9Helper.js index ecd0c02..f51f8ac 100644 --- a/runtime_module/framework/entity/X9Helper.js +++ b/runtime_module/framework/entity/X9Helper.js @@ -56,6 +56,10 @@ var Helper = { return Dispatcher.instance(); }, + decodeBase64URL(){ + + }, + editCCClass: function(manualOptionHandler, completedDefineClassHandler){ if(!this.__edited){ var newCCClass = (function(superCCClass){ @@ -90,7 +94,7 @@ Helper.editCCClass(function(options){ let className = cc.js.getClassName(cls); if(!className) return; if(options && options.extends && options.extends !== cc.Class && (typeof options.extends !== 'object') ){ - let catalogName = ''; + let catalogName; let superClass = options.extends; let X9Command = require("X9Cmd"); let X9Component = require("X9Com"); @@ -100,14 +104,15 @@ Helper.editCCClass(function(options){ }else if(cc.js.isChildClassOf(superClass, X9Command)){ catalogName = 'X9 Command'; } - // Sửa lại nội dung của menu theo custome catalog. - let menuPath = catalogName + '/' + className; - cc._componentMenuItems.forEach(item => { - if(item.component === cls){ - item.menuPath = menuPath; - } - }); - + if(catalogName){ + // Sửa lại nội dung của menu theo custome catalog. + let menuPath = catalogName + '/' + className; + cc._componentMenuItems.forEach(item => { + if(item.component === cls){ + item.menuPath = menuPath; + } + }); + } } } }); diff --git a/runtime_module/framework/entity/X9LocalData.js b/runtime_module/framework/entity/X9LocalData.js index 2c1d867..2c5329d 100644 --- a/runtime_module/framework/entity/X9LocalData.js +++ b/runtime_module/framework/entity/X9LocalData.js @@ -1,5 +1,5 @@ -const SAVEANDSHARE_URI = 'x9data://'; +const DATA_URI = 'x9data://'; const DATA_NODE_NAME = 'share-data'; /** * Tính năng save và share data cho X9Cmd và X9Com. @@ -14,6 +14,7 @@ const X9LocalData = cc.Class({ statics:{ SEPARATE : '::', }, + /** * Lấy share data trước. Nếu không có sẽ lấy vào từ localStorage. @@ -30,7 +31,7 @@ const X9LocalData = cc.Class({ if(uuid && data){ data = data[uuid] if(!data){ - let classDataId = dataId.replace(SAVEANDSHARE_URI,''); + let classDataId = dataId.replace(DATA_URI,''); return (classDataId != id) ? this.getData(classDataId) : null; } }else if(data && typeof data === 'object'){ @@ -49,6 +50,7 @@ const X9LocalData = cc.Class({ _splitDataIdToArray(id){ let uuid = null; let dataId = this._validateId(id); + dataId = id ? dataId : (dataId + X9LocalData.SEPARATE + this.uuid) ; if(dataId.indexOf(X9LocalData.SEPARATE) !== -1){ let dataIdArr = dataId.split(X9LocalData.SEPARATE); dataId = dataIdArr[0]; @@ -60,7 +62,7 @@ const X9LocalData = cc.Class({ _validateId(id){ // Do not encript uri by Hash let className = this.__className || cc.js.getClassName(this.constructor); - return SAVEANDSHARE_URI + (id ? id : (className + X9LocalData.SEPARATE + this.uuid)); + return DATA_URI + (id ? id : className); }, _validateData(data, keyPass){ diff --git a/runtime_module/framework/entity/X9NetworkCommand.js b/runtime_module/framework/entity/X9NetworkCommand.js deleted file mode 100644 index f7cbe49..0000000 --- a/runtime_module/framework/entity/X9NetworkCommand.js +++ /dev/null @@ -1,44 +0,0 @@ - -const X9Command = require('X9Cmd'); - -var X9NetworkCommand = cc.Class({ - - /** - * - * @param {*} url - * @param {*} data - */ - request(url, data, token) { - // if(this.serviceURL && (this.serviceURL.length > 4)){ - return new Promise((resolve, reject) => { - let xhr = new XMLHttpRequest(); - xhr.onreadystatechange = ()=>{ - // this._httpStatus = xhr.status; - if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) { - var response = xhr.responseText; - resolve(response); - }else{ - reject(); - } - // this._httpStatus = -1; - }; - xhr.onerror = ()=>{ - reject(); - // this._httpStatus = -1; - } ; - xhr.open("POST", url, true); - // xhr.open("GET", url, true); - xhr.setRequestHeader("Content-type", "application/json"); - if(token){ - xhr.setRequestHeader('Authorization', 'Bearer ' + token); - } - // Nếu muốn bổ sung Header thì kế thừa class này và viết lại. - xhr.send(JSON.stringify(data)); - // this._httpStatus = 0; - }); - // } - // - // return new Promise((resolve) => {resolve(data)}); - }, - -}) \ No newline at end of file diff --git a/runtime_module/framework/entity/X9NetworkData.js b/runtime_module/framework/entity/X9NetworkData.js new file mode 100644 index 0000000..31789b0 --- /dev/null +++ b/runtime_module/framework/entity/X9NetworkData.js @@ -0,0 +1,103 @@ +var X9NetworkData = cc.Class({ + statics:{ + TOKEN_KEY: '__token__', + }, + + /** + * Lấy character id từ URL + */ + // getCharacterIDFromURL() { + // let id, token; + // if (cc.sys.isBrowser) { + // let searchParams = new URLSearchParams(location.search); + // token = searchParams.get("i"); + // if(token){ + // let base64Url = token.split('.')[1]; + // let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); + // let jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) { + // return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); + // }).join('')); + // // Verify + // // if(KJUR){ + // // var isValid = KJUR.jws.JWS.verify(jsonPayload, "x9.guru.1M$$$", ["HS256"]); + // // cc.log('>>>> Charid :: ' + id + ' valid: ' + isValid); + // // } + // // Save token + // let buf = new ArrayBuffer(jsonPayload.length*2); // 2 bytes for each char + // let bufView = new Uint16Array(buf); + // for (var i=0, strLen=jsonPayload.length; i < strLen; i++) { + // bufView[i] = str.charCodeAt(i); + // } + // this._jwtToken = buf; + // // + // let payloadObj = JSON.parse(jsonPayload); + // id = payloadObj.charId; + // // + // cc.log('parse token:: ' + String.fromCharCode.apply(null, new Uint16Array(this._jwtToken))) + // // + + // }else if(CC_DEBUG){ + // id = searchParams.get("id"); + // } + // // + // id = parseInt(id); + // if (id == NaN) return null; + // } + // return id; + // }, + + getURLParams(key){ + return new URLSearchParams(location.search).get(key); + }, + + /** + * + * @param {*} tokenKey + */ + setToken(tokenKey){ + let classPrototype = this.constructor.prototype; + classPrototype.__$request = classPrototype.__$request ? classPrototype.__$request : classPrototype.request; + classPrototype.request = function(url, data, token){ + let requestToken = token ? token : tokenKey; + return this.__$request(url, data, requestToken); + } + }, + + + /** + * + * @param {*} url + * @param {*} data + */ + request(url, data, token) { + return new Promise((resolve, reject) => { + let xhr = new XMLHttpRequest(); + xhr.onreadystatechange = ()=>{ + // this._httpStatus = xhr.status; + if (xhr.readyState == 4) { + if(xhr.status >= 200 && xhr.status < 400){ + let response = xhr.responseText; + resolve(response); + }else{ + reject(url); + } + } + }; + xhr.onerror = ()=>{ + reject(url); + } ; + + xhr.open( (data ? 'POST' : 'GET'), url, true ); + xhr.setRequestHeader("Content-type", "application/json"); + if(token){ + CC_DEBUG && cc.log(this.uuid + ' Request with Token:: ' + token); + xhr.setRequestHeader('Authorization', 'Bearer ' + token); + } + data ? xhr.send(JSON.stringify(data)) : xhr.send(); + + }); + }, + + + +}) \ No newline at end of file diff --git a/runtime_module/framework/entity/X9SaveAndShareData.js b/runtime_module/framework/entity/X9SaveAndShareData.js index a47ffd0..b7f38e7 100644 --- a/runtime_module/framework/entity/X9SaveAndShareData.js +++ b/runtime_module/framework/entity/X9SaveAndShareData.js @@ -77,7 +77,10 @@ const X9SaveAndShareData = cc.Class({ let id = this.__className; let dataURIArr = this._splitDataIdToArray();// className::uuid let dataId = dataURIArr[0]; - let saveData = data || this.getState(); + let saveData = data || Object.assign({},this.getState()); + if(this._clearPrivateArgs){ + this._clearPrivateArgs(saveData); + } let lastSave = cc.sys.localStorage.getItem(dataId); lastSave = lastSave ? JSON.parse(lastSave) : null; saveData = lastSave ? Object.assign(lastSave, saveData) : saveData; diff --git a/runtime_module/hotupdate/HotupdateCommand.js b/runtime_module/hotupdate/HotupdateCommand.js index 103571a..3e6de84 100644 --- a/runtime_module/hotupdate/HotupdateCommand.js +++ b/runtime_module/hotupdate/HotupdateCommand.js @@ -170,8 +170,7 @@ var HotupdateCommand = cc.Class({ getAssetManager(){ if(!this._am){ // Hot update is only available in Native build - if (!cc.sys.isNative) { - CC_DEBUG && cc.log("Không phải native !"); + if (!cc.sys.isNative) { this.cmd({msg:'Only support for native devices.'}, HotupdateCommand.HOTUPDATE_SKIPPED); return; } @@ -241,9 +240,9 @@ var HotupdateCommand = cc.Class({ */ _getNativeURL(){ if(!this.manifestUrl) { - throw new Error(this.constructor.name + ": Chưa khai báo đường dẫn file project manifest !") + throw new Error(this.__className + ": Chưa khai báo đường dẫn file project manifest !") }else if(!(this.manifestUrl instanceof cc.Asset)){ - throw new Error(this.constructor.name + ": Sai kiểu manifest, phải là cc.Asset !") + throw new Error(this.__className + ": Sai kiểu manifest, phải là cc.Asset !") } return this.manifestUrl.nativeUrl; }, diff --git a/runtime_module/hotupdate/HotupdateComponent.js b/runtime_module/hotupdate/HotupdateComponent.js index d74f323..12eddf0 100644 --- a/runtime_module/hotupdate/HotupdateComponent.js +++ b/runtime_module/hotupdate/HotupdateComponent.js @@ -7,7 +7,7 @@ cc.Class({ extends: X9Component, // mixins:[CodeStyle], - properties:{ + properties: { manifestUrl: { type: cc.Asset, default: null @@ -15,21 +15,21 @@ cc.Class({ infoLabel: cc.Label, }, - onLoad(){ + onLoad() { this._super(); //fortestting - CC_DEBUG && cc.sys.localStorage.clear(); + // CC_DEBUG && cc.sys.localStorage.clear(); // this.hotupdateComand = this.use('HotupdateCommand'); this.hotupdateComand.setLocalManifest(this.manifestUrl); // this.applyPrivateCommandToSubclass(false); }, - - onUpdateView(done){ + + onUpdateView(done) { var state = this.getState(); - if(state.msg){ + if (state.msg) { this.infoLabel.string = state.msg; } done() @@ -37,30 +37,30 @@ cc.Class({ // - allowCommandTypes(){ - return [ 'default', - HotupdateCommand.SHOW_MESSAGE, - HotupdateCommand.START_UPDATING, - HotupdateCommand.RETRY_UPDATING, - HotupdateCommand.UPDATE_PROGRESS, - HotupdateCommand.ERROR_NO_LOCAL_MANIFEST, - HotupdateCommand.ERROR_MANIFEST, - HotupdateCommand.ALREADY_UP_TO_DATE, - HotupdateCommand.NEW_VERSION_FOUND, - HotupdateCommand.HOTUPDATE_SKIPPED, - HotupdateCommand.ALREADY_UP_TO_DATE, - HotupdateCommand.UPDATE_FINISHED, - HotupdateCommand.UPDATE_FAILED, - ]; + allowCommandTypes() { + return ['default', + HotupdateCommand.SHOW_MESSAGE, + HotupdateCommand.START_UPDATING, + HotupdateCommand.RETRY_UPDATING, + HotupdateCommand.UPDATE_PROGRESS, + HotupdateCommand.ERROR_NO_LOCAL_MANIFEST, + HotupdateCommand.ERROR_MANIFEST, + HotupdateCommand.ALREADY_UP_TO_DATE, + HotupdateCommand.NEW_VERSION_FOUND, + HotupdateCommand.HOTUPDATE_SKIPPED, + HotupdateCommand.ALREADY_UP_TO_DATE, + HotupdateCommand.UPDATE_FINISHED, + HotupdateCommand.UPDATE_FAILED, + ]; }, - test(){ - if(this.hotupdateComand){ + test() { + if (this.hotupdateComand) { this.hotupdateComand.test(); } }, - startLoadingResource(){ + startLoadingResource() { // this.hotupdateComand.checkUpdate(); this.hotupdateComand.startUpdate(); }