Skip to content

Commit

Permalink
Eliminate polymorphism from the CommandInterface hierarchy
Browse files Browse the repository at this point in the history
This is similar to what I did to Gravity in 9068920. The idea is that we can choose between the implementations at compile time.
  • Loading branch information
LBPHacker committed Jan 24, 2024
1 parent d87130b commit 7a10847
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 209 deletions.
6 changes: 3 additions & 3 deletions src/gui/game/GameController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ GameController::GameController():

gameView->SetDebugHUD(GlobalPrefs::Ref().Get("Renderer.DebugMode", false));

CommandInterface::Create(this, gameModel);
commandInterface = CommandInterface::Create(this, gameModel);

Client::Ref().AddListener(this);

Expand Down Expand Up @@ -146,7 +146,7 @@ GameController::~GameController()
{
delete *iter;
}
delete commandInterface;
commandInterface.reset();
delete gameModel;
if (gameView->CloseActiveWindow())
{
Expand Down Expand Up @@ -1386,7 +1386,7 @@ void GameController::OpenOptions()
void GameController::ShowConsole()
{
if (!console)
console = new ConsoleController(NULL, commandInterface);
console = new ConsoleController(NULL, commandInterface.get());
if (console->GetView() != ui::Engine::Ref().GetWindow())
ui::Engine::Ref().ShowWindow(console->GetView());
}
Expand Down
4 changes: 3 additions & 1 deletion src/gui/game/GameController.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "lua/CommandInterfacePtr.h"
#include "client/ClientListener.h"
#include "client/StartupInfo.h"
#include "gui/interface/Point.h"
Expand Down Expand Up @@ -28,7 +29,6 @@ class LocalBrowserController;
class SearchController;
class PreviewController;
class RenderController;
class CommandInterface;
class VideoBuffer;
class Tool;
class Menu;
Expand All @@ -39,6 +39,8 @@ class TagsController;
class ConsoleController;
class GameController: public ClientListener
{
CommandInterfacePtr commandInterface;

private:
bool firstTick;
int foundSignID;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/game/GameModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,13 +1712,13 @@ void GameModel::BeforeSim()
{
if (!sim->sys_pause || sim->framerender)
{
commandInterface->HandleEvent(BeforeSimEvent{});
CommandInterface::Ref().HandleEvent(BeforeSimEvent{});
}
sim->BeforeSim();
}

void GameModel::AfterSim()
{
sim->AfterSim();
commandInterface->HandleEvent(AfterSimEvent{});
CommandInterface::Ref().HandleEvent(AfterSimEvent{});
}
13 changes: 2 additions & 11 deletions src/lua/CommandInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@
#include <cstring>
#include <deque>

CommandInterface *commandInterface = nullptr;

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

CommandInterface::~CommandInterface()
{
commandInterface = nullptr;
}

void CommandInterface::Log(LogType type, String message)
{
m->Log(message, type == LogError || type == LogNotice);
Expand Down Expand Up @@ -80,7 +71,7 @@ String CommandInterface::GetLastError()
return lastError;
}

int CommandInterface::Command(String command)
int CommandInterface::PlainCommand(String command)
{
lastError = "";
std::deque<String> words;
Expand Down Expand Up @@ -273,7 +264,7 @@ AnyType CommandInterface::eval(std::deque<String> * words)
return StringType(word);
}

String CommandInterface::FormatCommand(String command)
String CommandInterface::PlainFormatCommand(String command)
{
std::deque<String> words;
std::deque<AnyType> commandWords;
Expand Down
26 changes: 15 additions & 11 deletions src/lua/CommandInterface.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include "CommandInterfacePtr.h"
#include "common/ExplicitSingleton.h"
#include "common/String.h"
#include "gui/game/GameControllerEvents.h"
#include "TPTSTypes.h"
Expand All @@ -8,34 +10,38 @@ class GameModel;
class GameController;
class Tool;

class CommandInterface
class CommandInterface : public ExplicitSingleton<CommandInterface>
{
protected:
String lastError;
GameModel * m;
GameController * c;
CommandInterface(GameController *newGameController, GameModel *newGameModel);


int PlainCommand(String command);
String PlainFormatCommand(String command);

public:
CommandInterface(GameController *newGameController, GameModel *newGameModel);

enum LogType { LogError, LogWarning, LogNotice };
enum FormatType { FormatInt, FormatString, FormatChar, FormatFloat, FormatElement };
int GetPropertyOffset(ByteString key, FormatType & format);
void Log(LogType type, String message);
//void AttachGameModel(GameModel * m);

virtual void OnTick() { }
virtual void Init() { }
void OnTick();
void Init();

virtual bool HandleEvent(const GameControllerEvent &event) { return true; }
bool HandleEvent(const GameControllerEvent &event);

virtual int Command(String command);
virtual String FormatCommand(String command);
int Command(String command);
String FormatCommand(String command);
void SetLastError(String err)
{
lastError = err;
}
String GetLastError();
virtual ~CommandInterface();

AnyType eval(std::deque<String> * words);
int parseNumber(String str);
Expand All @@ -48,7 +54,5 @@ class CommandInterface
AnyType tptS_quit(std::deque<String> * words);
ValueType testType(String word);

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

extern CommandInterface *commandInterface;
9 changes: 9 additions & 0 deletions src/lua/CommandInterfacePtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <memory>

class CommandInterface;
struct CommandInterfaceDeleter
{
void operator ()(CommandInterface *ptr) const;
};
using CommandInterfacePtr = std::unique_ptr<CommandInterface, CommandInterfaceDeleter>;
2 changes: 1 addition & 1 deletion src/lua/LuaComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LuaComponent::LuaComponent(lua_State *L) : component(nullptr), owner_ref(LUA_REF
{
this->L = L; // I don't get how this doesn't cause crashes later on

ci = static_cast<LuaScriptInterface *>(commandInterface);
ci = static_cast<LuaScriptInterface *>(&CommandInterface::Ref());
}

int LuaComponent::position(lua_State *L)
Expand Down
22 changes: 11 additions & 11 deletions src/lua/LuaElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static int luaUpdateWrapper(UPDATE_FUNC_ARGS)
{
return 0;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &builtinElements = GetElements();
auto *builtinUpdate = builtinElements[parts[i].type].Update;
auto &customElements = lsi->customElements;
Expand Down Expand Up @@ -152,7 +152,7 @@ static int luaGraphicsWrapper(GRAPHICS_FUNC_ARGS)
{
return Element::defaultGraphics(GRAPHICS_FUNC_SUBCALL_ARGS);
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
auto *sim = lsi->sim;
if (customElements[cpart->type].graphics)
Expand Down Expand Up @@ -215,7 +215,7 @@ static void luaCreateWrapper(ELEMENT_CREATE_FUNC_ARGS)
{
return;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
if (customElements[sim->parts[i].type].create)
{
Expand All @@ -242,7 +242,7 @@ static bool luaCreateAllowedWrapper(ELEMENT_CREATE_ALLOWED_FUNC_ARGS)
// instances of something that should be limited to one instance.
return 1;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
bool ret = false;
if (customElements[t].createAllowed)
Expand Down Expand Up @@ -273,7 +273,7 @@ static void luaChangeTypeWrapper(ELEMENT_CHANGETYPE_FUNC_ARGS)
{
return;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
if (customElements[sim->parts[i].type].changeType)
{
Expand All @@ -297,7 +297,7 @@ static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)
{
return false;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
bool ret = false;
if (customElements[sim->parts[i].type].ctypeDraw)
Expand All @@ -323,7 +323,7 @@ static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)

static int allocate(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TSTRING);
auto group = tpt_lua_toByteString(L, 1).ToUpper();
Expand Down Expand Up @@ -410,7 +410,7 @@ static int allocate(lua_State *L)
static int element(lua_State *L)
{
auto &builtinElements = GetElements();
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
int id = luaL_checkinteger(L, 1);
if (!SimulationData::CRef().IsElementOrNone(id))
Expand Down Expand Up @@ -556,7 +556,7 @@ static int element(lua_State *L)
static int property(lua_State *L)
{
auto &builtinElements = GetElements();
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
int id = luaL_checkinteger(L, 1);
if (!SimulationData::CRef().IsElementOrNone(id))
Expand Down Expand Up @@ -750,7 +750,7 @@ static int ffree(lua_State *L)
std::unique_lock lk(sd.elementGraphicsMx);
sd.elements[id].Enabled = false;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameModel->BuildMenus();

lua_getglobal(L, "elements");
Expand All @@ -775,7 +775,7 @@ static int loadDefault(lua_State *L)
std::unique_lock lk(sd.elementGraphicsMx);
auto &elements = sd.elements;
auto &builtinElements = GetElements();
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
{
auto loadDefaultOne = [L, &elements, &builtinElements](int id) {
lua_getglobal(L, "elements");
Expand Down
4 changes: 2 additions & 2 deletions src/lua/LuaEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

static int fregister(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int eventType = luaL_checkinteger(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
if (eventType < 0 || eventType >= int(lsi->gameControllerEventHandlers.size()))
Expand All @@ -21,7 +21,7 @@ static int fregister(lua_State *L)

static int unregister(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int eventType = luaL_checkinteger(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
if (eventType < 0 || eventType >= int(lsi->gameControllerEventHandlers.size()))
Expand Down
6 changes: 3 additions & 3 deletions src/lua/LuaGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static int32_t int32Truncate(double n)

static std::variant<Graphics *, Renderer *> currentGraphics()
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->eventTraits & eventTraitSimGraphics)
{
return lsi->ren;
Expand Down Expand Up @@ -71,7 +71,7 @@ static int drawPixel(lua_State *L)
else if (b > 255) b = 255;
if (a < 0 ) a = 0 ;
else if (a > 255) a = 255;
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->g->BlendPixel({ x, y }, RGBA<uint8_t>(r, g, b, a));
return 0;
}
Expand Down Expand Up @@ -259,7 +259,7 @@ static int getHexColor(lua_State *L)

static int setClipRect(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->eventTraits & eventTraitSimGraphics)
{
return luaL_error(L, "simulation graphics do not support clip rects");
Expand Down

0 comments on commit 7a10847

Please sign in to comment.