diff --git a/Makefile b/Makefile index ff7c65e4..4e3ebed6 100644 --- a/Makefile +++ b/Makefile @@ -72,11 +72,13 @@ bsnes/out/libsnes.$(ARCHIVE_SUFFIX): forcelook $(REALRANLIB) bsnes/out/libsnes.$(ARCHIVE_SUFFIX) -src/__all_files__: src/core/version.cpp buildaux/mkdeps.exe forcelook +src/__all_files__: src/core/version.cpp buildaux/mkdeps.exe buildaux/txt2cstr.exe forcelook $(MAKE) -C src precheck $(MAKE) -C src cp src/lsnes$(DOT_EXECUTABLE_SUFFIX) . +buildaux/txt2cstr.exe: buildaux/txt2cstr.cpp + $(HOSTCC) $(HOSTCCFLAGS) -o $@ $< buildaux/version.exe: buildaux/version.cpp VERSION $(HOSTCC) $(HOSTCCFLAGS) -o $@ $< buildaux/mkdeps.exe: buildaux/mkdeps.cpp VERSION diff --git a/buildaux/txt2cstr.cpp b/buildaux/txt2cstr.cpp new file mode 100644 index 00000000..0cf79219 --- /dev/null +++ b/buildaux/txt2cstr.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include + +const char* hexes = "0123456789ABCDEF"; + +struct encoder +{ + encoder(std::ostream& _output) : output(_output) + { + have_quote = false; + } + size_t operator()(unsigned char* buf, size_t bufuse, bool eof) + { + if(!bufuse) return 0; + std::ostringstream out; + size_t i = 0; + while(i < bufuse) { + if(!have_quote) { + out << "\""; + have_quote = true; + } + unsigned char ch = buf[i]; + if(ch == 9) { + out << "\\t"; + } else if(ch == 10) { + out << "\\n\"" << std::endl; + have_quote = false; + } else if(ch == 13) { + out << "\\r"; + } else if(ch < 32) { + out << "\\x" << hexes[(ch >> 4)] << hexes[ch & 15]; + } else if(ch == '\"') { + out << "\\\""; + } else if(ch == '\\') { + out << "\\\\"; + } else if(ch < 127) { + out << ch; + } else { + out << "\\x" << hexes[(ch >> 4)] << hexes[ch & 15]; + } + i++; + } + output << out.str() << std::endl; + return i; + } + size_t operator()() + { + if(have_quote) { + output << "\""; + have_quote = false; + } + } +private: + std::ostream& output; + bool have_quote; +}; + +void do_encode(std::istream& input, std::ostream& output) +{ + char buf[4096]; + size_t bufuse = 0; + bool eof = false; + encoder e(output); + while(true) { + if(!eof) { + input.read(buf + bufuse, 4096 - bufuse); + bufuse += input.gcount(); + } + if(!input) + eof = true; + size_t bytes = e(reinterpret_cast(buf), bufuse, eof); + memmove(buf, buf + bytes, bufuse - bytes); + bufuse -= bytes; + if(eof && !bufuse) break; + } + e(); +} + +int main(int argc, char** argv) +{ + if(argc != 3) { + std::cerr << "Usage: txt2cstr " << std::endl; + return 1; + } + std::ifstream in(argv[2], std::ios::binary); + std::cout << "const char* " << argv[1] << " =" << std::endl; + do_encode(in, std::cout); + std::cout << ";" << std::endl; +} diff --git a/src/lua/.gitignore b/src/lua/.gitignore new file mode 100644 index 00000000..f9254829 --- /dev/null +++ b/src/lua/.gitignore @@ -0,0 +1,2 @@ +sysrc.cpp + diff --git a/src/lua/Makefile b/src/lua/Makefile index 237b709d..d666be8e 100644 --- a/src/lua/Makefile +++ b/src/lua/Makefile @@ -1,4 +1,4 @@ -OBJECTS=$(patsubst %.cpp,%.$(OBJECT_SUFFIX),$(wildcard *.cpp)) +OBJECTS=$(patsubst %.cpp,%.$(OBJECT_SUFFIX),$(wildcard *.cpp)) sysrc.$(OBJECT_SUFFIX) .PRECIOUS: %.$(OBJECT_SUFFIX) %.files @@ -6,6 +6,12 @@ __all__.files: $(OBJECTS) lua ../genfilelist.lua $^ >$@ echo >__all__.ldflags +sysrc.cpp: sysrc.lua + ../../buildaux/txt2cstr.exe lua_sysrc_script $< >$@ + +sysrc.cpp.dep: sysrc.cpp + touch sysrc.cpp.dep + %.$(OBJECT_SUFFIX): %.cpp %.cpp.dep $(REALCC) -c -o $@ $< -I../../include $(CFLAGS) @@ -17,4 +23,4 @@ forcelook: @true clean: - rm -f *.$(OBJECT_SUFFIX) __all__.ldflags __all__.files + rm -f *.$(OBJECT_SUFFIX) __all__.ldflags __all__.files sysrc.cpp diff --git a/src/lua/sysrc.cpp b/src/lua/sysrc.cpp deleted file mode 100644 index 06699cb1..00000000 --- a/src/lua/sysrc.cpp +++ /dev/null @@ -1,31 +0,0 @@ -const char* lua_sysrc_script = -"loopwrapper = function(fn, ...)\n" -" local routine = coroutine.create(fn);\n" -" local resume = function(...)\n" -" if coroutine.status(routine) ~= \"dead\" then\n" -" local x, y;\n" -" x, y = coroutine.resume(routine, ...);\n" -" if not x then\n" -" error(y);\n" -" end\n" -" end\n" -" end\n" -" local yield = function()\n" -" return coroutine.yield(routine);\n" -" end\n" -" resume(yield, ...);\n" -" return resume;\n" -"end;\n" -"print=print2;\n" -"loadfile=loadfile2;\n" -"dofile=dofile2;\n" -"memory2=memory2();\n" -"callback=callback();\n" -"render_queue_function=function(rq)\n" -" local _rq = rq;\n" -" return function()\n" -" _rq:run();\n" -" end;\n" -"end;\n" -"string.byteU=_lsnes_string_byteU;\n" -"string.charU=_lsnes_string_charU;\n"; diff --git a/src/lua/sysrc.lua b/src/lua/sysrc.lua new file mode 100644 index 00000000..a88051c4 --- /dev/null +++ b/src/lua/sysrc.lua @@ -0,0 +1,30 @@ +loopwrapper = function(fn, ...) + local routine = coroutine.create(fn); + local resume = function(...) + if coroutine.status(routine) ~= "dead" then + local x, y; + x, y = coroutine.resume(routine, ...); + if not x then + error(y); + end + end + end + local yield = function() + return coroutine.yield(routine); + end + resume(yield, ...); + return resume; +end; +print=print2; +loadfile=loadfile2; +dofile=dofile2; +memory2=memory2(); +callback=callback(); +render_queue_function=function(rq) + local _rq = rq; + return function() + _rq:run(); + end; +end; +string.byteU=_lsnes_string_byteU; +string.charU=_lsnes_string_charU;