Skip to content

Commit

Permalink
Refactor python code (#873)
Browse files Browse the repository at this point in the history
* Refactor python code

Replace project specific macros with original boost python ones.
Add python unit test - demonstrates how to properly call a python module from C++
Delete unused files - mmorpg
Add missing imports
Replace include command.h with proper header file for that cpp file (rendertext)
  • Loading branch information
royfalk authored Oct 2, 2024
1 parent 307d2c4 commit 50f1678
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 512 deletions.
9 changes: 9 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ SET(LIBRESOURCE
src/resource/product.cpp
src/resource/cargo.cpp
src/resource/manifest.cpp
src/resource/python_utils.cpp
src/resource/random_utils.cpp
src/cmd/json.cpp
)
Expand Down Expand Up @@ -1720,6 +1721,7 @@ IF (USE_GTEST)
src/resource/tests/resource_test.cpp
src/resource/tests/manifest_tests.cpp
src/resource/tests/random_tests.cpp
src/resource/tests/python_tests.cpp
src/exit_unit_tests.cpp
src/components/tests/energy_container_tests.cpp
src/components/tests/balancing_tests.cpp
Expand All @@ -1744,6 +1746,8 @@ IF (USE_GTEST)

TARGET_LINK_LIBRARIES(
${TEST_NAME}
${Boost_LIBRARIES}
${Python3_LIBRARIES}
gtest_main
vegastrike-testing
Boost::log
Expand All @@ -1767,6 +1771,11 @@ IF (USE_GTEST)
DESTINATION ${CMAKE_BINARY_DIR}/test_assets
)

FILE(
COPY "src/components/tests/python_tests.py"
DESTINATION ${CMAKE_BINARY_DIR}
)

INCLUDE(GoogleTest)
gtest_discover_tests(${TEST_NAME})
ENDIF (USE_GTEST)
100 changes: 50 additions & 50 deletions engine/src/cmd/base_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,60 +82,60 @@ static boost::python::tuple GetRandomBarMessage() {
}
}

