From 36d034dc2e8832d13504aecfce0b75f4cf35cc4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Mon, 22 Aug 2022 15:54:04 +0200 Subject: [PATCH] Fix 8-bit-uncleanliness of most of the Lua API This fixes bugs like "type\0hello mom" being a property name sim.partProperty accepts and half-fixes bugs like text formatting codes making gfx.drawText exit prematurely. --- src/Misc.cpp | 6 + src/Misc.h | 11 ++ src/lua/CommandInterface.cpp | 5 +- src/lua/LegacyLuaAPI.cpp | 133 +++++++------- src/lua/LuaButton.cpp | 10 +- src/lua/LuaCheckbox.cpp | 8 +- src/lua/LuaCompat.c | 3 +- src/lua/LuaComponent.cpp | 2 +- src/lua/LuaEvents.cpp | 16 +- src/lua/LuaLabel.cpp | 6 +- src/lua/LuaProgressBar.cpp | 6 +- src/lua/LuaScriptHelper.h | 1 - src/lua/LuaScriptInterface.cpp | 309 ++++++++++++++++++++------------- src/lua/LuaScriptInterface.h | 26 +++ src/lua/LuaSlider.cpp | 2 +- src/lua/LuaTCPSocket.cpp | 25 +-- src/lua/LuaTextbox.cpp | 10 +- src/lua/LuaWindow.cpp | 30 ++-- src/simulation/Simulation.cpp | 10 +- 19 files changed, 365 insertions(+), 254 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index e303f5ae2..6b429128c 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -8,6 +8,7 @@ #include #include "common/tpt-minmax.h" +#include "common/String.h" const static char hex[] = "0123456789ABCDEF"; void strcaturl(char *dst, char *src) @@ -218,3 +219,8 @@ void membwand(void * destv, void * srcv, size_t destsize, size_t srcsize) vector2d v2d_zero = {0,0}; matrix2d m2d_identity = {1,0,0,1}; + +bool byteStringEqualsString(const ByteString &str, const char *data, size_t size) +{ + return str.size() == size && !memcmp(str.data(), data, size); +} diff --git a/src/Misc.h b/src/Misc.h index 358079a38..02a7ed589 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -4,6 +4,7 @@ #include #include #include +#include #include //Linear interpolation @@ -100,4 +101,14 @@ vector2d v2d_new(float x, float y); extern vector2d v2d_zero; extern matrix2d m2d_identity; +class ByteString; + +bool byteStringEqualsString(const ByteString &str, const char *data, size_t size); +template +// TODO: use std::literals::string_literals::operator""s if we get rid of ByteString +bool byteStringEqualsLiteral(const ByteString &str, const char (&lit)[N]) +{ + return byteStringEqualsString(str, lit, N - 1U); +} + #endif diff --git a/src/lua/CommandInterface.cpp b/src/lua/CommandInterface.cpp index 890c1b872..c6f5f91c2 100644 --- a/src/lua/CommandInterface.cpp +++ b/src/lua/CommandInterface.cpp @@ -6,8 +6,8 @@ #include #endif +#include "Misc.h" #include "gui/game/GameModel.h" - #include "simulation/Particle.h" CommandInterface::CommandInterface(GameController * c, GameModel * m) { @@ -54,7 +54,7 @@ int CommandInterface::GetPropertyOffset(ByteString key, FormatType & format) switch (prop.Type) { case StructProperty::ParticleType: - format = (key == "type") ? FormatElement : FormatInt; // FormatElement is tightly coupled with "type" + format = byteStringEqualsLiteral(key, "type") ? FormatElement : FormatInt; // FormatElement is tightly coupled with "type" break; case StructProperty::Integer: @@ -81,4 +81,3 @@ String CommandInterface::GetLastError() CommandInterface::~CommandInterface() { } - diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index d24ab9345..305981449 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -77,7 +77,7 @@ int luacon_partread(lua_State* l) { int tempinteger, i = cIndex; float tempfloat; - ByteString key = luaL_optstring(l, 2, ""); + ByteString key = tpt_lua_optByteString(l, 2, ""); CommandInterface::FormatType format; int offset = luacon_ci->GetPropertyOffset(key, format); @@ -85,7 +85,7 @@ int luacon_partread(lua_State* l) return luaL_error(l, "Out of range"); if (offset == -1) { - if (!key.compare("id")) + if (byteStringEqualsLiteral(key, "id")) { lua_pushnumber(l, i); return 1; @@ -113,7 +113,7 @@ int luacon_partread(lua_State* l) int luacon_partwrite(lua_State* l) { int i = cIndex; - ByteString key = luaL_optstring(l, 2, ""); + ByteString key = tpt_lua_optByteString(l, 2, ""); CommandInterface::FormatType format; int offset = luacon_ci->GetPropertyOffset(key, format); @@ -159,13 +159,13 @@ int luacon_partswrite(lua_State* l) int luacon_transitionread(lua_State* l) { - ByteString key = luaL_optstring(l, 2, ""); + ByteString key = tpt_lua_optByteString(l, 2, ""); if (legacyTransitionNames.find(key) == legacyTransitionNames.end()) return luaL_error(l, "Invalid property"); StructProperty prop = legacyTransitionNames[key]; //Get Raw Index value for element - lua_pushstring(l, "id"); + lua_pushliteral(l, "id"); lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); @@ -182,13 +182,13 @@ int luacon_transitionread(lua_State* l) int luacon_transitionwrite(lua_State* l) { - ByteString key = luaL_optstring(l, 2, ""); + ByteString key = tpt_lua_optByteString(l, 2, ""); if (legacyTransitionNames.find(key) == legacyTransitionNames.end()) return luaL_error(l, "Invalid property"); StructProperty prop = legacyTransitionNames[key]; //Get Raw Index value for element - lua_pushstring(l, "id"); + lua_pushliteral(l, "id"); lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); @@ -214,13 +214,13 @@ int luacon_transitionwrite(lua_State* l) int luacon_elementread(lua_State* l) { - ByteString key = luaL_optstring(l, 2, ""); + ByteString key = tpt_lua_optByteString(l, 2, ""); if (legacyPropNames.find(key) == legacyPropNames.end()) return luaL_error(l, "Invalid property"); StructProperty prop = legacyPropNames[key]; //Get Raw Index value for element - lua_pushstring(l, "id"); + lua_pushliteral(l, "id"); lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); @@ -237,13 +237,13 @@ int luacon_elementread(lua_State* l) int luacon_elementwrite(lua_State* l) { - ByteString key = luaL_optstring(l, 2, ""); + ByteString key = tpt_lua_optByteString(l, 2, ""); if (legacyPropNames.find(key) == legacyPropNames.end()) return luaL_error(l, "Invalid property"); StructProperty prop = legacyPropNames[key]; //Get Raw Index value for element - lua_pushstring(l, "id"); + lua_pushliteral(l, "id"); lua_rawget(l, 1); int i = lua_tointeger (l, lua_gettop(l)); lua_pop(l, 1); @@ -269,12 +269,6 @@ int luacon_elementwrite(lua_State* l) return 0; } -int luacon_eval(const char *command) -{ - ui::Engine::Ref().LastTick(Platform::GetTime()); - return luaL_dostring (luacon_ci->l, command); -} - void luacon_hook(lua_State * l, lua_Debug * ar) { if(ar->event == LUA_HOOKCOUNT && Platform::GetTime()-ui::Engine::Ref().LastTick() > 3000) @@ -288,7 +282,7 @@ void luacon_hook(lua_State * l, lua_Debug * ar) String luacon_geterror() { luaL_tostring(luacon_ci->l, -1); - String err = ByteString(luaL_optstring(luacon_ci->l, -1, "failed to execute")).FromUtf8(); + String err = tpt_lua_optString(luacon_ci->l, -1, "failed to execute"); lua_pop(luacon_ci->l, 1); return err; } @@ -304,14 +298,14 @@ int luatpt_getelement(lua_State *l) { return luaL_error(l, "Unrecognised element number '%d'", t); } - lua_pushstring(l, luacon_sim->elements[t].Name.ToUtf8().c_str()); + tpt_lua_pushString(l, luacon_sim->elements[t].Name); } else { luaL_checktype(l, 1, LUA_TSTRING); - const char* name = luaL_optstring(l, 1, ""); + auto name = tpt_lua_optByteString(l, 1, ""); if ((t = luacon_sim->GetParticleType(name))==-1) - return luaL_error(l, "Unrecognised element '%s'", name); + return luaL_error(l, "Unrecognised element '%s'", name.c_str()); lua_pushinteger(l, t); } return 1; @@ -319,18 +313,17 @@ int luatpt_getelement(lua_State *l) int luatpt_error(lua_State* l) { - String errorMessage = ByteString(luaL_optstring(l, 1, "Error text")).FromUtf8(); + String errorMessage = tpt_lua_optString(l, 1, "Error text"); ErrorMessage::Blocking("Error", errorMessage); return 0; } int luatpt_drawtext(lua_State* l) { - const char *string; int textx, texty, textred, textgreen, textblue, textalpha; textx = luaL_optint(l, 1, 0); texty = luaL_optint(l, 2, 0); - string = luaL_optstring(l, 3, ""); + auto string = tpt_lua_optString(l, 3, ""); textred = luaL_optint(l, 4, 255); textgreen = luaL_optint(l, 5, 255); textblue = luaL_optint(l, 6, 255); @@ -346,7 +339,7 @@ int luatpt_drawtext(lua_State* l) if (textalpha<0) textalpha = 0; if (textalpha>255) textalpha = 255; - luacon_g->drawtext(textx, texty, ByteString(string).FromUtf8(), textred, textgreen, textblue, textalpha); + luacon_g->drawtext(textx, texty, string, textred, textgreen, textblue, textalpha); return 0; } @@ -365,9 +358,9 @@ int luatpt_create(lua_State* l) return luaL_error(l, "Unrecognised element number '%d'", t); } } else { - const char* name = luaL_optstring(l, 3, "dust"); - if ((t = luacon_sim->GetParticleType(ByteString(name))) == -1) - return luaL_error(l,"Unrecognised element '%s'", name); + auto name = tpt_lua_optByteString(l, 3, "dust"); + if ((t = luacon_sim->GetParticleType(name)) == -1) + return luaL_error(l,"Unrecognised element '%s'", name.c_str()); } retid = luacon_sim->create_part(-1, x, y, t); // failing to create a particle often happens (e.g. if space is already occupied) and isn't usually important, so don't raise an error @@ -421,14 +414,14 @@ int luatpt_setconsole(lua_State* l) int luatpt_log(lua_State* l) { int args = lua_gettop(l); - String text = ""; + String text; for(int i = 1; i <= args; i++) { luaL_tostring(l, -1); if(text.length()) - text=ByteString(luaL_optstring(l, -1, "")).FromUtf8() + ", " + text; + text=tpt_lua_optString(l, -1, "") + ", " + text; else - text=ByteString(luaL_optstring(l, -1, "")).FromUtf8(); + text=tpt_lua_optString(l, -1, ""); lua_pop(l, 2); } if((*luacon_currentCommand)) @@ -438,7 +431,7 @@ int luatpt_log(lua_State* l) *luacon_lastError += text; } else - luacon_ci->Log(CommandInterface::LogNotice, text.c_str()); + luacon_ci->Log(CommandInterface::LogNotice, text); return 0; } @@ -563,24 +556,23 @@ int luatpt_reset_spark(lua_State* l) int luatpt_set_property(lua_State* l) { - const char *name; int r, i, x, y, w, h, t = 0, nx, ny, partsel = 0; float f = 0; int acount = lua_gettop(l); - const char* prop = luaL_optstring(l, 1, ""); + auto prop = tpt_lua_optByteString(l, 1, ""); CommandInterface::FormatType format; int offset = luacon_ci->GetPropertyOffset(prop, format); if (offset == -1) - return luaL_error(l, "Invalid property '%s'", prop); + return luaL_error(l, "Invalid property '%s'", prop.c_str()); if (acount > 2) { if(!lua_isnumber(l, acount) && lua_isstring(l, acount)) { - name = luaL_optstring(l, acount, "none"); - if ((partsel = luacon_sim->GetParticleType(ByteString(name))) == -1) - return luaL_error(l, "Unrecognised element '%s'", name); + auto name = tpt_lua_optByteString(l, acount, "none"); + if ((partsel = luacon_sim->GetParticleType(name)) == -1) + return luaL_error(l, "Unrecognised element '%s'", name.c_str()); } } if (lua_isnumber(l, 2)) @@ -590,14 +582,14 @@ int luatpt_set_property(lua_State* l) else t = luaL_optint(l, 2, 0); - if (!strcmp(prop, "type") && !luacon_sim->IsElementOrNone(t)) + if (byteStringEqualsLiteral(prop, "type") && !luacon_sim->IsElementOrNone(t)) return luaL_error(l, "Unrecognised element number '%d'", t); } else if (lua_isstring(l, 2)) { - name = luaL_checklstring(l, 2, NULL); - if ((t = luacon_sim->GetParticleType(ByteString(name)))==-1) - return luaL_error(l, "Unrecognised element '%s'", name); + auto name = tpt_lua_checkByteString(l, 2); + if ((t = luacon_sim->GetParticleType(name))==-1) + return luaL_error(l, "Unrecognised element '%s'", name.c_str()); } else luaL_error(l, "Expected number or element name as argument 2"); @@ -789,7 +781,7 @@ int luatpt_get_elecmap(lua_State* l) int luatpt_get_property(lua_State* l) { - ByteString prop = luaL_optstring(l, 1, ""); + ByteString prop = tpt_lua_optByteString(l, 1, ""); int i = luaL_optint(l, 2, 0); //x coord or particle index, depending on arguments int y = luaL_optint(l, 3, -1); if (y!=-1 && y=0 && i < XRES && i>=0) @@ -800,7 +792,7 @@ int luatpt_get_property(lua_State* l) r = luacon_sim->photons[y][i]; if (!r) { - if (!prop.compare("type")) + if (byteStringEqualsLiteral(prop, "type")) { lua_pushinteger(l, 0); return 1; @@ -824,7 +816,7 @@ int luatpt_get_property(lua_State* l) if (offset == -1) { - if (!prop.compare("id")) + if (byteStringEqualsLiteral(prop, "id")) { lua_pushnumber(l, i); return 1; @@ -848,7 +840,7 @@ int luatpt_get_property(lua_State* l) } return 1; } - else if (!prop.compare("type")) + else if (byteStringEqualsLiteral(prop, "type")) { lua_pushinteger(l, 0); return 1; @@ -967,9 +959,8 @@ int luatpt_drawline(lua_State* l) int luatpt_textwidth(lua_State* l) { - int strwidth = 0; - const char* string = luaL_optstring(l, 1, ""); - strwidth = Graphics::textwidth(ByteString(string).FromUtf8()); + auto string = tpt_lua_optString(l, 1, ""); + int strwidth = Graphics::textwidth(string); lua_pushinteger(l, strwidth); return 1; } @@ -978,10 +969,10 @@ int luatpt_get_name(lua_State* l) { if (luacon_model->GetUser().UserID) { - lua_pushstring(l, luacon_model->GetUser().Username.c_str()); + tpt_lua_pushByteString(l, luacon_model->GetUser().Username); return 1; } - lua_pushstring(l, ""); + lua_pushliteral(l, ""); return 1; } @@ -1006,22 +997,21 @@ int luatpt_delete(lua_State* l) int luatpt_input(lua_State* l) { - String prompt, title, result, shadow, text; - title = ByteString(luaL_optstring(l, 1, "Title")).FromUtf8(); - prompt = ByteString(luaL_optstring(l, 2, "Enter some text:")).FromUtf8(); - text = ByteString(luaL_optstring(l, 3, "")).FromUtf8(); - shadow = ByteString(luaL_optstring(l, 4, "")).FromUtf8(); + String title = tpt_lua_optString(l, 1, "Title"); + String prompt = tpt_lua_optString(l, 2, "Enter some text:"); + String text = tpt_lua_optString(l, 3, ""); + String shadow = tpt_lua_optString(l, 4, ""); - result = TextPrompt::Blocking(title, prompt, text, shadow, false); + String result = TextPrompt::Blocking(title, prompt, text, shadow, false); - lua_pushstring(l, result.ToUtf8().c_str()); + tpt_lua_pushString(l, result); return 1; } int luatpt_message_box(lua_State* l) { - String title = ByteString(luaL_optstring(l, 1, "Title")).FromUtf8(); - String message = ByteString(luaL_optstring(l, 2, "Message")).FromUtf8(); + String title = tpt_lua_optString(l, 1, "Title"); + String message = tpt_lua_optString(l, 2, "Message"); int large = lua_toboolean(l, 3); new InformationMessage(title, message, large); return 0; @@ -1029,9 +1019,9 @@ int luatpt_message_box(lua_State* l) int luatpt_confirm(lua_State *l) { - String title = ByteString(luaL_optstring(l, 1, "Title")).FromUtf8(); - String message = ByteString(luaL_optstring(l, 2, "Message")).FromUtf8(); - String buttonText = ByteString(luaL_optstring(l, 3, "Confirm")).FromUtf8(); + String title = tpt_lua_optString(l, 1, "Title"); + String message = tpt_lua_optString(l, 2, "Message"); + String buttonText = tpt_lua_optString(l, 3, "Confirm"); bool ret = ConfirmPrompt::Blocking(title, message, buttonText); lua_pushboolean(l, ret ? 1 : 0); return 1; @@ -1261,7 +1251,7 @@ int luatpt_setdrawcap(lua_State* l) int luatpt_getscript(lua_State* l) { int scriptID = luaL_checkinteger(l, 1); - const char *filename = luaL_checkstring(l, 2); + auto filename = tpt_lua_checkByteString(l, 2); int runScript = luaL_optint(l, 3, 0); int confirmPrompt = luaL_optint(l, 4, 1); @@ -1271,7 +1261,7 @@ int luatpt_getscript(lua_State* l) int ret; ByteString scriptData = http::Request::Simple(url, &ret); - if (!scriptData.size() || !filename) + if (!scriptData.size()) { return luaL_error(l, "Server did not return data"); } @@ -1285,14 +1275,15 @@ int luatpt_getscript(lua_State* l) return luaL_error(l, "Invalid Script ID"); } - FILE *outputfile = fopen(filename, "r"); + // FIXME: winapi + FILE *outputfile = fopen(filename.c_str(), "r"); if (outputfile) { fclose(outputfile); outputfile = NULL; - if (!confirmPrompt || ConfirmPrompt::Blocking("File already exists, overwrite?", ByteString(filename).FromUtf8(), "Overwrite")) + if (!confirmPrompt || ConfirmPrompt::Blocking("File already exists, overwrite?", filename.FromUtf8(), "Overwrite")) { - outputfile = fopen(filename, "wb"); + outputfile = fopen(filename.c_str(), "wb"); } else { @@ -1301,7 +1292,7 @@ int luatpt_getscript(lua_State* l) } else { - outputfile = fopen(filename, "wb"); + outputfile = fopen(filename.c_str(), "wb"); } if (!outputfile) { @@ -1313,7 +1304,7 @@ int luatpt_getscript(lua_State* l) outputfile = NULL; if (runScript) { - luaL_dostring(l, ByteString::Build("dofile('", filename, "')").c_str()); + tpt_lua_dostring(l, ByteString::Build("dofile('", filename, "')")); } return 0; @@ -1341,7 +1332,7 @@ int luatpt_screenshot(lua_State* l) int fileType = luaL_optint(l, 2, 0); ByteString filename = luacon_controller->TakeScreenshot(captureUI, fileType); - lua_pushstring(l, filename.c_str()); + tpt_lua_pushByteString(l, filename); return 1; } diff --git a/src/lua/LuaButton.cpp b/src/lua/LuaButton.cpp index 00a7e01ac..eb766bc21 100644 --- a/src/lua/LuaButton.cpp +++ b/src/lua/LuaButton.cpp @@ -28,8 +28,8 @@ LuaButton::LuaButton(lua_State * l) : int posY = luaL_optinteger(l, 2, 0); int sizeX = luaL_optinteger(l, 3, 10); int sizeY = luaL_optinteger(l, 4, 10); - String text = ByteString(luaL_optstring(l, 5, "")).FromUtf8(); - String toolTip = ByteString(luaL_optstring(l, 6, "")).FromUtf8(); + String text = tpt_lua_optString(l, 5, ""); + String toolTip = tpt_lua_optString(l, 6, ""); button = new ui::Button(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, toolTip); component = button; @@ -62,12 +62,12 @@ int LuaButton::text(lua_State * l) int args = lua_gettop(l); if(args) { - button->SetText(ByteString(luaL_checkstring(l, 1)).FromUtf8()); + button->SetText(tpt_lua_checkString(l, 1)); return 0; } else { - lua_pushstring(l, button->GetText().ToUtf8().c_str()); + tpt_lua_pushString(l, button->GetText()); return 1; } } @@ -80,7 +80,7 @@ void LuaButton::triggerAction() lua_rawgeti(l, LUA_REGISTRYINDEX, owner_ref); if (lua_pcall(l, 1, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } diff --git a/src/lua/LuaCheckbox.cpp b/src/lua/LuaCheckbox.cpp index f40c9c75d..5807d4a8e 100644 --- a/src/lua/LuaCheckbox.cpp +++ b/src/lua/LuaCheckbox.cpp @@ -28,7 +28,7 @@ LuaCheckbox::LuaCheckbox(lua_State * l) : int posY = luaL_optinteger(l, 2, 0); int sizeX = luaL_optinteger(l, 3, 10); int sizeY = luaL_optinteger(l, 4, 10); - String text = ByteString(luaL_optstring(l, 5, "")).FromUtf8(); + String text = tpt_lua_optString(l, 5, ""); checkbox = new ui::Checkbox(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, ""); component = checkbox; @@ -60,12 +60,12 @@ int LuaCheckbox::text(lua_State * l) int args = lua_gettop(l); if(args) { - checkbox->SetText(ByteString(luaL_checkstring(l, 1)).FromUtf8()); + checkbox->SetText(tpt_lua_checkString(l, 1)); return 0; } else { - lua_pushstring(l, checkbox->GetText().ToUtf8().c_str()); + tpt_lua_pushString(l, checkbox->GetText()); return 1; } } @@ -79,7 +79,7 @@ void LuaCheckbox::triggerAction() lua_pushboolean(l, checkbox->GetChecked()); if (lua_pcall(l, 2, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } diff --git a/src/lua/LuaCompat.c b/src/lua/LuaCompat.c index dd84b1881..3df2efbb3 100644 --- a/src/lua/LuaCompat.c +++ b/src/lua/LuaCompat.c @@ -60,7 +60,8 @@ int luaL_tostring(lua_State *L, int n) switch (lua_type(L, n)) { case LUA_TNUMBER: - lua_pushstring(L, lua_tostring(L, n)); + lua_tostring(L, n); + lua_pushvalue(L, n); break; case LUA_TSTRING: lua_pushvalue(L, n); diff --git a/src/lua/LuaComponent.cpp b/src/lua/LuaComponent.cpp index fce25c82b..886771f50 100644 --- a/src/lua/LuaComponent.cpp +++ b/src/lua/LuaComponent.cpp @@ -23,7 +23,7 @@ LuaComponent::LuaComponent(lua_State * l) : component(nullptr), owner_ref(LUA_RE { this->l = l; // I don't get how this doesn't cause crashes later on - lua_pushstring(l, "Luacon_ci"); + lua_pushliteral(l, "Luacon_ci"); lua_gettable(l, LUA_REGISTRYINDEX); ci = (LuaScriptInterface*)lua_touserdata(l, -1); lua_pop(l, 1); diff --git a/src/lua/LuaEvents.cpp b/src/lua/LuaEvents.cpp index 229687b45..495f85cde 100644 --- a/src/lua/LuaEvents.cpp +++ b/src/lua/LuaEvents.cpp @@ -23,7 +23,7 @@ void Event::PushBoolean(lua_State * l, bool flag) void Event::PushString(lua_State * l, ByteString str) { #ifdef LUACONSOLE - lua_pushstring(l, str.c_str()); + tpt_lua_pushByteString(l, str); #endif } @@ -137,13 +137,13 @@ int LuaEvents::RegisterEventHook(lua_State *l, ByteString eventName) { if (lua_isfunction(l, 2)) { - lua_pushstring(l, eventName.c_str()); + tpt_lua_pushByteString(l, eventName); lua_rawget(l, LUA_REGISTRYINDEX); if (!lua_istable(l, -1)) { lua_pop(l, 1); lua_newtable(l); - lua_pushstring(l, eventName.c_str()); + tpt_lua_pushByteString(l, eventName); lua_pushvalue(l, -2); lua_rawset(l, LUA_REGISTRYINDEX); } @@ -159,13 +159,13 @@ int LuaEvents::UnregisterEventHook(lua_State *l, ByteString eventName) { if (lua_isfunction(l, 2)) { - lua_pushstring(l, eventName.c_str()); + tpt_lua_pushByteString(l, eventName); lua_rawget(l, LUA_REGISTRYINDEX); if (!lua_istable(l, -1)) { lua_pop(l, -1); lua_newtable(l); - lua_pushstring(l, eventName.c_str()); + tpt_lua_pushByteString(l, eventName); lua_pushvalue(l, -2); lua_rawset(l, LUA_REGISTRYINDEX); } @@ -196,13 +196,13 @@ bool LuaEvents::HandleEvent(LuaScriptInterface *luacon_ci, Event *event, ByteStr ui::Engine::Ref().LastTick(Platform::GetTime()); bool cont = true; lua_State* l = luacon_ci->l; - lua_pushstring(l, eventName.c_str()); + tpt_lua_pushByteString(l, eventName); lua_rawget(l, LUA_REGISTRYINDEX); if (!lua_istable(l, -1)) { lua_pop(l, 1); lua_newtable(l); - lua_pushstring(l, eventName.c_str()); + tpt_lua_pushByteString(l, eventName); lua_pushvalue(l, -2); lua_rawset(l, LUA_REGISTRYINDEX); } @@ -245,7 +245,7 @@ bool LuaEvents::HandleEvent(LuaScriptInterface *luacon_ci, Event *event, ByteStr String LuaEvents::luacon_geterror(LuaScriptInterface * luacon_ci) { luaL_tostring(luacon_ci->l, -1); - String err = ByteString(luaL_optstring(luacon_ci->l, -1, "failed to execute")).FromUtf8(); + String err = tpt_lua_optString(luacon_ci->l, -1, "failed to execute"); lua_pop(luacon_ci->l, 1); return err; } diff --git a/src/lua/LuaLabel.cpp b/src/lua/LuaLabel.cpp index deb333471..349c74e54 100644 --- a/src/lua/LuaLabel.cpp +++ b/src/lua/LuaLabel.cpp @@ -26,7 +26,7 @@ LuaLabel::LuaLabel(lua_State * l) : int posY = luaL_optinteger(l, 2, 0); int sizeX = luaL_optinteger(l, 3, 10); int sizeY = luaL_optinteger(l, 4, 10); - String text = ByteString(luaL_optstring(l, 5, "")).FromUtf8(); + String text = tpt_lua_optString(l, 5, ""); label = new ui::Label(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text); component = label; @@ -37,12 +37,12 @@ int LuaLabel::text(lua_State * l) int args = lua_gettop(l); if(args) { - label->SetText(ByteString(luaL_checkstring(l, 1)).FromUtf8()); + label->SetText(tpt_lua_checkString(l, 1)); return 0; } else { - lua_pushstring(l, label->GetText().ToUtf8().c_str()); + tpt_lua_pushString(l, label->GetText()); return 1; } } diff --git a/src/lua/LuaProgressBar.cpp b/src/lua/LuaProgressBar.cpp index bc4d9cdb9..f4faba654 100644 --- a/src/lua/LuaProgressBar.cpp +++ b/src/lua/LuaProgressBar.cpp @@ -27,7 +27,7 @@ LuaProgressBar::LuaProgressBar(lua_State * l) : int sizeX = luaL_optinteger(l, 3, 10); int sizeY = luaL_optinteger(l, 4, 10); int value = luaL_optinteger(l, 5, 0); - String status = ByteString(luaL_optstring(l, 6, "")).FromUtf8(); + String status = tpt_lua_optString(l, 6, ""); progressBar = new ui::ProgressBar(ui::Point(posX, posY), ui::Point(sizeX, sizeY), value, status); component = progressBar; @@ -53,12 +53,12 @@ int LuaProgressBar::status(lua_State * l) int args = lua_gettop(l); if(args) { - progressBar->SetStatus(ByteString(luaL_checkstring(l, 1)).FromUtf8()); + progressBar->SetStatus(tpt_lua_checkString(l, 1)); return 0; } else { - lua_pushstring(l, progressBar->GetStatus().ToUtf8().c_str()); + tpt_lua_pushString(l, progressBar->GetStatus()); return 1; } } diff --git a/src/lua/LuaScriptHelper.h b/src/lua/LuaScriptHelper.h index 407b005b9..ba261261f 100644 --- a/src/lua/LuaScriptHelper.h +++ b/src/lua/LuaScriptHelper.h @@ -35,7 +35,6 @@ extern int tptParts, tptPartsMeta, tptElementTransitions, tptPartsCData, tptPart extern LuaSmartRef *tptPart; void luacon_hook(lua_State *L, lua_Debug *ar); -int luacon_eval(const char *command); String luacon_geterror(); void luacon_close(); void initLegacyProps(); diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 63284fbbf..8605e283f 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -95,7 +95,7 @@ LuaSmartRef *tptPart = nullptr; int atPanic(lua_State *l) { - throw std::runtime_error("Unprotected lua panic: " + ByteString(lua_tostring(l, -1))); + throw std::runtime_error("Unprotected lua panic: " + tpt_lua_toByteString(l, -1)); } int TptIndexClosure(lua_State *l) @@ -146,7 +146,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): luaL_openlibs(l); luaopen_bit(l); - lua_pushstring(l, "Luacon_ci"); + lua_pushliteral(l, "Luacon_ci"); lua_pushlightuserdata(l, this); lua_settable(l, LUA_REGISTRYINDEX); @@ -265,7 +265,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): lua_pushlightuserdata(l, parts); lua_setfield(l, tptProperties, "partsdata"); - luaL_dostring (l, "ffi = require(\"ffi\")\n\ + tpt_lua_dostring (l, "ffi = require(\"ffi\")\n\ ffi.cdef[[\n\ typedef struct { int type; int life, ctype; float x, y, vx, vy; float temp; int tmp3; int tmp4; int flags; int tmp; int tmp2; unsigned int dcolour; } particle;\n\ ]]\n\ @@ -273,7 +273,7 @@ tpt.parts = ffi.cast(\"particle *\", tpt.partsdata)\n\ ffi = nil\n\ tpt.partsdata = nil"); //Since ffi is REALLY REALLY dangrous, we'll remove it from the environment completely (TODO) - //lua_pushstring(l, "parts"); + //lua_pushliteral(l, "parts"); //tptPartsCData = lua_gettable(l, tptProperties); #else lua_newtable(l); @@ -372,7 +372,7 @@ tpt.partsdata = nil"); ui::Engine::Ref().LastTick(Platform::GetTime()); if (luaL_loadbuffer(l, (const char *)eventcompat_lua, eventcompat_lua_size, "@[built-in eventcompat.lua]") || lua_pcall(l, 0, 0, 0)) { - throw std::runtime_error(ByteString("failed to load built-in eventcompat: ") + lua_tostring(l, -1)); + throw std::runtime_error(ByteString("failed to load built-in eventcompat: ") + tpt_lua_toByteString(l, -1)); } } @@ -410,26 +410,26 @@ void LuaScriptInterface::SetWindow(ui::Window * window) int LuaScriptInterface::tpt_index(lua_State *l) { - ByteString key = luaL_checkstring(l, 2); - if (!key.compare("mousex")) + ByteString key = tpt_lua_checkByteString(l, 2); + if (byteStringEqualsLiteral(key, "mousex")) return lua_pushnumber(l, c->GetView()->GetMousePosition().X), 1; - else if (!key.compare("mousey")) + else if (byteStringEqualsLiteral(key, "mousey")) return lua_pushnumber(l, c->GetView()->GetMousePosition().Y), 1; - else if (!key.compare("selectedl")) - return lua_pushstring(l, m->GetActiveTool(0)->GetIdentifier().c_str()), 1; - else if (!key.compare("selectedr")) - return lua_pushstring(l, m->GetActiveTool(1)->GetIdentifier().c_str()), 1; - else if (!key.compare("selecteda")) - return lua_pushstring(l, m->GetActiveTool(2)->GetIdentifier().c_str()), 1; - else if (!key.compare("selectedreplace")) - return lua_pushstring(l, m->GetActiveTool(3)->GetIdentifier().c_str()), 1; - else if (!key.compare("brushx")) + else if (byteStringEqualsLiteral(key, "selectedl")) + return tpt_lua_pushByteString(l, m->GetActiveTool(0)->GetIdentifier()), 1; + else if (byteStringEqualsLiteral(key, "selectedr")) + return tpt_lua_pushByteString(l, m->GetActiveTool(1)->GetIdentifier()), 1; + else if (byteStringEqualsLiteral(key, "selecteda")) + return tpt_lua_pushByteString(l, m->GetActiveTool(2)->GetIdentifier()), 1; + else if (byteStringEqualsLiteral(key, "selectedreplace")) + return tpt_lua_pushByteString(l, m->GetActiveTool(3)->GetIdentifier()), 1; + else if (byteStringEqualsLiteral(key, "brushx")) return lua_pushnumber(l, m->GetBrush()->GetRadius().X), 1; - else if (!key.compare("brushy")) + else if (byteStringEqualsLiteral(key, "brushy")) return lua_pushnumber(l, m->GetBrush()->GetRadius().Y), 1; - else if (!key.compare("brushID")) + else if (byteStringEqualsLiteral(key, "brushID")) return lua_pushnumber(l, m->GetBrushID()), 1; - else if (!key.compare("decoSpace")) + else if (byteStringEqualsLiteral(key, "decoSpace")) return lua_pushnumber(l, m->GetDecoSpace()), 1; //if not a special key, return the value in the table @@ -438,40 +438,40 @@ int LuaScriptInterface::tpt_index(lua_State *l) int LuaScriptInterface::tpt_newIndex(lua_State *l) { - ByteString key = luaL_checkstring(l, 2); - if (!key.compare("selectedl")) + ByteString key = tpt_lua_checkByteString(l, 2); + if (byteStringEqualsLiteral(key, "selectedl")) { - Tool *t = m->GetToolFromIdentifier(luaL_checkstring(l, 3)); + Tool *t = m->GetToolFromIdentifier(tpt_lua_checkByteString(l, 3)); if (t) c->SetActiveTool(0, t); else luaL_error(l, "Invalid tool identifier: %s", lua_tostring(l, 3)); } - else if (!key.compare("selectedr")) + else if (byteStringEqualsLiteral(key, "selectedr")) { - Tool *t = m->GetToolFromIdentifier(luaL_checkstring(l, 3)); + Tool *t = m->GetToolFromIdentifier(tpt_lua_checkByteString(l, 3)); if (t) c->SetActiveTool(1, t); else luaL_error(l, "Invalid tool identifier: %s", lua_tostring(l, 3)); } - else if (!key.compare("selecteda")) + else if (byteStringEqualsLiteral(key, "selecteda")) { - Tool *t = m->GetToolFromIdentifier(luaL_checkstring(l, 3)); + Tool *t = m->GetToolFromIdentifier(tpt_lua_checkByteString(l, 3)); if (t) c->SetActiveTool(2, t); else luaL_error(l, "Invalid tool identifier: %s", lua_tostring(l, 3)); } - else if (!key.compare("selectedreplace")) + else if (byteStringEqualsLiteral(key, "selectedreplace")) { - Tool *t = m->GetToolFromIdentifier(luaL_checkstring(l, 3)); + Tool *t = m->GetToolFromIdentifier(tpt_lua_checkByteString(l, 3)); if( t) c->SetActiveTool(3, t); else luaL_error(l, "Invalid tool identifier: %s", lua_tostring(l, 3)); } - else if (!key.compare("brushx")) + else if (byteStringEqualsLiteral(key, "brushx")) { int brushx = luaL_checkinteger(l, 3); if (brushx < 0 || brushx >= XRES) @@ -479,7 +479,7 @@ int LuaScriptInterface::tpt_newIndex(lua_State *l) c->SetBrushSize(ui::Point(brushx, m->GetBrush()->GetRadius().Y)); } - else if (!key.compare("brushy")) + else if (byteStringEqualsLiteral(key, "brushy")) { int brushy = luaL_checkinteger(l, 3); if (brushy < 0 || brushy >= YRES) @@ -487,9 +487,9 @@ int LuaScriptInterface::tpt_newIndex(lua_State *l) c->SetBrushSize(ui::Point(m->GetBrush()->GetRadius().X, brushy)); } - else if (!key.compare("brushID")) + else if (byteStringEqualsLiteral(key, "brushID")) m->SetBrushID(luaL_checkinteger(l, 3)); - else if (!key.compare("decoSpace")) + else if (byteStringEqualsLiteral(key, "decoSpace")) m->SetDecoSpace(luaL_checkinteger(l, 3)); else { @@ -642,10 +642,10 @@ int LuaScriptInterface::interface_closeWindow(lua_State * l) int LuaScriptInterface::simulation_signIndex(lua_State *l) { - ByteString key = luaL_checkstring(l, 2); + ByteString key = tpt_lua_checkByteString(l, 2); //Get Raw Index value for element. Maybe there is a way to get the sign index some other way? - lua_pushstring(l, "id"); + lua_pushliteral(l, "id"); lua_rawget(l, 1); int id = lua_tointeger(l, lua_gettop(l))-1; @@ -660,35 +660,35 @@ int LuaScriptInterface::simulation_signIndex(lua_State *l) } int x, y, w, h; - if (!key.compare("text")) - return lua_pushstring(l, luacon_sim->signs[id].text.ToUtf8().c_str()), 1; - else if (!key.compare("displayText")) - return lua_pushstring(l, luacon_sim->signs[id].getDisplayText(luacon_sim, x, y, w, h, false).ToUtf8().c_str()), 1; - else if (!key.compare("justification")) + if (byteStringEqualsLiteral(key, "text")) + return tpt_lua_pushString(l, luacon_sim->signs[id].text), 1; + else if (byteStringEqualsLiteral(key, "displayText")) + return tpt_lua_pushString(l, luacon_sim->signs[id].getDisplayText(luacon_sim, x, y, w, h, false)), 1; + else if (byteStringEqualsLiteral(key, "justification")) return lua_pushnumber(l, (int)luacon_sim->signs[id].ju), 1; - else if (!key.compare("x")) + else if (byteStringEqualsLiteral(key, "x")) return lua_pushnumber(l, luacon_sim->signs[id].x), 1; - else if (!key.compare("y")) + else if (byteStringEqualsLiteral(key, "y")) return lua_pushnumber(l, luacon_sim->signs[id].y), 1; - else if (!key.compare("screenX")) + else if (byteStringEqualsLiteral(key, "screenX")) { luacon_sim->signs[id].getDisplayText(luacon_sim, x, y, w, h); lua_pushnumber(l, x); return 1; } - else if (!key.compare("screenY")) + else if (byteStringEqualsLiteral(key, "screenY")) { luacon_sim->signs[id].getDisplayText(luacon_sim, x, y, w, h); lua_pushnumber(l, y); return 1; } - else if (!key.compare("width")) + else if (byteStringEqualsLiteral(key, "width")) { luacon_sim->signs[id].getDisplayText(luacon_sim, x, y, w, h); lua_pushnumber(l, w); return 1; } - else if (!key.compare("height")) + else if (byteStringEqualsLiteral(key, "height")) { luacon_sim->signs[id].getDisplayText(luacon_sim, x, y, w, h); lua_pushnumber(l, h); @@ -700,10 +700,10 @@ int LuaScriptInterface::simulation_signIndex(lua_State *l) int LuaScriptInterface::simulation_signNewIndex(lua_State *l) { - ByteString key = luaL_checkstring(l, 2); + ByteString key = tpt_lua_checkByteString(l, 2); //Get Raw Index value for element. Maybe there is a way to get the sign index some other way? - lua_pushstring(l, "id"); + lua_pushliteral(l, "id"); lua_rawget(l, 1); int id = lua_tointeger(l, lua_gettop(l))-1; @@ -717,17 +717,17 @@ int LuaScriptInterface::simulation_signNewIndex(lua_State *l) luaL_error(l, "Sign doesn't exist"); } - if (!key.compare("text")) + if (byteStringEqualsLiteral(key, "text")) { - const char *temp = luaL_checkstring(l, 3); - String cleaned = format::CleanString(ByteString(temp).FromUtf8(), false, true, true).Substr(0, 45); + auto temp = tpt_lua_checkString(l, 3); + String cleaned = format::CleanString(temp, false, true, true).Substr(0, 45); if (!cleaned.empty()) luacon_sim->signs[id].text = cleaned; else luaL_error(l, "Text is empty"); return 0; } - else if (!key.compare("justification")) + else if (byteStringEqualsLiteral(key, "justification")) { int ju = luaL_checkinteger(l, 3); if (ju >= 0 && ju <= 3) @@ -736,7 +736,7 @@ int LuaScriptInterface::simulation_signNewIndex(lua_State *l) luaL_error(l, "Invalid justification"); return 0; } - else if (!key.compare("x")) + else if (byteStringEqualsLiteral(key, "x")) { int x = luaL_checkinteger(l, 3); if (x >= 0 && x < XRES) @@ -745,7 +745,7 @@ int LuaScriptInterface::simulation_signNewIndex(lua_State *l) luaL_error(l, "Invalid X coordinate"); return 0; } - else if (!key.compare("y")) + else if (byteStringEqualsLiteral(key, "y")) { int y = luaL_checkinteger(l, 3); if (y >= 0 && y < YRES) @@ -754,7 +754,11 @@ int LuaScriptInterface::simulation_signNewIndex(lua_State *l) luaL_error(l, "Invalid Y coordinate"); return 0; } - else if (!key.compare("displayText") || !key.compare("screenX") || !key.compare("screenY") || !key.compare("width") || !key.compare("height")) + else if (byteStringEqualsLiteral(key, "displayText") || + byteStringEqualsLiteral(key, "screenX") || + byteStringEqualsLiteral(key, "screenY") || + byteStringEqualsLiteral(key, "width") || + byteStringEqualsLiteral(key, "height")) { luaL_error(l, "That property can't be directly set"); } @@ -767,7 +771,7 @@ int LuaScriptInterface::simulation_newsign(lua_State *l) if (luacon_sim->signs.size() >= MAXSIGNS) return lua_pushnil(l), 1; - String text = format::CleanString(ByteString(luaL_checkstring(l, 1)).FromUtf8(), false, true, true).Substr(0, 45); + String text = format::CleanString(tpt_lua_checkString(l, 1), false, true, true).Substr(0, 45); int x = luaL_checkinteger(l, 2); int y = luaL_checkinteger(l, 3); int ju = luaL_optinteger(l, 4, 1); @@ -1136,7 +1140,7 @@ int LuaScriptInterface::simulation_partProperty(lua_State * l) } else if (lua_type(l, 2) == LUA_TSTRING) { - ByteString fieldName = lua_tostring(l, 2); + ByteString fieldName = tpt_lua_toByteString(l, 2); for (auto &alias : Particle::GetPropertyAliases()) { if (fieldName == alias.from) @@ -1807,7 +1811,7 @@ int LuaScriptInterface::simulation_saveStamp(lua_State * l) int w = luaL_optint(l,3,XRES-1); int h = luaL_optint(l,4,YRES-1); ByteString name = luacon_controller->StampRegion(ui::Point(x, y), ui::Point(x+w, y+h)); - lua_pushstring(l, name.c_str()); + tpt_lua_pushByteString(l, name); return 1; } @@ -1820,7 +1824,7 @@ int LuaScriptInterface::simulation_loadStamp(lua_State * l) int y = luaL_optint(l,3,0); if (lua_isstring(l, 1)) //Load from 10 char name, or full filename { - const char * filename = luaL_optstring(l, 1, ""); + auto filename = tpt_lua_optByteString(l, 1, ""); tempfile = Client::Ref().GetStamp(filename); } if ((!tempfile || !tempfile->GetGameSave()) && lua_isnumber(l, 1)) //Load from stamp ID @@ -1849,7 +1853,7 @@ int LuaScriptInterface::simulation_loadStamp(lua_State * l) { pushed = 2; lua_pushnil(l); - lua_pushstring(l, luacon_ci->GetLastError().ToUtf8().c_str()); + tpt_lua_pushString(l, luacon_ci->GetLastError()); } delete tempfile; } @@ -1869,12 +1873,12 @@ int LuaScriptInterface::simulation_deleteStamp(lua_State * l) if (lua_isstring(l, 1)) //note: lua_isstring returns true on numbers too { - const char * filename = luaL_optstring(l, 1, ""); - for (std::vector::const_iterator iterator = stamps.begin(), end = stamps.end(); iterator != end; ++iterator) + auto filename = tpt_lua_optByteString(l, 1, ""); + for (auto &stamp : stamps) { - if (*iterator == filename) + if (stamp == filename) { - Client::Ref().DeleteStamp(*iterator); + Client::Ref().DeleteStamp(stamp); return 0; } } @@ -2343,9 +2347,9 @@ int LuaScriptInterface::simulation_listCustomGol(lua_State *l) for (auto &cgol : luacon_sim->GetCustomGol()) { lua_newtable(l); - lua_pushstring(l, cgol.nameString.ToUtf8().c_str()); + tpt_lua_pushString(l, cgol.nameString); lua_setfield(l, -2, "name"); - lua_pushstring(l, cgol.ruleString.ToUtf8().c_str()); + tpt_lua_pushString(l, cgol.ruleString); lua_setfield(l, -2, "rulestr"); lua_pushnumber(l, cgol.rule); lua_setfield(l, -2, "rule"); @@ -2370,10 +2374,10 @@ int LuaScriptInterface::simulation_addCustomGol(lua_State *l) } else { - ruleString = ByteString(luaL_checkstring(l, 1)).FromUtf8(); + ruleString = tpt_lua_checkString(l, 1); rule = ParseGOLString(ruleString); } - String nameString = ByteString(luaL_checkstring(l, 2)).FromUtf8(); + String nameString = tpt_lua_checkString(l, 2); unsigned int color1 = luaL_checkinteger(l, 3); unsigned int color2 = luaL_checkinteger(l, 4); @@ -2392,7 +2396,7 @@ int LuaScriptInterface::simulation_addCustomGol(lua_State *l) int LuaScriptInterface::simulation_removeCustomGol(lua_State *l) { - ByteString nameString = luaL_checkstring(l, 1); + ByteString nameString = tpt_lua_checkByteString(l, 1); bool removedAny = luacon_model->RemoveCustomGOLType("DEFAULT_PT_LIFECUST_" + nameString); if (removedAny) luacon_model->BuildMenus(); @@ -2792,14 +2796,12 @@ void LuaScriptInterface::LuaGetProperty(lua_State* l, StructProperty property, i break; case StructProperty::BString: { - ByteString byteStringProperty = *((ByteString*)propertyAddress); - lua_pushstring(l, byteStringProperty.c_str()); + tpt_lua_pushByteString(l, *((ByteString*)propertyAddress)); break; } case StructProperty::String: { - ByteString byteStringProperty = (*((String*)propertyAddress)).ToUtf8(); - lua_pushstring(l, byteStringProperty.c_str()); + tpt_lua_pushString(l, *((String*)propertyAddress)); break; } case StructProperty::Colour: @@ -2842,10 +2844,10 @@ void LuaScriptInterface::LuaSetProperty(lua_State* l, StructProperty property, i *((unsigned char*)propertyAddress) = int32_truncate(luaL_checknumber(l, stackPos)); break; case StructProperty::BString: - *((ByteString*)propertyAddress) = ByteString(luaL_checkstring(l, stackPos)); + *((ByteString*)propertyAddress) = tpt_lua_checkByteString(l, stackPos); break; case StructProperty::String: - *((String*)propertyAddress) = ByteString(luaL_checkstring(l, stackPos)).FromUtf8(); + *((String*)propertyAddress) = tpt_lua_checkString(l, stackPos); break; case StructProperty::Colour: #if PIXELSIZE == 4 @@ -2922,11 +2924,10 @@ int LuaScriptInterface::elements_loadDefault(lua_State * l) int LuaScriptInterface::elements_allocate(lua_State * l) { - ByteString group, id, identifier; luaL_checktype(l, 1, LUA_TSTRING); luaL_checktype(l, 2, LUA_TSTRING); - group = ByteString(lua_tostring(l, 1)).ToUpper(); - id = ByteString(lua_tostring(l, 2)).ToUpper(); + auto group = tpt_lua_toByteString(l, 1).ToUpper(); + auto id = tpt_lua_toByteString(l, 2).ToUpper(); if (id.Contains("_")) { @@ -2941,7 +2942,7 @@ int LuaScriptInterface::elements_allocate(lua_State * l) return luaL_error(l, "You cannot create elements in the 'DEFAULT' group."); } - identifier = group + "_PT_" + id; + auto identifier = group + "_PT_" + id; for(int i = 0; i < PT_NUM; i++) { @@ -3295,7 +3296,7 @@ int LuaScriptInterface::elements_element(lua_State * l) lua_setfield(l, -2, prop.Name.c_str()); } - lua_pushstring(l, luacon_sim->elements[id].Identifier.c_str()); + tpt_lua_pushByteString(l, luacon_sim->elements[id].Identifier); lua_setfield(l, -2, "Identifier"); GetDefaultProperties(l, id); @@ -3356,7 +3357,7 @@ int LuaScriptInterface::elements_property(lua_State * l) { return luaL_error(l, "Invalid element"); } - ByteString propertyName(luaL_checklstring(l, 2, NULL)); + ByteString propertyName = tpt_lua_checkByteString(l, 2); auto &properties = Element::GetProperties(); auto prop = std::find_if(properties.begin(), properties.end(), [&propertyName](StructProperty const &p) { @@ -3502,7 +3503,7 @@ int LuaScriptInterface::elements_property(lua_State * l) } else if (propertyName == "Identifier") { - lua_pushstring(l, luacon_sim->elements[id].Identifier.c_str()); + tpt_lua_pushByteString(l, luacon_sim->elements[id].Identifier); return 1; } else if (propertyName == "DefaultProperties") @@ -3576,8 +3577,8 @@ void LuaScriptInterface::initGraphicsAPI() int LuaScriptInterface::graphics_textSize(lua_State * l) { int width, height; - const char* text = luaL_optstring(l, 1, ""); - Graphics::textsize(ByteString(text).FromUtf8(), width, height); + auto text = tpt_lua_optString(l, 1, ""); + Graphics::textsize(text, width, height); lua_pushinteger(l, width); lua_pushinteger(l, height); @@ -3588,7 +3589,7 @@ int LuaScriptInterface::graphics_drawText(lua_State * l) { int x = lua_tointeger(l, 1); int y = lua_tointeger(l, 2); - const char * text = luaL_optstring(l, 3, ""); + auto text = tpt_lua_optString(l, 3, ""); int r = luaL_optint(l, 4, 255); int g = luaL_optint(l, 5, 255); int b = luaL_optint(l, 6, 255); @@ -3603,7 +3604,7 @@ int LuaScriptInterface::graphics_drawText(lua_State * l) if (a<0) a = 0; else if (a>255) a = 255; - luacon_g->drawtext(x, y, ByteString(text).FromUtf8(), r, g, b, a); + luacon_g->drawtext(x, y, text, r, g, b, a); return 0; } @@ -3781,7 +3782,7 @@ void LuaScriptInterface::initFileSystemAPI() int LuaScriptInterface::fileSystem_list(lua_State * l) { - const char * directoryName = luaL_checkstring(l, 1); + auto directoryName = tpt_lua_checkByteString(l, 1); int index = 1; lua_newtable(l); @@ -3789,7 +3790,8 @@ int LuaScriptInterface::fileSystem_list(lua_State * l) DIR * directory; struct dirent * entry; - directory = opendir(directoryName); + // FIXME: winapi + directory = opendir(directoryName.c_str()); if (directory != NULL) { while ((entry = readdir(directory))) @@ -3812,7 +3814,7 @@ int LuaScriptInterface::fileSystem_list(lua_State * l) int LuaScriptInterface::fileSystem_exists(lua_State * l) { - const char * filename = luaL_checkstring(l, 1); + auto filename = tpt_lua_checkByteString(l, 1); bool ret = Platform::Stat(filename); lua_pushboolean(l, ret); @@ -3821,7 +3823,7 @@ int LuaScriptInterface::fileSystem_exists(lua_State * l) int LuaScriptInterface::fileSystem_isFile(lua_State * l) { - const char * filename = luaL_checkstring(l, 1); + auto filename = tpt_lua_checkByteString(l, 1); bool ret = Platform::FileExists(filename); lua_pushboolean(l, ret); @@ -3830,7 +3832,7 @@ int LuaScriptInterface::fileSystem_isFile(lua_State * l) int LuaScriptInterface::fileSystem_isDirectory(lua_State * l) { - const char * dirname = luaL_checkstring(l, 1); + auto dirname = tpt_lua_checkByteString(l, 1); bool ret = Platform::DirectoryExists(dirname); lua_pushboolean(l, ret); @@ -3839,7 +3841,7 @@ int LuaScriptInterface::fileSystem_isDirectory(lua_State * l) int LuaScriptInterface::fileSystem_makeDirectory(lua_State * l) { - const char * dirname = luaL_checkstring(l, 1); + auto dirname = tpt_lua_checkByteString(l, 1); int ret = 0; ret = Platform::MakeDirectory(dirname); @@ -3849,7 +3851,7 @@ int LuaScriptInterface::fileSystem_makeDirectory(lua_State * l) int LuaScriptInterface::fileSystem_removeDirectory(lua_State * l) { - const char * directory = luaL_checkstring(l, 1); + auto directory = tpt_lua_checkByteString(l, 1); bool ret = Platform::DeleteDirectory(directory); lua_pushboolean(l, ret); @@ -3858,7 +3860,7 @@ int LuaScriptInterface::fileSystem_removeDirectory(lua_State * l) int LuaScriptInterface::fileSystem_removeFile(lua_State * l) { - const char * filename = luaL_checkstring(l, 1); + auto filename = tpt_lua_checkByteString(l, 1); bool ret = Platform::RemoveFile(filename); lua_pushboolean(l, ret); @@ -3867,11 +3869,12 @@ int LuaScriptInterface::fileSystem_removeFile(lua_State * l) int LuaScriptInterface::fileSystem_move(lua_State * l) { - const char * filename = luaL_checkstring(l, 1); - const char * newFilename = luaL_checkstring(l, 2); + auto filename = tpt_lua_checkByteString(l, 1); + auto newFilename = tpt_lua_checkByteString(l, 2); int ret = 0; - ret = rename(filename, newFilename); + // FIXME: winapi + ret = rename(filename.c_str(), newFilename.c_str()); lua_pushboolean(l, ret == 0); return 1; @@ -3879,12 +3882,13 @@ int LuaScriptInterface::fileSystem_move(lua_State * l) int LuaScriptInterface::fileSystem_copy(lua_State * l) { - const char * filename = luaL_checkstring(l, 1); - const char * newFilename = luaL_checkstring(l, 2); + auto filename = tpt_lua_checkByteString(l, 1); + auto newFilename = tpt_lua_checkByteString(l, 2); int ret = 0; try { + // FIXME: winapi std::ifstream source(filename, std::ios::binary); std::ofstream dest(newFilename, std::ios::binary); source.exceptions(std::ifstream::failbit | std::ifstream::badbit); @@ -3933,25 +3937,25 @@ void LuaScriptInterface::initPlatformAPI() int LuaScriptInterface::platform_platform(lua_State * l) { - lua_pushstring(l, IDENT_PLATFORM); + lua_pushliteral(l, IDENT_PLATFORM); return 1; } int LuaScriptInterface::platform_ident(lua_State * l) { - lua_pushstring(l, IDENT); + lua_pushliteral(l, IDENT); return 1; } int LuaScriptInterface::platform_build(lua_State * l) { - lua_pushstring(l, IDENT_BUILD); + lua_pushliteral(l, IDENT_BUILD); return 1; } int LuaScriptInterface::platform_releaseType(lua_State * l) { - lua_pushstring(l, IDENT_RELTYPE); + lua_pushliteral(l, IDENT_RELTYPE); return 1; } @@ -3959,7 +3963,7 @@ int LuaScriptInterface::platform_exeName(lua_State * l) { ByteString name = Platform::ExecutableName(); if (name.length()) - lua_pushstring(l, name.c_str()); + tpt_lua_pushByteString(l, name); else luaL_error(l, "Error, could not get executable name"); return 1; @@ -3973,21 +3977,21 @@ int LuaScriptInterface::platform_restart(lua_State * l) int LuaScriptInterface::platform_openLink(lua_State * l) { - const char * uri = luaL_checkstring(l, 1); + auto uri = tpt_lua_checkByteString(l, 1); Platform::OpenURI(uri); return 0; } int LuaScriptInterface::platform_clipboardCopy(lua_State * l) { - lua_pushstring(l, ClipboardPull().c_str()); + tpt_lua_pushByteString(l, ClipboardPull()); return 1; } int LuaScriptInterface::platform_clipboardPaste(lua_State * l) { luaL_checktype(l, 1, LUA_TSTRING); - ClipboardPush(luaL_optstring(l, 1, "")); + ClipboardPush(tpt_lua_optByteString(l, 1, "")); return 0; } @@ -4022,7 +4026,6 @@ void LuaScriptInterface::initEventAPI() int LuaScriptInterface::event_register(lua_State * l) { - //ByteString eventname = luaL_checkstring(l, 1); int eventName = luaL_checkinteger(l, 1); luaL_checktype(l, 2, LUA_TFUNCTION); return LuaEvents::RegisterEventHook(l, ByteString::Build("tptevents-", eventName)); @@ -4030,7 +4033,6 @@ int LuaScriptInterface::event_register(lua_State * l) int LuaScriptInterface::event_unregister(lua_State * l) { - //ByteString eventname = luaL_checkstring(l, 1); int eventName = luaL_checkinteger(l, 1); luaL_checktype(l, 2, LUA_TFUNCTION); return LuaEvents::UnregisterEventHook(l, ByteString::Build("tptevents-", eventName)); @@ -4183,7 +4185,7 @@ static int http_request_finish(lua_State *l) static int http_request(lua_State *l, bool isPost) { - ByteString uri(luaL_checkstring(l, 1)); + ByteString uri = tpt_lua_checkByteString(l, 1); std::map post_data; if (isPost) { @@ -4193,7 +4195,7 @@ static int http_request(lua_State *l, bool isPost) while (lua_next(l, 2)) { lua_pushvalue(l, -2); - post_data.emplace(lua_tostring(l, -1), lua_tostring(l, -2)); + post_data.emplace(tpt_lua_toByteString(l, -1), tpt_lua_toByteString(l, -2)); lua_pop(l, 2); } } @@ -4209,7 +4211,7 @@ static int http_request(lua_State *l, bool isPost) for (auto i = 0U; i < size; ++i) { lua_rawgeti(l, headersIndex, i + 1); - headers.push_back(lua_tostring(l, -1)); + headers.push_back(tpt_lua_toByteString(l, -1)); lua_pop(l, 1); } } @@ -4220,7 +4222,7 @@ static int http_request(lua_State *l, bool isPost) while (lua_next(l, headersIndex)) { lua_pushvalue(l, -2); - headers.push_back(lua_tostring(l, -1) + ByteString(": ") + lua_tostring(l, -2)); + headers.push_back(tpt_lua_toByteString(l, -1) + ByteString(": ") + tpt_lua_toByteString(l, -2)); lua_pop(l, 2); } } @@ -4311,12 +4313,12 @@ int LuaScriptInterface::Command(String command) lastCode += command; ByteString tmp = ("return " + lastCode).ToUtf8(); ui::Engine::Ref().LastTick(Platform::GetTime()); - luaL_loadbuffer(l, tmp.c_str(), tmp.length(), "@console"); + luaL_loadbuffer(l, tmp.data(), tmp.size(), "@console"); if (lua_type(l, -1) != LUA_TFUNCTION) { lua_pop(l, 1); ByteString lastCodeUtf8 = lastCode.ToUtf8(); - luaL_loadbuffer(l, lastCodeUtf8.c_str(), lastCodeUtf8.length(), "@console"); + luaL_loadbuffer(l, lastCodeUtf8.data(), lastCodeUtf8.size(), "@console"); } if (lua_type(l, -1) != LUA_TFUNCTION) { @@ -4339,9 +4341,9 @@ int LuaScriptInterface::Command(String command) { luaL_tostring(l, level); if (text.length()) - text += ", " + ByteString(luaL_optstring(l, -1, "")).FromUtf8(); + text += ", " + tpt_lua_optString(l, -1, ""); else - text = ByteString(luaL_optstring(l, -1, "")).FromUtf8(); + text = tpt_lua_optString(l, -1, ""); lua_pop(l, 1); } if (text.length()) @@ -4592,4 +4594,77 @@ void LuaScriptInterface::initSocketAPI() } #endif +void tpt_lua_pushByteString(lua_State *L, const ByteString &str) +{ + lua_pushlstring(L, str.data(), str.size()); +} + +void tpt_lua_pushString(lua_State *L, const String &str) +{ + tpt_lua_pushByteString(L, str.ToUtf8()); +} + +ByteString tpt_lua_toByteString(lua_State *L, int index) +{ + size_t size; + if (auto *data = lua_tolstring(L, index, &size)) + { + return ByteString(data, size); + } + return {}; +} + +String tpt_lua_toString(lua_State *L, int index, bool ignoreError) +{ + return tpt_lua_toByteString(L, index).FromUtf8(ignoreError); +} + +ByteString tpt_lua_checkByteString(lua_State *L, int index) +{ + size_t size; + if (auto *data = luaL_checklstring(L, index, &size)) + { + return ByteString(data, size); + } + return {}; +} + +String tpt_lua_checkString(lua_State *L, int index, bool ignoreError) +{ + return tpt_lua_checkByteString(L, index).FromUtf8(ignoreError); +} + +ByteString tpt_lua_optByteString(lua_State *L, int index, ByteString defaultValue) +{ + if (lua_isnoneornil(L, index)) + { + return defaultValue; + } + return tpt_lua_checkByteString(L, index); +} + +String tpt_lua_optString(lua_State *L, int index, String defaultValue, bool ignoreError) +{ + if (lua_isnoneornil(L, index)) + { + return defaultValue; + } + return tpt_lua_checkString(L, index, ignoreError); +} + +int tpt_lua_loadstring(lua_State *L, const ByteString &str) +{ + return luaL_loadbuffer(L, str.data(), str.size(), str.data()); +} + +int tpt_lua_dostring(lua_State *L, const ByteString &str) +{ + return tpt_lua_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0); +} + +bool tpt_lua_equalsString(lua_State *L, int index, const char *data, size_t size) +{ + return lua_isstring(L, index) && lua_objlen(L, index) == size && !memcmp(lua_tostring(L, index), data, size); +} + #endif diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index 1b21dfbd5..292d70b35 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -231,4 +231,30 @@ public: extern LuaScriptInterface *luacon_ci; +void tpt_lua_pushByteString(lua_State *L, const ByteString &str); +void tpt_lua_pushString(lua_State *L, const String &str); + +// TODO: toByteStringView once we have a ByteStringView (or std::string_view, if we get rid of ByteString) +ByteString tpt_lua_toByteString(lua_State *L, int index); +String tpt_lua_toString(lua_State *L, int index, bool ignoreError = true); + +// TODO: toByteStringView once we have a ByteStringView (or std::string_view, if we get rid of ByteString) +ByteString tpt_lua_checkByteString(lua_State *L, int index); +String tpt_lua_checkString(lua_State *L, int index, bool ignoreError = true); + +// TODO: toByteStringView once we have a ByteStringView (or std::string_view, if we get rid of ByteString) +ByteString tpt_lua_optByteString(lua_State *L, int index, ByteString defaultValue = {}); +String tpt_lua_optString(lua_State *L, int index, String defaultValue = {}, bool ignoreError = true); + +int tpt_lua_loadstring(lua_State *L, const ByteString &str); +int tpt_lua_dostring(lua_State *L, const ByteString &str); + +bool tpt_lua_equalsString(lua_State *L, int index, const char *data, size_t size); +template +// TODO: use std::literals::string_literals::operator""s if we get rid of ByteString +bool tpt_lua_equalsLiteral(lua_State *L, int index, const char (&lit)[N]) +{ + return tpt_lua_equalsString(L, index, lit, N - 1U); +} + #endif /* LUASCRIPTINTERFACE_H_ */ diff --git a/src/lua/LuaSlider.cpp b/src/lua/LuaSlider.cpp index c107e4ddd..e79eb693f 100644 --- a/src/lua/LuaSlider.cpp +++ b/src/lua/LuaSlider.cpp @@ -79,7 +79,7 @@ void LuaSlider::triggerOnValueChanged() lua_pushinteger(l, slider->GetValue()); if (lua_pcall(l, 2, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } diff --git a/src/lua/LuaTCPSocket.cpp b/src/lua/LuaTCPSocket.cpp index bbffa03aa..670bd3ff0 100644 --- a/src/lua/LuaTCPSocket.cpp +++ b/src/lua/LuaTCPSocket.cpp @@ -17,6 +17,9 @@ # include #endif +#include "LuaScriptInterface.h" +#include "Misc.h" + void SetupCurlEasyCiphers(CURL *easy); namespace LuaTCPSocket @@ -264,11 +267,11 @@ namespace LuaTCPSocket readLine, } pattern = readN; size_t len = 4096; - if (lua_isstring(l, 2) && !strcmp(lua_tostring(l, 2), "*a")) + if (tpt_lua_equalsLiteral(l, 2, "*a")) { pattern = readAll; } - else if (lua_isstring(l, 2) && !strcmp(lua_tostring(l, 2), "*l")) + else if (tpt_lua_equalsLiteral(l, 2, "*l")) { pattern = readLine; } @@ -459,7 +462,7 @@ namespace LuaTCPSocket // the hostnames. curl_easy_setopt(tcps->easy, CURLOPT_ERRORBUFFER, tcps->errorBuf); curl_easy_setopt(tcps->easy, CURLOPT_CONNECT_ONLY, 1L); - ByteString address = luaL_checkstring(l, 2); + ByteString address = tpt_lua_checkByteString(l, 2); curl_easy_setopt(tcps->easy, CURLOPT_PORT, long(luaL_checkinteger(l, 3))); curl_easy_setopt(tcps->easy, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(tcps->easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); @@ -582,18 +585,18 @@ namespace LuaTCPSocket static int SetOption(lua_State *l) { auto *tcps = (TCPSocket *)luaL_checkudata(l, 1, "TCPSocket"); - auto *option = luaL_checkstring(l, 2); - if (!strcmp(option, "keepalive")) + auto option = tpt_lua_checkByteString(l, 2); + if (byteStringEqualsLiteral(option, "keepalive")) { curl_easy_setopt(tcps->easy, CURLOPT_TCP_KEEPALIVE, long(lua_toboolean(l, 3))); return 0; } - else if (!strcmp(option, "tcp-nodelay")) + else if (byteStringEqualsLiteral(option, "tcp-nodelay")) { curl_easy_setopt(tcps->easy, CURLOPT_TCP_NODELAY, long(lua_toboolean(l, 3))); return 0; } - else if (!strcmp(option, "verify-peer")) + else if (byteStringEqualsLiteral(option, "verify-peer")) { curl_easy_setopt(tcps->easy, CURLOPT_SSL_VERIFYPEER, long(lua_toboolean(l, 3))); return 0; @@ -604,18 +607,18 @@ namespace LuaTCPSocket static int Shutdown(lua_State *l) { auto *tcps = (TCPSocket *)luaL_checkudata(l, 1, "TCPSocket"); - auto *direction = luaL_optstring(l, 2, "both"); - if (!strcmp(direction, "receive")) + auto direction = tpt_lua_optByteString(l, 2, "both"); + if (byteStringEqualsLiteral(direction, "receive")) { tcps->readClosed = true; return 0; } - else if (!strcmp(direction, "send")) + else if (byteStringEqualsLiteral(direction, "send")) { tcps->writeClosed = true; return 0; } - else if (!strcmp(direction, "both")) + else if (byteStringEqualsLiteral(direction, "both")) { tcps->readClosed = true; tcps->writeClosed = true; diff --git a/src/lua/LuaTextbox.cpp b/src/lua/LuaTextbox.cpp index 57dd475db..16adcd466 100644 --- a/src/lua/LuaTextbox.cpp +++ b/src/lua/LuaTextbox.cpp @@ -29,8 +29,8 @@ LuaTextbox::LuaTextbox(lua_State * l) : int posY = luaL_optinteger(l, 2, 0); int sizeX = luaL_optinteger(l, 3, 10); int sizeY = luaL_optinteger(l, 4, 10); - String text = ByteString(luaL_optstring(l, 5, "")).FromUtf8(); - String placeholder = ByteString(luaL_optstring(l, 6, "")).FromUtf8(); + String text = tpt_lua_optString(l, 5, ""); + String placeholder = tpt_lua_optString(l, 6, ""); textbox = new ui::Textbox(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, placeholder); textbox->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; @@ -67,7 +67,7 @@ void LuaTextbox::triggerOnTextChanged() lua_rawgeti(l, LUA_REGISTRYINDEX, owner_ref); if (lua_pcall(l, 1, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_optString(l, -1)); } } } @@ -77,12 +77,12 @@ int LuaTextbox::text(lua_State * l) int args = lua_gettop(l); if(args) { - textbox->SetText(ByteString(luaL_checkstring(l, 1)).FromUtf8()); + textbox->SetText(tpt_lua_checkString(l, 1)); return 0; } else { - lua_pushstring(l, textbox->GetText().ToUtf8().c_str()); + tpt_lua_pushString(l, textbox->GetText()); return 1; } } diff --git a/src/lua/LuaWindow.cpp b/src/lua/LuaWindow.cpp index dc39fc093..d88cd76ef 100644 --- a/src/lua/LuaWindow.cpp +++ b/src/lua/LuaWindow.cpp @@ -73,7 +73,7 @@ LuaWindow::LuaWindow(lua_State * l) : if (sizeY < 10) sizeY = 10; - lua_pushstring(l, "Luacon_ci"); + lua_pushliteral(l, "Luacon_ci"); lua_gettable(l, LUA_REGISTRYINDEX); ci = (LuaScriptInterface*)lua_touserdata(l, -1); lua_pop(l, 1); @@ -232,7 +232,7 @@ void LuaWindow::triggerOnInitialized() lua_rawgeti(l, LUA_REGISTRYINDEX, onInitializedFunction); if(lua_pcall(l, 0, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -244,7 +244,7 @@ void LuaWindow::triggerOnExit() lua_rawgeti(l, LUA_REGISTRYINDEX, onExitFunction); if(lua_pcall(l, 0, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -257,7 +257,7 @@ void LuaWindow::triggerOnTick(float dt) lua_pushnumber(l, dt); if(lua_pcall(l, 1, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -269,7 +269,7 @@ void LuaWindow::triggerOnDraw() lua_rawgeti(l, LUA_REGISTRYINDEX, onDrawFunction); if(lua_pcall(l, 0, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -281,7 +281,7 @@ void LuaWindow::triggerOnFocus() lua_rawgeti(l, LUA_REGISTRYINDEX, onFocusFunction); if(lua_pcall(l, 0, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -293,7 +293,7 @@ void LuaWindow::triggerOnBlur() lua_rawgeti(l, LUA_REGISTRYINDEX, onBlurFunction); if(lua_pcall(l, 0, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -305,7 +305,7 @@ void LuaWindow::triggerOnTryExit() lua_rawgeti(l, LUA_REGISTRYINDEX, onTryExitFunction); if(lua_pcall(l, 0, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -317,7 +317,7 @@ void LuaWindow::triggerOnTryOkay() lua_rawgeti(l, LUA_REGISTRYINDEX, onTryOkayFunction); if(lua_pcall(l, 0, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -333,7 +333,7 @@ void LuaWindow::triggerOnMouseMove(int x, int y, int dx, int dy) lua_pushinteger(l, dy); if(lua_pcall(l, 4, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -348,7 +348,7 @@ void LuaWindow::triggerOnMouseDown(int x, int y, unsigned button) lua_pushinteger(l, button); if(lua_pcall(l, 3, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -363,7 +363,7 @@ void LuaWindow::triggerOnMouseUp(int x, int y, unsigned button) lua_pushinteger(l, button); if(lua_pcall(l, 3, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -378,7 +378,7 @@ void LuaWindow::triggerOnMouseWheel(int x, int y, int d) lua_pushinteger(l, d); if(lua_pcall(l, 3, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -395,7 +395,7 @@ void LuaWindow::triggerOnKeyPress(int key, int scan, bool repeat, bool shift, bo lua_pushboolean(l, alt); if(lua_pcall(l, 5, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } @@ -412,7 +412,7 @@ void LuaWindow::triggerOnKeyRelease(int key, int scan, bool repeat, bool shift, lua_pushboolean(l, alt); if(lua_pcall(l, 5, 0, 0)) { - ci->Log(CommandInterface::LogError, ByteString(lua_tostring(l, -1)).FromUtf8()); + ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1)); } } } diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 6321897bf..f3c82881c 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -4626,18 +4626,18 @@ movedone: int Simulation::GetParticleType(ByteString type) { - char * txt = (char*)type.c_str(); + type = type.ToUpper(); // alternative names for some elements - if (!strcasecmp(txt, "C4")) + if (byteStringEqualsLiteral(type, "C4")) return PT_PLEX; - else if (!strcasecmp(txt, "C5")) + else if (byteStringEqualsLiteral(type, "C5")) return PT_C5; - else if (!strcasecmp(txt, "NONE")) + else if (byteStringEqualsLiteral(type, "NONE")) return PT_NONE; for (int i = 1; i < PT_NUM; i++) { - if (!strcasecmp(txt, elements[i].Name.ToUtf8().c_str()) && elements[i].Name.size() && elements[i].Enabled) + if (elements[i].Name.size() && elements[i].Enabled && type == elements[i].Name.ToUtf8().ToUpper()) { return i; }