foxygit / fake-08-xbox360 Log in
commit 96f4bfa01672ba1d18ed1a55fa33fd0192ab383b
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sun May 3 12:50:16 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sun May 3 12:50:16 2020 -0600

    update lua cart patching and fix test cart not parsing
---
 source/cartPatcher.cpp | 85 ++++++++++++++++++++++++--------------------------
 1 file changed, 41 insertions(+), 44 deletions(-)

diff --git a/source/cartPatcher.cpp b/source/cartPatcher.cpp
index 9f860b4..da05992 100644
--- a/source/cartPatcher.cpp
+++ b/source/cartPatcher.cpp
@@ -8,47 +8,56 @@ extern "C" {
   #include <lauxlib.h>
 }

-//from LovePotion: https://github.com/gamax92/picolove/blob/master/cart.lua (zlib license)
+//from https://github.com/benwiley4000/pico8-to-lua, which was in turn adapted from LovePotion:
+//https://github.com/gamax92/picolove/blob/master/cart.lua (zlib license)
 //it should be possible to do this kind of pattern matching with another library, or use z8Lua
 //but for now I have lua 5.3 working so I'm going with this since I know it works
+const char * patchLuaFunction = R"#(

-const char * patchLuaFunction = R"#(function patchLuaForP8(lua)
-    -- patch the lua
-    lua=lua:gsub("!=", "~=").."\n"
+    function patch_lua(lua)
+    -- patch lua code
+    lua = lua:gsub("!=","~=")
+    lua = lua:gsub("//","--")
     -- rewrite shorthand if statements eg. if (not b) i=1 j=2
-    lua=lua:gsub("if%s*(%b())%s*([^\n]*)\n", function(a, b)
-        local nl=a:find('\n', nil, true)
-        local th=b:find('%f[%w]then%f[%W]')
-        local an=b:find('%f[%w]and%f[%W]')
-        local o=b:find('%f[%w]or%f[%W]')
-        local ce=b:find('--', nil, true)
-        if not (nl or th or an or o) then
-            if ce then
-                local c, t=b:match("(.-)(%s-%-%-.*)")
-                return "if "..a:sub(2, -2).." then "..c.." end"..t.."\n"
-            else
-                return "if "..a:sub(2, -2).." then "..b.." end\n"
-            end
+    lua = lua:gsub("if%s*(%b())%s*([^\n]*)\n",function(a,b)
+      local nl = a:find('\n',nil,true)
+      local th = b:find('%f[%w]then%f[%W]')
+      local an = b:find('%f[%w]and%f[%W]')
+      local o = b:find('%f[%w]or%f[%W]')
+      local ce = b:find('--',nil,true)
+      if not (nl or th or an or o) then
+        if ce then
+          local c,t = b:match("(.-)(%s-%-%-.*)")
+          return "if "..a:sub(2,-2).." then "..c.." end"..t.."\n"
+        else
+          return "if "..a:sub(2,-2).." then "..b.." end\n"
         end
+      end
     end)
     -- rewrite assignment operators
-    lua=lua:gsub("(%S+)%s*([%+-%*/%%])=", "%1 = %1 %2 ")
+    lua = lua:gsub("(%S+)%s*([%+-%*/%%])=","%1 = %1 %2 ")
+    -- rewrite inspect operator "?"
+    lua = lua:gsub("^(%s*)?([^\n\r]*)","%1print(%2)")
     -- convert binary literals to hex literals
-    lua=lua:gsub("([^%w_])0[bB]([01.]+)", function(a, b)
-        local p1, p2=b, ""
-        if b:find('.', nil, true) then
-            p1, p2=b:match("(.-)%.(.*)")
-        end
-        -- pad to 4 characters
-        p2=p2..string.rep("0", 3-((#p2-1)%4))
-        p1, p2=tonumber(p1, 2), tonumber(p2, 2)
-        if p1 and p2 then
-            return string.format("%s0x%x.%x", a, p1, p2)
+    lua = lua:gsub("([^%w_])0[bB]([01.]+)", function(a, b)
+      local p1, p2 = b, ""
+      if b:find(".", nil, true) then
+        p1, p2 = b:match("(.-)%.(.*)")
+      end
+      -- pad to 4 characters
+      p2 = p2 .. string.rep("0", 3 - ((#p2 - 1) % 4))
+      p1, p2 = tonumber(p1, 2), tonumber(p2, 2)
+      if p1 then
+        if p2 then
+          return string.format("%s0x%x.%x", a, p1, p2)
+        else
+          return string.format("%s0x%x", a, p1)
         end
+      end
     end)
-
-    return lua;
-end
+
+    return lua
+  end
 )#";


@@ -56,33 +65,21 @@ const char* getPatchedLua(const char* unpatchedLua){

     lua_State* L;

-    // initialize Lua interpreter
     L = luaL_newstate();
-
-    // load Lua base libraries (print / math / etc)
     luaL_openlibs(L);
-
-    ////////////////////////////////////////////
-    // We can use Lua here !
-    //   Need access to the LuaState * variable L
-    /////////////////////////////////////////////

     luaL_dostring(L, patchLuaFunction);

-    // Push the fib function on the top of the lua stack
-    lua_getglobal(L, "patchLuaForP8");
+    lua_getglobal(L, "patch_lua");

-    // Push the argument (the number 13) on the stack
     lua_pushstring(L, unpatchedLua);

     lua_pcall(L, 1, 1, 0);

-    // Get the result from the lua stack
     const char* result = lua_tostring(L, -1);

     lua_pop(L,1);

-    // Cleanup:  Deallocate all space assocatated with the lua state */
     lua_close(L);

     return result;