Skip to content

Commit

Permalink
Organize Lua API into multiple TUs
Browse files Browse the repository at this point in the history
  • Loading branch information
LBPHacker committed Jan 24, 2024
1 parent 2a43e8a commit 1cb8f03
Show file tree
Hide file tree
Showing 49 changed files with 6,380 additions and 6,428 deletions.
1 change: 1 addition & 0 deletions src/common/platform/Platform.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "common/String.h"
#include <cstdint>
#include <string>
#include <vector>
#include <optional>
Expand Down
6 changes: 3 additions & 3 deletions src/lua/CommandInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

CommandInterface *commandInterface = nullptr;

CommandInterface::CommandInterface(GameController * c, GameModel * m)
CommandInterface::CommandInterface(GameController *newGameController, GameModel *newGameModel)
{
assert(!commandInterface);
commandInterface = this;
this->m = m;
this->c = c;
this->m = newGameModel;
this->c = newGameController;
}

CommandInterface::~CommandInterface()
Expand Down
4 changes: 2 additions & 2 deletions src/lua/CommandInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CommandInterface
String lastError;
GameModel * m;
GameController * c;
CommandInterface(GameController * c, GameModel * m);
CommandInterface(GameController *newGameController, GameModel *newGameModel);

public:
enum LogType { LogError, LogWarning, LogNotice };
Expand All @@ -35,7 +35,7 @@ class CommandInterface
String GetLastError();
virtual ~CommandInterface();

static CommandInterface *Create(GameController * c, GameModel * m);
static CommandInterface *Create(GameController *newGameController, GameModel *newGameModel);
};

extern CommandInterface *commandInterface;
46 changes: 23 additions & 23 deletions src/lua/LuaButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,53 @@ Luna<LuaButton>::RegType LuaButton::methods[] = {
{0, 0}
};

LuaButton::LuaButton(lua_State * l) :
LuaComponent(l)
LuaButton::LuaButton(lua_State *L) :
LuaComponent(L)
{
int posX = luaL_optinteger(l, 1, 0);
int posY = luaL_optinteger(l, 2, 0);
int sizeX = luaL_optinteger(l, 3, 10);
int sizeY = luaL_optinteger(l, 4, 10);
String text = tpt_lua_optString(l, 5, "");
String toolTip = tpt_lua_optString(l, 6, "");
int posX = luaL_optinteger(L, 1, 0);
int posY = luaL_optinteger(L, 2, 0);
int sizeX = luaL_optinteger(L, 3, 10);
int sizeY = luaL_optinteger(L, 4, 10);
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;
button->SetActionCallback({ [this] { triggerAction(); } });
}

int LuaButton::enabled(lua_State * l)
int LuaButton::enabled(lua_State *L)
{
int args = lua_gettop(l);
int args = lua_gettop(L);
if(args)
{
luaL_checktype(l, 1, LUA_TBOOLEAN);
button->Enabled = lua_toboolean(l, 1);
luaL_checktype(L, 1, LUA_TBOOLEAN);
button->Enabled = lua_toboolean(L, 1);
return 0;
}
else
{
lua_pushboolean(l, button->Enabled);
lua_pushboolean(L, button->Enabled);
return 1;
}
}

int LuaButton::action(lua_State * l)
int LuaButton::action(lua_State *L)
{
return actionFunction.CheckAndAssignArg1(l);
return actionFunction.CheckAndAssignArg1(L);
}

int LuaButton::text(lua_State * l)
int LuaButton::text(lua_State *L)
{
int args = lua_gettop(l);
int args = lua_gettop(L);
if(args)
{
button->SetText(tpt_lua_checkString(l, 1));
button->SetText(tpt_lua_checkString(L, 1));
return 0;
}
else
{
tpt_lua_pushString(l, button->GetText());
tpt_lua_pushString(L, button->GetText());
return 1;
}
}
Expand All @@ -70,11 +70,11 @@ void LuaButton::triggerAction()
{
if(actionFunction)
{
lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction);
lua_rawgeti(l, LUA_REGISTRYINDEX, owner_ref);
if (tpt_lua_pcall(l, 1, 0, 0, eventTraitNone))
lua_rawgeti(L, LUA_REGISTRYINDEX, actionFunction);
lua_rawgeti(L, LUA_REGISTRYINDEX, owner_ref);
if (tpt_lua_pcall(L, 1, 0, 0, eventTraitNone))
{
ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1));
ci->Log(CommandInterface::LogError, tpt_lua_toString(L, -1));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lua/LuaButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class LuaButton: public LuaComponent
ui::Button * button;
LuaComponentCallback actionFunction;
void triggerAction();
int action(lua_State * l);
int text(lua_State * l);
int enabled(lua_State * l);
int action(lua_State *L);
int text(lua_State *L);
int enabled(lua_State *L);
public:
static const char className[];
static Luna<LuaButton>::RegType methods[];

