protected override void GetDepsFromImpl(object obj, GetDepsFromContext context)
        {
            base.GetDepsFromImpl(obj, context);
            if (obj == null)
            {
                return;
            }

            object persistentCalls = m_persistentCallGroupInfo.GetValue(obj);

            if (persistentCalls == null)
            {
                return;
            }

            object calls = m_callsInfo.GetValue(persistentCalls);

            if (calls == null)
            {
                return;
            }

            IList list = (IList)calls;

            for (int i = 0; i < list.Count; ++i)
            {
                object call = list[i];
                PersistentPersistentCall persistentCall = new PersistentPersistentCall();
                persistentCall.GetDepsFrom(call, context);
            }
        }
예제 #2
0
        private static void CreateAssetLibraryForScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs)
        {
            TypeMap           typeMap    = new TypeMap();
            AssetDB           assetDB    = new AssetDB();
            RuntimeShaderUtil shaderUtil = new RuntimeShaderUtil();

            IOC.Register <ITypeMap>(typeMap);
            IOC.Register <IAssetDB>(assetDB);
            IOC.Register <IRuntimeShaderUtil>(shaderUtil);

            PersistentRuntimeScene rtScene = new PersistentRuntimeScene();

            GetDepsFromContext ctx = new GetDepsFromContext();

            rtScene.GetDepsFrom(scene, ctx);

            Queue <UnityObject> depsQueue  = new Queue <UnityObject>(ctx.Dependencies.OfType <UnityObject>());
            GetDepsFromContext  getDepsCtx = new GetDepsFromContext();

            while (depsQueue.Count > 0)
            {
                UnityObject uo = depsQueue.Dequeue();
                if (!uo)
                {
                    continue;
                }

                Type persistentType = typeMap.ToPersistentType(uo.GetType());
                if (persistentType != null)
                {
                    getDepsCtx.Clear();

                    try
                    {
                        PersistentObject persistentObject = (PersistentObject)Activator.CreateInstance(persistentType);
                        //persistentObject.ReadFrom(uo);
                        persistentObject.GetDepsFrom(uo, getDepsCtx);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                    }

                    foreach (UnityObject dep in getDepsCtx.Dependencies)
                    {
                        if (!ctx.Dependencies.Contains(dep))
                        {
                            ctx.Dependencies.Add(dep);
                            depsQueue.Enqueue(dep);
                        }
                    }
                }
            }

            IOC.Unregister <IRuntimeShaderUtil>(shaderUtil);
            IOC.Unregister <ITypeMap>(typeMap);
            IOC.Unregister <IAssetDB>(assetDB);

            CreateAssetLibrary(ctx.Dependencies.ToArray(), "Scenes/" + scene.name, "SceneAssetLibrary", index, asset, folder, hs);
        }
