1212#include < resources/PackFile.h>
1313#include < resources/ResourceSystem.h>
1414#include < resources/SceneData.h>
15- #include < utils/FileSystem.h>
1615#include < utils/Logging.h>
1716
1817#include < algorithm>
1918
2019#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);
2225
2326namespace Siege
2427{
25- void SceneFile::RegisterSerialisable (const String& name ,
28+ void SceneFile::RegisterSerialisable (Token type ,
2629 const Serialiser& serialise,
2730 const Deserialiser& deserialise)
2831{
29- GetSerialisables ().emplace (name , std::make_pair (serialise, deserialise));
32+ GetSerialisables ().emplace (type , std::make_pair (serialise, deserialise));
3033}
3134
3235bool SceneFile::Serialise (const std::vector<Entity*>& entities)
@@ -65,14 +68,14 @@ bool SceneFile::SerialiseToString(Entity* entity, String& fileData)
6568 auto & serialisables = GetSerialisables ();
6669
6770 // Only serialise entities that register a serialisable interface
68- auto it = serialisables.find (entity->GetName ());
71+ auto it = serialisables.find (entity->GetType ());
6972 if (it == serialisables.end ()) return false ;
7073
7174 // 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 ()));
7679
7780 // Apply its serialiser if it
7881 Serialiser serialiser = it->second .first ;
@@ -141,34 +144,25 @@ bool SceneFile::Deserialise(std::vector<Entity*>& entities)
141144
142145Entity* SceneFile::DeserialiseFromString (const String& fileData)
143146{
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);
153148
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 ())
158150 {
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 ;
160153 }
161154
162155 // Check if the entity has a relevant serialisable interface registered
163156 auto & serialisables = GetSerialisables ();
164- auto it = serialisables.find (args[ENTITY_NAME]);
157+ Token typeToken (attributes[TOKEN_TYPE]);
158+ auto it = serialisables.find (typeToken);
165159 if (it != serialisables.end ())
166160 {
167161 // Apply its deserialiser
168162 Deserialiser deserialiser = it->second .second ;
169- if (deserialiser) return deserialiser (data, args );
163+ if (deserialiser) return deserialiser (attributes );
170164 }
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 ]);
172166 return nullptr ;
173167}
174168
@@ -190,6 +184,29 @@ void SceneFile::InitialiseEntityPathMappings()
190184 }
191185}
192186
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+
193210String SceneFile::GetOrCreateEntityFilepath (Entity* entity)
194211{
195212 // Try to find the entity path amongst the deserialised
@@ -212,7 +229,8 @@ String SceneFile::GetOrCreateEntityFilepath(Entity* entity)
212229
213230 // Failed attempts to find a file index are serialised as 0
214231 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;
216234}
217235
218236String SceneFile::MakeScenePath (const String& sceneName)
0 commit comments