diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-02 20:46:41 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-02 20:46:41 -0400 |
| commit | a02c7589c6c9e902c59a632aa650635336fe648c (patch) | |
| tree | 8ec4800ff9f9effbd9141e02131123448679bc93 /editor | |
| parent | fad302f7db146a78900f9b21dbbcd97761093c1b (diff) | |
pke: checkpoint: major serialization refactor
Diffstat (limited to 'editor')
| -rw-r--r-- | editor/editor-io.cpp | 77 | ||||
| -rw-r--r-- | editor/editor-io.hpp | 10 | ||||
| -rw-r--r-- | editor/editor-types.cpp | 4 | ||||
| -rw-r--r-- | editor/editor-types.hpp | 16 | ||||
| -rw-r--r-- | editor/editor.cpp | 17 |
5 files changed, 113 insertions, 11 deletions
diff --git a/editor/editor-io.cpp b/editor/editor-io.cpp new file mode 100644 index 0000000..1a80d2d --- /dev/null +++ b/editor/editor-io.cpp @@ -0,0 +1,77 @@ + +#include "editor-io.hpp" + +#include "game-settings.hpp" +#include "scene.hpp" +#include "serialization.hpp" + +#include <fstream> + +void pke_editor_scene_save(const char *file_path) { + std::ostringstream stream{}; + srlztn_serialize_helper *helper = pke_serialize_init(pkeSettings.mem.bkt); + bool failed = false; + + try { + pke_serialize_file_scene(helper); + } catch (std::exception &e) { + fprintf(stderr, "[%s][Game_SaveSceneFile] Failed to serialize scene file: %s\n", __FILE__, e.what()); + failed = false; + } catch (...) { + fprintf(stderr, "[%s][Game_SaveSceneFile] Failed to serialize scene file, uncaught exception.\n", __FILE__); + failed = false; + } + + if (failed == false) { + std::ofstream f(file_path); + if (!f.is_open()) { + failed = true; + } else { + f << stream.str(); + } + f.flush(); + f.close(); + } + + if (failed) { + NULL_CHAR_ARR(errFileName, 256); + strncpy(errFileName, file_path, 256); + strncpy(errFileName + strlen(file_path), ".err", 256 - strlen(file_path)); + std::ofstream errF(file_path); + if (errF.is_open()) { + errF << stream.str(); + errF.flush(); + errF.close(); + fprintf(stderr, "Failed to save scene file '%s', partial output saved to '%s'\n", file_path, errFileName); + } else { + fprintf(stderr, "Failed to save scene file '%s' and also failed to write failed output\n", file_path); + } + } + pke_serialize_teardown(helper); +} + +void pke_editor_scene_load(const char *file_path) { + std::ifstream f(file_path); + if (!f.is_open()) { + fprintf(stderr, "Failed to load requested scene file: '%s'\n", file_path); + return; + } + srlztn_deserialize_helper *helper = pke_deserialize_init(pkeSettings.mem.bkt); + // TODO scene name is in the file? + helper->scene = pke_scene_get_by_name(file_path); + if (helper->scene == nullptr) { + helper->scene = pke_scene_create(file_path); + } + + pke_deserialize_file_scene(helper); + + pke_deserialize_teardown(helper); + + f.close(); +} + +void pke_editor_project_save(const char *file_path) { +} + +void pke_editor_project_load(const char *file_path) { +} diff --git a/editor/editor-io.hpp b/editor/editor-io.hpp new file mode 100644 index 0000000..07e9354 --- /dev/null +++ b/editor/editor-io.hpp @@ -0,0 +1,10 @@ +#ifndef PKE_EDITOR_EDITOR_IO_HPP +#define PKE_EDITOR_EDITOR_IO_HPP + +void pke_editor_scene_save(const char *file_path); +void pke_editor_scene_load(const char *file_path); + +void pke_editor_project_save(const char *file_path); +void pke_editor_project_load(const char *file_path); + +#endif /* PKE_EDITOR_EDITOR_IO_HPP */ diff --git a/editor/editor-types.cpp b/editor/editor-types.cpp new file mode 100644 index 0000000..b9d278a --- /dev/null +++ b/editor/editor-types.cpp @@ -0,0 +1,4 @@ + +#include "editor-types.hpp" + +struct editor_master editor_mstr; diff --git a/editor/editor-types.hpp b/editor/editor-types.hpp new file mode 100644 index 0000000..ff8239b --- /dev/null +++ b/editor/editor-types.hpp @@ -0,0 +1,16 @@ +#ifndef PKE_EDITOR_EDITOR_TYPES_HPP +#define PKE_EDITOR_EDITOR_TYPES_HPP + +#include "scene-types.hpp" +#include "pk.h" + +// TODO editor state (scene vs level) +struct editor_master { + pke_scene *active_scene; + pk_str target_scene_path; + bool shouldLoadScene = false; + bool shouldSaveScene = false; +}; +extern struct editor_master editor_mstr; + +#endif /* PKE_EDITOR_EDITOR_TYPES_HPP */ diff --git a/editor/editor.cpp b/editor/editor.cpp index af05781..48e5cc1 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -1,5 +1,7 @@ #include "editor.hpp" +#include "editor-io.hpp" +#include "editor-types.hpp" #include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h" #include "asset-manager.hpp" @@ -16,6 +18,7 @@ #include "msdfgen.h" #include "player-input.hpp" #include "plugins.hpp" +#include "project-settings.hpp" #include "project.hpp" #include "scene.hpp" #include "static-ui.hpp" @@ -54,14 +57,6 @@ const char* const dbgCtrl_ClearSelection = "debug-clear-selection"; const char* const dbgCtrl_DeleteSelectedItem = "debug-delete-selected-item"; const char* const dbgCtrl_ImGui_Toggle = "debug-imgui-toggle"; -// TODO editor state (scene vs level) -struct editor_master { - pke_scene *active_scene; - pk_str target_scene_path; - bool shouldLoadScene = false; - bool shouldSaveScene = false; -} editor_mstr; - ThreadPoolHandle threadPoolHandle = ThreadPoolHandle_MAX; InputActionSetHandle debugControlsHandle = InputActionSetHandle_MAX; @@ -194,7 +189,7 @@ void PkeEditor_Tick(double delta) { } else { sprintf(file_path, "%.16s", editor_mstr.active_scene->name); } - Game_SaveSceneFile(file_path); + pke_editor_scene_save(file_path); shouldRebuildProjectDir = true; } if (editor_mstr.target_scene_path.val != nullptr) { @@ -202,7 +197,7 @@ void PkeEditor_Tick(double delta) { pke_scene_remove(editor_mstr.active_scene->scene_handle); } ActiveCamera = &NullCamera; - Game_LoadSceneFile(editor_mstr.target_scene_path.val); + pke_editor_scene_load(editor_mstr.target_scene_path.val); std::filesystem::path p(editor_mstr.target_scene_path.val); editor_mstr.active_scene = pke_scene_get_by_name(p.stem().c_str()); if (editor_mstr.active_scene) { @@ -318,7 +313,7 @@ void PkeEditor_Tick(double delta) { // reinterpret_cast<void(*)()>(et->createInstanceCallback.func)(); fprintf(stderr, "[%s] Attempted to call EntityType::createInstanceCallback without a function signature", __FILE__); } else { - EntityType_CreateGenericInstance(et, PkeLevel_Get(pkeSettings.rt.activeLevel), nullptr); + EntityType_CreateGenericInstance(et, editor_mstr.active_scene, nullptr); } } |
