示例#1
0
        void WriteEntity(Saveable saveable, int id, PackedEnt[] packedEnts)
        {
            var packedEnt = packedEnts[id] = new PackedEnt();
            var entity    = saveable.GetEntity();

            packedEnt.Position = entity.transform.position;
            packedEnt.Rotation = entity.transform.rotation;
            packedEnt.SaveId   = saveable.SaveId;
            packedEnt.PrefabId = saveable.PrefabId;

            var tags = entity.Tags.GetTagsCopy();

            packedEnt.Tags = new int[tags.Count, 2];
            var tagIndex = 0;

            foreach (var tagCountPair in tags)
            {
                packedEnt.Tags[tagIndex, 0] = (int)tagCountPair.Key;
                packedEnt.Tags[tagIndex, 1] = tagCountPair.Value;

                tagIndex++;
            }

            var parts = entity.GetComponents <Part>();

            var packedParts = new List <PackedPart>();

            for (var w = 0; w < parts.Length; w++)
            {
                WritePart(parts[w], packedParts);
            }

            packedEnt.Parts = packedParts.ToArray();
        }
示例#2
0
        public string Serialize(List <Saveable> saveables)
        {
            var packedEnts = new PackedEnt[saveables.Count];

            for (int q = 0; q < saveables.Count; q++)
            {
                WriteEntity(saveables[q], q, packedEnts);
            }

            var sb = new StringBuilder();
            var sw = new StringWriter(sb);

            using (writer = new JsonTextWriter(sw))
            {
                writer.Formatting = formatting;

                serializer.Serialize(writer, packedEnts);
            }

            return(sw.ToString());
        }
示例#3
0
        Entity SpawnLoadedEntity(PackedEnt packedEnt, List <Saveable> saveablesOnScene)
        {
            Entity ent = null;

            for (var i = 0; i < saveablesOnScene.Count; i++)
            {
                if (saveablesOnScene[i].SaveId == packedEnt.SaveId)
                {
                    ent = saveablesOnScene[i].GetEntity();
                    ent.transform.position = packedEnt.Position;
                    ent.transform.rotation = packedEnt.Rotation;

                    saveablesOnScene.RemoveAt(i);
                    break;
                }
            }

            if (ent == null)
            {
                var prefab = assetMap.GetById(packedEnt.PrefabId);

                ent = Entity.Spawn(prefab, packedEnt.Position, packedEnt.Rotation);
            }

            for (var i = 0; i < packedEnt.Tags.GetLength(0); i++)
            {
                var tag      = packedEnt.Tags[i, 0];
                var tagCount = packedEnt.Tags[i, 1];

                var entTagCount = ent.Tags.Count(tag);
                for (int w = 0; w < entTagCount; w++) // better will be to correct tags count, not to make these for loops...
                {
                    ent.Tags.Remove(tag);
                }

                for (int w = 0; w < tagCount; w++)
                {
                    ent.Tags.Add(tag); // Pluto ECL can't add several tag count per time, so adding it in for loop
                }
            }

            for (var i = 0; i < packedEnt.Parts.Length; i++)
            {
                var packedPart = packedEnt.Parts[i];
                var type       = Type.GetType(packedPart.PartTypeName);

                if (type == null)
                {
                    Debug.LogWarning("Saved type " + packedPart.PartTypeName + " is missing. Skipped from loading.");
                    continue;
                }

                var part = ent.Get(type);

                var packedFields = packedPart.Fields;

                for (var w = 0; w < packedFields.Length; w++)
                {
                    var packedField = packedFields[w];
                    var field       = type.GetField(packedField.FieldName);

                    var realFieldType = field.FieldType;

                    // works, but looks dangerouns. If Convert.ChangeType fails, all load fails.
                    try
                    {
                        object unpackedValue;

                        if (packedField.FieldValue is JObject jObj)
                        {
                            unpackedValue = jObj.ToObject(realFieldType);
                        }
                        else if (packedField.FieldValue is JArray jArr)
                        {
                            unpackedValue = jArr.ToObject(realFieldType);
                        }
                        else
                        {
                            unpackedValue = Convert.ChangeType(packedField.FieldValue, realFieldType);
                        }

                        field.SetValue(part, unpackedValue);
                    }
                    catch (Exception ex)
                    {
                        Debug.LogError(ex.ToString());
                    }
                }
            }

            return(ent);
        }