优化框架代码

This commit is contained in:
dgflash
2022-09-01 22:56:15 +08:00
parent 2f335412fc
commit a7d31b051f
13 changed files with 69 additions and 71 deletions

View File

@@ -8,10 +8,10 @@ import { ECSRootSystem } from "../libs/ecs/ECSSystem";
import { LanguageManager } from "../libs/gui/language/Language";
import { HttpRequest } from "../libs/network/HttpRequest";
import { AudioManager } from "./common/audio/AudioManager";
import { Message } from "./common/event/MessageManager";
import { MessageManager } from "./common/event/MessageManager";
import { Logger } from "./common/log/Logger";
import { TimerManager } from "./common/manager/TimerManager";
import { storage } from "./common/storage/StorageManager";
import { StorageManager } from "./common/storage/StorageManager";
import { GameManager } from "./game/GameManager";
import { LayerManager } from "./gui/layer/LayerManager";
@@ -24,9 +24,9 @@ export class oops {
/** 日志管理 */
static log = Logger;
/** 全局消息 */
static message = Message;
static message: MessageManager;
/** 本地存储 */
static storage = storage;
static storage: StorageManager;
/** 游戏时间管理 */
static timer: TimerManager;
/** 游戏音乐管理 */

View File

@@ -11,8 +11,9 @@ import { HttpRequest } from "../libs/network/HttpRequest";
import { config } from "../module/config/Config";
import { AudioManager } from "./common/audio/AudioManager";
import { EventMessage } from "./common/event/EventMessage";
import { Message } from "./common/event/MessageManager";
import { MessageManager } from "./common/event/MessageManager";
import { TimerManager } from "./common/manager/TimerManager";
import { StorageManager } from "./common/storage/StorageManager";
import { GameManager } from "./game/GameManager";
import { GUI } from "./gui/GUI";
import { LayerManager } from "./gui/layer/LayerManager";
@@ -60,6 +61,8 @@ export class Root extends Component {
}
protected init() {
oops.message = MessageManager.Instance;
oops.storage = new StorageManager();
oops.language = new LanguageManager();
oops.timer = new TimerManager(this);
oops.audio = AudioManager.instance;
@@ -79,7 +82,7 @@ export class Root extends Component {
oops.audio.resumeAll();
director.resume();
game.resume();
Message.dispatchEvent(EventMessage.GAME_ENTER);
oops.message.dispatchEvent(EventMessage.GAME_ENTER);
});
// 游戏隐藏事件
@@ -89,14 +92,14 @@ export class Root extends Component {
oops.audio.pauseAll();
director.pause();
game.pause();
Message.dispatchEvent(EventMessage.GAME_EXIT);
oops.message.dispatchEvent(EventMessage.GAME_EXIT);
});
// 游戏尺寸修改事件
var c_gui = this.gui?.addComponent(GUI)!;
view.setResizeCallback(() => {
c_gui.resize();
Message.dispatchEvent(EventMessage.GAME_RESIZE);
oops.message.dispatchEvent(EventMessage.GAME_RESIZE);
});
}
}

View File

