12
12
#include < resources/PackFile.h>
13
13
#include < resources/ResourceSystem.h>
14
14
#include < resources/SceneData.h>
15
- #include < utils/FileSystem.h>
16
15
#include < utils/Logging.h>
17
16
18
17
#include < algorithm>
19
18
20
19
#include " SceneSystem.h"
21
- #include " resources/GenericFileData.h"
20
+
21
+ REGISTER_TOKEN (TYPE);
22
+ REGISTER_TOKEN (ROTATION);
23
+ REGISTER_TOKEN (Z_INDEX);
24
+ REGISTER_TOKEN (POSITION);
22
25
23
26
namespace Siege
24
27
{
25
- void SceneFile::RegisterSerialisable (const String& name ,
28
+ void SceneFile::RegisterSerialisable (Token type ,
26
29
const Serialiser& serialise,
27
30
const Deserialiser& deserialise)
28
31
{
29
- GetSerialisables ().emplace (name , std::make_pair (serialise, deserialise));
32
+ GetSerialisables ().emplace (type , std::make_pair (serialise, deserialise));
30
33
}
31
34
32
35
bool SceneFile::Serialise (const std::vector<Entity*>& entities)
@@ -65,14 +68,14 @@ bool SceneFile::SerialiseToString(Entity* entity, String& fileData)
65
68
auto & serialisables = GetSerialisables ();
66
69
67
70
// Only serialise entities that register a serialisable interface
68
- auto it = serialisables.find (entity->GetName ());
71
+ auto it = serialisables.find (entity->GetType ());
69
72
if (it == serialisables.end ()) return false ;
70
73
71
74
// Serialise the general entity information
72
- fileData += ( entity->GetName () + LINE_SEP );
73
- fileData += DefineField (" POSITION " , ToString (entity->GetPosition ()));
74
- fileData += DefineField (" ROTATION " , String::FromFloat (entity->GetRotation ().y ));
75
- fileData += DefineField (" Z-INDEX " , String::FromInt (entity->GetZIndex ()));
75
+ fileData += DefineField (TOKEN_TYPE, entity->GetType (). GetId () );
76
+ fileData += DefineField (TOKEN_POSITION , ToString (entity->GetPosition ()));
77
+ fileData += DefineField (TOKEN_ROTATION , String::FromFloat (entity->GetRotation ().y ));
78
+ fileData += DefineField (TOKEN_Z_INDEX , String::FromInt (entity->GetZIndex ()));
76
79
77
80
// Apply its serialiser if it
78
81
Serialiser serialiser = it->second .first ;
@@ -141,34 +144,25 @@ bool SceneFile::Deserialise(std::vector<Entity*>& entities)
141
144
142
145
Entity* SceneFile::DeserialiseFromString (const String& fileData)
143
146
{
144
- if (fileData.IsEmpty ())
145
- {
146
- CC_LOG_WARNING (" Found empty entity during deserialisation" );
147
- return nullptr ;
148
- }
149
-
150
- // Split the file into arguments and strip the labels from each item
151
- std::vector<String> args = fileData.Split (LINE_SEP);
152
- for (String& arg : args) arg = arg.SubString ((int ) arg.Find (NAME_SEP) + 1 );
147
+ std::map<Token, String> attributes = Siege::FileSystem::ParseAttributeFileData (fileData);
153
148
154
- // Get the standard entity fields
155
- EntityData data;
156
- if (!(args.size () >= 4 && args[ENTITY_ROT].GetFloat (data.rotation ) &&
157
- args[ENTITY_Z_IDX].GetInt (data.zIndex ) && FromString (data.position , args[ENTITY_POS])))
149
+ if (attributes.empty ())
158
150
{
159
- CC_LOG_WARNING (" Failed to deserialise fields for entity \" {}\" " , args[ENTITY_NAME]);
151
+ CC_LOG_WARNING (" Found empty entity during deserialisation!" );
152
+ return nullptr ;
160
153
}
161
154
162
155
// Check if the entity has a relevant serialisable interface registered
163
156
auto & serialisables = GetSerialisables ();
164
- auto it = serialisables.find (args[ENTITY_NAME]);
157
+ Token typeToken (attributes[TOKEN_TYPE]);
158
+ auto it = serialisables.find (typeToken);
165
159
if (it != serialisables.end ())
166
160
{
167
161
// Apply its deserialiser
168
162
Deserialiser deserialiser = it->second .second ;
169
- if (deserialiser) return deserialiser (data, args );
163
+ if (deserialiser) return deserialiser (attributes );
170
164
}
171
- else CC_LOG_WARNING (" \" {}\" has no deserialisation protocols defined" , args[ENTITY_NAME ]);
165
+ else CC_LOG_WARNING (" \" {}\" has no deserialisation protocols defined" , attributes[TOKEN_TYPE ]);
172
166
return nullptr ;
173
167
}
174
168
@@ -190,6 +184,29 @@ void SceneFile::InitialiseEntityPathMappings()
190
184
}
191
185
}
192
186
187
+ EntityData SceneFile::GetBaseEntityData (const std::map<Token, String>& attributes)
188
+ {
189
+ EntityData data;
190
+ auto it = attributes.find (TOKEN_ROTATION);
191
+ if (it == attributes.end () || !it->second .GetFloat (data.rotation ))
192
+ {
193
+ CC_LOG_WARNING (" Failed to deserialise ROTATION field for entity attributes" );
194
+ }
195
+
196
+ it = attributes.find (TOKEN_Z_INDEX);
197
+ if (it == attributes.end () || !it->second .GetInt (data.zIndex ))
198
+ {
199
+ CC_LOG_WARNING (" Failed to deserialise Z_INDEX field for entity attributes" );
200
+ }
201
+
202
+ it = attributes.find (TOKEN_POSITION);
203
+ if (it == attributes.end () || !FromString (data.position , it->second ))
204
+ {
205
+ CC_LOG_WARNING (" Failed to deserialise POSITION field for entity attributes" );
206
+ }
207
+ return data;
208
+ }
209
+
193
210
String SceneFile::GetOrCreateEntityFilepath (Entity* entity)
194
211
{
195
212
// Try to find the entity path amongst the deserialised
@@ -212,7 +229,8 @@ String SceneFile::GetOrCreateEntityFilepath(Entity* entity)
212
229
213
230
// Failed attempts to find a file index are serialised as 0
214
231
String index = result ? String::FromInt (newFileIndex) : " 0" ;
215
- return MakeScenePath (sceneName) + ' /' + entity->GetName () + ' .' + index + ENTITY_FILE_EXT;
232
+ return MakeScenePath (sceneName) + ' /' + entity->GetType ().GetId () + ' .' + index +
233
+ ENTITY_FILE_EXT;
216
234
}
217
235
218
236
String SceneFile::MakeScenePath (const String& sceneName)
0 commit comments