forked from rive-app/rive-cpp-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
432 lines (408 loc) · 13.5 KB
/
file.cpp
File metadata and controls
432 lines (408 loc) · 13.5 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "rive/file.hpp"
#include "rive/runtime_header.hpp"
#include "rive/animation/animation.hpp"
#include "rive/core/field_types/core_color_type.hpp"
#include "rive/core/field_types/core_double_type.hpp"
#include "rive/core/field_types/core_string_type.hpp"
#include "rive/core/field_types/core_uint_type.hpp"
#include "rive/generated/core_registry.hpp"
#include "rive/importers/artboard_importer.hpp"
#include "rive/importers/backboard_importer.hpp"
#include "rive/importers/file_asset_importer.hpp"
#include "rive/importers/import_stack.hpp"
#include "rive/importers/keyed_object_importer.hpp"
#include "rive/importers/keyed_property_importer.hpp"
#include "rive/importers/linear_animation_importer.hpp"
#include "rive/importers/state_machine_importer.hpp"
#include "rive/importers/state_machine_listener_importer.hpp"
#include "rive/importers/state_machine_layer_importer.hpp"
#include "rive/importers/layer_state_importer.hpp"
#include "rive/importers/state_transition_importer.hpp"
#include "rive/importers/state_machine_layer_component_importer.hpp"
#include "rive/animation/blend_state_transition.hpp"
#include "rive/animation/any_state.hpp"
#include "rive/animation/entry_state.hpp"
#include "rive/animation/exit_state.hpp"
#include "rive/animation/animation_state.hpp"
#include "rive/animation/blend_state_1d.hpp"
#include "rive/animation/blend_state_direct.hpp"
#include "rive/assets/file_asset.hpp"
#include "rive/assets/audio_asset.hpp"
#include "rive/assets/file_asset_contents.hpp"
// Default namespace for Rive Cpp code
using namespace rive;
#if !defined(RIVE_FMT_U64)
#if defined(__ANDROID__)
#if INTPTR_MAX == INT64_MAX
#define RIVE_FMT_U64 "%lu"
#define RIVE_FMT_I64 "%ld"
#else
#define RIVE_FMT_U64 "%llu"
#define RIVE_FMT_I64 "%lld"
#endif
#elif defined(_WIN32)
#define RIVE_FMT_U64 "%lld"
#define RIVE_FMT_I64 "%llu"
#else
#include <inttypes.h>
#define RIVE_FMT_U64 "%" PRIu64
#define RIVE_FMT_I64 "%" PRId64
#endif
#endif
// Import a single Rive runtime object.
// Used by the file importer.
static Core* readRuntimeObject(BinaryReader& reader, const RuntimeHeader& header)
{
auto coreObjectKey = reader.readVarUintAs<int>();
auto object = CoreRegistry::makeCoreInstance(coreObjectKey);
while (true)
{
auto propertyKey = reader.readVarUintAs<uint16_t>();
if (propertyKey == 0)
{
// Terminator. https://media.giphy.com/media/7TtvTUMm9mp20/giphy.gif
break;
}
if (reader.hasError())
{
delete object;
return nullptr;
}
if (object == nullptr || !object->deserialize(propertyKey, reader))
{
// We have an unknown object or property, first see if core knows
// the property type.
int id = CoreRegistry::propertyFieldId(propertyKey);
if (id == -1)
{
// No, check if it's in toc.
id = header.propertyFieldId(propertyKey);
}
if (id == -1)
{
// Still couldn't find it, give up.
fprintf(stderr,
"Unknown property key %d, missing from property ToC.\n",
propertyKey);
delete object;
return nullptr;
}
switch (id)
{
case CoreUintType::id:
CoreUintType::deserialize(reader);
break;
case CoreStringType::id:
CoreStringType::deserialize(reader);
break;
case CoreDoubleType::id:
CoreDoubleType::deserialize(reader);
break;
case CoreColorType::id:
CoreColorType::deserialize(reader);
break;
}
}
}
if (object == nullptr)
{
// fprintf(stderr,
// "File contains an unknown object with coreType " RIVE_FMT_U64
// ", which " "this runtime doesn't understand.\n",
// coreObjectKey);
return nullptr;
}
return object;
}
File::File(Factory* factory, FileAssetLoader* assetLoader) :
m_factory(factory), m_assetLoader(assetLoader)
{
assert(factory);
}
File::~File()
{
for (auto artboard : m_artboards)
{
delete artboard;
}
// Assets delete after artboards as they reference them.
for (auto asset : m_fileAssets)
{
delete asset;
}
delete m_backboard;
}
std::unique_ptr<File> File::import(Span<const uint8_t> bytes,
Factory* factory,
ImportResult* result,
FileAssetLoader* assetLoader)
{
BinaryReader reader(bytes);
RuntimeHeader header;
if (!RuntimeHeader::read(reader, header))
{
fprintf(stderr, "Bad header\n");
if (result)
{
*result = ImportResult::malformed;
}
return nullptr;
}
if (header.majorVersion() != majorVersion)
{
fprintf(stderr,
"Unsupported version %u.%u expected %u.%u.\n",
header.majorVersion(),
header.minorVersion(),
majorVersion,
minorVersion);
if (result)
{
*result = ImportResult::unsupportedVersion;
}
return nullptr;
}
auto file = std::unique_ptr<File>(new File(factory, assetLoader));
auto readResult = file->read(reader, header);
if (readResult != ImportResult::success)
{
file.reset(nullptr);
}
if (result)
{
*result = ImportResult::success;
}
return file;
}
ImportResult File::read(BinaryReader& reader, const RuntimeHeader& header)
{
ImportStack importStack;
while (!reader.reachedEnd())
{
auto object = readRuntimeObject(reader, header);
if (object == nullptr)
{
importStack.readNullObject();
continue;
}
if (object->import(importStack) == StatusCode::Ok)
{
switch (object->coreType())
{
case Backboard::typeKey:
m_backboard = object->as<Backboard>();
break;
case Artboard::typeKey:
{
Artboard* ab = object->as<Artboard>();
ab->m_Factory = m_factory;
m_artboards.push_back(ab);
}
break;
case ImageAsset::typeKey:
case FontAsset::typeKey:
case AudioAsset::typeKey:
{
auto fa = object->as<FileAsset>();
m_fileAssets.push_back(fa);
}
break;
}
}
else
{
fprintf(stderr, "Failed to import object of type %d\n", object->coreType());
delete object;
continue;
}
ImportStackObject* stackObject = nullptr;
auto stackType = object->coreType();
switch (stackType)
{
case Backboard::typeKey:
stackObject = new BackboardImporter(object->as<Backboard>());
break;
case Artboard::typeKey:
stackObject = new ArtboardImporter(object->as<Artboard>());
break;
case LinearAnimation::typeKey:
stackObject = new LinearAnimationImporter(object->as<LinearAnimation>());
break;
case KeyedObject::typeKey:
stackObject = new KeyedObjectImporter(object->as<KeyedObject>());
break;
case KeyedProperty::typeKey:
{
auto importer =
importStack.latest<LinearAnimationImporter>(LinearAnimation::typeKey);
if (importer == nullptr)
{
return ImportResult::malformed;
}
stackObject =
new KeyedPropertyImporter(importer->animation(), object->as<KeyedProperty>());
break;
}
case StateMachine::typeKey:
stackObject = new StateMachineImporter(object->as<StateMachine>());
break;
case StateMachineLayer::typeKey:
{
auto artboardImporter = importStack.latest<ArtboardImporter>(ArtboardBase::typeKey);
if (artboardImporter == nullptr)
{
return ImportResult::malformed;
}
stackObject = new StateMachineLayerImporter(object->as<StateMachineLayer>(),
artboardImporter->artboard());
break;
}
case EntryState::typeKey:
case ExitState::typeKey:
case AnyState::typeKey:
case AnimationState::typeKey:
case BlendState1D::typeKey:
case BlendStateDirect::typeKey:
stackObject = new LayerStateImporter(object->as<LayerState>());
stackType = LayerState::typeKey;
break;
case StateTransition::typeKey:
case BlendStateTransition::typeKey:
stackObject = new StateTransitionImporter(object->as<StateTransition>());
stackType = StateTransition::typeKey;
break;
case StateMachineListener::typeKey:
stackObject = new StateMachineListenerImporter(object->as<StateMachineListener>());
break;
case ImageAsset::typeKey:
case FontAsset::typeKey:
case AudioAsset::typeKey:
stackObject =
new FileAssetImporter(object->as<FileAsset>(), m_assetLoader, m_factory);
stackType = FileAsset::typeKey;
break;
}
if (importStack.makeLatest(stackType, stackObject) != StatusCode::Ok)
{
// Some previous stack item didn't resolve.
return ImportResult::malformed;
}
if (object->is<StateMachineLayerComponent>() &&
importStack.makeLatest(StateMachineLayerComponent::typeKey,
new StateMachineLayerComponentImporter(
object->as<StateMachineLayerComponent>())) != StatusCode::Ok)
{
return ImportResult::malformed;
}
}
return !reader.hasError() && importStack.resolve() == StatusCode::Ok ? ImportResult::success
: ImportResult::malformed;
}
Artboard* File::artboard(std::string name) const
{
for (const auto& artboard : m_artboards)
{
if (artboard->name() == name)
{
return artboard;
}
}
return nullptr;
}
Artboard* File::artboard() const
{
if (m_artboards.empty())
{
return nullptr;
}
return m_artboards[0];
}
Artboard* File::artboard(size_t index) const
{
if (index >= m_artboards.size())
{
return nullptr;
}
return m_artboards[index];
}
std::string File::artboardNameAt(size_t index) const
{
auto ab = this->artboard(index);
return ab ? ab->name() : "";
}
std::unique_ptr<ArtboardInstance> File::artboardDefault() const
{
auto ab = this->artboard();
return ab ? ab->instance() : nullptr;
}
std::unique_ptr<ArtboardInstance> File::artboardAt(size_t index) const
{
auto ab = this->artboard(index);
return ab ? ab->instance() : nullptr;
}
std::unique_ptr<ArtboardInstance> File::artboardNamed(std::string name) const
{
auto ab = this->artboard(name);
return ab ? ab->instance() : nullptr;
}
const std::vector<FileAsset*>& File::assets() const { return m_fileAssets; }
#ifdef WITH_RIVE_TOOLS
const std::vector<uint8_t> File::stripAssets(Span<const uint8_t> bytes,
std::set<uint16_t> typeKeys,
ImportResult* result)
{
std::vector<uint8_t> strippedData;
strippedData.reserve(bytes.size());
BinaryReader reader(bytes);
RuntimeHeader header;
if (!RuntimeHeader::read(reader, header))
{
if (result)
{
*result = ImportResult::malformed;
}
}
else if (header.majorVersion() != majorVersion)
{
if (result)
{
*result = ImportResult::unsupportedVersion;
}
}
else
{
strippedData.insert(strippedData.end(), bytes.data(), reader.position());
const uint8_t* from = reader.position();
const uint8_t* to = reader.position();
uint16_t lastAssetType = 0;
while (!reader.reachedEnd())
{
auto object = readRuntimeObject(reader, header);
if (object == nullptr)
{
continue;
}
if (object->is<FileAssetBase>())
{
lastAssetType = object->coreType();
}
if (object->is<FileAssetContents>() && typeKeys.find(lastAssetType) != typeKeys.end())
{
if (from != to)
{
strippedData.insert(strippedData.end(), from, to);
}
from = reader.position();
}
delete object;
to = reader.position();
}
if (from != to)
{
strippedData.insert(strippedData.end(), from, to);
}
*result = ImportResult::success;
}
return strippedData;
}
#endif