Пример #1
0
        public void Load(string projectPath, string[] assetPaths, Type[] types, StorageEventHandler <PersistentObject[]> callback)
        {
            PersistentObject[] result = new PersistentObject[assetPaths.Length];
            for (int i = 0; i < assetPaths.Length; ++i)
            {
                string assetPath = assetPaths[i];
                assetPath = Path.Combine(FullPath(projectPath), assetPath);
                ISerializer serializer = RTSL2Deps.Get.Serializer;
                try
                {
                    if (File.Exists(assetPath))
                    {
                        using (FileStream fs = File.OpenRead(assetPath))
                        {
                            result[i] = (PersistentObject)serializer.Deserialize(fs, types[i]);
                        }
                    }
                    else
                    {
                        callback(new Error(Error.E_NotFound), new PersistentObject[0]);
                        return;
                    }
                }
                catch (Exception e)
                {
                    Debug.LogErrorFormat("Unable to load asset: {0} -> got exception: {1} ", assetPath, e.ToString());
                    callback(new Error(Error.E_Exception)
                    {
                        ErrorText = e.ToString()
                    }, new PersistentObject[0]);
                    return;
                }
            }

            callback(new Error(Error.OK), result);
        }
Пример #2
0
        protected PersistentDescriptor CreateDescriptorAndData(GameObject go, List <PersistentObject> persistentData, List <long> persistentIdentifiers, HashSet <int> usings, GetDepsContext getDepsCtx, PersistentDescriptor parentDescriptor = null)
        {
            if (go.GetComponent <RTSL2Ignore>())
            {
                //Do not save persistent ignore objects
                return(null);
            }
            Type persistentType = m_typeMap.ToPersistentType(go.GetType());

            if (persistentType == null)
            {
                return(null);
            }

            long persistentID = ToID(go);

            if (m_assetDB.IsResourceID(persistentID))
            {
                int ordinal = m_assetDB.ToOrdinal(persistentID);
                usings.Add(ordinal);
            }

            PersistentDescriptor descriptor = new PersistentDescriptor(m_typeMap.ToGuid(persistentType), persistentID, go.name);

            descriptor.Parent = parentDescriptor;

            PersistentObject goData = (PersistentObject)Activator.CreateInstance(persistentType);

            goData.ReadFrom(go);
            goData.GetDeps(getDepsCtx);
            persistentData.Add(goData);
            persistentIdentifiers.Add(persistentID);

            Component[] components = go.GetComponents <Component>().Where(c => c != null).ToArray();
            if (components.Length > 0)
            {
                List <PersistentDescriptor> componentDescriptors = new List <PersistentDescriptor>();
                for (int i = 0; i < components.Length; ++i)
                {
                    Component component = components[i];
                    Type      persistentComponentType = m_typeMap.ToPersistentType(component.GetType());
                    if (persistentComponentType == null)
                    {
                        continue;
                    }

                    long componentID = ToID(component);
                    if (m_assetDB.IsResourceID(componentID))
                    {
                        int ordinal = m_assetDB.ToOrdinal(componentID);
                        usings.Add(ordinal);
                    }
                    PersistentDescriptor componentDescriptor = new PersistentDescriptor(m_typeMap.ToGuid(persistentComponentType), componentID, component.name);
                    componentDescriptor.Parent = descriptor;
                    componentDescriptors.Add(componentDescriptor);

                    PersistentObject componentData = (PersistentObject)Activator.CreateInstance(persistentComponentType);
                    componentData.ReadFrom(component);
                    componentData.GetDeps(getDepsCtx);
                    persistentData.Add(componentData);
                    persistentIdentifiers.Add(componentID);
                }

                if (componentDescriptors.Count > 0)
                {
                    descriptor.Components = componentDescriptors.ToArray();
                }
            }

            Transform transform = go.transform;

            if (transform.childCount > 0)
            {
                List <PersistentDescriptor> children = new List <PersistentDescriptor>();
                foreach (Transform child in transform)
                {
                    PersistentDescriptor childDescriptor = CreateDescriptorAndData(child.gameObject, persistentData, persistentIdentifiers, usings, getDepsCtx, descriptor);
                    if (childDescriptor != null)
                    {
                        children.Add(childDescriptor);
                    }
                }

                descriptor.Children = children.ToArray();
            }

            return(descriptor);
        }