@@ -1,5 +1,5 @@
import { Component, director, Node } from "cc";
import { storage } from "../storage/StorageManager";
import { oops } from "../../Oops";
import { AudioEffect } from "./AudioEffect";
import { AudioMusic } from "./AudioMusic";
@@ -141,13 +141,13 @@ export class AudioManager extends Component {
this.local_data.switch_effect = this._switch_effect;
let data = JSON.stringify(this.local_data);
storage.set(LOCAL_STORE_KEY, data);
oops.storage.set(LOCAL_STORE_KEY, data);
}
/** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
load() {
let data = storage.get(LOCAL_STORE_KEY);
let data = oops.storage.get(LOCAL_STORE_KEY);
if (data) {
try {
this.local_data = JSON.parse(data);

View File

@@ -49,7 +49,7 @@ export class MessageEventData {
}
}
class MessageManager {
export class MessageManager {
public static readonly Instance: MessageManager = new MessageManager();
private events: any = {};
@@ -150,4 +150,4 @@ class MessageManager {
}
}
export const Message = MessageManager.Instance;
const Message = MessageManager.Instance;

View File

@@ -1,30 +1,30 @@
import { sys } from "cc";
import { PREVIEW } from "cc/env";
import { md5 } from "../../../libs/security/Md5";
import { EncryptUtil } from "./EncryptUtil";
import { EncryptUtil } from "../../utils/EncryptUtil";
/** 本地存储 */
export module storage {
let _key: string | null = null;
let _iv: string | null = null;
let _id: number = -1;
export class StorageManager {
private _key: string | null = null;
private _iv: string | null = null;
private _id: number = -1;
/**
* 初始化密钥
* @param key aes加密的key
* @param iv aes加密的iv
*/
export function init(key: string, iv: string) {
_key = md5(key);
_iv = md5(iv);
init(key: string, iv: string) {
this._key = md5(key);
this._iv = md5(iv);
}
/**
* 设置用户唯一标识
* @param id
*/
export function setUser(id: number) {
_id = id;
setUser(id: number) {
this._id = id;
}
/**
@@ -33,8 +33,8 @@ export module storage {
* @param value 存储值
* @returns
*/
export function set(key: string, value: any) {
key = `${key}_${_id}`;
set(key: string, value: any) {
key = `${key}_${this._id}`;
if (null == key) {
console.error("存储的key不能为空");
@@ -45,7 +45,7 @@ export module storage {
}
if (null == value) {
console.warn("存储的值为空,则直接移除该存储");
remove(key);
this.remove(key);
return;
}
if (typeof value === 'function') {
@@ -65,9 +65,9 @@ export module storage {
value = value + "";
}
if (!PREVIEW && null != _key && null != _iv) {
if (!PREVIEW && null != this._key && null != this._iv) {
try {
value = EncryptUtil.aesEncrypt(`${value}`, _key, _iv);
value = EncryptUtil.aesEncrypt(`${value}`, this._key, this._iv);
}
catch (e) {
value = null;
@@ -77,26 +77,26 @@ export module storage {
}
/**
* 获取本地数据
* @param key 获取的key
* 获取指定关键字的数据
* @param key 获取的关键字
* @param defaultValue 获取的默认值
* @returns
*/
export function get(key: string, defaultValue?: any): string {
get(key: string, defaultValue?: any): string {
if (null == key) {
console.error("存储的key不能为空");
return null!;
}
key = `${key}_${_id}`;
key = `${key}_${this._id}`;
if (!PREVIEW) {
key = md5(key);
}
let str: string | null = sys.localStorage.getItem(key);
if (null != str && '' !== str && !PREVIEW && null != _key && null != _iv) {
if (null != str && '' !== str && !PREVIEW && null != this._key && null != this._iv) {
try {
str = EncryptUtil.aesDecrypt(str, _key, _iv);
str = EncryptUtil.aesDecrypt(str, this._key, this._iv);
}
catch (e) {
str = null;
@@ -109,18 +109,21 @@ export module storage {
return str;
}
export function getNumber(key: string, defaultValue: number = 0): number {
var r = get(key);
/** 获取指定关键字的数值 */
getNumber(key: string, defaultValue: number = 0): number {
var r = this.get(key);
return Number(r) || defaultValue;
}
export function getBoolean(key: string): boolean {
var r = get(key);
/** 获取指定关键字的布尔值 */
getBoolean(key: string): boolean {
var r = this.get(key);
return Boolean(r) || false;
}
export function getJson(key: string, defaultValue?: any): any {
var r = get(key);
/** 获取指定关键字的JSON对象 */
getJson(key: string, defaultValue?: any): any {
var r = this.get(key);
return (r && JSON.parse(r)) || defaultValue;
}
@@ -129,13 +132,13 @@ export module storage {
* @param key 需要移除的关键字
* @returns
*/
export function remove(key: string) {
remove(key: string) {
if (null == key) {
console.error("存储的key不能为空");
return;
}
key = `${key}_${_id}`;
key = `${key}_${this._id}`;
if (!PREVIEW) {
key = md5(key);
@@ -144,7 +147,7 @@ export module storage {
}
/** 清空整个本地存储 */
export function clear() {
clear() {
sys.localStorage.clear();
}
}

View File

@@ -6,7 +6,7 @@
*/
/** 数组工具 */
export default class ArrayUtil {
export class ArrayUtil {
/** 去重 */
public static noRepeated(arr: any[]) {
var res = [arr[0]];

View File

@@ -1,24 +1,16 @@
/*
* @Author: gagahappy<15020055@qq.com>
* @Date: 2022-09-01 15:13:19
* @LastEditors: dgflash
* @LastEditTime: 2022-09-01 19:17:24
* @Description:
*/
/** Crypto加密 */
export module EncryptUtil {
export class EncryptUtil {
/**
* AES
* @param msg
* @param key
* @param iv
* @param msg
* @param key aes加密的key
* @param iv aes加密的iv
* @returns
*/
export function aesEncrypt(msg: string, key: string, iv: string): string {
static aesEncrypt(msg: string, key: string, iv: string): string {
//@ts-ignore
let encrypt = CryptoJS.AES.encrypt(msg, utf8Parse(key), {
iv: utf8Parse(iv),
iv: this.utf8Parse(iv),
//@ts-ignore
mode: CryptoJS.mode.CBC,
//@ts-ignore
@@ -29,15 +21,15 @@ export module EncryptUtil {
/**
* AES
* @param str
* @param key
* @param iv
* @param str
* @param key aes加密的key
* @param iv aes加密的iv
* @returns
*/
export function aesDecrypt(str: string, key: string, iv: string): string {
static aesDecrypt(str: string, key: string, iv: string): string {
//@ts-ignore
let decrypt = CryptoJS.AES.decrypt(str, utf8Parse(key), {
iv: utf8Parse(iv),
let decrypt = CryptoJS.AES.decrypt(str, this.utf8Parse(key), {
iv: this.utf8Parse(iv),
//@ts-ignore
mode: CryptoJS.mode.CBC,
//@ts-ignore
@@ -47,7 +39,7 @@ export module EncryptUtil {
return CryptoJS.enc.Utf8.stringify(decrypt);
}
function utf8Parse(utf8Str: string): string {
private static utf8Parse(utf8Str: string): string {
//@ts-ignore
return CryptoJS.enc.Utf8.parse(utf8Str);
}

View File

@@ -2,7 +2,7 @@
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "a74a14e9-aeda-4bc7-9e16-f2fee1a4b927",
"uuid": "46d12071-e097-4a9c-8607-85cbcd93a4b5",
"files": [],
"subMetas": {},
"userData": {}

View File

@@ -3,7 +3,7 @@ import { Color, Texture2D } from "cc";
/**
* 图像工具
*/
export default class ImageUtil {
export class ImageUtil {
/**
* 获取纹理中指定像素的颜色,原点为左上角,从像素 (1, 1) 开始。
* @param texture 纹理

View File

@@ -6,7 +6,7 @@
*/
/** 对象工具 */
export default class ObjectUtil {
export class ObjectUtil {
/**
* 判断指定的值是否为对象
* @param value 值

View File

@@ -6,7 +6,7 @@
*/
/** 正则工具 */
export default class RegexUtil {
export class RegexUtil {
/**
* 判断字符是否为双字节字符(如中文字符)
* @param string 原字符串

View File

@@ -17,7 +17,7 @@ export class LanguageManager extends EventDispatcher {
private _languagePack: LanguagePack = new LanguagePack(); // 语言包
/** 设置多语言系统支持哪些语种 */
public set supportLanguages(supportLanguages: Array<string>) {
public set supportLanguages(supportLanguages: Array<string>) {
this._support = supportLanguages;
}

View File

@@ -17,7 +17,6 @@ export * from './assets/core/common/loader/ResLoader';
export * from './assets/core/common/log/Logger';
export * from './assets/core/common/manager/RandomManager';
export * from './assets/core/common/manager/TimerManager';
export * from './assets/core/common/storage/EncryptUtil';
export * from './assets/core/common/storage/StorageManager';
export * from './assets/core/game/GameCollision';
export * from './assets/core/game/GameComponent';
@@ -36,6 +35,7 @@ export * from './assets/core/gui/prompt/Notify';
export * from './assets/core/gui/GUI';
export * from './assets/core/utils/ArrayUtil';
export * from './assets/core/utils/CameraUtil';
export * from './assets/core/utils/EncryptUtil';
export * from './assets/core/utils/ImageUtil';
export * from './assets/core/utils/JsonUtil';
export * from './assets/core/utils/LayerUtil';