132 lines
4.0 KiB
TypeScript
132 lines
4.0 KiB
TypeScript
import { sys, native } from "cc";
|
||
import { EDITOR, PREVIEW } from "cc/env";
|
||
|
||
function isWeChatMiniGame(): boolean {
|
||
const g: any = globalThis as any;
|
||
return !!g.wx?.getSystemInfoSync;
|
||
}
|
||
function isTTMiniGame(): boolean {
|
||
const g: any = globalThis as any;
|
||
return !!g.tt?.getSystemInfoSync;
|
||
}
|
||
|
||
/** 小游戏平台:优先 brand + model */
|
||
function tryMiniGameModel(): string | null {
|
||
const g: any = globalThis as any;
|
||
if (g.tt?.getSystemInfoSync) {
|
||
const info = g.tt.getSystemInfoSync();
|
||
const s = `${info.brand ?? ""} ${info.model ?? ""}`.trim();
|
||
return s || null;
|
||
}
|
||
if (g.wx?.getSystemInfoSync) {
|
||
const info = g.wx.getSystemInfoSync();
|
||
const s = `${info.brand ?? ""} ${info.model ?? ""}`.trim();
|
||
return s || null;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/** 原生:Android 使用 Build.MANUFACTURER + Build.MODEL;iOS 使用硬件码(如 iPhone14,5) */
|
||
function tryNativeModel(): string | null {
|
||
// if (!sys.isNative) return null;
|
||
// try {
|
||
// if (sys.platform === sys.Platform.ANDROID) {
|
||
// const s = native.reflection.callStaticMethod(
|
||
// "org/cocos2dx/javascript/AppActivity",
|
||
// "getDeviceModel",
|
||
// "()Ljava/lang/String;"
|
||
// );
|
||
// return (s && String(s).trim()) || null;
|
||
// }
|
||
// if (sys.platform === sys.Platform.IOS) {
|
||
// const s = native.reflection.callStaticMethod("AppController", "getHardwareModel");
|
||
// return (s && String(s).trim()) || null;
|
||
// }
|
||
// } catch {
|
||
// // 反射失败直接兜底
|
||
// }
|
||
return null;
|
||
}
|
||
|
||
/** 浏览器:拿不到“设备名”,仅做极简标识(避免泄露 UA 细节) */
|
||
function tryWebHint(): string {
|
||
// 可按需增强:navigator.userAgentData?.platform
|
||
// 这里仅返回平台信息,合规且稳定
|
||
// @ts-ignore
|
||
const plat = (navigator as any)?.userAgentData?.platform || (navigator as any)?.platform || "Web";
|
||
return String(plat);
|
||
}
|
||
|
||
export class MachineInfoService {
|
||
private static _I: MachineInfoService;
|
||
static get I() { return this._I ?? (this._I = new MachineInfoService()); }
|
||
|
||
private _cached: string | null = null;
|
||
|
||
/** 运行时标签,方便排查:editor / editor-preview / native-android / native-ios / minigame-wechat / minigame-tt / web */
|
||
public runtimeTag(): string {
|
||
if (EDITOR) return PREVIEW ? "editor-preview" : "editor";
|
||
if (sys.isNative) {
|
||
if (sys.platform === sys.Platform.ANDROID) return "native-android";
|
||
if (sys.platform === sys.Platform.IOS) return "native-ios";
|
||
return "native";
|
||
}
|
||
if (isWeChatMiniGame()) return "minigame-wechat";
|
||
if (isTTMiniGame()) return "minigame-tt";
|
||
return "web";
|
||
}
|
||
|
||
/** 获取“机器标识”(推荐:品牌+型号/硬件码;编辑器返回固定标识;浏览器返回平台提示) */
|
||
public getMachineIdentifier(): string {
|
||
if (this._cached) return this._cached;
|
||
|
||
let id: string | null = null;
|
||
|
||
// 1) 编辑器环境:不要做任何原生/平台调用,返回明确标签
|
||
if (EDITOR) {
|
||
id = PREVIEW ? `EditorPreview-${sys.os}` : `Editor-${sys.os}`;
|
||
this._cached = id;
|
||
return id;
|
||
}
|
||
|
||
// 浏览器/H5
|
||
id = tryWebHint();
|
||
return (this._cached = this.trimSafe(id || "Unknown"));
|
||
|
||
// 小游戏
|
||
id = tryMiniGameModel();
|
||
if (id) return (this._cached = this.trimSafe(id));
|
||
|
||
// 原生
|
||
id = tryNativeModel();
|
||
if (id) return (this._cached = this.trimSafe(id));
|
||
}
|
||
|
||
private trimSafe(s: string): string {
|
||
// 限长,去掉不可见字符,避免异常字符串
|
||
s = s.replace(/[^\x20-\x7E\u4e00-\u9fa5]/g, "").trim();
|
||
if (s.length > 64) s = s.slice(0, 64);
|
||
return s || "Unknown";
|
||
}
|
||
|
||
/** 获取机器系统标识 1: android 2:ios; 3: windows */
|
||
public getMachineOs(): number {
|
||
if (EDITOR) {
|
||
return 3; // 在 Cocos Creator 编辑器里运行
|
||
}
|
||
|
||
if (sys.platform === sys.Platform.ANDROID) {
|
||
return 1;
|
||
}
|
||
if (sys.platform === sys.Platform.IOS) {
|
||
return 2;
|
||
}
|
||
if (sys.platform === sys.Platform.WIN32) {
|
||
return 3;
|
||
}
|
||
return 3;
|
||
}
|
||
}
|
||
|
||
export default MachineInfoService;
|