Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code to python - starting with configuration #892

Merged
merged 15 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ IF (UNIX)
${Vega_Strike_SOURCE_DIR}/src/damage
${Vega_Strike_SOURCE_DIR}/src/resource
${Vega_Strike_SOURCE_DIR}/src/components
${Vega_Strike_SOURCE_DIR}/src/python/config
${Vega_Strike_BINARY_DIR}
${Vega_Strike_BINARY_DIR}/src
/usr/include/harfbuzz/
Expand All @@ -177,6 +178,7 @@ ELSE ()
${Vega_Strike_SOURCE_DIR}/src/damage
${Vega_Strike_SOURCE_DIR}/src/resource
${Vega_Strike_SOURCE_DIR}/src/components
${Vega_Strike_SOURCE_DIR}/src/python/config
${Vega_Strike_BINARY_DIR}
${Vega_Strike_BINARY_DIR}/src
)
Expand Down Expand Up @@ -374,6 +376,7 @@ IF (Python3_FOUND)
SET(TST_INCLUDES ${Python3_INCLUDE_DIRS})
SET(TST_LIBS ${Python3_LIBRARIES})
SET(HAVE_PYTHON 1)
add_compile_definitions(Python_SITELIB=${Python3_SITELIB})
ELSE (Python3_FOUND)
MESSAGE(FATAL_ERROR "Can't find python")
ENDIF (Python3_FOUND)
Expand Down Expand Up @@ -695,9 +698,9 @@ SET(LIBVS_LOGGING
)

SET(LIBCONFIG
src/abstract_config.cpp
src/configuration/configuration.cpp
src/configuration/game_config.cpp
src/configuration/graphics_config.cpp
)

SET(LIBDAMAGE
Expand All @@ -712,11 +715,14 @@ 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
)

SET(LIBPYTHON
src/python/config/python_utils.cpp
)

