203 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			203 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| /*
 | |
| 文件名:MemoryClass.nut
 | |
| 路径:Base/_Tool/MemoryClass.nut
 | |
| 创建日期:2024-09-24	08:22
 | |
| 文件用途:
 | |
| */
 | |
| 
 | |
| class Memory {
 | |
| 
 | |
|     function alloc(Size) {
 | |
|         return NativePointer(Size.tostring());
 | |
|     }
 | |
| 
 | |
|     function allocUtf8String(Str) {
 | |
|         return NativePointer(Str_Ptr(Str));
 | |
|     }
 | |
| }
 | |
| 
 | |
| class NativePointer {
 | |
| 
 | |
|     C_Object = null;
 | |
| 
 | |
|     constructor(T) {
 | |
|         if (type(T) == "integer") {
 | |
|             C_Object = L_sq_I2P(T);
 | |
|         } else if (type(T) == "userdata") {
 | |
|             C_Object = T;
 | |
|         } else if (type(T) == "string") {
 | |
|             C_Object = Sq_New_Point(T.tointeger());
 | |
|             //注册销毁伪析构
 | |
|             Register_Destruction(C_Object, this);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     function add(intoffset) {
 | |
|         this.C_Object = L_sq_I2P(L_sq_P2I(this.C_Object) + intoffset);
 | |
|         return this;
 | |
|     }
 | |
| 
 | |
|     function sub(intoffset) {
 | |
|         this.C_Object = L_sq_I2P(L_sq_P2I(this.C_Object) - intoffset);
 | |
|         return this;
 | |
|     }
 | |
| 
 | |
|     function writeByteArray(arr) {
 | |
|         Sq_Memory_WriteByteArr(this.C_Object, arr);
 | |
|     }
 | |
| 
 | |
|     function readByteArray(size) {
 | |
|         local PointB = Sq_Point2Blob(this.C_Object, size);
 | |
|         local arr = [];
 | |
|         foreach(value in PointB) {
 | |
|             arr.append(value);
 | |
|         }
 | |
|         return arr;
 | |
|     }
 | |
| 
 | |
| 
 | |
|     function write(value, type) {
 | |
|         local Buf = blob(0);
 | |
|         Buf.writen(value, type);
 | |
|         local arr = [];
 | |
|         foreach(value in Buf) {
 | |
|             arr.append(value);
 | |
|         }
 | |
|         writeByteArray(arr);
 | |
|     }
 | |
| 
 | |
|     function writeS8(value) {
 | |
|         write(value, 'c');
 | |
|     }
 | |
| 
 | |
|     function writeU8(value) {
 | |
|         write(value, 'b');
 | |
|     }
 | |
| 
 | |
|     function writeS16(value) {
 | |
|         write(value, 's');
 | |
|     }
 | |
| 
 | |
|     function writeU16(value) {
 | |
|         write(value, 'w');
 | |
|     }
 | |
| 
 | |
|     function writeS32(value) {
 | |
|         write(value, 'i');
 | |
|     }
 | |
| 
 | |
|     function writeU32(value) {
 | |
|         write(value, 'i');
 | |
|     }
 | |
| 
 | |
|     function writeShort(value) {
 | |
|         write(value, 's');
 | |
|     }
 | |
| 
 | |
|     function writeUShort(value) {
 | |
|         write(value, 'w');
 | |
|     }
 | |
| 
 | |
|     function writeInt(value) {
 | |
|         write(value, 'i');
 | |
|     }
 | |
| 
 | |
|     function writeUInt(value) {
 | |
|         write(value, 'i');
 | |
|     }
 | |
| 
 | |
|     function writeFloat(value) {
 | |
|         write(value, 'f');
 | |
|     }
 | |
| 
 | |
|     function writeDouble(value) {
 | |
|         write(value, 'd');
 | |
|     }
 | |
| 
 | |
| 
 | |
|     function read(type) {
 | |
|         local Buf = Sq_Point2Blob(L_sq_P2I(this.C_Object), 4);
 | |
|         return Buf.readn(type);
 | |
|     }
 | |
| 
 | |
|     function readS8() {
 | |
|         return read('c');
 | |
|     }
 | |
| 
 | |
|     function readU8() {
 | |
|         return read('b');
 | |
|     }
 | |
| 
 | |
|     function readS16() {
 | |
|         return read('s');
 | |
|     }
 | |
| 
 | |
|     function readU16() {
 | |
|         return read('w');
 | |
|     }
 | |
| 
 | |
|     function readS32() {
 | |
|         return read('i');
 | |
|     }
 | |
| 
 | |
|     function readU32() {
 | |
|         return read('i');
 | |
|     }
 | |
| 
 | |
|     function readShort() {
 | |
|         return read('s');
 | |
|     }
 | |
| 
 | |
|     function readUShort() {
 | |
|         return read('w');
 | |
|     }
 | |
| 
 | |
|     function readInt() {
 | |
|         return read('i');
 | |
|     }
 | |
| 
 | |
|     function readUInt() {
 | |
|         return read('i');
 | |
|     }
 | |
| 
 | |
|     function readFloat() {
 | |
|         return read('f');
 | |
|     }
 | |
| 
 | |
|     function readDouble() {
 | |
|         return read('d');
 | |
|     }
 | |
| 
 | |
| 
 | |
|     function readUnicodeString(...) {
 | |
|         if (vargc > 0) {
 | |
|             return Sq_Memory_ReadString(this.C_Object, vargv[0]);
 | |
|         } else {
 | |
|             return Sq_Memory_ReadString(this.C_Object);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     function readUtf8String(...) {
 | |
|         if (vargc > 0) {
 | |
|             return Sq_Memory_ReadStringByUtf8(this.C_Object, vargv[0]);
 | |
|         } else {
 | |
|             return Sq_Memory_ReadStringByUtf8(this.C_Object);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     function readBig5String(...) {
 | |
|         if (vargc > 0) {
 | |
|             return Sq_Memory_ReadStringByBig5(this.C_Object, vargv[0]);
 | |
|         } else {
 | |
|             return Sq_Memory_ReadStringByBig5(this.C_Object);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     function readPointer() {
 | |
|         return Sq_ReadPoint(this.C_Object);
 | |
|     }
 | |
| 
 | |
|     function tostring() {
 | |
|         return this.C_Object.tostring();
 | |
|     }
 | |
| } |