PYTHON_BEGIN_MODULE(Base)
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Room, "Room");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetCurRoom, "SetCurRoom");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::GetCurRoom, "GetCurRoom");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::GetNumRoom, "GetNumRoom");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::GetNumRoom, "HasObject");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Comp, "Comp");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::CompPython, "CompPython");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Launch, "Launch");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::LaunchPython, "LaunchPython");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Link, "Link");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::LinkPython, "LinkPython");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Python, "Python");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::EraseLink, "EraseLink");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Ship, "Ship");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Texture, "Texture");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Video, "Video");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::VideoStream, "VideoStream");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::PlayVideo, "PlayVideo");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::StopVideo, "StopVideo");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetVideoCallback, "SetVideoCallback");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetDJEnabled, "SetDJEnabled");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetTexture, "SetTexture");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetTextureSize, "SetTextureSize");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetTexturePos, "SetTexturePos");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::EnqueueMessageToRoom, "EnqueueMessageToRoom");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::MessageToRoom, "MessageToRoom");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::EnqueueMessage, "EnqueueMessage");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::Message, "Message");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::EraseObj, "EraseObj");
PYTHON_DEFINE_GLOBAL(Base, &::GetRandomBarMessage, "GetRandomBarMessage");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::RunScript, "RunScript");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::TextBox, "TextBox");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetTextBoxText, "SetTextBoxText");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::GlobalKeyPython, "GlobalKeyPython");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetLinkArea, "SetLinkArea");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetLinkText, "SetLinkText");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetLinkPython, "SetLinkPython");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetLinkRoom, "SetLinkRoom");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetLinkEventMask, "SetLinkEventMask");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::BuyShip, "BuyShip");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SellShip, "SellShip");
BOOST_PYTHON_MODULE(Base) {
boost::python::def("Room", &BaseUtil::Room);
boost::python::def("SetCurRoom", &BaseUtil::SetCurRoom);
boost::python::def("GetCurRoom", &BaseUtil::GetCurRoom);
boost::python::def("GetNumRoom", &BaseUtil::GetNumRoom);
boost::python::def("HasObject", &BaseUtil::GetNumRoom);
boost::python::def("Comp", &BaseUtil::Comp);
boost::python::def("CompPython", &BaseUtil::CompPython);
boost::python::def("Launch", &BaseUtil::Launch);
boost::python::def("LaunchPython", &BaseUtil::LaunchPython);
boost::python::def("Link", &BaseUtil::Link);
boost::python::def("LinkPython", &BaseUtil::LinkPython);
boost::python::def("Python", &BaseUtil::Python);
boost::python::def("EraseLink", &BaseUtil::EraseLink);
boost::python::def("Ship", &BaseUtil::Ship);
boost::python::def("Texture", &BaseUtil::Texture);
boost::python::def("Video", &BaseUtil::Video);
boost::python::def("VideoStream", &BaseUtil::VideoStream);
boost::python::def("PlayVideo", &BaseUtil::PlayVideo);
boost::python::def("StopVideo", &BaseUtil::StopVideo);
boost::python::def("SetVideoCallback", &BaseUtil::SetVideoCallback);
boost::python::def("SetDJEnabled", &BaseUtil::SetDJEnabled);
boost::python::def("SetTexture", &BaseUtil::SetTexture);
boost::python::def("SetTextureSize", &BaseUtil::SetTextureSize);
boost::python::def("SetTexturePos", &BaseUtil::SetTexturePos);
boost::python::def("EnqueueMessageToRoom", &BaseUtil::EnqueueMessageToRoom);
boost::python::def("MessageToRoom", &BaseUtil::MessageToRoom);
boost::python::def("EnqueueMessage", &BaseUtil::EnqueueMessage);
boost::python::def("Message", &BaseUtil::Message);
boost::python::def("EraseObj", &BaseUtil::EraseObj);
boost::python::def("GetRandomBarMessage", &::GetRandomBarMessage);
boost::python::def("RunScript", &BaseUtil::RunScript);
boost::python::def("TextBox", &BaseUtil::TextBox);
boost::python::def("SetTextBoxText", &BaseUtil::SetTextBoxText);
boost::python::def("GlobalKeyPython", &BaseUtil::GlobalKeyPython);
boost::python::def("SetLinkArea", &BaseUtil::SetLinkArea);
boost::python::def("SetLinkText", &BaseUtil::SetLinkText);
boost::python::def("SetLinkPython", &BaseUtil::SetLinkPython);
boost::python::def("SetLinkRoom", &BaseUtil::SetLinkRoom);
boost::python::def("SetLinkEventMask", &BaseUtil::SetLinkEventMask);
boost::python::def("BuyShip", &BaseUtil::BuyShip);
boost::python::def("SellShip", &BaseUtil::SellShip);

PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetEventData, "SetEventData");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::SetMouseEventData, "SetMouseEventData");
PYTHON_DEFINE_GLOBAL(Base, &::GetEventDataPython, "GetEventData");
boost::python::def("SetEventData", &BaseUtil::SetEventData);
boost::python::def("SetMouseEventData", &BaseUtil::SetMouseEventData);
boost::python::def("GetEventData", &::GetEventDataPython);

PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::GetTextWidth, "GetTextWidth");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::GetTextHeight, "GetTextHeight");
boost::python::def("GetTextWidth", &BaseUtil::GetTextWidth);
boost::python::def("GetTextHeight", &BaseUtil::GetTextHeight);

PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::LoadBaseInterface, "LoadBaseInterface");
PYTHON_DEFINE_GLOBAL(Base, &BaseUtil::ExitGame, "ExitGame");
boost::python::def("LoadBaseInterface", &BaseUtil::LoadBaseInterface);
boost::python::def("ExitGame", &BaseUtil::ExitGame);

PYTHON_END_MODULE(Base)
}

