-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameAnimationMsgPack.cpp
More file actions
350 lines (294 loc) · 11.9 KB
/
FrameAnimationMsgPack.cpp
File metadata and controls
350 lines (294 loc) · 11.9 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "FrameAnimationMsgPack.h"
#include "FrameAnimationData.h"
#include "DekiLogSystem.h"
#include "assets/AssetManager.h"
#include <cstdio>
#include <cstring>
#include <fstream>
#include <vector>
#ifdef DEKI_EDITOR
// Editor uses nlohmann/json for MessagePack
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#else
// ESP32/Simulator uses mpack for MessagePack
#include <mpack/mpack.h>
#endif
bool FrameAnimationMsgPackHelper::LoadAnimation(const char* msgpack_path, FrameAnimationData* out_data)
{
if (!msgpack_path || !out_data)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::LoadAnimation - null parameters");
return false;
}
// Read file into buffer
std::ifstream file(msgpack_path, std::ios::binary | std::ios::ate);
if (!file.is_open())
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::LoadAnimation - failed to open: %s", msgpack_path);
return false;
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer(size);
if (!file.read(reinterpret_cast<char*>(buffer.data()), size))
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::LoadAnimation - failed to read file");
return false;
}
return LoadAnimationFromMemory(buffer.data(), static_cast<size_t>(size), out_data);
}
bool FrameAnimationMsgPackHelper::LoadAnimationFromMemory(const uint8_t* data, size_t size, FrameAnimationData* out_data)
{
if (!data || size == 0 || !out_data)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::LoadAnimationFromMemory - invalid parameters");
return false;
}
// Clear output
out_data->spritesheet_guid.clear();
out_data->animations.clear();
#ifdef DEKI_EDITOR
// Use nlohmann/json to deserialize MessagePack
try
{
json j = json::from_msgpack(data, data + size);
// Version check
uint16_t version = 1;
if (j.contains("v"))
{
version = j["v"].get<uint16_t>();
if (version > FRAMEANIM_MSGPACK_VERSION)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper - unsupported version: %d", version);
return false;
}
}
// Spritesheet GUID
if (j.contains("s")) out_data->spritesheet_guid = j["s"].get<std::string>();
// Version 2: multiple animations
if (version >= 2 && j.contains("a") && j["a"].is_array())
{
for (const auto& anim_obj : j["a"])
{
FrameAnimSequence seq;
seq.loop = true;
if (anim_obj.contains("n")) seq.name = anim_obj["n"].get<std::string>();
if (anim_obj.contains("l")) seq.loop = anim_obj["l"].get<bool>();
if (anim_obj.contains("f") && anim_obj["f"].is_array())
{
for (const auto& frame_obj : anim_obj["f"])
{
FrameAnimFrame frame;
frame.duration = 100;
if (frame_obj.contains("g")) frame.frame_guid = frame_obj["g"].get<std::string>();
if (frame_obj.contains("d")) frame.duration = frame_obj["d"].get<int32_t>();
seq.frames.push_back(frame);
}
}
out_data->animations.push_back(seq);
}
}
// Version 1 backwards compatibility: single animation
else if (j.contains("f") && j["f"].is_array())
{
FrameAnimSequence seq;
if (j.contains("n")) seq.name = j["n"].get<std::string>();
if (j.contains("l")) seq.loop = j["l"].get<bool>();
for (const auto& frame_obj : j["f"])
{
FrameAnimFrame frame;
frame.duration = 100;
if (frame_obj.contains("g")) frame.frame_guid = frame_obj["g"].get<std::string>();
if (frame_obj.contains("d")) frame.duration = frame_obj["d"].get<int32_t>();
seq.frames.push_back(frame);
}
out_data->animations.push_back(seq);
}
}
catch (const std::exception& e)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::LoadAnimationFromMemory - parse error: %s", e.what());
return false;
}
#else
// Use mpack to deserialize MessagePack
mpack_tree_t tree;
mpack_tree_init_data(&tree, reinterpret_cast<const char*>(data), size);
mpack_tree_parse(&tree);
if (mpack_tree_error(&tree) != mpack_ok)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::LoadAnimationFromMemory - mpack error: %d", (int)mpack_tree_error(&tree));
mpack_tree_destroy(&tree);
return false;
}
mpack_node_t root = mpack_tree_root(&tree);
// Version check
uint16_t version = 1;
mpack_node_t vNode = mpack_node_map_cstr_optional(root, "v");
if (!mpack_node_is_missing(vNode))
{
version = mpack_node_u16(vNode);
if (version > FRAMEANIM_MSGPACK_VERSION)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper - unsupported version: %d", version);
mpack_tree_destroy(&tree);
return false;
}
}
// Spritesheet GUID
mpack_node_t sNode = mpack_node_map_cstr_optional(root, "s");
if (!mpack_node_is_missing(sNode))
out_data->spritesheet_guid = std::string(mpack_node_str(sNode), mpack_node_strlen(sNode));
// Version 2: multiple animations
mpack_node_t aNode = mpack_node_map_cstr_optional(root, "a");
if (version >= 2 && !mpack_node_is_missing(aNode))
{
size_t animCount = mpack_node_array_length(aNode);
for (size_t i = 0; i < animCount; i++)
{
mpack_node_t anim_obj = mpack_node_array_at(aNode, i);
FrameAnimSequence seq;
seq.loop = true;
mpack_node_t nNode = mpack_node_map_cstr_optional(anim_obj, "n");
if (!mpack_node_is_missing(nNode))
seq.name = std::string(mpack_node_str(nNode), mpack_node_strlen(nNode));
mpack_node_t lNode = mpack_node_map_cstr_optional(anim_obj, "l");
if (!mpack_node_is_missing(lNode))
seq.loop = mpack_node_bool(lNode);
mpack_node_t fNode = mpack_node_map_cstr_optional(anim_obj, "f");
if (!mpack_node_is_missing(fNode))
{
size_t frameCount = mpack_node_array_length(fNode);
for (size_t fi = 0; fi < frameCount; fi++)
{
mpack_node_t frame_obj = mpack_node_array_at(fNode, fi);
FrameAnimFrame frame;
frame.duration = 100;
mpack_node_t gNode = mpack_node_map_cstr_optional(frame_obj, "g");
if (!mpack_node_is_missing(gNode))
frame.frame_guid = std::string(mpack_node_str(gNode), mpack_node_strlen(gNode));
mpack_node_t dNode = mpack_node_map_cstr_optional(frame_obj, "d");
if (!mpack_node_is_missing(dNode))
frame.duration = mpack_node_i32(dNode);
seq.frames.push_back(frame);
}
}
out_data->animations.push_back(seq);
}
}
// Version 1 backwards compatibility: single animation
else
{
mpack_node_t fNode = mpack_node_map_cstr_optional(root, "f");
if (!mpack_node_is_missing(fNode))
{
FrameAnimSequence seq;
seq.loop = true;
mpack_node_t nNode = mpack_node_map_cstr_optional(root, "n");
if (!mpack_node_is_missing(nNode))
seq.name = std::string(mpack_node_str(nNode), mpack_node_strlen(nNode));
mpack_node_t lNode = mpack_node_map_cstr_optional(root, "l");
if (!mpack_node_is_missing(lNode))
seq.loop = mpack_node_bool(lNode);
size_t frameCount = mpack_node_array_length(fNode);
for (size_t fi = 0; fi < frameCount; fi++)
{
mpack_node_t frame_obj = mpack_node_array_at(fNode, fi);
FrameAnimFrame frame;
frame.duration = 100;
mpack_node_t gNode = mpack_node_map_cstr_optional(frame_obj, "g");
if (!mpack_node_is_missing(gNode))
frame.frame_guid = std::string(mpack_node_str(gNode), mpack_node_strlen(gNode));
mpack_node_t dNode = mpack_node_map_cstr_optional(frame_obj, "d");
if (!mpack_node_is_missing(dNode))
frame.duration = mpack_node_i32(dNode);
seq.frames.push_back(frame);
}
out_data->animations.push_back(seq);
}
}
mpack_tree_destroy(&tree);
#endif
int totalFrames = 0;
for (const auto& anim : out_data->animations)
totalFrames += static_cast<int>(anim.frames.size());
DEKI_LOG_INTERNAL("FrameAnimationMsgPackHelper::LoadAnimation - loaded %d animations with %d total frames",
static_cast<int>(out_data->animations.size()), totalFrames);
return true;
}
#ifdef DEKI_EDITOR
bool FrameAnimationMsgPackHelper::SaveAnimation(const char* msgpack_path, const FrameAnimationData* anim_data)
{
if (!msgpack_path || !anim_data)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::SaveAnimation - null parameters");
return false;
}
try
{
// Build JSON structure (version 2 format)
json j;
j["v"] = static_cast<uint16_t>(FRAMEANIM_MSGPACK_VERSION);
j["s"] = anim_data->spritesheet_guid;
// Animations array
json anims_arr = json::array();
for (const auto& seq : anim_data->animations)
{
json anim_obj;
anim_obj["n"] = seq.name;
anim_obj["l"] = seq.loop;
json frames_arr = json::array();
for (const auto& frame : seq.frames)
{
json frame_obj;
frame_obj["g"] = frame.frame_guid;
frame_obj["d"] = static_cast<uint16_t>(frame.duration);
frames_arr.push_back(frame_obj);
}
anim_obj["f"] = frames_arr;
anims_arr.push_back(anim_obj);
}
j["a"] = anims_arr;
// Convert to MessagePack
std::vector<uint8_t> msgpack_data = json::to_msgpack(j);
// Write to file
std::ofstream file(msgpack_path, std::ios::binary);
if (!file.is_open())
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::SaveAnimation - failed to open for writing: %s", msgpack_path);
return false;
}
file.write(reinterpret_cast<const char*>(msgpack_data.data()), msgpack_data.size());
int totalFrames = 0;
for (const auto& seq : anim_data->animations)
totalFrames += static_cast<int>(seq.frames.size());
DEKI_LOG_INTERNAL("FrameAnimationMsgPackHelper::SaveAnimation - saved %d animations with %d total frames (%zu bytes)",
static_cast<int>(anim_data->animations.size()), totalFrames, msgpack_data.size());
return file.good();
}
catch (const std::exception& e)
{
DEKI_LOG_ERROR("FrameAnimationMsgPackHelper::SaveAnimation - error: %s", e.what());
return false;
}
}
#endif
// Self-register animation loader with AssetManager
namespace {
struct _AnimLoaderReg {
_AnimLoaderReg() {
auto loader = [](const char* p) -> void* {
auto* data = new FrameAnimationData();
if (FrameAnimationMsgPackHelper::LoadAnimation(p, data))
return data;
delete data;
return nullptr;
};
auto unloader = [](void* a) { delete static_cast<FrameAnimationData*>(a); };
Deki::AssetManager::RegisterLoader("FrameAnimationData", loader, unloader);
Deki::AssetManager::RegisterLoader("Animation", loader, unloader);
}
};
static _AnimLoaderReg s_animLoaderReg;
}