summaryrefslogtreecommitdiff
path: root/editor/editor-io.cpp
blob: 27d84fb4c2d36bb2a8d93345b5b71ada5be43f82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#include "editor-io.hpp"

#include "game-settings.hpp"
#include "serialization.hpp"

#include <fstream>

void pke_editor_scene_save(const char *file_path) {
	srlztn_serialize_helper *helper = pke_serialize_init(pkeSettings.mem_bkt.game_transient);
	bool failed = false;
	std::ostringstream o;

	try {
		pke_serialize_scene(helper);
		pke_serialize_scene_to_stream(o, 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 << o.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 << o.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(pke_level *level, 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(level, pkeSettings.mem_bkt.game_transient);

	pke_deserialize_scene_from_stream(f, helper);
	pke_deserialize_scene(helper);

	pke_deserialize_teardown(helper);

	f.close();
}

void pke_editor_project_save(const char *file_path) {
	(void)file_path;
}

void pke_editor_project_load(const char *file_path) {
	(void)file_path;
}