diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 8e3fb92de5..06b3278fbd 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -1784,22 +1784,17 @@ IF (USE_GTEST) ENDIF() FILE( - COPY "src/cmd/tests/units.json" - DESTINATION ${CMAKE_BINARY_DIR}/test_assets - ) - - FILE( - COPY "src/configuration/tests/python_tests.py" + COPY "src/configuration/tests/vegastrike.config" DESTINATION ${CMAKE_BINARY_DIR}/test_assets ) FILE( - COPY "src/configuration/tests/config.py" + COPY "src/cmd/tests/units.json" DESTINATION ${CMAKE_BINARY_DIR}/test_assets ) FILE( - COPY "src/configuration/tests/config.yaml" + COPY "src/configuration/tests/python_tests.py" DESTINATION ${CMAKE_BINARY_DIR}/test_assets ) diff --git a/engine/src/configuration/configuration.cpp b/engine/src/configuration/configuration.cpp index 4120fb6126..29aac0c2ad 100644 --- a/engine/src/configuration/configuration.cpp +++ b/engine/src/configuration/configuration.cpp @@ -30,16 +30,17 @@ #endif #include -#include "vsfilesystem.h" +#include using vega_config::GetGameConfig; Configuration::Configuration() { //logging.verbose_debug = GetGameConfig().GetBool("data.verbose_debug", false); - graphics2_config = GetGraphics2Config( - VSFileSystem::programdir, - VSFileSystem::datadir, - "config", "get_config"); + 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. diff --git a/engine/src/configuration/tests/config.py b/engine/src/configuration/tests/config.py deleted file mode 100644 index 34385b7e30..0000000000 --- a/engine/src/configuration/tests/config.py +++ /dev/null @@ -1,24 +0,0 @@ -import yaml -import vegastrike_python - - -def get_config(): - with open('config.yaml', 'r') as file: - yaml_file = yaml.safe_load(file) - print(yaml_file) - - graphics = vegastrike_python.GraphicsConfig() - graphics.screen = yaml_file['graphics']['screen'] - graphics.resolution_x = yaml_file['graphics']['resolution_x'] - graphics.resolution_y = yaml_file['graphics']['resolution_y'] - - - return graphics - - -#gfx = get_config() -#print('screen: ', gfx.screen) -#print('x: ', gfx.resolution_x) -#print('y: ', gfx.resolution_y) - - diff --git a/engine/src/configuration/tests/config.yaml b/engine/src/configuration/tests/config.yaml deleted file mode 100644 index 819b1e7e12..0000000000 --- a/engine/src/configuration/tests/config.yaml +++ /dev/null @@ -1,8 +0,0 @@ -general: - -graphics: - resolution_x: 2560 - resolution_y: 1600 - screen: 0 - -advanced: diff --git a/engine/src/configuration/tests/python_tests.cpp b/engine/src/configuration/tests/python_tests.cpp index ca161b0722..b508cb2ca9 100644 --- a/engine/src/configuration/tests/python_tests.cpp +++ b/engine/src/configuration/tests/python_tests.cpp @@ -131,26 +131,3 @@ TEST(Python, Call_Function) { // Uncomment to see prints //EXPECT_FALSE(true); } - -// Test ability to get config file from python -TEST(Python, Config) { - boost::filesystem::path test_path(boost::filesystem::current_path()); - boost::filesystem::path lib_path = test_path.parent_path(); - std::cout << "test_path: " << test_path << std::endl; - std::cout << "lib_path: " << lib_path << std::endl; - - std::cout << "Try to run GetClassFromPython." << std::endl; - PyObject* object = GetClassFromPython( - lib_path.string(), - test_path.string(), - "config", "get_config"); - std::cout << "Ran GetClassFromPython successfully. Got " << object << std::endl; - - Graphics2Config& cfg2 = extract(object); - EXPECT_EQ(cfg2.screen, 0); - EXPECT_EQ(cfg2.resolution_x, 2560); - EXPECT_EQ(cfg2.resolution_y, 1600); - - // Uncomment to see prints*/ - //EXPECT_FALSE(true); -} diff --git a/engine/src/python/config/graphics_config.cpp b/engine/src/python/config/graphics_config.cpp index 84076135c5..d165a243dd 100644 --- a/engine/src/python/config/graphics_config.cpp +++ b/engine/src/python/config/graphics_config.cpp @@ -1,30 +1,50 @@ #include -#include #include #include "graphics_config.h" -#include "python_utils.h" +#include "json.h" - - -BOOST_PYTHON_MODULE(vegastrike_python) { +// 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_("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(config); + + screen = GetValue("screen", 0, json_graphics); + resolution_x = GetValue("resolution_x", 2560, json_graphics); + resolution_y = GetValue("resolution_y", 1600, json_graphics); } -Graphics2Config GetGraphics2Config( - const std::string build_path, - const std::string path_string, - const std::string file_name, - const std::string function_name) { - PyObject* object = GetClassFromPython( - build_path, path_string, file_name, function_name); - - Graphics2Config cfg2 = boost::python::extract(object); - return cfg2; -} \ No newline at end of file diff --git a/engine/src/python/config/graphics_config.h b/engine/src/python/config/graphics_config.h index 2b0b818662..f0df85d55c 100644 --- a/engine/src/python/config/graphics_config.h +++ b/engine/src/python/config/graphics_config.h @@ -35,13 +35,9 @@ struct Graphics2Config { int resolution_y{1080}; Graphics2Config() = default; + Graphics2Config(const std::string config); }; -Graphics2Config GetGraphics2Config( - const std::string build_path, - const std::string path_string, - const std::string file_name, - const std::string function_name -); + #endif // VEGA_STRIKE_ENGINE_PYTHON_CONFIG_GRAPHICS_CONFIG_H \ No newline at end of file