Example #1
0
        static void assign_ids_components()
        {
            var allData = Resources.LoadAll(SaveSystemSettings.Current.EntitiesFolder);

            prefabObjects = new List <GameObject>(1000);
            foreach (var item in allData)
            {
                GameObject go = item as GameObject;
                if (go == null)
                {
                    continue;
                }

                var entity = go.GetComponent <SaveEntity>();
                if (!entity)
                {
                    Debug.LogError("Database: GameObject without entity found, skipping.", go);
                    continue;
                }

                prefabObjects.Add(go);
                entities.Add(entity.entityID, entity);

                SavedComponent[] components      = go.GetComponentsInChildren <SavedComponent>();
                HashSet <int>    comps_in_entity = null;
                Components.TryGetValue(entity.entityID, out comps_in_entity);

                if (comps_in_entity == null)
                {
                    comps_in_entity = new HashSet <int>();
                }

                foreach (var c in components)
                {
                    if (c.componentID == 0)
                    {
                        c.componentID = SaveSystemUtilities.GetUniqueID(comps_in_entity);
                    }
                    comps_in_entity.Add(c.componentID);
                }
            }
        }
Example #2
0
        static void ProcessEntity(SaveObject file, SaveEntity ent, HashSet <int> bp_ids, Transform root)
        {
            EntityObject eobj        = new EntityObject();
            SaveEntity   entity      = ent;
            bool         isBlueprint = bp_ids != null;

            file.isBlueprint = isBlueprint;
            CompRefSerializationProcessor.blueprint = isBlueprint;
            if (isBlueprint && entity.blueprintID == 0)
            {
                entity.blueprintID = SaveSystemUtilities.GetUniqueID(bp_ids);
            }

            eobj.blueprint_ID = entity.blueprintID;
            Transform tr = entity.transform;

            eobj.database_ID = entity.entityID;
            eobj.instance_ID = entity.instanceID;
            eobj.prefabPath  = SaveEntityDatabase.GetPrefabPath(entity.entityID);
            eobj.Enabled     = ent.gameObject.activeSelf;

            if (isBlueprint)
            {
                eobj.position = root.InverseTransformPoint(tr.position);
                eobj.rotation = tr.localRotation.eulerAngles;
            }
            else
            {
                eobj.position = tr.position;
                eobj.rotation = tr.rotation.eulerAngles;
            }
            bool           hasParent  = tr.parent != null;
            SavedComponent parentComp = null;

            if (hasParent)
            {
                parentComp = tr.parent.GetComponent <SavedComponent>();
            }
            if (tr.parent != root && parentComp)
            {
                eobj.parentIsComponent = true;
                if (isBlueprint)
                {
                    eobj.parent_entity_ID = parentComp.Entity.blueprintID;
                }
                else
                {
                    eobj.parent_entity_ID = parentComp.Entity.ID;
                }
                eobj.parent_component_ID = parentComp.componentID;
            }
            else
            {
                SaveEntity parentEntity = null;
                if (hasParent)
                {
                    parentEntity = tr.parent.GetComponent <SaveEntity>();
                }
                if (tr.parent != root && parentEntity)
                {
                    eobj.parentIsEntity = true;
                    if (isBlueprint)
                    {
                        eobj.parent_entity_ID = parentEntity.blueprintID;
                    }
                    else
                    {
                        eobj.parent_entity_ID = parentEntity.ID;
                    }
                }
                else
                {
                    if (isBlueprint)
                    {
                        eobj.parentName = "null";
                    }
                    else
                    {
                        eobj.parentName = tr.parent == null ? "null" : tr.parent.name;
                    }
                }
            }
            eobj.gameObjectName = entity.name;

            file.entities.Add(eobj);

            getComponentsSwapList.Clear();

            entity.GetComponentsInChildren <SavedComponent>(true, getComponentsSwapList);

            foreach (var comp in getComponentsSwapList)
            {
                if (comp.componentID == 0)
                {
                    //Debug.Log("Skipping component without ID : " + comp.GetType(), entity.gameObject);
                    continue;
                }

                comp.SendMessage("OnBeforeSave", SendMessageOptions.DontRequireReceiver);

                ComponentObject cobj = new ComponentObject();

                cobj.component_ID = comp.componentID;
                cobj.data         = GetDataFromComponent(comp);
                cobj.initialized  = comp.Initialized;
                cobj.enabled      = comp.enabled;

                Dictionary <int, ComponentObject> entityComponents = null;
                if (isBlueprint)
                {
                    file.components.TryGetValue(entity.blueprintID, out entityComponents);
                }
                else
                {
                    file.components.TryGetValue(entity.instanceID, out entityComponents);
                }

                if (entityComponents == null)
                {
                    entityComponents = new Dictionary <int, ComponentObject>();
                    if (isBlueprint)
                    {
                        file.components.Add(entity.blueprintID, entityComponents);
                    }
                    else
                    {
                        file.components.Add(entity.instanceID, entityComponents);
                    }
                }

                if (entityComponents.ContainsKey(comp.componentID))
                {
                    Debug.LogError("Super fatal error with duplicate component id's on entity.", entity.gameObject);
                }
                entityComponents.Add(comp.componentID, cobj);
                if (cobj.data != null)
                {
                    Type t      = cobj.data.GetType();
                    var  fields = t.GetFields();
                    foreach (var f in fields)
                    {
                        if (f.FieldType == typeof(CompRef))
                        {
                            file.comprefs.Add(f.GetValue(cobj.data) as CompRef);
                        }
                    }
                }
            }
        }
