foxygit / fake-08-xbox360 Log in
commit ddce34b877b69d9781512bf3eb1cda149b38f20d
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sun Apr 19 11:35:26 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sun Apr 19 11:35:26 2020 -0600

    Patch p8 lua to be compatible with lua 5.3
---
 source/cart.cpp            |  6 ++++++
 source/cartPatcher.cpp     | 52 ++++++++++++++++++++++++++++++++++++++++++++++
 source/cartPatcher.h       |  3 +++
 source/cartPatcher.lua     | 41 ++++++++++++++++++++++++++++++++++++
 source/tests/cart_test.cpp | 29 ++++++++++++++++++++++++--
 source/tests/test_base.cpp | 12 +++++++++++
 source/tests/test_base.h   |  2 +-
 testcart.p8                |  4 ++--
 8 files changed, 144 insertions(+), 5 deletions(-)

diff --git a/source/cart.cpp b/source/cart.cpp
index e5c6c2e..af089bd 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -8,6 +8,8 @@

 #include "logger.h"

+#include "cartPatcher.h"
+
 #include "tests/test_base.h"
 #if _TEST
 #include "tests/cart_test.h"
@@ -51,6 +53,10 @@ Cart::Cart(std::string filename){
         }
 	}

+    const char * patched = getPatchedLua(LuaString.c_str());
+
+    LuaString = patched;
+

     #if _TEST
     //run tests to make sure cart is parsed correctly
diff --git a/source/cartPatcher.cpp b/source/cartPatcher.cpp
new file mode 100644
index 0000000..e678e82
--- /dev/null
+++ b/source/cartPatcher.cpp
@@ -0,0 +1,52 @@
+#include <string>
+
+#include "cartPatcher.h"
+
+extern "C" {
+  #include <lua.h>
+  #include <lualib.h>
+  #include <lauxlib.h>
+}
+
+//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
+
+
+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_dofile(L, "cartPatcher.lua");
+
+    // Push the fib function on the top of the lua stack
+    lua_getglobal(L, "patchLuaForP8");
+
+    // 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;
+
+}
+
diff --git a/source/cartPatcher.h b/source/cartPatcher.h
new file mode 100644
index 0000000..40f5389
--- /dev/null
+++ b/source/cartPatcher.h
@@ -0,0 +1,3 @@
+#pragma once
+
+const char* getPatchedLua(const char* unpatchedLua);
diff --git a/source/cartPatcher.lua b/source/cartPatcher.lua
new file mode 100644
index 0000000..292dbcf
--- /dev/null
+++ b/source/cartPatcher.lua
@@ -0,0 +1,41 @@
+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
diff --git a/source/tests/cart_test.cpp b/source/tests/cart_test.cpp
index aef7d71..ba91782 100644
--- a/source/tests/cart_test.cpp
+++ b/source/tests/cart_test.cpp
@@ -30,6 +30,11 @@ local r1dy = 2
 local r2dx = 2
 local r2dy = 2

+local sprx = 40;
+local spry = 40;
+
+local sprn = 7;
+
 function _draw()
 	cls()

@@ -61,10 +66,17 @@ function _draw()

     print("test str", 64, 64, 7)

-    spr(7, 40, 40, 1, 1, false, false)
+    spr(sprn, sprx, spry, 1, 1, false, false)
 end

 function _update()
+    if btn(0) then sprx = sprx - 2 end
+	if btn(1) then sprx = sprx + 2 end
+	if btn(2) then spry = spry - 2 end
+	if btn(3) then spry = spry + 2 end
+	if btnp(4) then sprn +=1 end
+    if btnp(5) then sprn -=1 end
+
     if (r1x0 < 0 or r1x1 > 127) then
         r1dx = -1 * r1dx;
     end
@@ -130,6 +142,11 @@ local r1dy = 2
 local r2dx = 2
 local r2dy = 2

+local sprx = 40;
+local spry = 40;
+
+local sprn = 7;
+
 function _draw()
 	cls()

@@ -161,10 +178,17 @@ function _draw()

     print("test str", 64, 64, 7)

-    spr(7, 40, 40, 1, 1, false, false)
+    spr(sprn, sprx, spry, 1, 1, false, false)
 end

 function _update()
+    if btn(0) then sprx = sprx - 2 end
+	if btn(1) then sprx = sprx + 2 end
+	if btn(2) then spry = spry - 2 end
+	if btn(3) then spry = spry + 2 end
+	if btnp(4) then sprn = sprn + 1 end
+    if btnp(5) then sprn = sprn - 1 end
+
     if (r1x0 < 0 or r1x1 > 127) then
         r1dx = -1 * r1dx;
     end
@@ -182,6 +206,7 @@ function _update()
     r1y0 = r1dy + r1y0
     r1y1 = r1dy + r1y1
 end
+
 )";

 bool verifyLuaText(std::string luaText){
diff --git a/source/tests/test_base.cpp b/source/tests/test_base.cpp
index c5f7501..70b4039 100644
--- a/source/tests/test_base.cpp
+++ b/source/tests/test_base.cpp
@@ -6,9 +6,21 @@
 #include <string>
 #include <sstream>

+#include "../logger.h"
+
 bool assertStringsEqual(std::string expected, std::string actual, std::string testName){
     bool equal = expected == actual;
     printTestOuput(testName, equal);
+
+    if (!equal){
+        Logger::Write("start expected: \n");
+        Logger::Write(expected.c_str());
+        Logger::Write("end expected: \n");
+
+        Logger::Write("start actual: \n");
+        Logger::Write(actual.c_str());
+        Logger::Write("end actual: \n");
+    }

     return equal;
 }
diff --git a/source/tests/test_base.h b/source/tests/test_base.h
index 8b517e2..1471fc3 100644
--- a/source/tests/test_base.h
+++ b/source/tests/test_base.h
@@ -1,7 +1,7 @@
 #pragma once

 //if this is set to 1, test cases will be run on the test cart
-#define _TEST 0
+#define _TEST 1

 //if this flag is set, only failures get printed to console
 #define _PRINT_SUCCESS 1
diff --git a/testcart.p8 b/testcart.p8
index f2409bd..b96aab3 100644
--- a/testcart.p8
+++ b/testcart.p8
@@ -63,8 +63,8 @@ function _update()
 	if btn(1) then sprx = sprx + 2 end
 	if btn(2) then spry = spry - 2 end
 	if btn(3) then spry = spry + 2 end
-	--if btnp(4) then sprn +=1 end
-    --if btnp(5) then sprn -=1 end
+	if btnp(4) then sprn +=1 end
+    if btnp(5) then sprn -=1 end

     if (r1x0 < 0 or r1x1 > 127) then
         r1dx = -1 * r1dx;