/// <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);
        }
Example #2
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);
            }
        }