42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { _decorator, Component, Node } from 'cc';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
@ccclass('GlobalTool')
|
|
export class GlobalTool {
|
|
|
|
public static CrcDecode(data: Uint8Array, crc32: number): void {
|
|
const num: number = 2175242257; // 2175242257L in hexadecimal
|
|
const dataView = new DataView(
|
|
data.buffer,
|
|
data.byteOffset,
|
|
data.byteLength
|
|
);
|
|
|
|
for (let i = 0; i < data.length; i += 4) {
|
|
// Ensure we don't read beyond the end of the data
|
|
if (i + 4 > data.length) break;
|
|
|
|
// Read a 32-bit integer (little endian) from the buffer
|
|
let anInt = dataView.getUint32(i, true);
|
|
|
|
// Perform the XOR operations
|
|
let val = anInt ^ num ^ crc32;
|
|
|
|
// Rotate right 6 bits
|
|
let jiemi = (val >>> 6) | (val << (32 - 6));
|
|
|
|
// Write the result back into the buffer
|
|
dataView.setUint32(i, jiemi, true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//字节数组转string
|
|
public static uint8ArrayToString(data: Uint8Array, Code?: string): string {
|
|
if (!Code) Code = 'utf-8';
|
|
const decoder = new TextDecoder(Code); // 默认使用 'utf-8' 编码
|
|
return decoder.decode(data);
|
|
}
|
|
} |