foxygit / fake-08-xbox360 Log in
commit 496190f3a38e1e9a663bba5a4ab8214042b7c965
Author:     Ram Dobson <fringd@gmail.com>
AuthorDate: Thu Feb 9 01:44:05 2023 -0500
Commit:     Ram Dobson <fringd@gmail.com>
CommitDate: Thu Feb 9 02:07:02 2023 -0500

    fix null printing
---
 source/picoluaapi.cpp    |  8 +++++---
 source/printHelper.cpp   | 14 ++++++++++++--
 test/picoluaapitests.cpp | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 3254536..9cdb4b1 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -301,7 +301,7 @@ int print(lua_State *L){
         return 0;
     }

-    const char * str = "";
+    std::string str = "";
     int newx = 0;

     //todo: handle other cases, maybe move this somewhere else
@@ -310,10 +310,12 @@ int print(lua_State *L){
         str = "[nil]";
     }
     else if (lua_isstring(L, 1)){
-        str = lua_tolstring(L, 1, nullptr);
+      size_t len;
+      const char *raw_string = lua_tolstring(L, 1, &len);
+      str = std::string(raw_string, len);
     }
     else if (lua_isnumber(L, 1)){
-        str = lua_tolstring(L, 1, nullptr);
+        str = lua_tolstring(L, 1, nullptr) ;
     }
     else if (lua_isboolean(L, 1)){
         str = lua_toboolean(L, 1) ? "true" : "false";
diff --git a/source/printHelper.cpp b/source/printHelper.cpp
index 90fc26c..e1bccbb 100644
--- a/source/printHelper.cpp
+++ b/source/printHelper.cpp
@@ -131,7 +131,17 @@ int print(std::string str, int x, int y, uint8_t c) {
 	for (size_t n = 0; n < str.length(); n++) {
 		uint8_t ch = str[n];
         framesToPause = framesBetweenChars;
-		if (ch == 1) { // "\*{p0}" repeat the next character p0 times
+		if (ch == 0) { // null stop printing
+      _ph_mem->drawState.text_x=x;
+      _ph_mem->drawState.text_y=y;
+
+      //restore palette
+      for(int i = 0; i < 16; i++) {
+        _ph_mem->drawState.drawPaletteMap[i] = prevDrawPal[i];
+      }
+      return x;
+    }
+    else if (ch == 1) { // "\*{p0}" repeat the next character p0 times
 			uint8_t timesChr = str[++n];
 			int times = p0CharToNum(timesChr);

@@ -379,4 +389,4 @@ int print(std::string str, int x, int y, uint8_t c) {
 	_ph_mem->drawState.text_y = y + lineHeight;

 	return x;
-}
\ No newline at end of file
+}
diff --git a/test/picoluaapitests.cpp b/test/picoluaapitests.cpp
index c12ab58..9af2a03 100644
--- a/test/picoluaapitests.cpp
+++ b/test/picoluaapitests.cpp
@@ -165,3 +165,35 @@ TEST_CASE("audio stats") {
   }

 }
+
+
+TEST_CASE("print") {
+  // setup
+  PicoRam picoRam;
+  picoRam.Reset();
+  Audio* audio = new Audio(&picoRam);
+  std::string fontdata = get_font_data();
+  Graphics* graphics = new Graphics(fontdata, &picoRam);
+  //audioState_t* audioState = audio->getAudioState();
+  Input * input = new Input(&picoRam);
+  StubHost* stubHost = new StubHost();
+  Vm* vm = new Vm(stubHost, &picoRam, graphics, input, audio);
+  initPicoApi(&picoRam, graphics,  input, vm, audio);
+  lua_State *L = luaL_newstate();
+
+  SUBCASE("print puts a newline at the end implicitly") {
+    lua_pushstring(L, "hello world");
+    print(L);
+
+    CHECK_EQ(picoRam.drawState.text_x, 0);
+    CHECK_EQ(picoRam.drawState.text_y, 6);
+  }
+
+  SUBCASE("print gets the whole string including nulls") {
+    lua_pushlstring(L, "hello world\0", 12);
+    print(L);
+
+    CHECK_EQ(picoRam.drawState.text_x, 11*4);
+    CHECK_EQ(picoRam.drawState.text_y, 0);
+  }
+}