diff options
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | editor/editor.cpp | 16 | ||||
| -rw-r--r-- | editor/main.cpp | 2 | ||||
| -rw-r--r-- | runtime/main.cpp | 2 | ||||
| -rw-r--r-- | src/arg-handler.cpp | 42 | ||||
| -rw-r--r-- | src/arg-handler.hpp | 6 | ||||
| -rw-r--r-- | src/game-settings.hpp | 1 | ||||
| -rw-r--r-- | src/game.cpp | 3 |
8 files changed, 70 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bd5b237..4ede1a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -pthread") set(PKE_SOURCE_FILES src/macros.hpp + src/arg-handler.hpp + src/arg-handler.cpp src/array.hpp src/camera.hpp src/camera.cpp diff --git a/editor/editor.cpp b/editor/editor.cpp index 53d8883..4170ac9 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -105,16 +105,24 @@ void PkeEditor_Tick(double delta) { PkeThreads_Enqueue(threadPoolHandle, std::packaged_task<void()>( [] { auto pid = fork(); if (pid == 0) { + char pluginOpt[128]; + pluginOpt[127] = '\n'; + sprintf(pluginOpt, "--plugin %s", pkeSettings.pluginPath == nullptr ? "example.o" : pkeSettings.pluginPath); + int status = -1; const char *argv[] = { + pluginOpt, NULL, }; - int status = execvp("pke_runtime", const_cast<char **>(argv)); - fprintf(stderr, "pke_runtime exited with a status of %i\n", status); + status = execvp("pke_runtime", const_cast<char **>(argv)); + fprintf(stderr, "pke_runtime (child) exited with a status of %i\n", status); + subProgramRunning = false; + } else if (pid == -1) { + fprintf(stdout, "pke_runtime failed to fork\n"); subProgramRunning = false; } else { - int status; + int status = 0xCAFEBABE; waitpid(pid, &status, 0); - fprintf(stdout, "pke_runtime exited cleanly with a status of %i\n", status); + fprintf(stdout, "pke_runtime (parent) exited with a status of %i (0x%08X)\n", status, status); subProgramRunning = false; } })); diff --git a/editor/main.cpp b/editor/main.cpp index a431183..af0e66a 100644 --- a/editor/main.cpp +++ b/editor/main.cpp @@ -1,5 +1,6 @@ #include <csignal> +#include "arg-handler.hpp" #include "plugins.hpp" #include "editor.hpp" #include "event.hpp" @@ -28,6 +29,7 @@ int main(int argc, char *argv[]) { pkePlugin.OnImGuiRender = PkeEditor_RecordImGui; } // run + PkeArgs_Parse(argc, argv); Game_Main({}, argv[0]); fprintf(stdout, "PKE_EDITOR EXITING\n"); return 0; diff --git a/runtime/main.cpp b/runtime/main.cpp index fb636a6..36f50d3 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -1,6 +1,7 @@ #include <csignal> +#include "arg-handler.hpp" #include "plugins.hpp" #include "event.hpp" #include "game.hpp" @@ -15,6 +16,7 @@ void signal_handler(int signal_num) { int main(int argc, char *argv[]) { signal(SIGTERM, signal_handler); fprintf(stdout, "PKE_EDITOR ENTERING\n"); + PkeArgs_Parse(argc, argv); Game_Main({}, argv[0]); fprintf(stdout, "PKE_EDITOR EXITING\n"); return 0; diff --git a/src/arg-handler.cpp b/src/arg-handler.cpp new file mode 100644 index 0000000..1b40039 --- /dev/null +++ b/src/arg-handler.cpp @@ -0,0 +1,42 @@ + +#include "arg-handler.hpp" +#include "game-settings.hpp" + +#include <cstdio> +#include <getopt.h> + +void PkeArgs_Parse(int argc, char *argv[]) { + while (1) { + + static struct option long_options[] = { + {"plugin", required_argument, 0, 'p'}, + {0, 0, 0, 0}, + }; + + int optionIndex = 0; + int c = getopt_long(argc, argv, "p:", long_options, &optionIndex); + if (c == -1) { + break; + } + + switch (c) { + case 0: + break; + case 'p': + pkeSettings.pluginPath = optarg; + break; + default: + fprintf(stderr, "Unused parameter: %c\n", c); + } + } + +#ifndef NDEBUG + if (optind < argc) { + fprintf(stdout, "non-option args:\n"); + while (optind < argc) { + fprintf(stdout, "%s ", argv[optind++]); + } + fprintf(stdout, "\n"); + } +#endif +} diff --git a/src/arg-handler.hpp b/src/arg-handler.hpp new file mode 100644 index 0000000..5471f01 --- /dev/null +++ b/src/arg-handler.hpp @@ -0,0 +1,6 @@ +#ifndef PKE_ARG_HANDLER_HPP +#define PKE_ARG_HANDLER_HPP + +void PkeArgs_Parse(int argc, char *argv[]); + +#endif /* PKE_ARG_HANDLER_HPP */ diff --git a/src/game-settings.hpp b/src/game-settings.hpp index 7e1d31d..7bc6765 100644 --- a/src/game-settings.hpp +++ b/src/game-settings.hpp @@ -8,6 +8,7 @@ struct GameSettings { const char *executablePath; + const char *pluginPath = nullptr; bool isGameRunning = true; bool isGamePaused = false; bool isShowingEditor = true; diff --git a/src/game.cpp b/src/game.cpp index 4dde008..7dfca42 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -481,6 +481,9 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { CreateWindow(windowProps); PkeInput_Init(); EntityType_Init(); + if (pkeSettings.pluginPath) { + Pke_LoadPlugin(pkeSettings.pluginPath); + } if (pkePlugin.OnInit) { pkePlugin.OnInit(); } |
