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 | #include "fsCSerialisableToStr.h"
#include <fsCore/src/debug/fsAssert.h>
#include <fsCore/src/fsCUniqueId.h>
#include <sstream>
#include <iomanip>
#define PARAMETER_DELIMITER ","
#define COMMA_REPLACEMENT "~"
fsStr fsCSerialisableToStr::myNameHashGet(const fsStr& pName) const<--- The member function 'fsCSerialisableToStr::myNameHashGet' can be static.
{
std::stringstream stream;
stream << std::setfill('0') << std::setw(sizeof(fsU32) * 2)
<< std::hex << fsCUniqueId::idCalculate(pName);
return fsStr(stream.str().c_str());
}
fsBool fsCSerialisableToStr::deserialiseInitDo(const fsStr& pName, fsStr& pLoadStr)
{
const fsStr nameHash = myNameHashGet(pName);
if (fsStr::npos == pLoadStr.find(nameHash))
{
fsLogMsg("No saved data for entity %s exists.\n", pName.utf8Get());
return false;
}
fsStr::sizeT myStrStart = pLoadStr.find(nameHash);
const char* const strRawData = pLoadStr.utf8Get();
fsS32 numOpenBraces = 1;
fsS32 myStrEnd = myStrStart + nameHash.dataSizeGet();
while (numOpenBraces > 0)
{
const char nextChar = strRawData[++myStrEnd];
if ('{' == nextChar)
{
++numOpenBraces;
}
else if ('}' == nextChar)
{
--numOpenBraces;
}
else if (0 == nextChar)
{
fsAssert(false, "Failed to match '{' with '}' in saved data.\n");
numOpenBraces = 0;
}
}
mSerialisationStr.copy(strRawData, myStrStart + nameHash.dataSizeGet() + 1,
(myStrEnd - myStrStart - nameHash.dataSizeGet() - 1));
pLoadStr.erase(myStrStart, ((myStrEnd - myStrStart) + 1));
return true;
}
void fsCSerialisableToStr::serialiseInitDo(const fsStr& pName)
{
const fsStr nameHash = myNameHashGet(pName);
mSerialisationStr.erase();
mSerialisationStr += nameHash;
mSerialisationStr += "{";
}
fsStr fsCSerialisableToStr::serialiseFinaliseDo()
{
mSerialisationStr += "}";
return mSerialisationStr;
}
fsStr fsCSerialisableToStr::strStipFromSaveStr(fsStr& pStr)
{
return pStr.chomp(PARAMETER_DELIMITER);
}
fsS32 fsCSerialisableToStr::s32StipFromSaveStr(fsStr& pStr)
{
return pStr.chomp(PARAMETER_DELIMITER).toS32();
}
void fsCSerialisableToStr::s32SerialiseDo(fsS32 pVal)
{
mSerialisationStr += fsStr(pVal) + PARAMETER_DELIMITER;
}
fsS32 fsCSerialisableToStr::s32DeserialiseDo()
{
fsAssert(fsStr::npos != mSerialisationStr.find(PARAMETER_DELIMITER), "Expected to find a ',' in the saved data.\n");
return s32StipFromSaveStr(mSerialisationStr);
}
void fsCSerialisableToStr::boolSerialiseDo(fsBool pVal)
{
mSerialisationStr += (pVal ? "1" : "0");
mSerialisationStr += PARAMETER_DELIMITER;
}
fsBool fsCSerialisableToStr::boolDeserialiseDo()
{
fsAssert(fsStr::npos != mSerialisationStr.find(PARAMETER_DELIMITER), "Expected to find a ',' in the saved data.\n");
return ("1" == mSerialisationStr.chomp(PARAMETER_DELIMITER));
}
void fsCSerialisableToStr::strSerialiseDo(const fsStr& pVal)
{
if (pVal.find(",") != fsStr::npos)
{
auto noCommaStr = pVal;
noCommaStr.replaceAll(",", COMMA_REPLACEMENT);
mSerialisationStr += noCommaStr + PARAMETER_DELIMITER;
return;
}
mSerialisationStr += pVal + PARAMETER_DELIMITER;
}
fsStr fsCSerialisableToStr::strDeserialiseDo()
{
fsAssert(fsStr::npos != mSerialisationStr.find(PARAMETER_DELIMITER), "Expected to find a ',' in the saved data.\n");
auto tmp = strStipFromSaveStr(mSerialisationStr);
if (tmp.find(COMMA_REPLACEMENT))
{
tmp.replaceAll(COMMA_REPLACEMENT, ",");
}
return tmp;
}
void fsCSerialisableToStr::pointSerialiseDo(const fsPoint& pVal)
{
mSerialisationStr += fsStr(pVal.x) + PARAMETER_DELIMITER + fsStr(pVal.y) + PARAMETER_DELIMITER;
}
fsPoint fsCSerialisableToStr::pointDeserialiseDo()
{
fsPoint val;
val.x = mSerialisationStr.chomp(PARAMETER_DELIMITER).toS32();
val.y = mSerialisationStr.chomp(PARAMETER_DELIMITER).toS32();
return val;
}
|