summaryrefslogtreecommitdiff
path: root/source/modules/asura-utils/IO/binding/_file_system.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/modules/asura-utils/IO/binding/_file_system.cpp')
-rw-r--r--source/modules/asura-utils/IO/binding/_file_system.cpp265
1 files changed, 265 insertions, 0 deletions
diff --git a/source/modules/asura-utils/IO/binding/_file_system.cpp b/source/modules/asura-utils/IO/binding/_file_system.cpp
new file mode 100644
index 0000000..ace3c5f
--- /dev/null
+++ b/source/modules/asura-utils/IO/binding/_file_system.cpp
@@ -0,0 +1,265 @@
+#include "../FileSystem.h"
+
+using namespace Luax;
+
+namespace AsuraEngine
+{
+ namespace IO
+ {
+
+#define PREPARE(l) \
+ LUAX_STATE(l); \
+ Filesystem* fs = Filesystem::Get();
+
+ LUAX_REGISTRY(Filesystem)
+ {
+ LUAX_REGISTER_METHODS(state,
+ { "Init", _Init },
+ { "Mount", _Mount },
+ { "Unmount", _Unmount },
+ { "GetMountPoint", _GetMountPoint },
+ { "SetWriteDirectory", _SetWriteDirectory },
+ { "GetWriteDirectory", _GetWriteDirectory },
+ { "CreateFile", _CreateFile },
+ { "CreateDirectory", _CreateDirectory },
+ { "Write", _Write },
+ { "Append", _Append },
+ { "Remove", _Remove },
+ { "Read", _Read },
+ { "GetFileInfo", _GetFileInfo },
+ { "GetDirectoryItems", _GetDirectoryItems }
+ );
+ }
+
+ LUAX_POSTPROCESS(Filesystem)
+ {
+ LUAX_REGISTER_ENUM(state, "EFileType",
+ { "FILE", FILE_TYPE_FILE },
+ { "DIRECTORY", FILE_TYPE_DIRECTORY },
+ { "SYMLINK", FILE_TYPE_SYMLINK },
+ { "OTHER", FILE_TYPE_OTHER }
+ );
+ }
+
+ // Filesystem.Init(arg0)
+ LUAX_IMPL_METHOD(Filesystem, _Init)
+ {
+ PREPARE(L);
+
+ const char* arg0 = state.CheckValue<const char*>(1);
+ fs->Init(arg0);
+ return 0;
+ }
+
+ // successed = Filesystem.Mount(path, mountpoint[, prepend = false])
+ // successed = Filesystem.Mount(data buffer, archievename, mountpoint[, prepend = false])
+ LUAX_IMPL_METHOD(Filesystem, _Mount)
+ {
+ PREPARE(L);
+ bool mounted = false;
+
+ if (state.IsType(1, LUA_TSTRING))
+ {
+ cc8* path = state.GetValue<cc8*>(1, "");
+ cc8* moutpoint = state.GetValue<cc8*>(2, "/");
+ bool prepend = state.GetValue<bool>(3, false);
+ mounted = fs->Mount(path, moutpoint, prepend);
+ }
+ else if (state.IsType(1, LUA_TUSERDATA))
+ {
+ DataBuffer* db = state.CheckUserdata<DataBuffer>(1);
+ if (!db)
+ return state.ErrorType(1, "Data Buffer");
+ cc8* arcname = state.GetValue<cc8*>(2, "");
+ cc8* mountpoint = state.GetValue<cc8*>(3, "/");
+ bool prepend = state.GetValue<bool>(4, false);
+ mounted = fs->Mount(db, arcname, mountpoint, prepend);
+ // retain
+ fs->LuaxRetain<DataBuffer>(state, db);
+ }
+ state.Push(mounted);
+ return 1;
+ }
+
+ // successed = Filesystem.Unmount(path)
+ // successed = Filesystem.Unmount(data buffer)
+ LUAX_IMPL_METHOD(Filesystem, _Unmount)
+ {
+ PREPARE(L);
+ bool unmounted = false;
+
+ if (state.IsType(1, LUA_TSTRING))
+ {
+ cc8* path = state.GetValue<cc8*>(1, "");
+ unmounted = fs->Unmount(path);
+ }
+ else if (state.IsType(1, LUA_TUSERDATA))
+ {
+ DataBuffer* db = state.CheckUserdata<DataBuffer>(1);
+ if (!db)
+ return state.ErrorType(1, "Data Buffer");
+ unmounted = fs->Unmount(db);
+ if (unmounted)
+ fs->LuaxRelease<DataBuffer>(state, db);
+ }
+ state.Push(unmounted);
+ return 1;
+ }
+
+ // moutpoint = Filesystem.GetMountPoint(path)
+ LUAX_IMPL_METHOD(Filesystem, _GetMountPoint)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ std::string mp;
+ if (fs->GetMountPoint(path, ASURA_OUT mp))
+ state.Push(mp);
+ else
+ state.PushNil();
+
+ return 1;
+ }
+
+ // Filesystem.SetWriteDirectory(dir)
+ LUAX_IMPL_METHOD(Filesystem, _SetWriteDirectory)
+ {
+ PREPARE(L);
+
+ cc8* dir = state.CheckValue<cc8*>(1);
+ fs->SetWriteDirectory(dir);
+ return 0;
+ }
+
+ // dir = Filesystem.GetWriteDirectory()
+ LUAX_IMPL_METHOD(Filesystem, _GetWriteDirectory)
+ {
+ PREPARE(L);
+
+ std::string dir = fs->GetWriteDirectory();
+ state.Push(dir);
+ return 1;
+ }
+
+ // file = Filesystem.CreateFile(name)
+ LUAX_IMPL_METHOD(Filesystem, _CreateFile)
+ {
+ PREPARE(L);
+
+ cc8* name = state.CheckValue<cc8*>(1);
+ File* file = fs->NewFile(name);
+ if (file)
+ file->PushLuaxUserdata(state);
+ else
+ state.PushNil();
+ return 1;
+ }
+
+ // successed = Filesystem.CreateDirectory(name)
+ LUAX_IMPL_METHOD(Filesystem, _CreateDirectory)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ state.Push(fs->NewDirectory(path));
+ return 1;
+ }
+
+ // successed = Filesystem.Write(path, data buffer)
+ LUAX_IMPL_METHOD(Filesystem, _Write)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ DataBuffer* db = state.CheckUserdata<DataBuffer>(2);
+ state.Push(fs->Write(path, db));
+ return 1;
+ }
+
+ // successed = Filesystem.Append(path, data buffer)
+ LUAX_IMPL_METHOD(Filesystem, _Append)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ DataBuffer* db = state.CheckUserdata<DataBuffer>(2);
+ state.Push(fs->Append(path, db));
+ return 1;
+ }
+
+ // successed = Filesystem.Remove(path)
+ LUAX_IMPL_METHOD(Filesystem, _Remove)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ state.Push(fs->Remove(path));
+ return 1;
+ }
+
+ // filedata = Filesystem.Read(path)
+ LUAX_IMPL_METHOD(Filesystem, _Read)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ FileData* fd = fs->Read(path);
+ if (fd)
+ {
+ fd->m_Data->PushLuaxUserdata(state);
+ fd->SetLuaxMemberRef(state, fd->m_DataRef, -1); // fd->m_DataRef = data buffer
+ state.Pop(1); // data buffer
+ fd->PushLuaxUserdata(state);
+ }
+ else
+ {
+ state.PushNil();
+ }
+ return 1;
+ }
+
+ // fileinfo = Filesystem.GetFileInfo(path)
+ LUAX_IMPL_METHOD(Filesystem, _GetFileInfo)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ FileInfo info;
+ if (fs->GetFileInfo(path, &info))
+ {
+ lua_newtable(L); // info table
+ state.SetField(-1, "size", info.size);
+ state.SetField(-1, "modtime", info.modtime);
+ state.SetField(-1, "type", info.type);
+ }
+ else
+ {
+ state.PushNil();
+ }
+ return 1;
+ }
+
+ // items = Filesystem.GetDirectoryItems(path)
+ LUAX_IMPL_METHOD(Filesystem, _GetDirectoryItems)
+ {
+ PREPARE(L);
+
+ cc8* path = state.CheckValue<cc8*>(1);
+ std::vector<std::string> items;
+ if(fs->GetDirectoryItems(path, ASURA_OUT items))
+ {
+ lua_newtable(L); // item list
+ for (int i = 0; i < items.size(); ++i)
+ {
+ state.SetFieldByIndex(-1, i + 1, items[i]);
+ }
+ }
+ else
+ {
+ state.PushNil();
+ }
+ return 1;
+ }
+
+ }
+} \ No newline at end of file