foxygit / fake-08-xbox360 Log in
commit c9f00994c1d5ac60186f01a99cbe76a46154f5af
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sun Apr 19 16:36:14 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sun Apr 19 16:36:14 2020 -0600

    Move cartPatcher lua into a raw string literal
---
 source/cartPatcher.cpp | 41 ++++++++++++++++++++++++++++++++++++++++-
 source/cartPatcher.lua | 41 -----------------------------------------
 2 files changed, 40 insertions(+), 42 deletions(-)

diff --git a/source/cartPatcher.cpp b/source/cartPatcher.cpp
index e678e82..11adf20 100644
--- a/source/cartPatcher.cpp
+++ b/source/cartPatcher.cpp
@@ -12,6 +12,45 @@ extern "C" {
 //it may 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"#(function patchLuaForP8(lua)
+    -- patch the lua
+    lua=lua:gsub("!=", "~=").."\n"
+    -- 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
+        end
+    end)
+    -- rewrite assignment operators
+    lua=lua:gsub("(%S+)%s*([%+-%*/%%])=", "%1 = %1 %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)
+        end
+    end)
+
+    return lua;
+end
+)#";
+

 const char* getPatchedLua(const char* unpatchedLua){

@@ -28,7 +67,7 @@ const char* getPatchedLua(const char* unpatchedLua){
     //   Need access to the LuaState * variable L
     /////////////////////////////////////////////

-    luaL_dofile(L, "cartPatcher.lua");
+    luaL_dostring(L, patchLuaFunction);

     // Push the fib function on the top of the lua stack
     lua_getglobal(L, "patchLuaForP8");
diff --git a/source/cartPatcher.lua b/source/cartPatcher.lua
deleted file mode 100644
index 292dbcf..0000000
--- a/source/cartPatcher.lua
+++ /dev/null
@@ -1,41 +0,0 @@
-function patchLuaForP8(lua)
-    --from LovePotion: https://github.com/gamax92/picolove/blob/master/cart.lua (zlib license)
-    --it may 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
-
-    -- patch the lua
-    lua=lua:gsub("!=", "~=").."\n"
-    -- 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
-        end
-    end)
-    -- rewrite assignment operators
-    lua=lua:gsub("(%S+)%s*([%+-%*/%%])=", "%1 = %1 %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)
-        end
-    end)
-
-    return lua;
-end