From aaaa9160584a9669f0e1b77af758885656abdeff Mon Sep 17 00:00:00 2001 From: cracyc Date: Wed, 24 May 2017 16:42:54 -0500 Subject: luaengine: support lua style args for some emu.file members (nw) --- src/frontend/mame/luaengine.cpp | 98 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp index 0628afc2350..51c6d42ccd4 100644 --- a/src/frontend/mame/luaengine.cpp +++ b/src/frontend/mame/luaengine.cpp @@ -776,17 +776,111 @@ void lua_engine::initialize() return lua_yield(L, 0); }); - emu.new_usertype("file", sol::call_constructor, sol::constructors>(), +/* + * emu.file(opt searchpath, flags) - flags can be as in osdcore "OPEN_FLAG_*" or lua style with 'rwc' with addtional c for create *and truncate* (be careful) + * support zipped files on the searchpath + * file:open(name) - open first file matching name in searchpath, supports read and write sockets as "socket.127.0.0.1:1234" + * file:open_next() - open next file matching name in searchpath + * file:read(len) - only reads len bytes, doen't do lua style formats + * file:write(data) - write data to file + * file:seek(offset, whence) - whence is as C "SEEK_*" int + * file:seek(opt whence, opt offset) - lua style "set"|"cur"|"end", returns cur offset + * file:size() - + * file:filename() - name of current file, container name if file is in zip + * file:fullpath() - +*/ + + emu.new_usertype("file", sol::call_constructor, sol::initializers([](emu_file &file, u32 flags) { new (&file) emu_file(flags); }, + [](emu_file &file, const char *path, u32 flags) { new (&file) emu_file(path, flags); }, + [](emu_file &file, const char *mode) { + int flags = 0; + for(int i = 0; i < 2; i++) // limit to three chars + { + switch(mode[i]) + { + case 'r': + flags |= OPEN_FLAG_READ; + break; + case 'w': + flags |= OPEN_FLAG_WRITE; + break; + case 'c': + flags |= OPEN_FLAG_CREATE; + break; + } + } + new (&file) emu_file(flags); + }, + [](emu_file &file, const char *path, const char* mode) { + int flags = 0; + for(int i = 0; i < 2; i++) // limit to three chars + { + switch(mode[i]) + { + case 'r': + flags |= OPEN_FLAG_READ; + break; + case 'w': + flags |= OPEN_FLAG_WRITE; + break; + case 'c': + flags |= OPEN_FLAG_CREATE; + break; + } + } + new (&file) emu_file(path, flags); + }), "read", [](emu_file &file, sol::buffer *buff) { buff->set_len(file.read(buff->get_ptr(), buff->get_len())); return buff; }, "write", [](emu_file &file, const std::string &data) { return file.write(data.data(), data.size()); }, "open", static_cast(&emu_file::open), "open_next", &emu_file::open_next, - "seek", &emu_file::seek, + "seek", sol::overload([](emu_file &file) { return file.tell(); }, + [this](emu_file &file, s64 offset, int whence) -> sol::object { + if(file.seek(offset, whence)) + return sol::make_object(sol(), sol::nil); + else + return sol::make_object(sol(), file.tell()); + }, + [this](emu_file &file, const char* whence) -> sol::object { + int wval = -1; + const char *seekdirs[] = {"set", "cur", "end"}; + for(int i = 0; i < 3; i++) + { + if(!strncmp(whence, seekdirs[i], 3)) + { + wval = i; + break; + } + } + if(wval < 0 || wval >= 3) + return sol::make_object(sol(), sol::nil); + if(file.seek(0, wval)) + return sol::make_object(sol(), sol::nil); + return sol::make_object(sol(), file.tell()); + }, + [this](emu_file &file, const char* whence, s64 offset) -> sol::object { + int wval = -1; + const char *seekdirs[] = {"set", "cur", "end"}; + for(int i = 0; i < 3; i++) + { + if(!strncmp(whence, seekdirs[i], 3)) + { + wval = i; + break; + } + } + if(wval < 0 || wval >= 3) + return sol::make_object(sol(), sol::nil); + if(file.seek(offset, wval)) + return sol::make_object(sol(), sol::nil); + return sol::make_object(sol(), file.tell()); + }), "size", &emu_file::size, "filename", &emu_file::filename, "fullpath", &emu_file::fullpath); /* + * emu.thread() * thread.start(scr) - run scr (string not function) in a seperate thread in a new empty (other then modules) lua context * thread.continue(val) - resume thread and pass val to it * thread.result() - get thread result as string -- cgit v1.2.3