/// <summary> /// Generate a mapping of all scriptMappings in a project. /// Which means it creates a mapping between versions. /// </summary> /// <param name="oldIDs"></param> /// <param name="newIDs"></param> /// <returns></returns> public void MapAllClasses(List <ClassModel> oldIDs, List <ClassModel> newIDs, Action <List <ScriptMapping> > onComplete) { if (oldIDs == null || newIDs == null) { Debug.LogError("Old or new IDS are null. Cannot map without the old and new IDs"); } // List<ScriptMapping> unmergedScriptMappings = MapAllClasses(oldIDs, newIDs); List <ScriptMapping> unmergedScriptMappings = MapAllClassesRecursiveParent(oldIDs, newIDs); List <ScriptMapping> unmappedScriptMapping = unmergedScriptMappings .Where(script => script.HasBeenMapped == ScriptMapping.MappedState.NotMapped).ToList(); if (unmappedScriptMapping.Count == 0) { onComplete(unmappedScriptMapping); return; } ThreadUtility.RunMainTask(() => { MergeWizard mergeWizard = MergeWizard.CreateWizard(unmappedScriptMapping); mergeWizard.onComplete = (mergedScriptMapping) => { List <ScriptMapping> completed = unmappedScriptMapping.Merge(mergedScriptMapping); onComplete(completed); }; }); }
MigrateFields(string rootPath, string scenePath, List <ScriptMapping> scriptMappings, string[] lastSceneExport) { foreach (ScriptMapping script in scriptMappings) { if (script.HasBeenMapped == ScriptMapping.MappedState.NotChecked) { throw new NotImplementedException("Script has not been checked for mapping"); } } ScriptMapping[] unmappedScripts = scriptMappings .Where(field => field.HasBeenMapped == ScriptMapping.MappedState.NotMapped).ToArray(); if (unmappedScripts.Length > 0) { // Remove duplicate scripts List <ScriptMapping> scripts = unmappedScripts .GroupBy(field => field.newClassModel.FullName) .Select(group => group.First()).ToList(); EditorUtility.DisplayDialog("Merging fields necessary", "Could not merge all the fields to the class in the new project. You'll have to manually match old fields with the new fields", "Open merge window"); MergeWizard mergeWizard = MergeWizard.CreateWizard(scripts); mergeWizard.onComplete = (userAuthorizedList) => { MergingWizardCompleted(scriptMappings, rootPath, scenePath, lastSceneExport, userAuthorizedList); }; } else { MergingWizardCompleted(scriptMappings, rootPath, scenePath, lastSceneExport); } }
/// <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); }