LuaButton(lua_State * l);
LuaButton(lua_State *L);
~LuaButton();
};
64 changes: 64 additions & 0 deletions src/lua/LuaBz2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "LuaScriptInterface.h"
#include "bzip2/bz2wrap.h"

static int compress(lua_State *L)
{
auto src = tpt_lua_checkByteString(L, 1);
auto maxSize = size_t(luaL_optinteger(L, 2, 0));
std::vector<char> dest;
auto result = BZ2WCompress(dest, src.data(), src.size(), maxSize);
#define RETURN_ERR(str) lua_pushnil(L); lua_pushinteger(L, int(result)); lua_pushliteral(L, str); return 3
switch (result)
{
case BZ2WCompressOk: break;
case BZ2WCompressNomem: RETURN_ERR("out of memory");
case BZ2WCompressLimit: RETURN_ERR("size limit exceeded");
}
#undef RETURN_ERR
tpt_lua_pushByteString(L, ByteString(dest.begin(), dest.end()));
return 1;
}

static int decompress(lua_State *L)
{
auto src = tpt_lua_checkByteString(L, 1);
auto maxSize = size_t(luaL_optinteger(L, 2, 0));
std::vector<char> dest;
auto result = BZ2WDecompress(dest, src.data(), src.size(), maxSize);
#define RETURN_ERR(str) lua_pushnil(L); lua_pushinteger(L, int(result)); lua_pushliteral(L, str); return 3
switch (result)
{
case BZ2WDecompressOk: break;
case BZ2WDecompressNomem: RETURN_ERR("out of memory");
case BZ2WDecompressLimit: RETURN_ERR("size limit exceeded");
case BZ2WDecompressType:
case BZ2WDecompressBad:
case BZ2WDecompressEof: RETURN_ERR("corrupted stream");
}
#undef RETURN_ERR
tpt_lua_pushByteString(L, ByteString(dest.begin(), dest.end()));
return 1;
}

void LuaBz2::Open(lua_State *L)
{
static const luaL_Reg reg[] = {
#define LFUNC(v) { #v, v }
LFUNC(compress),
LFUNC(decompress),
#undef LFUNC
{ NULL, NULL }
};
lua_newtable(L);
luaL_register(L, NULL, reg);
#define LCONSTAS(k, v) lua_pushinteger(L, int(v)); lua_setfield(L, -2, k)
LCONSTAS("COMPRESS_NOMEM" , BZ2WCompressNomem );
LCONSTAS("COMPRESS_LIMIT" , BZ2WCompressLimit );
LCONSTAS("DECOMPRESS_NOMEM", BZ2WDecompressNomem);
LCONSTAS("DECOMPRESS_LIMIT", BZ2WDecompressLimit);
LCONSTAS("DECOMPRESS_TYPE" , BZ2WDecompressType );
LCONSTAS("DECOMPRESS_BAD" , BZ2WDecompressBad );
LCONSTAS("DECOMPRESS_EOF" , BZ2WDecompressEof );
#undef LCONSTAS
lua_setglobal(L, "bz2");
}
44 changes: 22 additions & 22 deletions src/lua/LuaCheckbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,51 @@ Luna<LuaCheckbox>::RegType LuaCheckbox::methods[] = {
{0, 0}
};

LuaCheckbox::LuaCheckbox(lua_State * l) :
LuaComponent(l)
LuaCheckbox::LuaCheckbox(lua_State *L) :
LuaComponent(L)
{
int posX = luaL_optinteger(l, 1, 0);
int posY = luaL_optinteger(l, 2, 0);
int sizeX = luaL_optinteger(l, 3, 10);
int sizeY = luaL_optinteger(l, 4, 10);
String text = tpt_lua_optString(l, 5, "");
int posX = luaL_optinteger(L, 1, 0);
int posY = luaL_optinteger(L, 2, 0);
int sizeX = luaL_optinteger(L, 3, 10);
int sizeY = luaL_optinteger(L, 4, 10);
String text = tpt_lua_optString(L, 5, "");

checkbox = new ui::Checkbox(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, "");
component = checkbox;
checkbox->SetActionCallback({ [this] { triggerAction(); } });
}