SET(LIBCOMPONENT
src/components/component.cpp

Expand Down Expand Up @@ -1036,22 +1042,19 @@ SET(LIBROOTGENERIC_SOURCES
src/atmospheric_fog_mesh.cpp
src/configxml.cpp
src/easydom.cpp
src/endianness.cpp
src/macosx_math.cpp
src/faction_generic.cpp
src/faction_util_generic.cpp
src/galaxy.cpp
src/galaxy_gen.cpp
src/galaxy_xml.cpp
src/galaxy_utils.cpp
src/hashtable.cpp
src/lin_time.cpp
src/load_mission.cpp
src/pk3.cpp
src/posh.cpp
src/savegame.cpp
src/system_factory.cpp
src/star_system_generic.cpp
src/star_system_xml.cpp
src/stardate.cpp
src/universe_globals.cpp
Expand Down Expand Up @@ -1132,7 +1135,25 @@ SET(LIBAUDIO_SOURCES
)


ADD_LIBRARY(vegastrike_python SHARED
${LIBPYTHON}
)

TARGET_COMPILE_OPTIONS(
vegastrike_python PRIVATE -fPIC
)

TARGET_LINK_LIBRARIES(
vegastrike_python
${Boost_LIBRARIES}
${Python3_LIBRARIES}
)

# Name must be witout the lib prefix
SET_TARGET_PROPERTIES(vegastrike_python PROPERTIES PREFIX "")

ADD_LIBRARY(vegastrike-engine_com
${LIBPYTHON}
${LIBVS_LOGGING}
${LIBCONFIG}
${LIBDAMAGE}
Expand Down Expand Up @@ -1723,13 +1744,14 @@ 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/configuration/tests/python_tests.cpp
src/exit_unit_tests.cpp
src/components/tests/energy_container_tests.cpp
src/components/tests/balancing_tests.cpp
)

ADD_LIBRARY(vegastrike-testing
${LIBPYTHON}
${LIBCONFIG}
${LIBDAMAGE}
${LIBRESOURCE}
Expand Down Expand Up @@ -1774,8 +1796,8 @@ IF (USE_GTEST)
)

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

INCLUDE(GoogleTest)
Expand Down
1 change: 0 additions & 1 deletion engine/objconv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ SET(MESHER_SOURCES
mesher/Modules/XMesh_to_BFXM.cpp
mesher/Modules/XMesh_to_Ogre.cpp
mesher/Modules/Wavefront_to_BFXM.cpp
${Vega_Strike_SOURCE_DIR}/src/hashtable.cpp
${Vega_Strike_SOURCE_DIR}/src/xml_support.cpp
)

Expand Down
16 changes: 0 additions & 16 deletions engine/src/abstract_config.cpp

This file was deleted.

39 changes: 0 additions & 39 deletions engine/src/abstract_config.h

This file was deleted.

1 change: 0 additions & 1 deletion engine/src/cmd/missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "configxml.h"
#include "images.h"
#include "collection.h"
#include "star_system_generic.h"
#include "role_bitmask.h"
#include "ai/order.h"
#include "faction_generic.h"
Expand Down
1 change: 0 additions & 1 deletion engine/src/cmd/unit_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ void UncheckUnit( class Unit*un );
#include "collection.h"
#include "script/flightgroup.h"
#include "faction_generic.h"
#include "star_system_generic.h"
#include "gfx/cockpit_generic.h"
#include "vsfilesystem.h"
#include "collide_map.h"
Expand Down
7 changes: 7 additions & 0 deletions engine/src/configuration/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@
#endif
#include <math.h>

#include <stdio.h>

using vega_config::GetGameConfig;

Configuration::Configuration() {
//logging.verbose_debug = GetGameConfig().GetBool("data.verbose_debug", false);
std::ifstream ifs("config.json", std::ifstream::in);
std::stringstream buffer;
buffer << ifs.rdbuf();
const std::string json_text = buffer.str();
graphics2_config = Graphics2Config(json_text);
}

/* Override the default value(provided by constructor) with the value from the user specified configuration file, if any.
Expand Down
3 changes: 3 additions & 0 deletions engine/src/configuration/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <memory>
#include <string>

#include "graphics_config.h"

namespace vega_config {
// Config Structs Declaration

Expand Down Expand Up @@ -449,6 +451,7 @@ class Configuration {
Configuration();
void OverrideDefaultsWithUserConfiguration();
vega_config::GeneralConfig general_config;
Graphics2Config graphics2_config;
vega_config::DataConfig data_config;
vega_config::AIConfig ai;
vega_config::AudioConfig audio_config;
Expand Down
50 changes: 50 additions & 0 deletions engine/src/configuration/graphics_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <string>
#include <boost/filesystem.hpp>

#include "graphics_config.h"
#include "json.h"

// Exposes the struct to python
// TODO: move this somewhere as the same library should have multiple
// definitions
// Can we spread a boost python module over several files?
/*BOOST_PYTHON_MODULE(vegastrike_python) {
boost::python::class_<Graphics2Config>("GraphicsConfig", boost::python::init<>())
.def_readwrite("screen", &Graphics2Config::screen)
.def_readwrite("resolution_x", &Graphics2Config::resolution_x)
.def_readwrite("resolution_y", &Graphics2Config::resolution_y);
}*/

// a temporary helper function until we move to Boost/JSON
// TODO: remove
static const int GetValue(
const std::string key,
const int default_value,
const json::jobject object) {
if(!object.has_key(key)) {
return default_value;
}

const std::string attribute = object.get(key);
const int value = std::stoi(attribute);
return value;
}

Graphics2Config::Graphics2Config(const std::string config) {
json::jobject json_root = json::jobject::parse(config);

if(!json_root.has_key("graphics")) {
return;
}

const std::string graphics_json = json_root.get("graphics");
json::jobject json_graphics = json::jobject::parse(graphics_json);

screen = GetValue("screen", 0, json_graphics);
resolution_x = GetValue("resolution_x", 2560, json_graphics);
resolution_y = GetValue("resolution_y", 1600, json_graphics);
}




Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* python_utils.cpp
* graphics_config.h
*
* Copyright (c) 2001-2002 Daniel Horn
* Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors
Expand All @@ -25,21 +25,19 @@

// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-

#include "python_utils.h"
#ifndef VEGA_STRIKE_ENGINE_PYTHON_CONFIG_GRAPHICS_CONFIG_H
#define VEGA_STRIKE_ENGINE_PYTHON_CONFIG_GRAPHICS_CONFIG_H

#include <boost/python.hpp>
#include <iostream>
// TODO: remove the other GraphicsConfig and rename this
struct Graphics2Config {
int screen{0};
int resolution_x{1920};
int resolution_y{1080};

using namespace boost::python;
Graphics2Config() = default;
Graphics2Config(const std::string config);
};

const std::string GetPythonPath() {
Py_Initialize();
wchar_t* w_path_ptr = Py_GetPath();
Py_Finalize();

std::wstring w_path_w( w_path_ptr );
std::string path( w_path_w.begin(), w_path_w.end() );
std::cout << "Python path: " << path << std::endl;

return path;
}

#endif // VEGA_STRIKE_ENGINE_PYTHON_CONFIG_GRAPHICS_CONFIG_H
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@
#include <boost/filesystem.hpp>
#include <iostream>

#include "python_utils.h"
#include "python/config/python_utils.h"

using namespace boost::python;

// This code shows how to call a python file from c++
// It is not portable

struct World
{
Expand Down Expand Up @@ -86,15 +84,6 @@ TEST(Python, Call_Function) {

Py_Initialize();

// Use the following code to figure out your original path, as the above Py_SetPath overrides it.
// Note that Py_GetPath must be called after Py_Initialize and Py_SetPath before.
/*wchar_t* w_path_ptr = Py_GetPath();
std::wstring w_path_w( w_path_ptr );
std::string path( w_path_w.begin(), w_path_w.end() );
std::cout << "Path: " << path << std::endl;*/

//PyObject* moduleString = PyUnicode_FromString((char*)"python_tests");
//PyObject* module = PyImport_Import(moduleString);
PyObject* module = PyImport_ImportModule("python_tests");
std::cout << "PyImport_ImportModule did not crash\n" << std::flush;
if(!module) {
Expand Down Expand Up @@ -140,4 +129,4 @@ TEST(Python, Call_Function) {

// Uncomment to see prints
//EXPECT_FALSE(true);
}
}
Loading
Loading