public void CustomLogic(ref string[] scene, ref YamlDocument yamlDocument, FoundScript foundScript) { // Get all values YamlNode yamlNodes = yamlDocument.RootNode.GetChildren()["MonoBehaviour"]; // Get the value we wish to change, this is the originalValue and has not been changed in the yaml document // That's why we need to use the original name to transform the data YamlNode originalValue = yamlNodes["testQuaternion"]; int line = originalValue.Start.Line - 1; // Transform the data Vector3 newValue = new Quaternion( float.Parse(originalValue["x"].ToString()), float.Parse(originalValue["y"].ToString()), float.Parse(originalValue["z"].ToString()), float.Parse(originalValue["w"].ToString()) ).eulerAngles; // Make the value we want to set string valueString = "{ x : " + newValue.x + ", y : " + newValue.y + ", z : " + newValue.z + " }"; // Get the key that is in the newest version of the scene (this is the changed value so testQuaternion2) // When setting values be careful with indenting as yaml uses indents for structure // Indents can be set by adding "\t" and new lines can be added by using "\r\n" string original = scene[line].Substring(0, scene[line].IndexOf(':')); // Replace it in the original file scene[line] = original + ": " + valueString; }
/// <summary> /// Change the fields after merging with the merging window /// </summary> /// <param name="originalFoundScripts"></param> /// <param name="rootPath"></param> /// <param name="scenePath"></param> /// <param name="linesToChange"></param> /// <param name="mergedFoundScripts"></param> private void MergingWizardCompleted(List <FoundScript> originalFoundScripts, string rootPath, string scenePath, string[] linesToChange, List <FoundScript> mergedFoundScripts = null) { if (mergedFoundScripts != null) { // Merge the MergeWindow changed FoundScripts with the originalFoundScripts for (var i = 0; i < originalFoundScripts.Count; i++) { FoundScript originalFoundScript = originalFoundScripts[i]; FoundScript changedFoundScript = mergedFoundScripts.FirstOrDefault(script => script.oldClassModel.FullName == originalFoundScript.oldClassModel.FullName); if (changedFoundScript != null) { originalFoundScripts[i] = changedFoundScript; } } } string[] newSceneExport = fieldMappingController.ReplaceFieldsByMergeNodes(linesToChange, originalFoundScripts); Debug.Log(string.Join("\n", newSceneExport)); SaveFoundScripts(rootPath, originalFoundScripts); SaveFile(rootPath + "/" + Path.GetFileName(scenePath), linesToChange); calculationThread = null; }
public FoundScriptWrapper(FoundScript _foundScript) { FoundScript = _foundScript; FieldSelectionStates = new bool[_foundScript.MergeNodes.Count]; for (int i = 0; i < FieldSelectionStates.Length; i++) { FieldSelectionStates[i] = true; } OptionSelections = new int[_foundScript.MergeNodes.Count]; for (int i = 0; i < OptionSelections.Length; i++) { OptionSelections[i] = 0; } }
/// <summary> /// Maps all foundScripts for all variables and children of the type of the variable /// </summary> /// <param name="newIDs">The IDs of the new project</param> /// <param name="foundScripts">The existing foundScripts that will be looked in and added to</param> /// <param name="oldClassModel">Current class data of the old project as this maps to the scene file</param> /// <returns></returns> /// <exception cref="NullReferenceException"></exception> private FoundScript FoundScriptMappingRecursively(List <ClassModel> newIDs, ref List <FoundScript> foundScripts, ClassModel oldClassModel) { if (oldClassModel == null) { throw new NullReferenceException("No old classData found"); } FoundScript existingFoundScript = foundScripts.FirstOrDefault(script => script.oldClassModel.FullName == oldClassModel.FullName); ClassModel replacementClassModel = existingFoundScript ?.newClassModel; if (replacementClassModel == null && oldClassModel.Fields != null) { replacementClassModel = findNewID(newIDs, oldClassModel); //todo : testScript gets called double if (replacementClassModel == null) { return(null); } } else if (replacementClassModel != null) { return(existingFoundScript); } else { return(null); } if (existingFoundScript != null) { return(existingFoundScript); } existingFoundScript = new FoundScript { oldClassModel = oldClassModel, newClassModel = replacementClassModel }; MappedState hasBeenMapped = existingFoundScript.CheckHasBeenMapped(); if (hasBeenMapped == MappedState.NotMapped) { existingFoundScript.GenerateMappingNode(foundScripts); } foundScripts.Add(existingFoundScript); //If it doesn't exist then create it if (oldClassModel.Fields != null && oldClassModel.Fields.Length != 0) { foreach (FieldModel field in oldClassModel.Fields) { // Check if the type is null as this would cause so errors in the mapping if (field.Type == null) { throw new NullReferenceException("type of field is null for some reason"); } FoundScriptMappingRecursively(newIDs, ref foundScripts, field.Type); } } return(existingFoundScript); }
/// <summary> /// Replaces the GUID and fileID, matching the oldIDs with the currentIDs /// </summary> /// <param name="linesToChange"></param> /// <param name="oldIDs">List of GUIDs and FileID for all classes in the previous project.</param> /// <param name="newIDs">List of GUIDs and FileID for all currently in the project classes.</param> /// <param name="foundScripts"></param> /// <returns></returns> private string[] MigrateGUIDsAndFieldIDs(string[] linesToChange, List <ClassModel> oldIDs, List <ClassModel> newIDs, ref List <FoundScript> foundScripts) { string sceneContent = string.Join("\r\n", linesToChange); YamlStream yamlStream = new YamlStream(); yamlStream.Load(new StringReader(sceneContent)); IEnumerable <YamlDocument> yamlDocuments = yamlStream.Documents.Where(document => document.GetName() == "MonoBehaviour"); foreach (YamlDocument document in yamlDocuments) { YamlNode monoBehaviour = document.RootNode.GetChildren()["MonoBehaviour"]; YamlNode oldFileIdNode = monoBehaviour["m_Script"]["fileID"]; YamlNode oldGuidNode = monoBehaviour["m_Script"]["guid"]; string oldFileId = oldFileIdNode.ToString(); string oldGuid = oldGuidNode.ToString(); ClassModel oldClassModel = oldIDs.FirstOrDefault(data => data.Guid == oldGuid && data.FileID == oldFileId); if (oldClassModel == null) { Debug.LogError("Could not find class for script with type, not migrating guid : " + oldGuid + " oldFileID : " + oldFileId); continue; } FoundScript mapping = FoundScriptMappingRecursively(newIDs, ref foundScripts, oldClassModel); if (mapping == null) { Debug.LogError("mapping is null for " + oldClassModel.FullName); continue; } int line = oldFileIdNode.Start.Line - 1; if (!string.IsNullOrEmpty(mapping.newClassModel.Guid)) { // Replace the Guid linesToChange[line] = linesToChange[line].ReplaceFirst(oldGuid, mapping.newClassModel.Guid); } else { Debug.Log("Found empty guid"); continue; //todo : this should throw an error //todo : this is when a non script is being used or the guid is not available. This should probably be a popup with a warning } if (!String.IsNullOrEmpty(oldFileId)) { linesToChange[line] = linesToChange[line].ReplaceFirst(oldFileId, mapping.newClassModel.FileID); } } return(linesToChange); }