diff --git a/.gitignore b/.gitignore index e570ce593..19aa90253 100644 --- a/.gitignore +++ b/.gitignore @@ -111,8 +111,11 @@ src/vizdoom/zlib/CTestTestfile.cmake **/freedm.wad **_vizdoom.ini -# CLion & PyCharm -**/.idea - # MacOS .DS_Store + +# Visual Studio Code +.vscode + +# CLion & PyCharm +**/.idea diff --git a/docs/api/cpp/doomGame.md b/docs/api/cpp/doomGame.md index 96cc0192c..abef630a0 100644 --- a/docs/api/cpp/doomGame.md +++ b/docs/api/cpp/doomGame.md @@ -620,6 +620,8 @@ Overwriting does not involve resetting to default values. Thus only overlapping The method returns true if the whole configuration file was correctly read and applied, false if the file contained errors. +If the file relative path is given, it will be searched for in the following order: current directory, current directory + `/scenarios/`, ViZDoom's installation directory + `/scenarios/`. + See also: - [ConfigFile](./configurationFiles.md) diff --git a/src/lib/ViZDoomConfigLoader.cpp b/src/lib/ViZDoomConfigLoader.cpp index e5af15f3a..d8c755d25 100644 --- a/src/lib/ViZDoomConfigLoader.cpp +++ b/src/lib/ViZDoomConfigLoader.cpp @@ -395,14 +395,13 @@ namespace vizdoom { std::string sharedConfigPath = getThisSharedObjectPath() + "/scenarios/" + scenarioName; // Check if scenario exists in library's scenerios directory - if (fileExists(filePath)) this->filePath = filePath; - else if (fileExists(workingConfigPath)) this->filePath = workingConfigPath; - else if (fileExists(sharedConfigPath)) this->filePath = sharedConfigPath; + if (fileExistsAndCanBeRead(filePath)) this->filePath = filePath; + else if (fileExistsAndCanBeRead(workingConfigPath)) this->filePath = workingConfigPath; + else if (fileExistsAndCanBeRead(sharedConfigPath)) this->filePath = sharedConfigPath; else throw FileDoesNotExistException(filePath + " | " + workingConfigPath + " | " + sharedConfigPath); bool success = true; - std::ifstream file(filePath); - + std::ifstream file(this->filePath); std::string line; int lineNumber = 0; diff --git a/src/lib/ViZDoomPathHelpers.cpp b/src/lib/ViZDoomPathHelpers.cpp index fe15ea042..144ac274b 100644 --- a/src/lib/ViZDoomPathHelpers.cpp +++ b/src/lib/ViZDoomPathHelpers.cpp @@ -62,6 +62,17 @@ namespace vizdoom { //return exist; } + bool ifstreamGood(std::string filePath) { + std::ifstream file(filePath); + bool isGood = file.good(); + file.close(); + return isGood; + } + + bool fileExistsAndCanBeRead(std::string filePath){ + return fileExists(filePath) && ifstreamGood(filePath); + } + std::string relativePath(std::string relativePath, std::string basePath) { bfs::path outPath(basePath); outPath.remove_filename(); diff --git a/src/lib/ViZDoomPathHelpers.h b/src/lib/ViZDoomPathHelpers.h index 54c411834..3c83865b1 100644 --- a/src/lib/ViZDoomPathHelpers.h +++ b/src/lib/ViZDoomPathHelpers.h @@ -43,6 +43,8 @@ namespace vizdoom { bool fileExists(std::string filePath); + bool fileExistsAndCanBeRead(std::string filePath); + std::string relativePath(std::string relativePath, std::string basePath = ""); std::string checkFile(std::string filePath, std::string expectedExt = ""); diff --git a/src/lib_python/ViZDoomMethodsDocstrings.h b/src/lib_python/ViZDoomMethodsDocstrings.h index 0155a0ab6..47e2d645a 100644 --- a/src/lib_python/ViZDoomMethodsDocstrings.h +++ b/src/lib_python/ViZDoomMethodsDocstrings.h @@ -145,7 +145,9 @@ Default value: 0)DOCSTRING"; In case of multiple invocations, older configurations will be overwritten by the recent ones. Overwriting does not involve resetting to default values. Thus only overlapping parameters will be changed. The method returns True if the whole configuration file was correctly read and applied, -False if the file contained errors.)DOCSTRING"; +False if the file contained errors. + +If the file relative path is given, it will be searched for in the following order: current directory, current directory + `/scenarios/`, ViZDoom's installation directory + `/scenarios/`.)DOCSTRING"; const char *getMode = R"DOCSTRING(Returns current mode (`PLAYER`, `SPECTATOR`, `ASYNC_PLAYER`, `ASYNC_SPECTATOR`).)DOCSTRING"; diff --git a/src/lib_python/__init__.py.in b/src/lib_python/__init__.py.in index b092b7406..f351bed2b 100644 --- a/src/lib_python/__init__.py.in +++ b/src/lib_python/__init__.py.in @@ -14,6 +14,10 @@ from .vizdoom import * import os as _os +root_path = __path__[0] +exe_path_windows = _os.path.join(__path__[0], "vizdoom.exe") +exe_path_unix = _os.path.join(__path__[0], "vizdoom") +exe_path = exe_path_windows if _os.path.exists(exe_path_windows) else exe_path_unix scenarios_path = _os.path.join(__path__[0], "scenarios") wads = [wad for wad in sorted(_os.listdir(scenarios_path)) if wad.endswith(".wad")] configs = [cfg for cfg in sorted(_os.listdir(scenarios_path)) if cfg.endswith(".cfg")] diff --git a/src/vizdoom/src/d_iwad.cpp b/src/vizdoom/src/d_iwad.cpp index 59466d846..d723cd8a5 100644 --- a/src/vizdoom/src/d_iwad.cpp +++ b/src/vizdoom/src/d_iwad.cpp @@ -556,7 +556,7 @@ int FIWadManager::IdentifyVersion (TArray &wadfiles, const char *iwad, if (pickwad < 0) exit (0); - // zdoom.pk3 must always be the first file loaded and the IWAD second. + // vizdoom.pk3 must always be the first file loaded and the IWAD second. wadfiles.Clear(); D_AddFile (wadfiles, zdoom_wad); diff --git a/tests/test_configs.py b/tests/test_configs.py new file mode 100755 index 000000000..df03ba669 --- /dev/null +++ b/tests/test_configs.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +# Tests for ViZDoom enums and related methods. +# This test can be run as Python script or via PyTest + +import os + +import vizdoom as vzd + + +def test_load_config(): + for file in os.listdir(vzd.scenarios_path): + if file.endswith(".cfg"): + vzd.DoomGame().load_config(os.path.join(vzd.scenarios_path, file)) + vzd.DoomGame().load_config(file) + + +if __name__ == "__main__": + test_load_config()