-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFsmGraphAsset.cpp
More file actions
58 lines (51 loc) · 2.07 KB
/
Copy pathFsmGraphAsset.cpp
File metadata and controls
58 lines (51 loc) · 2.07 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
#include "FsmGraph.h"
#include "FsmNodes.h" // pulls in the state node registrations (NodeFactory)
#include "FsmActions.h" // pulls in the action registrations (NodeFactory)
#include "assets/AssetManager.h"
#include "DekiLogSystem.h"
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <vector>
// Runtime loader for the FsmGraph state-machine asset. Mirrors the plain
// data-asset loaders: the editor compiles the ".asset" JSON to a MessagePack
// cache via the generic path, and here we parse that cache with the generic
// NodeGraphData loader (which creates state/action instances via NodeFactory).
// Same path on desktop and device. A malformed graph loads as nullptr, loudly.
namespace
{
FsmGraph* LoadGraphFromMemory(const uint8_t* data, size_t size)
{
NodeGraphData* graphData = NodeGraphData::LoadFromMemory(data, size);
if (!graphData)
{
DEKI_LOG_ERROR("FsmGraph: failed to load state machine asset");
return nullptr;
}
auto* graph = new FsmGraph();
graph->data = graphData;
return graph;
}
struct _FsmGraphLoaderReg
{
_FsmGraphLoaderReg()
{
auto pathLoader = [](const char* p) -> void*
{
std::ifstream f(p, std::ios::binary | std::ios::ate);
if (!f.is_open())
return nullptr;
std::streamsize n = f.tellg();
f.seekg(0, std::ios::beg);
std::vector<uint8_t> buf(static_cast<size_t>(n));
if (!f.read(reinterpret_cast<char*>(buf.data()), n))
return nullptr;
return LoadGraphFromMemory(buf.data(), static_cast<size_t>(n));
};
auto unloader = [](void* a) { delete static_cast<FsmGraph*>(a); };
auto memLoader = [](const uint8_t* d, size_t n) -> void* { return LoadGraphFromMemory(d, n); };
Deki::AssetManager::RegisterLoader("FsmGraph", pathLoader, unloader, memLoader);
}
};
static _FsmGraphLoaderReg s_fsmGraphLoaderReg;
}