예제 #3
0
        private static void SaveDependencies(GetDepsFromContext context, ITypeMap typeMap, IAssetDB assetDB, string path)
        {
            object[] dependencies = context.Dependencies.ToArray();
            foreach (UnityObject dep in dependencies)
            {
                Type persistentType = typeMap.ToPersistentType(dep.GetType());
                if (persistentType != null)
                {
                    context.Dependencies.Clear();

                    IPersistentSurrogate persistentObject = (IPersistentSurrogate)Activator.CreateInstance(persistentType);
                    persistentObject.GetDepsFrom(dep, context);

                    SaveDependencies(context, typeMap, assetDB, path);
                }
            }

            foreach (UnityObject dep in dependencies)
            {
                if (dep is Component || dep is GameObject || !assetDB.IsDynamicResourceID(assetDB.ToID(dep)))
                {
                    continue;
                }

                string name = dep.name;
                if (string.IsNullOrWhiteSpace(name))
                {
                    name = dep.GetType().Name;
                }

                string uniqueAssetPath = AssetDatabase.GenerateUniqueAssetPath(string.Format("{0}/{1}.asset", path, name));
                AssetDatabase.CreateAsset(dep, uniqueAssetPath);
            }
        }
 protected void AddDep(object obj, GetDepsFromContext context)
 {
     if (obj != null && !context.Dependencies.Contains(obj))
     {
         context.Dependencies.Add(obj);
     }
 }
 public virtual void GetDepsFrom(object obj, GetDepsFromContext context)
 {
     if (context.VisitedObjects.Contains(obj))
     {
         return;
     }
     context.VisitedObjects.Add(obj);
     GetDepsFromImpl(obj, context);
 }
 protected void AddDep <T>(T[] dependencies, GetDepsFromContext context)
 {
     if (dependencies == null)
     {
         return;
     }
     for (int i = 0; i < dependencies.Length; ++i)
     {
         AddDep(dependencies[i], context);
     }
 }
 protected void AddDep <T>(List <T> dependencies, GetDepsFromContext context)
 {
     if (dependencies == null)
     {
         return;
     }
     for (int i = 0; i < dependencies.Count; ++i)
     {
         AddDep(dependencies[i], context);
     }
 }
 protected void AddDep <T>(HashSet <T> dependencies, GetDepsFromContext context)
 {
     if (dependencies == null)
     {
         return;
     }
     foreach (T dep in dependencies)
     {
         AddDep(dep, context);
     }
 }
        protected override void GetDepsFromImpl(object obj, GetDepsFromContext context)
        {
            base.GetDepsFromImpl(obj, context);
            if (obj == null)
            {
                return;
            }

            Initialize(obj.GetType());
            UnityObject uobjArgument = (UnityObject)m_objectArgumentFieldInfo.GetValue(obj);

            AddDep(uobjArgument, context);
        }
        private void GetDependenciesFrom(GameObject go, List <object> prefabParts, GetDepsFromContext context)
        {
            if (go.GetComponent <RTSLIgnore>())
            {
                //Do not save persistent ignore objects
                return;
            }

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

            if (persistentType == null)
            {
                return;
            }

            prefabParts.Add(go);

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

            goData.GetDepsFrom(go, context);

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

                    prefabParts.Add(component);

                    PersistentObject componentData = (PersistentObject)Activator.CreateInstance(persistentComponentType);
                    componentData.GetDepsFrom(component, context);
                }
            }

            Transform transform = go.transform;

            if (transform.childCount > 0)
            {
                foreach (Transform child in transform)
                {
                    GetDependenciesFrom(child.gameObject, prefabParts, context);
                }
            }
        }
 protected void AddDep <T, V>(Dictionary <T, V> dependencies, GetDepsFromContext context)
 {
     if (dependencies == null)
     {
         return;
     }
     foreach (KeyValuePair <T, V> kvp in dependencies)
     {
         AddDep(kvp.Key, context);
         if (kvp.Value != null)
         {
             AddDep(kvp.Value, context);
         }
     }
 }
        protected override void GetDepsFromImpl(object obj, GetDepsFromContext context)
        {
            if (!(obj is Scene))
            {
                return;
            }

            Scene scene = (Scene)obj;

            GameObject[] gameObjects = scene.GetRootGameObjects();

            for (int i = 0; i < gameObjects.Length; ++i)
            {
                base.GetDepsFromImpl(gameObjects[i], context);
            }
        }
        protected override void GetDepsFromImpl(object obj, GetDepsFromContext context)
        {
            base.GetDepsFromImpl(obj, context);
            if (!(obj is GameObject))
            {
                return;
            }

            //Prefab parts should not be considered as external dependencies. This list required to remove prefab parts (children and components) from dependencies collection.
            List <object> prefabParts = new List <object>();

            GetDependenciesFrom((GameObject)obj, prefabParts, context);

            for (int i = 0; i < prefabParts.Count; ++i)
            {
                context.Dependencies.Remove(prefabParts[i]);
            }
        }
        protected override void ReadFromImpl(object obj)
        {
            base.ReadFromImpl(obj);
            GameObject go = (GameObject)obj;

            List <PersistentObject> data        = new List <PersistentObject>();
            List <long>             identifiers = new List <long>();
            //HashSet<int> usings = new HashSet<int>();
            GetDepsFromContext getDepsCtx = new GetDepsFromContext();

            Descriptors    = new PersistentDescriptor[1];
            Descriptors[0] = CreateDescriptorAndData(go, data, identifiers, /*usings,*/ getDepsCtx);

            Identifiers  = identifiers.ToArray();
            Data         = data.ToArray();
            Dependencies = getDepsCtx.Dependencies.OfType <UnityObject>().Select(d => m_assetDB.ToID(d)).ToArray();
            // Usings = usings.ToArray();
        }
        protected override void GetDepsFromImpl(object obj, GetDepsFromContext context)
        {
            base.GetDepsFromImpl(obj, context);
            if (obj == null)
            {
                return;
            }

            Initialize(obj.GetType());

            PersistentArgumentCache args = new PersistentArgumentCache();

            args.GetDepsFrom(m_argumentsFieldInfo.GetValue(obj), context);

            UnityObject target = (UnityObject)m_targetFieldInfo.GetValue(obj);

            AddDep(target, context);
        }
