2013-01-06 14:15:17 +00:00
|
|
|
unit LuaStream;
|
|
|
|
|
|
|
|
|
|
{$mode delphi}
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
Classes, SysUtils, lua, lauxlib, lualib, math;
|
|
|
|
|
|
|
|
|
|
procedure stream_addMetaData(L: PLua_state; metatable: integer; userdata: integer );
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
2019-12-18 12:07:17 +01:00
|
|
|
uses LuaClass, LuaHandler, LuaObject{$ifdef darwin},mactypes{$endif};
|
2013-01-06 14:15:17 +00:00
|
|
|
|
|
|
|
|
function stream_getSize(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
lua_pushinteger(L, stream.Size);
|
|
|
|
|
result:=1;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_setSize(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
2020-10-15 12:38:23 +02:00
|
|
|
oldsize: integer;
|
|
|
|
|
newsize: integer;
|
|
|
|
|
diff: integer;
|
2013-01-06 14:15:17 +00:00
|
|
|
begin
|
|
|
|
|
result:=0;
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2020-10-15 12:38:23 +02:00
|
|
|
if lua_gettop(L)>=1 then
|
|
|
|
|
begin
|
|
|
|
|
newsize:=lua_tointeger(L,1);
|
|
|
|
|
if stream is TMemoryStream then
|
|
|
|
|
begin
|
|
|
|
|
oldsize:=stream.size;
|
|
|
|
|
stream.size:=newsize;
|
|
|
|
|
|
|
|
|
|
//zero the new bytes
|
|
|
|
|
FillByte(pointer(ptruint(tmemorystream(stream).Memory)+oldsize)^, newsize-oldsize,0);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
stream.Size:=newsize;
|
|
|
|
|
end;
|
2013-01-06 14:15:17 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_getPosition(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
lua_pushinteger(L, stream.Position);
|
|
|
|
|
result:=1;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_setPosition(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
result:=0;
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
if lua_gettop(L)=1 then
|
|
|
|
|
stream.Position:=lua_tointeger(L,-1);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_copyFrom(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
s: tstream;
|
|
|
|
|
count: integer;
|
|
|
|
|
begin
|
|
|
|
|
result:=0;
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
if lua_gettop(L)=2 then
|
|
|
|
|
begin
|
|
|
|
|
s:=lua_ToCEUserData(L, 1);
|
|
|
|
|
count:=lua_tointeger(L, 2);
|
|
|
|
|
stream.CopyFrom(s, count);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_read(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
count: integer;
|
|
|
|
|
|
|
|
|
|
buf: PByteArray;
|
|
|
|
|
table: integer;
|
|
|
|
|
i: integer;
|
|
|
|
|
begin
|
|
|
|
|
result:=0;
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2013-06-13 11:31:33 +00:00
|
|
|
if lua_gettop(L)>=1 then
|
2013-01-06 14:15:17 +00:00
|
|
|
begin
|
|
|
|
|
if lua_isnumber(L, 1) then
|
|
|
|
|
begin
|
|
|
|
|
count:=lua_tointeger(L, 1);
|
|
|
|
|
getmem(buf, count);
|
|
|
|
|
try
|
|
|
|
|
stream.Read(buf^, count);
|
|
|
|
|
|
|
|
|
|
//everything ok, create a table
|
|
|
|
|
lua_newtable(L); //pobably 2
|
|
|
|
|
table:=lua_gettop(L);
|
|
|
|
|
|
|
|
|
|
for i:=0 to count-1 do
|
|
|
|
|
begin
|
|
|
|
|
lua_pushinteger(L, i+1);
|
|
|
|
|
lua_pushinteger(L, buf[i]);
|
|
|
|
|
lua_settable(L, table); //set table[i+1]=buf[i]
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
result:=1;
|
|
|
|
|
|
|
|
|
|
except
|
|
|
|
|
end;
|
2018-06-12 20:23:45 +02:00
|
|
|
FreeMemAndNil(buf);
|
2013-01-06 14:15:17 +00:00
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-19 21:13:57 +02:00
|
|
|
function stream_readByte(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2023-06-18 14:36:16 +02:00
|
|
|
try
|
|
|
|
|
lua_pushinteger(L,stream.ReadByte);
|
|
|
|
|
except
|
|
|
|
|
lua_pushstring(L,'stream error');
|
|
|
|
|
lua_error(L);
|
|
|
|
|
end;
|
2017-09-19 21:13:57 +02:00
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeByte(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2023-06-18 14:36:16 +02:00
|
|
|
try
|
|
|
|
|
stream.WriteByte(lua_tointeger(L,1));
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
end;
|
2017-09-19 21:13:57 +02:00
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_readWord(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2023-06-18 14:36:16 +02:00
|
|
|
try
|
|
|
|
|
lua_pushinteger(L,stream.ReadWord);
|
|
|
|
|
except
|
|
|
|
|
lua_pushstring(L,'stream error');
|
|
|
|
|
lua_error(L);
|
|
|
|
|
end;
|
2017-09-19 21:13:57 +02:00
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeWord(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
stream.WriteWord(lua_tointeger(L,1));
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_readDword(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2023-06-18 14:36:16 +02:00
|
|
|
try
|
|
|
|
|
lua_pushinteger(L,stream.ReadDword);
|
|
|
|
|
except
|
|
|
|
|
lua_pushstring(L,'stream error');
|
|
|
|
|
lua_error(L);
|
|
|
|
|
end;
|
2017-09-19 21:13:57 +02:00
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeDword(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
stream.WriteDword(lua_tointeger(L,1));
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_readQword(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2023-06-18 14:36:16 +02:00
|
|
|
try
|
|
|
|
|
lua_pushinteger(L,stream.ReadQword);
|
|
|
|
|
except
|
|
|
|
|
lua_pushstring(L,'stream error');
|
|
|
|
|
lua_error(L);
|
|
|
|
|
end;
|
2017-09-19 21:13:57 +02:00
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeQword(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
stream.WriteQword(lua_tointeger(L,1));
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
2023-06-07 01:55:06 +02:00
|
|
|
function stream_readFloat(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
f: single;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2023-06-18 14:36:16 +02:00
|
|
|
try
|
|
|
|
|
stream.Read(f,sizeof(f));
|
|
|
|
|
except
|
|
|
|
|
lua_pushstring(L,'stream error');
|
|
|
|
|
lua_error(L);
|
|
|
|
|
end;
|
|
|
|
|
|
2023-06-07 01:55:06 +02:00
|
|
|
lua_pushnumber(L,f);
|
|
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeFloat(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
f: single;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
f:=lua_tonumber(L,1);
|
|
|
|
|
stream.Write(f, sizeof(f));
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_readDouble(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
d: double;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2023-06-18 14:36:16 +02:00
|
|
|
try
|
|
|
|
|
stream.Read(d,sizeof(d));
|
|
|
|
|
except
|
|
|
|
|
lua_pushstring(L,'stream error');
|
|
|
|
|
lua_error(L);
|
|
|
|
|
end;
|
|
|
|
|
|
2023-06-07 01:55:06 +02:00
|
|
|
lua_pushnumber(L,d);
|
|
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeDouble(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
d: double;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
d:=lua_tonumber(L,1);
|
|
|
|
|
stream.Write(d, sizeof(d));
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-03 23:04:54 +01:00
|
|
|
function stream_readString(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
size: integer;
|
|
|
|
|
s: pchar;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
result:=0;
|
|
|
|
|
if lua_gettop(L)>=1 then
|
|
|
|
|
begin
|
|
|
|
|
size:=lua_tointeger(L,1);
|
|
|
|
|
getmem(s,size+1);
|
|
|
|
|
stream.ReadBuffer(s^,size);
|
|
|
|
|
s[size]:=#0;
|
|
|
|
|
|
|
|
|
|
lua_pushstring(L,s);
|
|
|
|
|
result:=1;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_pushstring(L,rsIncorrectNumberOfParameters);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeString(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
s: string;
|
|
|
|
|
includeterminator: boolean;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
result:=0;
|
|
|
|
|
|
|
|
|
|
if lua_gettop(L)>=1 then
|
|
|
|
|
begin
|
|
|
|
|
s:=Lua_ToString(L,1);
|
|
|
|
|
if lua_gettop(L)>=2 then
|
|
|
|
|
includeterminator:=lua_toboolean(L,2)
|
|
|
|
|
else
|
|
|
|
|
includeterminator:=false;
|
|
|
|
|
|
2023-04-07 21:35:47 +02:00
|
|
|
stream.WriteBuffer(s[1],length(s));
|
2023-03-03 23:04:54 +01:00
|
|
|
if includeterminator then
|
|
|
|
|
stream.WriteByte(0);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_pushstring(L,rsIncorrectNumberOfParameters);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-06-06 12:27:45 +02:00
|
|
|
function stream_readAnsiString(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
lua_pushstring(L,stream.ReadAnsiString);
|
|
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function stream_writeAnsiString(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
begin
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
|
|
|
|
stream.WriteAnsiString(Lua_ToString(L,1));
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-01-06 14:15:17 +00:00
|
|
|
function stream_write(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
stream: Tstream;
|
|
|
|
|
count: integer;
|
|
|
|
|
|
|
|
|
|
buf: PByteArray;
|
|
|
|
|
i: integer;
|
|
|
|
|
begin
|
|
|
|
|
result:=0;
|
|
|
|
|
stream:=luaclass_getClassObject(L);
|
2015-01-30 19:25:24 +00:00
|
|
|
if lua_gettop(L)>=1 then
|
2013-01-06 14:15:17 +00:00
|
|
|
begin
|
|
|
|
|
//table index is at 1
|
|
|
|
|
if lua_istable(L, 1) then
|
|
|
|
|
begin
|
|
|
|
|
if lua_gettop(L)>=2 then
|
2019-12-30 01:15:35 +01:00
|
|
|
{$if FPC_FULLVERSION < 030200}
|
2019-12-18 12:07:17 +01:00
|
|
|
count:=min(int64(lua_objlen(L, 1)), int64(lua_tointeger(L, 2)))
|
|
|
|
|
{$else}
|
2015-01-30 19:25:24 +00:00
|
|
|
count:=min(lua_objlen(L, 1), lua_tointeger(L, 2)) //prevent the length from exeeding the table
|
2019-12-18 12:07:17 +01:00
|
|
|
{$endif}
|
2013-01-06 14:15:17 +00:00
|
|
|
else
|
|
|
|
|
count:=lua_objlen(L, 1);
|
|
|
|
|
|
|
|
|
|
getmem(buf, count);
|
|
|
|
|
try
|
|
|
|
|
for i:=0 to count-1 do
|
|
|
|
|
begin
|
|
|
|
|
lua_pushinteger(L, i+1);
|
|
|
|
|
lua_gettable(L, 1); //get table[i+1]
|
|
|
|
|
buf[i]:=lua_tointeger(L, -1);
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
end;
|
|
|
|
|
stream.Write(buf^, count);
|
|
|
|
|
except
|
|
|
|
|
end;
|
2018-06-12 20:23:45 +02:00
|
|
|
FreeMemAndNil(buf);
|
2013-01-06 14:15:17 +00:00
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure stream_addMetaData(L: PLua_state; metatable: integer; userdata: integer );
|
|
|
|
|
begin
|
|
|
|
|
object_addMetaData(L, metatable, userdata);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'copyFrom', stream_copyFrom);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'read', stream_read);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'write', stream_write);
|
2017-09-19 21:13:57 +02:00
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readByte', stream_readByte);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeByte', stream_writeByte);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readWord', stream_readWord);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeWord', stream_writeWord);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readDword', stream_readDword);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeDword', stream_writeDword);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readQword', stream_readQword);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeQword', stream_writeQword);
|
2023-06-07 01:55:06 +02:00
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readFloat', stream_readFloat);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeFloat', stream_writeFloat);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readDouble', stream_readDouble);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeDouble', stream_writeDouble);
|
2023-03-03 23:04:54 +01:00
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readString', stream_readString);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeString', stream_writeString);
|
2019-06-06 12:27:45 +02:00
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'readAnsiString', stream_readAnsiString);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'writeAnsiString', stream_writeAnsiString);
|
2013-01-06 14:15:17 +00:00
|
|
|
|
|
|
|
|
luaclass_addPropertyToTable(L, metatable, userdata, 'Size', stream_getSize, stream_setSize);
|
|
|
|
|
luaclass_addPropertyToTable(L, metatable, userdata, 'Position', stream_getPosition, stream_setPosition);
|
2013-06-13 11:31:33 +00:00
|
|
|
end;
|
2013-01-06 14:15:17 +00:00
|
|
|
|
2013-06-13 11:31:33 +00:00
|
|
|
function memorystream_getMemory(L: PLua_State): integer; cdecl;
|
|
|
|
|
var ms: Tmemorystream;
|
|
|
|
|
begin
|
|
|
|
|
ms:=luaclass_getClassObject(L);
|
|
|
|
|
lua_pushinteger(L, ptruint(ms.Memory));
|
|
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
2014-02-13 02:19:30 +00:00
|
|
|
function memorystream_loadFromFile(L: PLua_State): integer; cdecl;
|
|
|
|
|
var ms: Tmemorystream;
|
|
|
|
|
begin
|
|
|
|
|
ms:=luaclass_getClassObject(L);
|
|
|
|
|
if lua_gettop(L)>=1 then
|
|
|
|
|
ms.LoadFromFile(Lua_ToString(L,1));
|
|
|
|
|
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function memorystream_saveToFile(L: PLua_State): integer; cdecl;
|
|
|
|
|
var ms: Tmemorystream;
|
|
|
|
|
begin
|
|
|
|
|
ms:=luaclass_getClassObject(L);
|
|
|
|
|
if lua_gettop(L)>=1 then
|
|
|
|
|
ms.SaveToFile(Lua_ToString(L,1));
|
|
|
|
|
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-12-23 18:26:28 +01:00
|
|
|
function memorystream_loadFromFileNoError(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
ms: Tmemorystream;
|
|
|
|
|
r: boolean=false;
|
|
|
|
|
begin
|
2021-02-28 23:08:10 +01:00
|
|
|
result:=0;
|
2019-12-23 18:26:28 +01:00
|
|
|
ms:=luaclass_getClassObject(L);
|
|
|
|
|
if lua_gettop(L)>=1 then
|
|
|
|
|
try
|
|
|
|
|
ms.LoadFromFile(Lua_ToString(L,1));
|
|
|
|
|
lua_pushboolean(L,true);
|
|
|
|
|
result:=1;
|
|
|
|
|
except
|
|
|
|
|
on e:exception do
|
|
|
|
|
begin
|
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
|
lua_pushstring(L,e.message);
|
|
|
|
|
result:=2;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function memorystream_saveToFileNoError(L: PLua_State): integer; cdecl;
|
|
|
|
|
var ms: Tmemorystream;
|
|
|
|
|
begin
|
2021-02-28 23:08:10 +01:00
|
|
|
result:=0;
|
|
|
|
|
|
2019-12-23 18:26:28 +01:00
|
|
|
ms:=luaclass_getClassObject(L);
|
|
|
|
|
if lua_gettop(L)>=1 then
|
|
|
|
|
try
|
|
|
|
|
ms.SaveToFile(Lua_ToString(L,1));
|
|
|
|
|
lua_pushboolean(L,true);
|
|
|
|
|
result:=1;
|
|
|
|
|
except
|
|
|
|
|
on e:exception do
|
|
|
|
|
begin
|
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
|
lua_pushstring(L,e.message);
|
|
|
|
|
result:=2;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2023-03-03 23:04:54 +01:00
|
|
|
function memorystream_clear(L: PLua_State): integer; cdecl;
|
|
|
|
|
begin
|
|
|
|
|
tmemorystream(luaclass_getClassObject(L)).Clear;
|
|
|
|
|
result:=0;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-06-13 11:31:33 +00:00
|
|
|
procedure memorystream_addMetaData(L: PLua_state; metatable: integer; userdata: integer );
|
|
|
|
|
begin
|
|
|
|
|
stream_addMetaData(L, metatable, userdata);
|
2014-02-13 02:19:30 +00:00
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'loadFromFile', memorystream_loadFromFile);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'saveToFile', memorystream_saveToFile);
|
2019-12-23 18:26:28 +01:00
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'loadFromFileNoError', memorystream_loadFromFileNoError);
|
|
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'saveToFileNoError', memorystream_saveToFileNoError);
|
2023-03-03 23:04:54 +01:00
|
|
|
luaclass_addClassFunctionToTable(L, metatable, userdata, 'clear', memorystream_clear);
|
2013-06-13 11:31:33 +00:00
|
|
|
luaclass_addPropertyToTable(L, metatable, userdata, 'Memory', memorystream_getMemory, nil);
|
2013-01-06 14:15:17 +00:00
|
|
|
end;
|
|
|
|
|
|
2015-12-25 01:32:21 +01:00
|
|
|
function stringstream_getDataString(L: PLua_State): integer; cdecl;
|
|
|
|
|
var ss: TStringStream;
|
2015-12-25 11:49:44 +01:00
|
|
|
ms: TMemoryStream;
|
|
|
|
|
oldpos: integer;
|
2015-12-25 01:32:21 +01:00
|
|
|
begin
|
|
|
|
|
ss:=luaclass_getClassObject(L);
|
2015-12-25 11:49:44 +01:00
|
|
|
ms:=TMemoryStream.create;
|
|
|
|
|
oldpos:=ss.Position;
|
|
|
|
|
|
|
|
|
|
ss.position:=0;
|
|
|
|
|
ms.LoadFromStream(ss);
|
|
|
|
|
lua_pushlstring(L, ms.Memory, ms.Size);
|
2015-12-25 01:32:21 +01:00
|
|
|
result:=1;
|
2015-12-25 11:49:44 +01:00
|
|
|
|
|
|
|
|
ss.position:=oldpos;
|
|
|
|
|
ms.free;
|
2015-12-25 01:32:21 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure stringstream_addMetaData(L: PLua_state; metatable: integer; userdata: integer );
|
|
|
|
|
begin
|
2022-05-02 11:00:00 +02:00
|
|
|
memorystream_addMetaData(L, metatable, userdata);
|
2015-12-25 01:32:21 +01:00
|
|
|
luaclass_addPropertyToTable(L, metatable, userdata, 'DataString', stringstream_getDataString, nil);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
2013-01-06 14:16:25 +00:00
|
|
|
initialization
|
|
|
|
|
luaclass_register(TStream, stream_addMetaData);
|
2013-06-13 11:31:33 +00:00
|
|
|
luaclass_register(TMemoryStream, memorystream_addMetaData);
|
2015-12-25 01:32:21 +01:00
|
|
|
luaclass_register(TStringStream, stringstream_addMetaData);
|
2013-01-06 14:16:25 +00:00
|
|
|
|
2013-01-06 14:15:17 +00:00
|
|
|
end.
|
|
|
|
|
|