Ejemplo n.º 1
0
        /// <summary>
        /// Write the prefab to a new file
        /// </summary>
        /// <param name="parsedPrefab"></param>
        /// <param name="currentPrefab"></param>
        /// <param name="destination"></param>
        public void SavePrefabFile(string[] parsedPrefab, PrefabModel currentPrefab, string destination)
        {
//            string newPrefabMetaPath = destination + @"\" + Path.GetFileName(currentPrefab.MetaPath);
            string newPrefabMetaPath = destination + currentPrefab.MetaPath.GetRelativeAssetPath();

            if (!Administration.Instance.OverWriteMode)
            {
                newPrefabMetaPath = ProjectPathUtility.AddTimestamp(newPrefabMetaPath);
            }

            if (!Directory.Exists(Path.GetDirectoryName(newPrefabMetaPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(newPrefabMetaPath));
            }

            // For when copying to the same project
            if (currentPrefab.MetaPath != newPrefabMetaPath)
            {
                File.Copy(currentPrefab.MetaPath, newPrefabMetaPath, true);
            }


            string newPrefabPath = newPrefabMetaPath.Substring(0, newPrefabMetaPath.Length - 5);

            if (!Directory.Exists(Path.GetDirectoryName(newPrefabPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(newPrefabPath));
            }


            File.WriteAllText(newPrefabPath,
                              string.Join("\r\n", parsedPrefab));
            Debug.Log("Migrated prefab to : " + newPrefabPath);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Change the fields after merging with the merging window
        /// </summary>
        /// <param name="originalScriptMappings"></param>
        /// <param name="rootPath"></param>
        /// <param name="scenePath"></param>
        /// <param name="linesToChange"></param>
        /// <param name="mergedScriptMappings"></param>
        private void MergingWizardCompleted(List <ScriptMapping> originalScriptMappings, string rootPath,
                                            string scenePath,
                                            string[] linesToChange,
                                            List <ScriptMapping> mergedScriptMappings = null)
        {
            if (mergedScriptMappings != null)
            {
                originalScriptMappings = originalScriptMappings.Merge(mergedScriptMappings);
            }

            ThreadUtility.RunTask(() =>
            {
                fieldMappingController.MigrateFields(scenePath, ref linesToChange, ref originalScriptMappings,
                                                     ProjectPathUtility.getProjectPathFromFile(scenePath), rootPath);

                string newScenePath = rootPath + scenePath.GetRelativeAssetPath();

                if (!Administration.Instance.OverWriteMode)
                {
                    newScenePath = ProjectPathUtility.AddTimestamp(newScenePath);
                }

                mappingView.SaveScriptMappings(rootPath, originalScriptMappings);
                SaveSceneFile(newScenePath, linesToChange);
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parse the prefab.
        /// Change the guid's and fileID's on scripts and port the fields
        /// </summary>
        /// <param name="prefabFile"></param>
        /// <param name="originalAssetPath"></param>
        /// <param name="destinationAssetPath"></param>
        /// <param name="prefabs"></param>
        /// <param name="prefabGuid"></param>
        /// <exception cref="FormatException"></exception>
        /// <exception cref="NullReferenceException"></exception>
        public List <ScriptMapping> MigratePrefab(string prefabFile, string originalAssetPath, string destinationAssetPath,
                                                  List <PrefabModel> prefabs,
                                                  string prefabGuid, List <ScriptMapping> scriptMappings)
        {
            try
            {
                if (!prefabFile.EndsWith(".prefab"))
                {
                    throw new FormatException("Could not parse prefab, not of type prefab, file : " + prefabFile);
                }

                Debug.Log("Started migration of prefab: " + prefabFile);
                if (BinaryUtility.IsBinaryFile(prefabFile))
                {
                    Debug.LogError("Could not parse file, since it's a binary file. Prefab file: " + prefabFile);
                    return(scriptMappings);
                }

                PrefabModel currentPrefab = prefabs.FirstOrDefault(prefab => prefab.Guid == prefabGuid);
                if (currentPrefab == null)
                {
                    Debug.LogError(
                        "Could not find reference to the prefab with the guid. Might be a model file. Prefab: " +
                        prefabFile);
                    return(scriptMappings);
                }

                string originalProjectPath = ProjectPathUtility.getProjectPathFromFile(prefabFile);

                //Deserialize the old ID's
                List <ClassModel> oldIDs =
                    Administration.Instance.oldIDsOverride ??
                    IDController.DeserializeIDs(originalProjectPath + constants.RelativeExportPath);
                if (oldIDs == null)
                {
                    throw new NullReferenceException("Old IDs not set");
                }

                //Deserialize the new ID's
                List <ClassModel> newIDs =
                    Administration.Instance.newIDsOverride ??
                    IDController.DeserializeIDs(destinationAssetPath + constants.RelativeExportPath);
                if (newIDs == null)
                {
                    throw new NullReferenceException("New IDs not set");
                }

                string[] parsedPrefab = idController.TransformIDs(currentPrefab.Path, oldIDs, newIDs, ref scriptMappings);

                var unmappedScriptMappings = scriptMappings
                                             .Where(script => script.HasBeenMapped == ScriptMapping.MappedState.NotMapped).ToList();
                if (unmappedScriptMappings.Count == 0)
                {
                    parsedPrefab = fieldMappingController.MigrateFields(prefabFile, ref parsedPrefab,
                                                                        ref scriptMappings,
                                                                        originalAssetPath, destinationAssetPath);
                    SavePrefabFile(parsedPrefab, currentPrefab, destinationAssetPath);
                }
                else
                {
                    bool completed = false;
                    ThreadUtility.RunMainTask(() =>
                    {
                        MergeWizard wizard = MergeWizard.CreateWizard(unmappedScriptMappings);
                        wizard.onComplete  = mergedScriptMappings =>
                        {
                            scriptMappings = scriptMappings.Merge(mergedScriptMappings);
                            File.WriteAllText(destinationAssetPath + constants.RelativeScriptMappingPath,
                                              JsonConvert.SerializeObject(scriptMappings, constants.IndentJson));

                            ThreadUtility.RunTask(() =>
                            {
                                parsedPrefab = fieldMappingController.MigrateFields(prefabFile, ref parsedPrefab,
                                                                                    ref scriptMappings, originalAssetPath, destinationAssetPath);
                                SavePrefabFile(parsedPrefab, currentPrefab, destinationAssetPath);
                            });
                            completed = true;
                        };
                    });

                    while (!completed)
                    {
                        Thread.Sleep(constants.THREAD_WAIT_TIME);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Could not parse prefab: " + prefabFile + "\r\nException: " + e);
            }

            return(scriptMappings);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Migrate a scene
        /// </summary>
        /// <param name="scenePath">The scene file to migrate</param>
        public void MigrateScene(string scenePath = null, string rootPath = null)
        {
            try
            {
                if (scenePath == null)
                {
                    ThreadUtility.RunWaitMainTask(() =>
                    {
                        scenePath =
                            EditorUtility.OpenFilePanel("Scene to import", constants.RootDirectory,
                                                        "unity");
                    });
                    if (scenePath.Length == 0)
                    {
                        Debug.LogWarning("No path was selected");
                        return;
                    }
                }

                if (rootPath == null)
                {
                    rootPath = constants.RootDirectory;
                }

                Debug.Log("Started migration of scene: " + scenePath);
                if (BinaryUtility.IsBinaryFile(scenePath))
                {
                    Debug.LogError("Could not parse file, since it's a binary file. Scene file: " + scenePath);
                    return;
                }

                string IDPath = ProjectPathUtility.getProjectPathFromFile(scenePath) + constants.RelativeExportPath;

                if (!File.Exists(IDPath))
                {
                    ThreadUtility.RunWaitMainTask(() =>
                    {
                        EditorUtility.DisplayDialog("Could not find old ID's",
                                                    "Could not find the ID's of the original project.  File does not exist : \r\n" + IDPath,
                                                    "Ok");
                    });
                    return;
                }

                List <ClassModel> oldIDs =
                    Administration.Instance.oldIDsOverride ?? IDController.DeserializeIDs(IDPath);

                string newIDsPath = rootPath + constants.RelativeExportPath;

                List <ClassModel> newIDs;
                if (Administration.Instance.newIDsOverride == null)
                {
                    newIDs = File.Exists(newIDsPath)
                        ? IDController.DeserializeIDs(newIDsPath)
                        : idController.ExportClassData(rootPath);
                }
                else
                {
                    newIDs = Administration.Instance.newIDsOverride;
                }

                List <ScriptMapping> scriptMappings = new List <ScriptMapping>();
                string scriptMappingsPath           = rootPath + constants.RelativeScriptMappingPath;
                if (Administration.Instance.ScriptMappingsOverride != null)
                {
                    scriptMappings = Administration.Instance.ScriptMappingsOverride;
                }
                else if (File.Exists(scriptMappingsPath))
                {
                    scriptMappings = MappingController.DeserializeMapping(scriptMappingsPath);
                }

                this.MigrateSceneIDs(rootPath, oldIDs, newIDs, scenePath, scriptMappings);
                Debug.Log("Migrated scene : " + scenePath);
            }
            catch (Exception e)
            {
                Debug.LogError("Could not migrate scene: " + scenePath + "\r\nException: " + e);
            }
        }