• R/O
  • SSH
  • HTTPS

green-candy: Commit


Commit MetaInfo

Revision11 (tree)
Zeit2022-01-13 09:58:31
Autorquiret

Log Message

- made the MTA:BLUE Lua Eir FileSystem module compile again

Ändern Zusammenfassung

Diff

--- blueMods/fileSystem/StdInc.h (revision 10)
+++ blueMods/fileSystem/StdInc.h (revision 11)
@@ -19,7 +19,7 @@
1919 /** MODULE SPECIFIC INFORMATION **/
2020 #define MODULE_NAME "Eir/GREEN FileSystem"
2121 #define MODULE_AUTHOR "The_GTA"
22-#define MODULE_VERSION 1.3
22+#define MODULE_VERSION 2.0
2323
2424 #pragma warning(disable: 4996)
2525
@@ -29,8 +29,8 @@
2929 #include "Common.h"
3030 #include <CFileSystem.h>
3131 #include "luaclass.h"
32-#include "../../lua_interpret/luafile.h"
33-#include "../../lua_interpret/luafilesystem.h"
32+#include "luafile.h"
33+#include "luafilesystem.h"
3434 #include "CFunctions.h"
3535 #include "include/ILuaModuleManager.h"
3636
--- blueMods/fileSystem/luafile.Utils.hxx (nonexistent)
+++ blueMods/fileSystem/luafile.Utils.hxx (revision 11)
@@ -0,0 +1,32 @@
1+/*****************************************************************************
2+*
3+* PROJECT: Lua Interpreter
4+* LICENSE: See LICENSE in the top level directory
5+* FILE: luafile.cpp
6+* PURPOSE: Lua filesystem utils
7+* DEVELOPERS: The_GTA <quiret@gmx.de>
8+*
9+* Multi Theft Auto is available from http://www.multitheftauto.com/
10+*
11+*****************************************************************************/
12+
13+#ifndef _LUAFILE_UTILS_
14+#define _LUAFILE_UTILS_
15+
16+static inline void luafile_pushStats( lua_State *L, fsOffsetNumber_t fileSize, const filesysStats& stats )
17+{
18+ lua_newtable( L );
19+
20+ int top = lua_gettop( L );
21+
22+ lua_pushnumber( L, (lua_Number)stats.atime );
23+ lua_setfield( L, top, "accessTime" );
24+ lua_pushnumber( L, (lua_Number)stats.ctime );;
25+ lua_setfield( L, top, "creationTime" );
26+ lua_pushnumber( L, (lua_Number)stats.mtime );
27+ lua_setfield( L, top, "modTime" );
28+ lua_pushnumber( L, (lua_Number)fileSize );
29+ lua_setfield( L, top, "size" );
30+}
31+
32+#endif //_LUAFILE_UITLS_
--- blueMods/fileSystem/luafile.cpp (nonexistent)
+++ blueMods/fileSystem/luafile.cpp (revision 11)
@@ -0,0 +1,481 @@
1+/*****************************************************************************
2+*
3+* PROJECT: Lua Interpreter
4+* LICENSE: See LICENSE in the top level directory
5+* FILE: luafile.cpp
6+* PURPOSE: Test environment for the filesystem
7+* DEVELOPERS: Martin Turski <quiret@gmx.de>
8+*
9+* For documentation visit http://wiki.mtasa.com/wiki/MTA:Eir/FileSystem/
10+*
11+* Multi Theft Auto is available from http://www.multitheftauto.com/
12+*
13+*****************************************************************************/
14+
15+#include <StdInc.h>
16+#include "luafile.Utils.hxx"
17+
18+using namespace std;
19+
20+static int luafile_onIndex( lua_State *lua )
21+{
22+ lua_pushvalue( lua, 2 );
23+ lua_gettable( lua, lua_upvalueindex( 1 ) );
24+
25+ if ( lua_type( lua, 3 ) == LUA_TBOOLEAN && lua_toboolean( lua, 3 ) == false )
26+ return 0;
27+
28+ lua_pop( lua, 1 );
29+ lua_gettable( lua, lua_upvalueindex( 2 ) );
30+ return 1;
31+}
32+
33+static int luafile_onNewindex( lua_State *lua )
34+{
35+ // We do not allow any modification from outside
36+ return 0;
37+}
38+
39+static int luafile_read( lua_State *L )
40+{
41+ luaL_checktype( L, 1, LUA_TNUMBER );
42+
43+ CFile *file = (CFile*)lua_getmethodtrans( L );
44+
45+ long byteCount = (long)lua_tonumber( L, 1 );
46+
47+ LUA_CHECK( byteCount >= 0 );
48+
49+ size_t bytesRead = (size_t)byteCount;
50+
51+ if ( bytesRead == 0 )
52+ {
53+ lua_pushlstring( L, "", 0 );
54+ return 1;
55+ }
56+
57+ std::vector <char> buf( bytesRead );
58+
59+#ifdef FU_CLASS
60+ bytesRead = file->Read( &buf[0], bytesRead );
61+
62+ if ( bytesRead == 0 )
63+ {
64+ lua_pushlstring( L, "", 0 );
65+ return 1;
66+ }
67+#else
68+ bytesRead = file->Read( &buf[0], 1, bytesRead );
69+#endif
70+
71+ lua_pushlstring( L, &buf[0], bytesRead );
72+ return 1;
73+}
74+
75+inline bool IsUnsigned( unsigned char )
76+{
77+ return true;
78+}
79+inline bool IsUnsigned( char )
80+{
81+ return false;
82+}
83+inline bool IsUnsigned( unsigned short )
84+{
85+ return true;
86+}
87+inline bool IsUnsigned( short )
88+{
89+ return false;
90+}
91+inline bool IsUnsigned( unsigned int )
92+{
93+ return true;
94+}
95+inline bool IsUnsigned( int )
96+{
97+ return false;
98+}
99+
100+struct _CheckDefaultValidity
101+{
102+ template <typename numberType>
103+ AINLINE bool IsNumberValid( lua_Number number )
104+ {
105+ return true;
106+ }
107+};
108+
109+template <typename numberType>
110+AINLINE int _fileReadNumber( lua_State *L )
111+{
112+ numberType out_num;
113+
114+ LUA_CHECK(
115+ ((CFile*)lua_getmethodtrans( L ))->ReadStruct( out_num )
116+ );
117+
118+ lua_pushnumber( L, (lua_Number)out_num );
119+ return 1;
120+}
121+
122+// We use the computer systems analogy char<->byte.
123+static int luafile_readByte( lua_State *L )
124+{
125+ return _fileReadNumber <fsChar_t> ( L );
126+}
127+static int luafile_readUByte( lua_State *L )
128+{
129+ return _fileReadNumber <fsUChar_t> ( L );
130+}
131+static int luafile_readShort( lua_State *L )
132+{
133+ return _fileReadNumber <fsShort_t> ( L );
134+}
135+static int luafile_readUShort( lua_State *L )
136+{
137+ return _fileReadNumber <fsUShort_t> ( L );
138+}
139+static int luafile_readInt( lua_State *L )
140+{
141+ return _fileReadNumber <fsInt_t> ( L );
142+}
143+static int luafile_readUInt( lua_State *L )
144+{
145+ return _fileReadNumber <fsUInt_t> ( L );
146+}
147+static int luafile_readWideInt( lua_State *L )
148+{
149+ return _fileReadNumber <fsWideInt_t> ( L );
150+}
151+static int luafile_readUWideInt( lua_State *L )
152+{
153+ return _fileReadNumber <fsUWideInt_t> ( L );
154+}
155+
156+static int luafile_readFloat( lua_State *L )
157+{
158+ return _fileReadNumber <fsFloat_t> ( L );
159+}
160+static int luafile_readDouble( lua_State *L )
161+{
162+ return _fileReadNumber <fsDouble_t> ( L );
163+}
164+
165+static int luafile_readBoolean( lua_State *L )
166+{
167+ bool out_b;
168+
169+ bool successful =
170+ ((CFile*)lua_getmethodtrans( L ))->ReadBool( out_b );
171+
172+ if ( !successful )
173+ lua_pushnil( L );
174+ else
175+ lua_pushboolean( L, out_b );
176+
177+ return 1;
178+}
179+
180+template <typename numberType, typename validityChecker>
181+AINLINE int _writeFileNumber( lua_State *L, const char *methodName )
182+{
183+ luaL_checktype( L, 1, LUA_TNUMBER );
184+
185+ lua_Number number = lua_tonumber( L, 1 );
186+
187+ // Check validity of number.
188+ {
189+ validityChecker checker;
190+
191+ if ( !checker.template IsNumberValid <numberType> ( number ) )
192+ {
193+ // todo: print a warning.
194+ }
195+ }
196+
197+ numberType realNum = (numberType)number;
198+
199+ lua_pushnumber( L, ((CFile*)lua_getmethodtrans( L ))->WriteStruct( realNum ) );
200+ return 1;
201+}
202+
203+static int luafile_write( lua_State *L )
204+{
205+ luaL_checktype( L, 1, LUA_TSTRING );
206+
207+ size_t len;
208+ const char *string = lua_tolstring( L, 1, &len );
209+
210+#ifndef FU_CLASS
211+ lua_pushwideinteger( L, ((CFile*)lua_getmethodtrans( L ))->Write( string, len ) );
212+#else
213+ lua_pushnumber( L, ((CFile*)lua_getmethodtrans( L ))->Write( string, len ) );
214+#endif
215+ return 1;
216+}
217+
218+static int luafile_writeByte( lua_State *L )
219+{
220+ return _writeFileNumber <fsChar_t, _CheckDefaultValidity> ( L, "writeByte" );
221+}
222+static int luafile_writeUByte( lua_State *L )
223+{
224+ return _writeFileNumber <fsUChar_t, _CheckDefaultValidity> ( L, "writeUByte" );
225+}
226+static int luafile_writeShort( lua_State *L )
227+{
228+ return _writeFileNumber <fsShort_t, _CheckDefaultValidity> ( L, "writeShort" );
229+}
230+static int luafile_writeUShort( lua_State *L )
231+{
232+ return _writeFileNumber <fsUShort_t, _CheckDefaultValidity> ( L, "writeUShort" );
233+}
234+static int luafile_writeInt( lua_State *L )
235+{
236+ return _writeFileNumber <fsInt_t, _CheckDefaultValidity> ( L, "writeInt" );
237+}
238+static int luafile_writeUInt( lua_State *L )
239+{
240+ return _writeFileNumber <fsUInt_t, _CheckDefaultValidity> ( L, "writeUInt" );
241+}
242+static int luafile_writeWideInt( lua_State *L )
243+{
244+ return _writeFileNumber <fsWideInt_t, _CheckDefaultValidity> ( L, "writeWideInt" );
245+}
246+static int luafile_writeUWideInt( lua_State *L )
247+{
248+ return _writeFileNumber <fsUWideInt_t, _CheckDefaultValidity> ( L, "writeUWideInt" );
249+}
250+
251+static int luafile_writeFloat( lua_State *L )
252+{
253+ return _writeFileNumber <fsFloat_t, _CheckDefaultValidity> ( L, "writeFloat" );
254+}
255+static int luafile_writeDouble( lua_State *L )
256+{
257+ return _writeFileNumber <fsDouble_t, _CheckDefaultValidity> ( L, "writeDouble" );
258+}
259+
260+static int luafile_writeBoolean( lua_State *L )
261+{
262+ luaL_checktype( L, 1, LUA_TBOOLEAN );
263+ lua_pushnumber( L,
264+ ((CFile*)lua_getmethodtrans( L ))->WriteBool(
265+ ( lua_toboolean( L, 1 ) != 0 )
266+ )
267+ );
268+ return 1;
269+}
270+
271+static int luafile_size( lua_State *L )
272+{
273+ lua_pushnumber( L, ((CFile*)lua_getmethodtrans( L ))->GetSize() );
274+ return 1;
275+}
276+
277+static int luafile_stat( lua_State *L )
278+{
279+ filesysStats stats;
280+
281+ CFile *filePtr = ((CFile*)lua_getmethodtrans( L ));
282+
283+ if ( !filePtr->QueryStats( stats ) )
284+ return 0;
285+
286+ fsOffsetNumber_t fileSize = filePtr->GetSizeNative();
287+
288+ luafile_pushStats( L, fileSize, stats );
289+ return 1;
290+}
291+
292+static int luafile_tell( lua_State *L )
293+{
294+ // (Attempt to) Get a large FileSystem number that stands for the current seek.
295+ fsOffsetNumber_t filePosition = ((CFile*)lua_getmethodtrans( L ))->TellNative();
296+
297+ lua_Number num = (lua_Number)filePosition;
298+
299+ lua_pushnumber( L, num );
300+ return 1;
301+}
302+
303+static int luafile_seek( lua_State *L )
304+{
305+ luaL_checktype( L, 1, LUA_TNUMBER );
306+
307+ int seekType;
308+
309+ switch( lua_type( L, 2 ) )
310+ {
311+ case LUA_TNUMBER:
312+ if ( (seekType = (int)lua_tonumber( L, 2 )) < 0 || seekType > SEEK_END )
313+ goto defMethod;
314+
315+ break;
316+ case LUA_TSTRING:
317+ {
318+ const char *type = lua_tostring( L, 2 );
319+
320+ if ( strcmp( type, "cur" ) == 0 )
321+ {
322+ seekType = SEEK_CUR;
323+ break;
324+ }
325+ else if ( strcmp( type, "set" ) == 0 )
326+ {
327+ seekType = SEEK_SET;
328+ break;
329+ }
330+ else if ( strcmp( type, "end" ) == 0 )
331+ {
332+ seekType = SEEK_END;
333+ break;
334+ }
335+ }
336+ default:
337+defMethod:
338+ lua_pushstring( L, "unknown seekmode" );
339+ lua_error( L );
340+ return -1;
341+ }
342+
343+ // Convert lua_Number into a large FileSystem number.
344+ lua_Number num = lua_tonumber( L, 1 );
345+
346+ fsOffsetNumber_t seekOffset = (fsOffsetNumber_t)num;
347+
348+ lua_pushnumber( L, ((CFile*)lua_getmethodtrans( L ))->SeekNative( seekOffset, seekType ) );
349+ return 1;
350+}
351+
352+static int luafile_eof( lua_State *L )
353+{
354+ lua_pushboolean( L, ((CFile*)lua_getmethodtrans( L ))->IsEOF() );
355+ return 1;
356+}
357+
358+static int luafile_flush( lua_State *L )
359+{
360+ ((CFile*)lua_getmethodtrans( L ))->Flush();
361+ lua_pushboolean( L, true );
362+ return 1;
363+}
364+
365+static int luafile_seekEnd( lua_State *L )
366+{
367+ ((CFile*)lua_getmethodtrans( L ))->SetSeekEnd();
368+ lua_pushboolean( L, true );
369+ return 1;
370+}
371+
372+static int luafile_isWritable( lua_State *L )
373+{
374+ lua_pushboolean( L, ((CFile*)lua_getmethodtrans( L ))->IsWriteable() );
375+ return 1;
376+}
377+
378+static int luafile_isReadable( lua_State *L )
379+{
380+ lua_pushboolean( L, ((CFile*)lua_getmethodtrans( L ))->IsReadable() );
381+ return 1;
382+}
383+
384+static int luafile_destroy( lua_State *lua )
385+{
386+ delete (CFile*)lua_touserdata( lua, lua_upvalueindex( 1 ) );
387+
388+ return 0;
389+}
390+
391+static const luaL_Reg fileInterface_sys[] =
392+{
393+#ifndef FU_CLASS
394+ { "__newindex", luafile_onNewindex },
395+#endif
396+ { "destroy", luafile_destroy },
397+#ifndef FU_CLASS
398+ { NULL, NULL }
399+};
400+
401+static const luaL_Reg fileInterface[] =
402+{
403+#endif
404+ { "read", luafile_read },
405+ { "readByte", luafile_readByte },
406+ { "readUByte", luafile_readUByte },
407+ { "readShort", luafile_readShort },
408+ { "readUShort", luafile_readUShort },
409+ { "readInt", luafile_readInt },
410+ { "readUInt", luafile_readUInt },
411+ { "readWideInt", luafile_readWideInt },
412+ { "readUWideInt", luafile_readUWideInt },
413+ { "readFloat", luafile_readFloat },
414+ { "readDouble", luafile_readDouble },
415+ { "readBoolean", luafile_readBoolean },
416+ { "write", luafile_write },
417+ { "writeByte", luafile_writeByte },
418+ { "writeUByte", luafile_writeUByte },
419+ { "writeShort", luafile_writeShort },
420+ { "writeUShort", luafile_writeUShort },
421+ { "writeInt", luafile_writeInt },
422+ { "writeUInt", luafile_writeUInt },
423+ { "writeWideInt", luafile_writeWideInt },
424+ { "wrideUWideInt", luafile_writeUWideInt },
425+ { "writeFloat", luafile_writeFloat },
426+ { "writeDouble", luafile_writeDouble },
427+ { "writeBoolean", luafile_writeBoolean },
428+ { "size", luafile_size },
429+ { "stat", luafile_stat },
430+ { "tell", luafile_tell },
431+ { "seek", luafile_seek },
432+ { "eof", luafile_eof },
433+ { "flush", luafile_flush },
434+ { "seekEnd", luafile_seekEnd },
435+ { "isWritable", luafile_isWritable },
436+ { "isReadable", luafile_isReadable },
437+ { NULL, NULL }
438+};
439+
440+int luaconstructor_file( lua_State *lua )
441+{
442+#ifndef FU_CLASS
443+ CFile *file = (CFile*)lua_touserdata( lua, lua_upvalueindex( 1 ) );
444+
445+ // Register as file
446+ ILuaClass *j = lua_refclass( lua, 1 );
447+ j->SetTransmit( LUACLASS_FILE, file );
448+
449+ lua_basicextend( lua );
450+
451+ // Create the illegal access table
452+ lua_newtable( lua );
453+ lua_pushboolean( lua, false );
454+ lua_setfield( lua, 2, "__index" );
455+ lua_pushboolean( lua, false );
456+ lua_setfield( lua, 2, "__newindex" );
457+
458+ // We need the class outer environment
459+ j->PushOuterEnvironment( lua );
460+
461+ lua_pushcclosure( lua, luafile_onIndex, 2 );
462+ lua_setfield( lua, LUA_ENVIRONINDEX, "__index" );
463+
464+ lua_pushvalue( lua, lua_upvalueindex( 1 ) );
465+ lua_setfield( lua, LUA_ENVIRONINDEX, "ioptr" );
466+
467+ j->RegisterInterfaceTrans( lua, fileInterface, 0, LUACLASS_FILE );
468+#endif
469+
470+ lua_pushvalue( lua, LUA_ENVIRONINDEX );
471+ lua_pushvalue( lua, lua_upvalueindex( 1 ) );
472+ luaL_openlib( lua, NULL, fileInterface_sys, 1 );
473+
474+ lua_pushlstring( lua, "file", 4 );
475+ lua_setfield( lua, LUA_ENVIRONINDEX, "__type" );
476+ return 0;
477+}
478+
479+void luafile_open( lua_State *lua )
480+{
481+}
--- blueMods/fileSystem/luafile.h (nonexistent)
+++ blueMods/fileSystem/luafile.h (revision 11)
@@ -0,0 +1,21 @@
1+/*****************************************************************************
2+*
3+* PROJECT: Lua Interpreter
4+* LICENSE: See LICENSE in the top level directory
5+* FILE: luafile.h
6+* PURPOSE: File environment header
7+* DEVELOPERS: Martin Turski <quiret@gmx.de>
8+*
9+* Multi Theft Auto is available from http://www.multitheftauto.com/
10+*
11+*****************************************************************************/
12+
13+#ifndef _FILELIB_
14+#define _FILELIB_
15+
16+#define LUACLASS_FILE 136
17+
18+int luaconstructor_file( lua_State *lua );
19+void luafile_open( lua_State *lua );
20+
21+#endif //_FILELIB_
--- blueMods/fileSystem/luafilesystem.cpp (nonexistent)
+++ blueMods/fileSystem/luafilesystem.cpp (revision 11)
@@ -0,0 +1,678 @@
1+/*****************************************************************************
2+*
3+* PROJECT: Lua Interpreter
4+* LICENSE: See LICENSE in the top level directory
5+* FILE: luafilesystem.cpp
6+* PURPOSE: Lua filesystem access
7+* DEVELOPERS: Martin Turski <quiret@gmx.de>
8+*
9+* For documentation visit http://wiki.mtasa.com/wiki/MTA:Eir/FileSystem/
10+*
11+* Multi Theft Auto is available from http://www.multitheftauto.com/
12+*
13+*****************************************************************************/
14+
15+#include <StdInc.h>
16+#include "luafile.Utils.hxx"
17+
18+extern CFileSystemInterface *pubFileSystem;
19+
20+static int filesystem_open( lua_State *L )
21+{
22+ int theTop = lua_gettop( L );
23+
24+ luaL_checktype( L, 1, LUA_TSTRING );
25+ luaL_checktype( L, 2, LUA_TSTRING );
26+
27+ lua_settop( L, 2 );
28+
29+ CFile *file = ((CFileTranslator*)lua_getmethodtrans( L ))->Open( lua_tostring( L, 1 ), lua_tostring( L, 2 ) );
30+
31+ if ( !file )
32+ {
33+ lua_pushboolean( L, false );
34+ return 1;
35+ }
36+
37+ lua_pushlightuserdata( L, file );
38+ lua_pushcclosure( L, luaconstructor_file, 1 );
39+#ifdef FU_CLASS
40+ lua_newclass( L, (unk*)file );
41+#else
42+ lua_newclass( L );
43+
44+ // Register the file
45+ lua_getfield( L, 3, "setParent" );
46+ lua_getmethodclass( L )->Push( L );
47+ lua_call( L, 1, 0 );
48+#endif
49+ return 1;
50+}
51+
52+static int filesystem_exists( lua_State *L )
53+{
54+ luaL_checktype( L, 1, LUA_TSTRING );
55+ lua_pushboolean( L, ((CFileTranslator*)lua_getmethodtrans( L ))->Exists( lua_tostring( L, 1 ) ) );
56+ return 1;
57+}
58+
59+static int filesystem_createDir( lua_State *L )
60+{
61+ luaL_checktype( L, 1, LUA_TSTRING );
62+ lua_pushboolean( L, ((CFileTranslator*)lua_getmethodtrans( L ))->CreateDir( lua_tostring( L, 1 ) ) );
63+ return 1;
64+}
65+
66+static int filesystem_chdir( lua_State *L )
67+{
68+ luaL_checktype( L, 1, LUA_TSTRING );
69+ lua_pushboolean( L, ((CFileTranslator*)lua_getmethodtrans( L ))->ChangeDirectory( lua_tostring( L, 1 ) ) );
70+ return 1;
71+}
72+
73+static int filesystem_delete( lua_State *L )
74+{
75+ luaL_checktype( L, 1, LUA_TSTRING );
76+ lua_pushboolean( L, ((CFileTranslator*)lua_getmethodtrans( L ))->Delete( lua_tostring( L, 1 ) ) );
77+ return 1;
78+}
79+
80+static int filesystem_copy( lua_State *L )
81+{
82+ luaL_checktype( L, 1, LUA_TSTRING );
83+ luaL_checktype( L, 2, LUA_TSTRING );
84+ lua_pushboolean( L, ((CFileTranslator*)lua_getmethodtrans( L ))->Copy( lua_tostring( L, 1 ), lua_tostring( L, 2 ) ) );
85+ return 1;
86+}
87+
88+static int filesystem_rename( lua_State *L )
89+{
90+ luaL_checktype( L, 1, LUA_TSTRING );
91+ luaL_checktype( L, 2, LUA_TSTRING );
92+ lua_pushboolean( L, ((CFileTranslator*)lua_getmethodtrans( L ))->Rename( lua_tostring( L, 1 ), lua_tostring( L, 2 ) ) );
93+ return 1;
94+}
95+
96+static int filesystem_size( lua_State *L )
97+{
98+ luaL_checktype( L, 1, LUA_TSTRING );
99+#ifndef FU_CLASS
100+ lua_pushwideinteger( L, (lua_WideInteger)((CFileTranslator*)lua_getmethodtrans( L ))->Size( lua_tostring( L, 1 ) ) );
101+#else
102+ lua_pushnumber( L, (lua_Number)((CFileTranslator*)lua_getmethodtrans( L ))->Size( lua_tostring( L, 1 ) ) );
103+#endif
104+ return 1;
105+}
106+
107+static int filesystem_stat( lua_State *L )
108+{
109+ luaL_checktype( L, 1, LUA_TSTRING );
110+
111+ filesysStats stats;
112+
113+ CFileTranslator *trans = ((CFileTranslator*)lua_getmethodtrans( L ));
114+
115+ if ( !trans->QueryStats( lua_tostring( L, 1 ), stats ) )
116+ {
117+ lua_pushboolean( L, false );
118+ return 1;
119+ }
120+
121+ size_t fileSize = trans->Size( lua_tostring( L, 1 ) );
122+
123+ luafile_pushStats( L, fileSize, stats );
124+ return 1;
125+}
126+
127+static int filesystem_relPath( lua_State *L )
128+{
129+ const char *src = lua_tostring( L, 1 );
130+ filePath path;
131+
132+ if ( !src )
133+ src = "";
134+
135+ if ( !((CFileTranslator*)lua_getmethodtrans( L ))->GetRelativePath( src, true, path ) )
136+ {
137+ lua_pushboolean( L, false );
138+ return 1;
139+ }
140+
141+ auto ansiPath = path.convert_ansi <FileSysCommonAllocator> ();
142+
143+ lua_pushlstring( L, ansiPath.GetConstString(), ansiPath.GetLength() );
144+ return 1;
145+}
146+
147+static int filesystem_relPathRoot( lua_State *L )
148+{
149+ const char *src = lua_tostring( L, 1 );
150+ filePath path;
151+
152+ if ( !src )
153+ src = "";
154+
155+ if ( !((CFileTranslator*)lua_getmethodtrans( L ))->GetRelativePathFromRoot( src, true, path ) )
156+ {
157+ lua_pushboolean( L, false );
158+ return 1;
159+ }
160+
161+ auto ansiPath = path.convert_ansi <FileSysCommonAllocator> ();
162+
163+ lua_pushlstring( L, ansiPath.GetConstString(), ansiPath.GetLength() );
164+ return 1;
165+}
166+
167+static int filesystem_absPath( lua_State *L )
168+{
169+ const char *src = lua_tostring( L, 1 );
170+ filePath path;
171+
172+ if ( !src )
173+ src = "";
174+
175+ if ( !((CFileTranslator*)lua_getmethodtrans( L ))->GetFullPath( src, true, path ) )
176+ {
177+ lua_pushboolean( L, false );
178+ return 1;
179+ }
180+
181+ auto ansiPath = path.convert_ansi <FileSysCommonAllocator> ();
182+
183+ lua_pushlstring( L, ansiPath.GetConstString(), ansiPath.GetLength() );
184+ return 1;
185+}
186+
187+static int filesystem_absPathRoot( lua_State *L )
188+{
189+ const char *src = lua_tostring( L, 1 );
190+ filePath path;
191+
192+ if ( !src )
193+ src = "";
194+
195+ if ( !((CFileTranslator*)lua_getmethodtrans( L ))->GetFullPathFromRoot( src, true, path ) )
196+ {
197+ lua_pushboolean( L, false );
198+ return 1;
199+ }
200+
201+ auto ansiPath = path.convert_ansi <FileSysCommonAllocator> ();
202+
203+ lua_pushlstring( L, ansiPath.GetConstString(), ansiPath.GetLength() );
204+ return 1;
205+}
206+
207+static void lua_findScanCallback( const filePath& path, void *ud )
208+{
209+ auto ansiFilePath = path.convert_ansi <FileSysCommonAllocator> ();
210+
211+ lua_pushlstring( (lua_State*)ud, ansiFilePath.GetConstString(), ansiFilePath.GetLength() );
212+ luaL_ref( (lua_State*)ud, -2 );
213+}
214+
215+static int filesystem_scanDir( lua_State *lua )
216+{
217+ luaL_checktype( lua, 1, LUA_TSTRING );
218+
219+ const char *path = lua_tostring( lua, 1 );
220+ const char *wildcard;
221+ bool recursive;
222+
223+ int top = lua_gettop( lua );
224+
225+ if ( top > 1 )
226+ {
227+ wildcard = lua_tostring( lua, 2 );
228+
229+ if ( top > 2 )
230+ recursive = ( lua_toboolean( lua, 3 ) != 0 );
231+ else
232+ recursive = false;
233+ }
234+ else
235+ {
236+ wildcard = "*";
237+ recursive = false;
238+ }
239+
240+ lua_newtable( lua );
241+
242+ ((CFileTranslator*)lua_getmethodtrans( lua ))->ScanDirectory( path, wildcard, recursive, lua_findScanCallback, lua_findScanCallback, lua );
243+ return 1;
244+}
245+
246+static int filesystem_getFiles( lua_State *lua )
247+{
248+ luaL_checktype( lua, 1, LUA_TSTRING );
249+
250+ const char *path = lua_tostring( lua, 1 );
251+ const char *wildcard;
252+ bool recursive;
253+
254+ int top = lua_gettop( lua );
255+
256+ if ( top > 1 )
257+ {
258+ wildcard = lua_tostring( lua, 2 );
259+
260+ if ( top > 2 )
261+ recursive = ( lua_toboolean( lua, 3 ) != 0 );
262+ else
263+ recursive = false;
264+ }
265+ else
266+ {
267+ wildcard = "*";
268+ recursive = false;
269+ }
270+
271+ lua_newtable( lua );
272+
273+ ((CFileTranslator*)lua_getmethodtrans( lua ))->ScanDirectory( path, wildcard, recursive, 0, lua_findScanCallback, lua );
274+ return 1;
275+}
276+
277+static int filesystem_getDirs( lua_State *lua )
278+{
279+ luaL_checktype( lua, 1, LUA_TSTRING );
280+
281+ const char *path = lua_tostring( lua, 1 );
282+ bool recursive;
283+
284+ if ( lua_gettop( lua ) > 1 )
285+ recursive = ( lua_toboolean( lua, 2 ) != 0 );
286+ else
287+ recursive = false;
288+
289+ lua_newtable( lua );
290+
291+ ((CFileTranslator*)lua_getmethodtrans( lua ))->ScanDirectory( path, "*", recursive, lua_findScanCallback, 0, lua );
292+ return 1;
293+}
294+
295+static void filesystem_exfilecb( const filePath& path, void *ud )
296+{
297+ auto ansiPath = path.convert_ansi <FileSysCommonAllocator> ();
298+
299+ lua_State *L = (lua_State*)ud;
300+ lua_pushvalue( L, 4 );
301+ lua_pushlstring( L, ansiPath.GetConstString(), ansiPath.GetLength() );
302+ lua_call( L, 1, 0 );
303+}
304+
305+static void filesystem_exdircb( const filePath& path, void *ud )
306+{
307+ auto ansiPath = path.convert_ansi <FileSysCommonAllocator> ();
308+
309+ lua_State *L = (lua_State*)ud;
310+ lua_pushvalue( L, 3 );
311+ lua_pushlstring( L, ansiPath.GetConstString(), ansiPath.GetLength() );
312+ lua_call( L, 1, 0 );
313+}
314+
315+static int filesystem_scanDirEx( lua_State *lua )
316+{
317+ luaL_checktype( lua, 1, LUA_TSTRING );
318+ luaL_checktype( lua, 2, LUA_TSTRING );
319+
320+ ((CFileTranslator*)lua_getmethodtrans( lua ))->ScanDirectory(
321+ lua_tostring( lua, 1 ),
322+ lua_tostring( lua, 2 ),
323+ ( lua_toboolean( lua, 5 ) != 0 ),
324+ lua_type( lua, 3 ) == LUA_TFUNCTION ? filesystem_exdircb : NULL,
325+ lua_type( lua, 4 ) == LUA_TFUNCTION ? filesystem_exfilecb : NULL, lua );
326+
327+ return 0;
328+}
329+
330+static int filesystem_destroy( lua_State *L )
331+{
332+ delete (CFileTranslator*)lua_touserdata( L, lua_upvalueindex( 1 ) );
333+
334+ return 0;
335+}
336+
337+static const luaL_Reg fsys_methods[] =
338+{
339+ { "destroy", filesystem_destroy },
340+#ifndef FU_CLASS
341+ { NULL, NULL }
342+};
343+
344+static const luaL_Reg fsys_methods_trans[] =
345+{
346+#endif //FU_CLASS
347+ { "open", filesystem_open },
348+ { "exists", filesystem_exists },
349+ { "createDir", filesystem_createDir },
350+ { "chdir", filesystem_chdir },
351+ { "delete", filesystem_delete },
352+ { "copy", filesystem_copy },
353+ { "rename", filesystem_rename },
354+ { "size", filesystem_size },
355+ { "stat", filesystem_stat },
356+ { "relPath", filesystem_relPath },
357+ { "relPathRoot", filesystem_relPathRoot },
358+ { "absPath", filesystem_absPath },
359+ { "absPathRoot", filesystem_absPathRoot },
360+ { "scanDir", filesystem_scanDir },
361+ { "scanDirEx", filesystem_scanDirEx },
362+ { "getDirs", filesystem_getDirs },
363+ { "getFiles", filesystem_getFiles },
364+ { NULL, NULL }
365+};
366+
367+int luafsys_constructor( lua_State *L )
368+{
369+#ifndef FU_CLASS
370+ CFileTranslator *trans = (CFileTranslator*)lua_touserdata( L, lua_upvalueindex( 1 ) );
371+
372+ ILuaClass *j = lua_refclass( L, 1 );
373+ j->SetTransmit( LUACLASS_FILETRANSLATOR, trans );
374+
375+ j->RegisterInterfaceTrans( L, fsys_methods_trans, 0, LUACLASS_FILETRANSLATOR );
376+#endif //FU_CLASS
377+
378+ lua_pushvalue( L, LUA_ENVIRONINDEX );
379+ lua_pushvalue( L, lua_upvalueindex( 1 ) );
380+ lua_getfield( L, LUA_ENVIRONINDEX, "this" );
381+ luaL_openlib( L, NULL, fsys_methods, 2 );
382+
383+ lua_pushlstring( L, "filesystem", 10 );
384+ lua_setfield( L, LUA_ENVIRONINDEX, "__type" );
385+ return 0;
386+}
387+
388+void luafsys_pushroot( lua_State *L, CFileTranslator *root )
389+{
390+ lua_pushlightuserdata( L, root );
391+ lua_pushcclosure( L, luafsys_constructor, 1 );
392+#ifdef FU_CLASS
393+ lua_newclass( L, (unk*)root );
394+#else
395+ lua_newclass( L );
396+#endif
397+}
398+
399+int luafsys_createTranslator( lua_State *L )
400+{
401+ luaL_checktype( L, 1, LUA_TSTRING );
402+
403+ CFileTranslator *root = pubFileSystem->CreateTranslator( lua_tostring( L, 1 ) );
404+
405+ if ( !root )
406+ {
407+ lua_pushboolean( L, false );
408+ return 1;
409+ }
410+
411+ luafsys_pushroot( L, root );
412+ return 1;
413+}
414+
415+#ifndef FU_CLASS
416+static int archive_save( lua_State *L )
417+{
418+ ILuaClass *j = lua_refclass( L, lua_upvalueindex( 1 ) );
419+
420+ CFile *file;
421+
422+ LUA_CHECK( j->GetTransmit( LUACLASS_FILE, (void*&)file ) && file->IsWriteable() );
423+
424+ ((CArchiveTranslator*)lua_touserdata( L, lua_upvalueindex( 2 ) ))->Save();
425+ lua_pushboolean( L, true );
426+ return 1;
427+}
428+
429+static int archive_destroy( lua_State *L )
430+{
431+ // Decrement the file reference count
432+ ILuaClass *j = lua_refclass( L, lua_upvalueindex( 1 ) );
433+ j->DecrementMethodStack( L );
434+
435+ return 0;
436+}
437+
438+static const luaL_Reg archiveLib[] =
439+{
440+ { "save", archive_save },
441+ { "destroy", archive_destroy },
442+ { NULL, NULL }
443+};
444+
445+static int archive_constructor( lua_State *L )
446+{
447+ lua_pushvalue( L, LUA_ENVIRONINDEX );
448+ lua_pushvalue( L, lua_upvalueindex( 1 ) );
449+ lua_pushvalue( L, lua_upvalueindex( 2 ) );
450+ luaL_openlib( L, NULL, archiveLib, 2 );
451+ return 0;
452+}
453+
454+template <typename archiveHandler>
455+static AINLINE int _archiveTranslatorFunctor( lua_State *L, archiveHandler& cb )
456+{
457+ luaL_checktype( L, 1, LUA_TCLASS );
458+
459+ ILuaClass *j = lua_refclass( L, 1 );
460+
461+ // Grab the file interface
462+ CFile *file;
463+
464+ if ( !j->GetTransmit( LUACLASS_FILE, (void*&)file ) )
465+ throw lua_exception( L, LUA_ERRRUN, "expected file at archive creation" );
466+
467+ // Attempt to read an archive.
468+ CArchiveTranslator *root = cb.CreateTranslator( file );
469+
470+ // Check that we actually suceeded in creating the archive
471+ LUA_CHECK( root );
472+
473+ // Keep the file alive during archive business
474+ j->IncrementMethodStack( L );
475+
476+ luafsys_pushroot( L, root );
477+
478+ // Extend the fileTranslator class
479+ lua_pushvalue( L, 1 );
480+ lua_pushlightuserdata( L, root );
481+ lua_pushcclosure( L, archive_constructor, 2 );
482+ luaJ_extend( L, 2, 0 );
483+ return 1;
484+}
485+
486+struct archiveTranslatorOpener
487+{
488+ AINLINE CArchiveTranslator* CreateTranslator( CFile *file )
489+ {
490+ return pubFileSystem->OpenArchive( *file );
491+ }
492+};
493+
494+int luafsys_createArchiveTranslator( lua_State *L )
495+{
496+ archiveTranslatorOpener opener;
497+
498+ return _archiveTranslatorFunctor( L, opener );
499+}
500+
501+struct zipArchiveTranslatorCreator
502+{
503+ AINLINE CArchiveTranslator* CreateTranslator( CFile *file )
504+ {
505+ return pubFileSystem->CreateZIPArchive( *file );
506+ }
507+};
508+
509+int luafsys_createZIPArchive( lua_State *L )
510+{
511+ zipArchiveTranslatorCreator creator;
512+
513+ return _archiveTranslatorFunctor( L, creator );
514+}
515+
516+static int luafsys_copyFile( lua_State *L )
517+{
518+ CFileTranslator *srcTranslator = lua_readclass <CFileTranslator> ( L, 1, LUACLASS_FILETRANSLATOR );
519+ const char *srcPath = lua_tostring( L, 2 );
520+ CFileTranslator *dstTranslator = lua_readclass <CFileTranslator> ( L, 3, LUACLASS_FILETRANSLATOR );
521+ const char *dstPath = lua_tostring( L, 4 );
522+
523+ LUA_CHECK( srcTranslator && srcPath && dstTranslator && dstPath );
524+
525+ bool success = FileSystem::FileCopy( srcTranslator, srcPath, dstTranslator, dstPath );
526+
527+ lua_pushboolean( L, success );
528+ return 1;
529+}
530+
531+static int luafsys_copyStream( lua_State *L )
532+{
533+ CFile *srcStream = lua_readclass <CFile> ( L, 1, LUACLASS_FILE );
534+ CFile *dstStream = lua_readclass <CFile> ( L, 2, LUACLASS_FILE );
535+
536+ LUA_CHECK( srcStream && dstStream );
537+
538+ FileSystem::StreamCopy( *srcStream, *dstStream );
539+
540+ lua_pushboolean( L, true );
541+ return 1;
542+}
543+
544+static int luafsys_copyStreamCount( lua_State *L )
545+{
546+ CFile *srcStream = lua_readclass <CFile> ( L, 1, LUACLASS_FILE );
547+ CFile *dstStream = lua_readclass <CFile> ( L, 2, LUACLASS_FILE );
548+ int count = (int)lua_tonumber( L, 3 );
549+
550+ LUA_CHECK( srcStream && dstStream );
551+
552+ LUA_CHECK( count > 0 );
553+
554+ FileSystem::StreamCopyCount( *srcStream, *dstStream, count );
555+
556+ lua_pushboolean( L, true );
557+ return 1;
558+}
559+#endif //FU_CLASS
560+
561+static int luafsys_pathToFilename( lua_State *L )
562+{
563+ luaL_checktype( L, 1, LUA_TSTRING );
564+ luaL_checktype( L, 2, LUA_TBOOLEAN );
565+
566+ const char *path = lua_tostring( L, 1 );
567+ bool includeExtension = ( lua_toboolean( L, 2 ) != 0 );
568+
569+ filePath directoryOut;
570+
571+ filePath fileName = FileSystem::GetFileNameItem( path, includeExtension, &directoryOut );
572+
573+ int iRet = 1;
574+
575+ lua_pushlstring( L, fileName.c_str(), fileName.size() );
576+
577+ if ( directoryOut.size() != 0 )
578+ {
579+ lua_pushlstring( L, directoryOut.c_str(), directoryOut.size() );
580+
581+ iRet++;
582+ }
583+
584+ return iRet;
585+}
586+
587+#ifndef FU_CLASS
588+
589+static int luafsys_streamCompare( lua_State *L )
590+{
591+ CFile *srcFile = lua_readclass <CFile> ( L, 1, LUACLASS_FILE );
592+ CFile *dstFile = lua_readclass <CFile> ( L, 2, LUACLASS_FILE );
593+
594+ LUA_CHECK( srcFile && dstFile );
595+
596+ char sourceBuf[2048];
597+ char targetBuf[2048];
598+
599+ bool isEqual = true;
600+
601+ while ( !srcFile->IsEOF() )
602+ {
603+ size_t sourceReadCount = srcFile->Read( sourceBuf, sizeof( sourceBuf ) );
604+ size_t destReadCount = dstFile->Read( targetBuf, sizeof( targetBuf ) );
605+
606+ if ( sourceReadCount != destReadCount )
607+ {
608+ isEqual = false;
609+ break;
610+ }
611+
612+ if ( sourceReadCount != 0 && memcmp( sourceBuf, targetBuf, sourceReadCount ) != 0 )
613+ {
614+ isEqual = false;
615+ break;
616+ }
617+ }
618+
619+ lua_pushboolean( L, isEqual );
620+ return 1;
621+}
622+#endif //FU_CLASS
623+
624+int luafsys_getRoot( lua_State *L )
625+{
626+ lua_pushvalue( L, lua_upvalueindex( 1 ) );
627+ return 1;
628+}
629+
630+static const luaL_Reg fsysLib[] =
631+{
632+ { "createTranslator", luafsys_createTranslator },
633+#ifndef FU_CLASS
634+ { "createArchiveTranslator", luafsys_createArchiveTranslator },
635+ { "createZIPArchive", luafsys_createZIPArchive },
636+ { "copyFile", luafsys_copyFile },
637+ { "copyStream", luafsys_copyStream },
638+ { "copyStreamCount", luafsys_copyStreamCount },
639+#endif //FU_CLASS
640+ { "pathToFilename", luafsys_pathToFilename },
641+#ifndef FU_CLASS
642+ { "streamCompare", luafsys_streamCompare },
643+#endif //FU_CLASS
644+ { NULL, NULL }
645+};
646+
647+int luafsys_init( lua_State *L )
648+{
649+ // Specify the root fileTranslator
650+ CFileTranslator *rootTranslator = pubFileSystem->CreateTranslator( "" ); // use the current directory.
651+
652+ // We could fail to obtain the handle to the translator if the directory is handle-locked.
653+ // In that case, return false.
654+ LUA_CHECK( rootTranslator != NULL );
655+
656+ // Return the Lua representation of the root translator.
657+ luafsys_pushroot( L, rootTranslator );
658+ return 1;
659+}
660+
661+void luafilesystem_open( lua_State *L )
662+{
663+ lua_newtable( L );
664+ luaL_openlib( L, NULL, fsysLib, 0 );
665+
666+ lua_pushlstring( L, "getRoot", 7 );
667+ lua_pushcclosure( L, luafsys_init, 0 );
668+ lua_call( L, 0, 1 );
669+
670+ lua_pushlstring( L, "root", 4 );
671+
672+ lua_pushvalue( L, -2 );
673+
674+ lua_rawset( L, -5 );
675+
676+ lua_pushcclosure( L, luafsys_getRoot, 1 );
677+ lua_rawset( L, -3 );
678+}
--- blueMods/fileSystem/luafilesystem.h (nonexistent)
+++ blueMods/fileSystem/luafilesystem.h (revision 11)
@@ -0,0 +1,23 @@
1+/*****************************************************************************
2+*
3+* PROJECT: Lua Interpreter
4+* LICENSE: See LICENSE in the top level directory
5+* FILE: luafilesystem.h
6+* PURPOSE: Lua filesystem access
7+* DEVELOPERS: Martin Turski <quiret@gmx.de>
8+*
9+* Multi Theft Auto is available from http://www.multitheftauto.com/
10+*
11+*****************************************************************************/
12+
13+#ifndef _FILESYSTEMLIB_
14+#define _FILESYSTEMLIB_
15+
16+#define LUACLASS_FILETRANSLATOR 137
17+
18+int luafsys_createArchiveTranslator( lua_State *L );
19+int luafsys_createZIPArchive( lua_State *L );
20+void luafsys_pushroot( lua_State *L, CFileTranslator *root );
21+void luafilesystem_open( lua_State *L );
22+
23+#endif //_FILESYSTEMLIB_
--- blueMods/fileSystem/ml_base.cpp (revision 10)
+++ blueMods/fileSystem/ml_base.cpp (revision 11)
@@ -18,14 +18,19 @@
1818
1919 #include "StdInc.h"
2020
21-ILuaModuleManager10 *pModuleManager = NULL;
21+ILuaModuleManager10 *pModuleManager = nullptr;
22+CFileSystemInterface *pubFileSystem = nullptr;
2223
2324 // Initialisation function (module entrypoint)
2425 MTAEXPORT bool InitModule ( ILuaModuleManager10 *pManager, char *szModuleName, char *szAuthor, float *fVersion )
2526 {
2627 pModuleManager = pManager;
28+ {
29+ fs_construction_params fsparams;
30+ // TODO: properly configure the Eir FileSystem module here.
2731
28- new CFileSystem;
32+ pubFileSystem = CFileSystem::Create( fsparams );
33+ }
2934
3035 // Set the module info
3136 strncpy ( szModuleName, MODULE_NAME, MAX_INFO_LENGTH );
Show on old repository browser