128 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			128 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
| import { _decorator, Component, Node } from 'cc';
 | |
| const { ccclass, property } = _decorator;
 | |
| 
 | |
| @ccclass('ReadStream')
 | |
| export class ReadStream {
 | |
| 
 | |
|     Buffer: Uint8Array;
 | |
| 
 | |
|     //当前读取位置
 | |
|     Pos: number = 0;
 | |
| 
 | |
|     constructor(public buffer: Uint8Array) {
 | |
|         this.Buffer = buffer;
 | |
|     }
 | |
| 
 | |
|     //string转字节数组
 | |
|     static StringToUint8Array(str) {
 | |
|         const encoder = new TextEncoder();
 | |
|         return encoder.encode(str);
 | |
|     }
 | |
| 
 | |
|     //字节数组转string
 | |
|     static Uint8ArrayToString(data: Uint8Array): string {
 | |
|         const decoder = new TextDecoder(); // 默认使用 'utf-8' 编码
 | |
|         return decoder.decode(data);
 | |
|     }
 | |
| 
 | |
|     Seekg(Pos: number) {
 | |
|         this.Pos = Pos;
 | |
|     }
 | |
| 
 | |
|     //读取数字 默认小端序读取
 | |
|     GetShort(littleEndian: boolean = true) {
 | |
|         //读2个字节
 | |
|         const Buf = new DataView(this.GetBufferByLength(2).buffer);
 | |
|         const view = new DataView(Buf.buffer);
 | |
|         return Number(view.getInt16(0, true));
 | |
|     }
 | |
| 
 | |
|     GetUShort(littleEndian: boolean = true) {
 | |
|         //读2个字节
 | |
|         const Buf = new DataView(this.GetBufferByLength(2).buffer);
 | |
|         const view = new DataView(Buf.buffer);
 | |
|         return Number(view.getUint16(0, true));
 | |
|     }
 | |
| 
 | |
|     //读取数字 默认小端序读取
 | |
|     GetInt(littleEndian: boolean = true) {
 | |
|         //读4个字节
 | |
|         const Buf = new DataView(this.GetBufferByLength(4).buffer);
 | |
|         return Buf.getInt32(0, littleEndian);
 | |
|     }
 | |
| 
 | |
|     //读取浮点数 默认小端序读取
 | |
|     GetFloat(littleEndian: boolean = true) {
 | |
|         //读4个字节
 | |
|         const Buf = new DataView(this.GetBufferByLength(4).buffer);
 | |
|         return Buf.getFloat32(0, littleEndian);
 | |
|     }
 | |
| 
 | |
|     //特殊方法
 | |
|     Get256(): any {
 | |
|         const Buf = Number(this.GetBufferByLength(1));
 | |
|         return (256.0 + Buf) % 256.0;
 | |
|     }
 | |
| 
 | |
|     //读取数字 默认小端序
 | |
|     GetLongInt(littleEndian: boolean = true) {
 | |
|         //读8个字节 
 | |
|         const Buf = this.GetBufferByLength(8);
 | |
| 
 | |
|         const view = new DataView(Buf.buffer);
 | |
|         return Number(view.getBigInt64(0, true));
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
|     //读取字符串
 | |
|     GetString(Length?: number) {
 | |
|         if (Length) {
 | |
|             //读指定字节
 | |
|             const Buf = this.GetBufferByLength(Length);
 | |
|             return ReadStream.Uint8ArrayToString(Buf);
 | |
|         }
 | |
|         else {
 | |
|             for (let i = 0; i < this.Buffer.length - this.Pos; i++) {
 | |
|                 if (this.Buffer[i + this.Pos] == 0) {
 | |
|                     const truncatedArray = this.Buffer.subarray(this.Pos, i + this.Pos);
 | |
|                     this.Pos += i + 1;
 | |
|                     return ReadStream.Uint8ArrayToString(truncatedArray);
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //读取指定长度流
 | |
|     GetBufferByLength(Length: number) {
 | |
|         const Buf = this.Buffer.slice(this.Pos, this.Pos + Length);
 | |
|         this.Pos += Length;
 | |
|         return Buf;
 | |
|     }
 | |
| 
 | |
|     //读取信息 通过Key
 | |
|     ReadInfo(Key): string {
 | |
|         let PathUA: Uint8Array = new Uint8Array(256);
 | |
|         for (let i = 0; i < 256; i++) {
 | |
|             PathUA[i] = this.Buffer[i + this.Pos] ^ Key[i];
 | |
|         }
 | |
|         // let Spos = 0;
 | |
|         // for (let i = 0; i < PathUA.length; i++) {
 | |
|         //     if (PathUA[i] == 0) Spos = i;
 | |
|         // }
 | |
| 
 | |
|         // const truncatedArray = PathUA.subarray(0, Spos);
 | |
| 
 | |
|         this.Pos += 256;
 | |
|         let str = ReadStream.Uint8ArrayToString(PathUA);
 | |
|         str = str.substring(0, str.indexOf(".img") + 4);
 | |
|         return str;
 | |
|     }
 | |
| 
 | |
| 
 | |
|     len() {
 | |
|         return this.Buffer.length;
 | |
|     }
 | |
| 
 | |
| }
 |