void InitBase() {
PyImport_AppendInittab("Base", PYTHON_MODULE_INIT_FUNCTION(Base));
Expand Down
34 changes: 17 additions & 17 deletions engine/src/cmd/script/director_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ static bool isUtf8SaveGamePy(string savegame) {
return isUtf8SaveGame(savegame);
}

PYTHON_BEGIN_MODULE(Director)
BOOST_PYTHON_MODULE(Director) {
PYTHON_BEGIN_INHERIT_CLASS(Director, pythonMission, PythonMissionBaseClass, "Mission")
PYTHON_DEFINE_METHOD_DEFAULT(Class, &PythonMissionBaseClass::Pickle, "Pickle", pythonMission::default_Pickle);
PYTHON_DEFINE_METHOD_DEFAULT(Class,
Expand All @@ -341,22 +341,22 @@ PYTHON_BEGIN_MODULE(Director)
"Execute",
pythonMission::default_Execute);
PYTHON_END_CLASS(Director, pythonMission)
PYTHON_DEFINE_GLOBAL(Director, &putSaveDataPy, "putSaveData");
PYTHON_DEFINE_GLOBAL(Director, &pushSaveDataPy, "pushSaveData");
PYTHON_DEFINE_GLOBAL(Director, &eraseSaveDataPy, "eraseSaveData");
PYTHON_DEFINE_GLOBAL(Director, &clearSaveDataPy, "clearSaveData");
PYTHON_DEFINE_GLOBAL(Director, &getSaveDataPy, "getSaveData");
PYTHON_DEFINE_GLOBAL(Director, &getSaveDataLengthPy, "getSaveDataLength");
PYTHON_DEFINE_GLOBAL(Director, &putSaveStringPy, "putSaveString");
PYTHON_DEFINE_GLOBAL(Director, &pushSaveStringPy, "pushSaveString");
PYTHON_DEFINE_GLOBAL(Director, &getSaveStringPy, "getSaveString");
PYTHON_DEFINE_GLOBAL(Director, &getSaveStringLengthPy, "getSaveStringLength");
PYTHON_DEFINE_GLOBAL(Director, &eraseSaveStringPy, "eraseSaveString");
PYTHON_DEFINE_GLOBAL(Director, &clearSaveStringPy, "clearSaveString");
PYTHON_DEFINE_GLOBAL(Director, &loadStringListPy, "loadStringList");
PYTHON_DEFINE_GLOBAL(Director, &saveStringListPy, "saveStringList");
PYTHON_DEFINE_GLOBAL(Director, &isUtf8SaveGamePy, "isUtf8SaveGame");
PYTHON_END_MODULE(Director)
boost::python::def("putSaveData", &putSaveDataPy);
boost::python::def("pushSaveData", &pushSaveDataPy);
boost::python::def("eraseSaveData", &eraseSaveDataPy);
boost::python::def("clearSaveData", &clearSaveDataPy);
boost::python::def("getSaveData", &getSaveDataPy);
boost::python::def("getSaveDataLength", &getSaveDataLengthPy);
boost::python::def("putSaveString", &putSaveStringPy);
boost::python::def("pushSaveString", &pushSaveStringPy);
boost::python::def("getSaveString", &getSaveStringPy);
boost::python::def("getSaveStringLength", &getSaveStringLengthPy);
boost::python::def("eraseSaveString", &eraseSaveStringPy);
boost::python::def("clearSaveString", &clearSaveStringPy);
boost::python::def("loadStringList", &loadStringListPy);
boost::python::def("saveStringList", &saveStringListPy);
boost::python::def("isUtf8SaveGame", &isUtf8SaveGamePy);
}

void InitDirector() {
PyImport_AppendInittab("Director", PYTHON_MODULE_INIT_FUNCTION(Director));
Expand Down
8 changes: 4 additions & 4 deletions engine/src/cmd/script/director_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
#include "cmd/unit_generic.h"
#include "mission.h"

PYTHON_BEGIN_MODULE(Base)
BOOST_PYTHON_MODULE(Base) {
//Nothing here, but keeps those files that do a "import Base" happy.
PYTHON_END_MODULE(Base)
}

PYTHON_BEGIN_MODULE(Briefing)
BOOST_PYTHON_MODULE(Briefing) {
//Nothing here, but keeps those files that do a "import Briefing" happy.
PYTHON_END_MODULE(Briefing)
}

void InitBase() {
PyImport_AppendInittab("Base", PYTHON_MODULE_INIT_FUNCTION(Base));
Expand Down
4 changes: 4 additions & 0 deletions engine/src/components/tests/python_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


def append(first, second):
return first+second
2 changes: 2 additions & 0 deletions engine/src/gfx/halo_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#define VEGA_STRIKE_ENGINE_GFX_HALOSYSTEM_H

#include <vector>
#include <string>

#include "matrix.h"
class Mesh;
struct GFXColor;
Expand Down
Loading

0 comments on commit 50f1678

Please sign in to comment.