Example #3
0
        static void assign_ids_entities()
        {
            HashSet <int> ids = new HashSet <int>();

            DatabaseManifest manifest  = new DatabaseManifest();
            string           resfolder = "Resources/" + SaveSystemSettings.Current.EntitiesFolder + "/";


            var prefabs = Resources.LoadAll("", typeof(GameObject));

            foreach (var p in prefabs)
            {
                var ent = ((GameObject)p).GetComponent <SaveEntity>();

                if (!ent)
                {
                    continue;
                }

                if (ent)
                {
                    Undo.RegisterCompleteObjectUndo(ent, "Assigning new ids");

                    ids.Add(ent.entityID);
                }

                EditorUtility.SetDirty(p);
            }

            var files = Directory.GetFiles(Application.dataPath, "*.prefab", SearchOption.AllDirectories);


            foreach (var f in files)
            {
                string pathfixed = f.Replace("\\", "/");

                if (!pathfixed.Contains(dbPath))
                {
                    continue;
                }

                int    index  = pathfixed.IndexOf("/Assets/");
                string adress = pathfixed.Remove(0, index + 1);

                //adress = adress;

                var prefab = AssetDatabase.LoadAssetAtPath(adress, typeof(GameObject)) as GameObject;

                // Resources.Load(URSASettings.Current.DatabaseRootFolder + "/" + adress) as GameObject;

                var entity = prefab.GetComponent <SaveEntity>();

                if (!entity)
                {
                    Debug.LogError("Database: GameObject without entity found, skipping.", prefab);
                    continue;
                }

                if (entity.entityID == 0)
                {
                    entity.entityID = SaveSystemUtilities.GetUniqueID(ids);
                }

                if (entity.instanceID != 0)
                {
                    entity.instanceID = 0;
                }

                if (entity.blueprintID != 0)
                {
                    entity.blueprintID = 0;
                }

                int    resIndex           = adress.IndexOf(resfolder);
                string entityResourcePath = adress.Replace(".prefab", string.Empty).Remove(0, resIndex + resfolder.Length);

                if (manifest.entity_adress_id.ContainsKey(entityResourcePath))
                {
                    Debug.LogError("Sadly duplicate Entity names are not allowed as they will no be uniquely identifiable on runtime.", entity);
                    continue;
                }

                if (manifest.entity_id_adress.ContainsKey(entity.entityID))
                {
                    Debug.LogError("Trying to add entity with the same id: " + entity.entityID, entity.gameObject);
                }
                else
                {
                    manifest.entity_id_adress.Add(entity.entityID, entityResourcePath);
                    manifest.entity_adress_id.Add(entityResourcePath, entity.entityID);
                }
            }

            SerializationHelper.Serialize(manifest, Application.dataPath + "/Resources/SaveSystem/" + SaveSystemSettings.Current.DatabaseManifest + ".json", true);
        }