int LuaCheckbox::checked(lua_State * l)
int LuaCheckbox::checked(lua_State *L)
{
int args = lua_gettop(l);
int args = lua_gettop(L);
if(args)
{
checkbox->SetChecked(lua_toboolean(l, 1));
checkbox->SetChecked(lua_toboolean(L, 1));
return 0;
}
else
{
lua_pushboolean(l, checkbox->GetChecked());
lua_pushboolean(L, checkbox->GetChecked());
return 1;
}
}

int LuaCheckbox::action(lua_State * l)
int LuaCheckbox::action(lua_State *L)
{
return actionFunction.CheckAndAssignArg1(l);
return actionFunction.CheckAndAssignArg1(L);
}

int LuaCheckbox::text(lua_State * l)
int LuaCheckbox::text(lua_State *L)
{
int args = lua_gettop(l);
int args = lua_gettop(L);
if(args)
{
checkbox->SetText(tpt_lua_checkString(l, 1));
checkbox->SetText(tpt_lua_checkString(L, 1));
return 0;
}
else
{
tpt_lua_pushString(l, checkbox->GetText());
tpt_lua_pushString(L, checkbox->GetText());
return 1;
}
}
Expand All @@ -68,12 +68,12 @@ void LuaCheckbox::triggerAction()
{
if(actionFunction)
{
lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction);
lua_rawgeti(l, LUA_REGISTRYINDEX, owner_ref);
lua_pushboolean(l, checkbox->GetChecked());
if (tpt_lua_pcall(l, 2, 0, 0, eventTraitNone))
lua_rawgeti(L, LUA_REGISTRYINDEX, actionFunction);
lua_rawgeti(L, LUA_REGISTRYINDEX, owner_ref);
lua_pushboolean(L, checkbox->GetChecked());
if (tpt_lua_pcall(L, 2, 0, 0, eventTraitNone))
{
ci->Log(CommandInterface::LogError, tpt_lua_toString(l, -1));
ci->Log(CommandInterface::LogError, tpt_lua_toString(L, -1));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lua/LuaCheckbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class LuaCheckbox: public LuaComponent
ui::Checkbox * checkbox;
LuaComponentCallback actionFunction;
void triggerAction();
int action(lua_State * l);
int checked(lua_State * l);
int text(lua_State * l);
int action(lua_State *L);
int checked(lua_State *L);
int text(lua_State *L);
public:
static const char className[];
static Luna<LuaCheckbox>::RegType methods[];

LuaCheckbox(lua_State * l);
LuaCheckbox(lua_State *L);
~LuaCheckbox();
};
55 changes: 2 additions & 53 deletions src/lua/LuaCompat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

#if LUA_VERSION_NUM >= 502
// Implement missing luaL_typerror function
int luaL_typerror (lua_State *L, int narg, const char *tname)
int luaL_typerror(lua_State *L, int narg, const char *tname)
{
const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
return luaL_argerror(L, narg, msg);
}

void luaL_register (lua_State *L,
const char *libname,
const luaL_Reg *l)
void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
{
if (libname)
{
Expand All @@ -20,61 +18,12 @@ void luaL_register (lua_State *L,
}
luaL_setfuncs(L, l, 0);
}

void tpt_lua_setmainthread(lua_State *L)
{
}

void tpt_lua_getmainthread(lua_State *L)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
}

#else

# ifndef lua_pushglobaltable // * Thank you moonjit
// Implement function added in lua 5.2 that we now use
void lua_pushglobaltable(lua_State *L)
{
lua_pushvalue(L, LUA_GLOBALSINDEX);
}
# endif

void tpt_lua_setmainthread(lua_State *L)
{
lua_pushthread(L);
lua_setfield(L, LUA_REGISTRYINDEX, "tpt_lua_mainthread");
}

void tpt_lua_getmainthread(lua_State *L)
{
lua_getfield(L, LUA_REGISTRYINDEX, "tpt_lua_mainthread");
}

#endif

// Useful helper function, mainly used for logging
int luaL_tostring(lua_State *L, int n)
{
luaL_checkany(L, n);
switch (lua_type(L, n))
{
case LUA_TNUMBER:
lua_tostring(L, n);
lua_pushvalue(L, n);
break;
case LUA_TSTRING:
lua_pushvalue(L, n);
break;
case LUA_TBOOLEAN:
lua_pushstring(L, (lua_toboolean(L, n) ? "true" : "false"));
break;
case LUA_TNIL:
lua_pushliteral(L, "nil");
break;
default:
lua_pushfstring(L, "%s: %p", luaL_typename(L, n), lua_topointer(L, n));
break;
}
return 1;
}

0 comments on commit 1cb8f03

Please sign in to comment.