diff options
Diffstat (limited to 'editor/editor-io.cpp')
| -rw-r--r-- | editor/editor-io.cpp | 77 |
1 files changed, 77 insertions, 0 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) { +} |