Пример #3
0
        private void OnLoadCompleted(AssetItem rootItem, AssetItem[] assetItems, PersistentObject[] persistentObjects, ProjectEventHandler <UnityObject> callback)
        {
            for (int i = 0; i < assetItems.Length; ++i)
            {
                AssetItem assetItem = assetItems[i];
                if (!m_assetDB.IsMapped(assetItem.ItemID))
                {
                    if (m_assetDB.IsStaticResourceID(assetItem.ItemID))
                    {
                        int ordinal = m_assetDB.ToOrdinal(assetItem.ItemID);
                        if (m_assetDB.IsLibraryRefLoaded(ordinal))
                        {
                            m_assetDB.RemoveLibrary(ordinal);
                        }

                        if (!m_assetDB.IsLibraryLoaded(ordinal))
                        {
                            AssetLibraryReferenceInfo reference = m_projectInfo.References.FirstOrDefault(r => r.Ordinal == ordinal);
                            if (reference != null)
                            {
                                m_assetDB.LoadLibrary(reference.AssetLibrary, reference.Ordinal);
                            }
                        }
                    }
                    else if (m_assetDB.IsDynamicResourceID(assetItem.ItemID))
                    {
                        PersistentObject persistentObject = persistentObjects[i];
                        if (persistentObject != null)
                        {
                            if (persistentObject is PersistentPrefab)
                            {
                                PersistentPrefab persistentPrefab                = (PersistentPrefab)persistentObject;
                                Dictionary <int, UnityObject> idToObj            = new Dictionary <int, UnityObject>();
                                List <GameObject>             createdGameObjects = new List <GameObject>();
                                persistentPrefab.CreateGameObjectWithComponents(m_typeMap, persistentPrefab.Descriptors[0], idToObj, createdGameObjects);
                                m_assetDB.RegisterDynamicResources(idToObj);
                                for (int j = 0; j < createdGameObjects.Count; ++j)
                                {
                                    GameObject createdGO = createdGameObjects[i];
                                    createdGO.transform.SetParent(createdGO.transform, false);
                                    m_dynamicResources.Add(unchecked ((int)m_assetDB.ToID(createdGO)), createdGO);
                                }
                            }
                            else
                            {
                                Type        type     = m_typeMap.ToType(assetItem.TypeGuid);
                                UnityObject instance = m_factory.CreateInstance(type);
                                m_assetDB.RegisterDynamicResource(unchecked ((int)assetItem.ItemID), instance);
                                m_dynamicResources.Add(unchecked ((int)assetItem.ItemID), instance);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < persistentObjects.Length; ++i)
            {
                PersistentObject persistentObject = persistentObjects[i];
                if (persistentObject != null)
                {
                    if (m_assetDB.IsSceneID(assetItems[i].ItemID))
                    {
                        persistentObject.WriteTo(SceneManager.GetActiveScene());
                    }
                    else
                    {
                        UnityObject obj = m_assetDB.FromID <UnityObject>(assetItems[i].ItemID);
                        Debug.Assert(obj != null);
                        if (obj != null)
                        {
                            persistentObject.WriteTo(obj);
                        }
                    }
                }
            }

            UnityObject result = m_assetDB.FromID <UnityObject>(rootItem.ItemID);

            callback(new Error(Error.OK), result);
        }
Пример #4
0
        public void Save(ProjectItem parent, byte[] previewData, object obj, ProjectEventHandler <ProjectItem> callback)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type persistentType = m_typeMap.ToPersistentType(obj.GetType());

            if (persistentType == null)
            {
                throw new ArgumentException(string.Format("PersistentClass for {0} does not exist", obj.GetType()), "obj");
            }

            if (parent == null)
            {
                parent = Root;
            }

            if (!parent.IsFolder)
            {
                throw new ArgumentException("parent is not folder", "parent");
            }

            int ordinal, id;

            if (!GetOrdinalAndId(m_projectInfo.AssetIdentifier, out ordinal, out id))
            {
                return;
            }

            PersistentObject persistentObject = (PersistentObject)Activator.CreateInstance(persistentType);

            persistentObject.ReadFrom(obj);

            AssetItem assetItem = new AssetItem();

            assetItem.ItemID   = m_assetDB.ToRuntimeResourceID(ordinal, id);
            assetItem.Name     = persistentObject.name;
            assetItem.Ext      = GetExt(obj);
            assetItem.TypeGuid = m_typeMap.ToGuid(obj.GetType());
            assetItem.Preview  = new Preview
            {
                ItemID      = assetItem.ItemID,
                PreviewData = previewData
            };

            if (persistentObject is PersistentPrefab)
            {
                PersistentPrefab persistentPrefab = (PersistentPrefab)persistentObject;
                if (persistentPrefab.Descriptors != null)
                {
                    List <PrefabPart> prefabParts = new List <PrefabPart>();
                    PersistentDescriptorsToPrefabPartItems(persistentPrefab.Descriptors, prefabParts);
                    assetItem.Parts = prefabParts.ToArray();
                }
            }

            GetDepsContext getDepsCtx = new GetDepsContext();

            persistentObject.GetDeps(getDepsCtx);
            assetItem.Dependencies = getDepsCtx.Dependencies.ToArray();

            m_storage.Save(m_projectPath, parent.ToString(), assetItem, persistentObject, m_projectInfo, error =>
            {
                if (!error.HasError)
                {
                    m_idToAssetItem.Add(assetItem.ItemID, assetItem);
                    if (assetItem.Parts != null)
                    {
                        for (int i = 0; i < assetItem.Parts.Length; ++i)
                        {
                            m_idToAssetItem.Add(assetItem.Parts[i].PartID, assetItem);
                        }
                    }

                    parent.AddChild(assetItem);
                }

                callback(error, assetItem);
            });
        }
Пример #5
0
        public void Save(string projectPath, string[] folderPaths, AssetItem[] assetItems, PersistentObject[] persistentObjects, ProjectInfo projectInfo, StorageEventHandler callback)
        {
            if (assetItems.Length != persistentObjects.Length || persistentObjects.Length != folderPaths.Length)
            {
                throw new ArgumentException("assetItems");
            }

            projectPath = FullPath(projectPath);
            string      projectInfoPath = projectPath + "/Project.rtmeta";
            ISerializer serializer      = IOC.Resolve <ISerializer>();
            Error       error           = new Error(Error.OK);

            for (int i = 0; i < assetItems.Length; ++i)
            {
                string           folderPath       = folderPaths[i];
                AssetItem        assetItem        = assetItems[i];
                PersistentObject persistentObject = persistentObjects[i];
                try
                {
                    string path = projectPath + folderPath;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    string previewPath = path + "/" + assetItem.NameExt + PreviewExt;
                    if (assetItem.Preview == null)
                    {
                        File.Delete(previewPath);
                    }
                    else
                    {
                        using (FileStream fs = File.OpenWrite(previewPath))
                        {
                            serializer.Serialize(assetItem.Preview, fs);
                        }
                    }

                    using (FileStream fs = File.OpenWrite(path + "/" + assetItem.NameExt + MetaExt))
                    {
                        serializer.Serialize(assetItem, fs);
                    }
                    using (FileStream fs = File.OpenWrite(path + "/" + assetItem.NameExt))
                    {
                        serializer.Serialize(persistentObject, fs);
                    }
                    using (FileStream fs = File.OpenWrite(projectInfoPath))
                    {
                        serializer.Serialize(projectInfo, fs);
                    }
                }
                catch (Exception e)
                {
                    Debug.LogErrorFormat("Unable to create asset: {0} -> got exception: {1} ", assetItem.NameExt, e.ToString());
                    error.ErrorCode = Error.E_Exception;
                    error.ErrorText = e.ToString();
                    break;
                }
            }

            callback(error);
        }
Пример #6
0
        public ProjectAsyncOperation <AssetItem> Save(ProjectItem parent, byte[] previewData, object obj, string nameOverride, ProjectEventHandler <AssetItem> callback)
        {
            if (m_root == null)
            {
                throw new InvalidOperationException("Project is not opened. Use OpenProject method");
            }

            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type objType        = obj.GetType();
            Type persistentType = m_typeMap.ToPersistentType(objType);

            if (persistentType == null)
            {
                throw new ArgumentException(string.Format("PersistentClass for {0} does not exist", obj.GetType()), "obj");
            }

            ProjectAsyncOperation <AssetItem> ao = new ProjectAsyncOperation <AssetItem>();

            if (persistentType == typeof(PersistentGameObject))
            {
                persistentType = typeof(PersistentRuntimePrefab);
            }

            if (parent == null)
            {
                parent = Root;
            }

            if (!parent.IsFolder)
            {
                throw new ArgumentException("parent is not folder", "parent");
            }

            int assetIdBackup = m_projectInfo.AssetIdentifier;
            int rootOrdinal;
            int rootId;

            if (!GetOrdinalAndId(ref m_projectInfo.AssetIdentifier, out rootOrdinal, out rootId))
            {
                OnExhausted(callback, ao, assetIdBackup);
                return(ao);
            }

            if (obj is GameObject)
            {
                Dictionary <int, UnityObject> idToObj = new Dictionary <int, UnityObject>();
                GameObject go = (GameObject)obj;
                idToObj.Add(unchecked ((int)m_assetDB.ToDynamicResourceID(rootOrdinal, rootId)), go);

                Transform[] transforms = go.GetComponentsInChildren <Transform>(true);
                for (int i = 0; i < transforms.Length; ++i)
                {
                    Transform tf = transforms[i];
                    if (tf.gameObject != go)
                    {
                        int ordinal;
                        int id;
                        if (!GetOrdinalAndId(ref m_projectInfo.AssetIdentifier, out ordinal, out id))
                        {
                            OnExhausted(callback, ao, assetIdBackup);
                            return(ao);
                        }
                        idToObj.Add(unchecked ((int)m_assetDB.ToDynamicResourceID(ordinal, id)), tf.gameObject);
                    }

                    Component[] components = tf.GetComponents <Component>();
                    for (int j = 0; j < components.Length; ++j)
                    {
                        Component comp = components[j];
                        int       ordinal;
                        int       id;
                        if (!GetOrdinalAndId(ref m_projectInfo.AssetIdentifier, out ordinal, out id))
                        {
                            OnExhausted(callback, ao, assetIdBackup);
                            return(ao);
                        }
                        idToObj.Add(unchecked ((int)m_assetDB.ToDynamicResourceID(ordinal, id)), comp);
                    }
                }

                m_assetDB.RegisterDynamicResources(idToObj);
            }
            else if (obj is UnityObject)
            {
                m_assetDB.RegisterDynamicResource((int)m_assetDB.ToDynamicResourceID(rootOrdinal, rootId), (UnityObject)obj);
            }

            PersistentObject persistentObject = (PersistentObject)Activator.CreateInstance(persistentType);

            persistentObject.ReadFrom(obj);

            if (!string.IsNullOrEmpty(nameOverride))
            {
                persistentObject.name = nameOverride;
            }

            AssetItem assetItem = new AssetItem();

            if (obj is Scene)
            {
                assetItem.ItemID = m_assetDB.ToSceneID(rootOrdinal, rootId);
            }
            else
            {
                assetItem.ItemID = m_assetDB.ToDynamicResourceID(rootOrdinal, rootId);
            }

            assetItem.Name     = persistentObject.name;
            assetItem.Ext      = GetExt(obj);
            assetItem.TypeGuid = m_typeMap.ToGuid(obj.GetType());
            assetItem.Preview  = new Preview
            {
                ItemID      = assetItem.ItemID,
                PreviewData = previewData
            };

            if (persistentObject is PersistentRuntimePrefab)
            {
                PersistentRuntimePrefab persistentPrefab = (PersistentRuntimePrefab)persistentObject;
                if (persistentPrefab.Descriptors != null)
                {
                    List <PrefabPart> prefabParts = new List <PrefabPart>();
                    PersistentDescriptorsToPrefabPartItems(persistentPrefab.Descriptors, prefabParts);
                    assetItem.Parts = prefabParts.ToArray();
                }
            }

            GetDepsContext getDepsCtx = new GetDepsContext();

            persistentObject.GetDeps(getDepsCtx);
            assetItem.Dependencies = getDepsCtx.Dependencies.ToArray();

            m_storage.Save(m_projectPath, parent.ToString(), assetItem, persistentObject, m_projectInfo, error =>
            {
                if (!error.HasError)
                {
                    if (!(obj is Scene))
                    {
                        if (assetItem.Parts != null)
                        {
                            for (int i = 0; i < assetItem.Parts.Length; ++i)
                            {
                                m_idToAssetItem.Add(assetItem.Parts[i].PartID, assetItem);
                            }
                        }
                        else
                        {
                            m_idToAssetItem.Add(assetItem.ItemID, assetItem);
                        }
                    }

                    parent.AddChild(assetItem);
                }

                if (callback != null)
                {
                    callback(error, assetItem);
                }
                ao.Error       = error;
                ao.Result      = assetItem;
                ao.IsCompleted = true;
            });

            return(ao);
        }
Пример #7
0
        protected override object WriteToImpl(object obj)
        {
            if (Descriptors == null && Data == null)
            {
                return(obj);
            }

            if (Descriptors == null && Data != null || Data != null && Descriptors == null)
            {
                throw new ArgumentException("data is corrupted", "scene");
            }

            if (Descriptors.Length == 0)
            {
                return(obj);
            }

            if (Identifiers == null || Identifiers.Length != Data.Length)
            {
                throw new ArgumentException("data is corrupted", "scene");
            }

            Scene scene = (Scene)obj;

            GameObject[] rootGameObjects = scene.GetRootGameObjects();
            for (int i = 0; i < rootGameObjects.Length; ++i)
            {
                GameObject rootGO = rootGameObjects[i];
                if (rootGO.GetComponent <RTSL2Ignore>())
                {
                    continue;
                }

                UnityObject.Destroy(rootGO);
            }

            Dictionary <int, UnityObject> idToUnityObj = new Dictionary <int, UnityObject>();

            for (int i = 0; i < Descriptors.Length; ++i)
            {
                PersistentDescriptor descriptor = Descriptors[i];
                if (descriptor != null)
                {
                    CreateGameObjectWithComponents(m_typeMap, descriptor, idToUnityObj);
                }
            }

            UnityObject[] assetInstances = null;
            if (AssetIdentifiers != null)
            {
                IUnityObjectFactory factory = IOC.Resolve <IUnityObjectFactory>();
                assetInstances = new UnityObject[AssetIdentifiers.Length];
                for (int i = 0; i < AssetIdentifiers.Length; ++i)
                {
                    PersistentObject asset = Assets[i];

                    Type uoType = m_typeMap.ToUnityType(asset.GetType());
                    if (uoType != null)
                    {
                        UnityObject assetInstance = factory.CreateInstance(uoType);
                        if (assetInstance != null)
                        {
                            assetInstances[i] = assetInstance;
                            idToUnityObj.Add(AssetIdentifiers[i], assetInstance);
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Unable to resolve unity type for " + asset.GetType().FullName);
                    }
                }
            }

            m_assetDB.RegisterSceneObjects(idToUnityObj);

            if (assetInstances != null)
            {
                for (int i = 0; i < AssetIdentifiers.Length; ++i)
                {
                    UnityObject assetInstance = assetInstances[i];
                    if (assetInstance != null)
                    {
                        PersistentObject asset = Assets[i];
                        asset.WriteTo(assetInstance);
                    }
                }
            }

            RestoreDataAndResolveDependencies();
            m_assetDB.UnregisterSceneObjects();

            return(scene);
        }
Пример #8
0
        protected override void ReadFromImpl(object obj)
        {
            Scene scene = (Scene)obj;

            GameObject[] rootGameObjects = scene.GetRootGameObjects();

            List <PersistentObject>     data            = new List <PersistentObject>();
            List <long>                 identifiers     = new List <long>();
            List <PersistentDescriptor> descriptors     = new List <PersistentDescriptor>(rootGameObjects.Length);
            GetDepsFromContext          getSceneDepsCtx = new GetDepsFromContext();

            for (int i = 0; i < rootGameObjects.Length; ++i)
            {
                GameObject           rootGO     = rootGameObjects[i];
                PersistentDescriptor descriptor = CreateDescriptorAndData(rootGO, data, identifiers, getSceneDepsCtx);
                if (descriptor != null)
                {
                    descriptors.Add(descriptor);
                }
            }

            HashSet <object>    allDeps      = getSceneDepsCtx.Dependencies;
            List <UnityObject>  externalDeps = new List <UnityObject>(allDeps.OfType <UnityObject>());
            Queue <UnityObject> depsQueue    = new Queue <UnityObject>(allDeps.OfType <UnityObject>());

            List <PersistentObject> assets = new List <PersistentObject>();
            List <int> assetIdentifiers    = new List <int>();

            GetDepsFromContext getDepsCtx = new GetDepsFromContext();

            while (depsQueue.Count > 0)
            {
                UnityObject uo = depsQueue.Dequeue();
                if (!uo)
                {
                    continue;
                }
                if (!m_assetDB.IsMapped(uo))
                {
                    if (!(uo is GameObject) && !(uo is Component))
                    {
                        Type persistentType = m_typeMap.ToPersistentType(uo.GetType());
                        if (persistentType != null)
                        {
                            getDepsCtx.Clear();

                            PersistentObject persistentObject = (PersistentObject)Activator.CreateInstance(persistentType);
                            persistentObject.ReadFrom(uo);
                            persistentObject.GetDepsFrom(uo, getDepsCtx);

                            assets.Add(persistentObject);
                            assetIdentifiers.Add(uo.GetInstanceID());

                            foreach (UnityObject dep in getDepsCtx.Dependencies)
                            {
                                if (!allDeps.Contains(dep))
                                {
                                    allDeps.Add(dep);
                                    depsQueue.Enqueue(dep);
                                }
                            }
                        }
                    }
                    externalDeps.Remove(uo);
                }
            }

            Descriptors  = descriptors.ToArray();
            Identifiers  = identifiers.ToArray();
            Data         = data.ToArray();
            Dependencies = externalDeps.Select(uo => m_assetDB.ToID(uo)).ToArray();

            Assets           = assets.ToArray();
            AssetIdentifiers = assetIdentifiers.ToArray();
        }