예제 #16
0
        private static void CreateAssetLibraryForScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs)
        {
            TypeMap typeMap = new TypeMap();
            AssetDB assetDB = new AssetDB();

            IOC.Register <ITypeMap>(typeMap);
            IOC.Register <IAssetDB>(assetDB);

            PersistentRuntimeScene rtScene = new PersistentRuntimeScene();

            GetDepsFromContext ctx = new GetDepsFromContext();

            rtScene.GetDepsFrom(scene, ctx);

            IOC.Unregister <ITypeMap>(typeMap);
            IOC.Unregister <IAssetDB>(assetDB);


            CreateAssetLibrary(ctx.Dependencies.ToArray(), "Scenes/" + scene.name, "SceneAssetLibrary", index, asset, folder, hs);
        }
        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();
        }
예제 #18
0
        public static void ConvertToAsset()
        {
            const string ext = ";*.rtscene;*.rtprefab;*.rtmat;*.rttex;*.rtmesh;";

            if (Application.isPlaying)
            {
                EditorUtility.DisplayDialog("Unable to load runtime asset", "Unable to load runtime asset in play mode", "OK");
                return;
            }

            string path = EditorUtility.OpenFilePanel("Select Runtime Asset", Application.persistentDataPath, ext);

            if (path.Length != 0)
            {
                GameObject         projGo = new GameObject();
                IAssetBundleLoader bundleLoader;
#if USE_GOOGLE_DRIVE
                if (File.Exists(Application.streamingAssetsPath + "/credentials.json"))
                {
                    bundleLoader = new GoogleDriveAssetBundleLoader();
                }
                else
#endif
                {
                    bundleLoader = new AssetBundleLoader();
                }

                IOC.Register(bundleLoader);

                ITypeMap typeMap = new TypeMap <long>();
                IOC.Register(typeMap);

                IUnityObjectFactory objFactory = new UnityObjectFactory();
                IOC.Register(objFactory);

                ISerializer serializer = new ProtobufSerializer();
                IOC.Register(serializer);

                IStorage <long> storage = new FileSystemStorage <long>();
                IOC.Register(storage);

                IRuntimeShaderUtil shaderUtil = new RuntimeShaderUtil();
                IOC.Register(shaderUtil);

                IMaterialUtil materialUtil = new StandardMaterialUtils();
                IOC.Register(materialUtil);

                IAssetDB assetDB = new AssetDB();
                IOC.Register <IIDMap>(assetDB);
                IOC.Register(assetDB);

                Project project = projGo.AddComponent <Project>();
                project.Awake_Internal();

                DirectoryInfo root     = new DirectoryInfo(Application.persistentDataPath);
                string        rootPath = root.ToString().ToLower();

                DirectoryInfo parent = Directory.GetParent(path);
                while (true)
                {
                    if (parent == null)
                    {
                        EditorUtility.DisplayDialog("Unable to load runtime asset", "Project.rtmeta was not found", "OK");
                        UnityObject.DestroyImmediate(projGo);
                        IOC.ClearAll();
                        return;
                    }

                    string projectPath = parent.FullName.ToLower();
                    if (rootPath == projectPath)
                    {
                        EditorUtility.DisplayDialog("Unable to load runtime asset", "Project.rtmeta was not found", "OK");
                        UnityObject.DestroyImmediate(projGo);
                        IOC.ClearAll();
                        return;
                    }

                    string projectFile = Path.Combine(projectPath, "Project.rtmeta");
                    if (File.Exists(projectFile))
                    {
                        storage.RootPath = Path.GetDirectoryName(projectPath).Replace('\\', '/') + "/";

                        string projectName = Path.GetFileNameWithoutExtension(projectPath);
                        project.OpenProject(projectName, (error, result) =>
                        {
                            if (error.HasError)
                            {
                                EditorUtility.DisplayDialog("Unable to load runtime asset", "Project " + projectName + " can not be loaded", "OK");
                                UnityObject.DestroyImmediate(projGo);
                                IOC.ClearAll();
                                return;
                            }

                            string relativePath = GetRelativePath(path, projectPath);
                            relativePath        = relativePath.Replace('\\', '/');
                            AssetItem assetItem = (AssetItem)project.Root.Get(relativePath);

                            project.Load(new[] { assetItem }, (loadError, loadedObjects) =>
                            {
                                if (loadError.HasError)
                                {
                                    EditorUtility.DisplayDialog("Unable to load runtime asset", loadError.ToString(), "OK");
                                }
                                else
                                {
                                    if (!project.IsScene(assetItem))
                                    {
                                        foreach (UnityObject asset in assetDB.GetDynamicResources())
                                        {
                                            asset.hideFlags = HideFlags.None;
                                        }

                                        UnityObject loadedObj = loadedObjects[0];
                                        if (loadedObj == null)
                                        {
                                            EditorUtility.DisplayDialog("Unable to load runtime asset", assetItem.Name, "OK");
                                        }
                                        else
                                        {
                                            GameObject loadedGo = loadedObj as GameObject;
                                            if (loadedGo != null)
                                            {
                                                string savePath = EditorUtility.SaveFilePanelInProject("Save " + loadedGo.name, loadedGo.name, "prefab", "Save prefab");

                                                if (!string.IsNullOrWhiteSpace(savePath))
                                                {
                                                    PersistentRuntimePrefab <long> runtimePrefab = new PersistentRuntimePrefab <long>();

                                                    GetDepsFromContext ctx = new GetDepsFromContext();
                                                    runtimePrefab.GetDepsFrom(loadedGo, ctx);

                                                    SaveDependencies(ctx, typeMap, assetDB, Path.GetDirectoryName(savePath));
                                                    PrefabUtility.SaveAsPrefabAsset(loadedGo, savePath);
                                                }
                                            }
                                            else
                                            {
                                                string savePath = EditorUtility.SaveFilePanelInProject("Save " + loadedObj.name, loadedObj.name, "asset", "Save asset");
                                                if (!string.IsNullOrWhiteSpace(savePath))
                                                {
                                                    AssetDatabase.CreateAsset(loadedObj, savePath);
                                                }
                                            }
                                        }
                                    }
                                }

                                IOC.ClearAll();
                                UnityObject.DestroyImmediate(projGo);
                            });
                        });

                        return;
                    }

                    parent = parent.Parent;
                }
            }
        }
        protected PersistentDescriptor CreateDescriptorAndData(GameObject go, List <PersistentObject> persistentData, List <long> persistentIdentifiers, /*HashSet<int> usings,*/ GetDepsFromContext getDepsFromCtx, PersistentDescriptor parentDescriptor = null)
        {
            if (go.GetComponent <RTSLIgnore>())
            {
                //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.GetDepsFrom(go, getDepsFromCtx);
            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.GetDepsFrom(component, getDepsFromCtx);
                    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,*/ getDepsFromCtx, descriptor);
                    if (childDescriptor != null)
                    {
                        children.Add(childDescriptor);
                    }
                }

                descriptor.Children = children.ToArray();
            }

            return(descriptor);
        }
 protected virtual void GetDepsFromImpl(object obj, GetDepsFromContext context)
 {
 }
        protected void AddSurrogateDeps <T, V, T1, V1>(Dictionary <T, V> dict, Func <T, T1> convertKey, Func <V, V1> convertValue, GetDepsFromContext context)
        {
            if (dict == null)
            {
                return;
            }
            foreach (KeyValuePair <T, V> kvp in dict)
            {
                T obj = kvp.Key;

                PersistentSurrogate surrogate = convertKey(obj) as PersistentSurrogate;
                if (surrogate != null)
                {
                    surrogate.GetDepsFrom(obj, context);
                }

                surrogate = convertValue(kvp.Value) as PersistentSurrogate;
                if (surrogate != null)
                {
                    surrogate.GetDepsFrom(obj, context);
                }
            }
        }
 protected void AddSurrogateDeps <T>(HashSet <T> objHs, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (objHs == null)
     {
         return;
     }
     foreach (T obj in objHs)
     {
         if (obj != null)
         {
             PersistentSurrogate surrogate = convert(obj);
             surrogate.GetDepsFrom(obj, context);
         }
     }
 }
 protected void AddSurrogateDeps <T>(List <T> objList, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (objList == null)
     {
         return;
     }
     for (int i = 0; i < objList.Count; ++i)
     {
         T obj = objList[i];
         if (obj != null)
         {
             PersistentSurrogate surrogate = convert(obj);
             surrogate.GetDepsFrom(obj, context);
         }
     }
 }
 protected void AddSurrogateDeps <T>(T[] objArray, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (objArray == null)
     {
         return;
     }
     for (int i = 0; i < objArray.Length; ++i)
     {
         T obj = objArray[i];
         if (obj != null)
         {
             PersistentSurrogate surrogate = convert(obj);
             surrogate.GetDepsFrom(obj, context);
         }
     }
 }
 protected void AddSurrogateDeps <T>(T obj, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (obj != null)
     {
         PersistentSurrogate surrogate = convert(obj);
         surrogate.GetDepsFrom(obj, context);
     }
 }