public static bool IsVariableInvalid(AddVariableWindow addVariableWindow, string resultName, IElement currentElement, out string failureMessage) { bool didFailureOccur = false; string whyItIsntValid = ""; didFailureOccur = NameVerifier.IsCustomVariableNameValid(resultName, null, currentElement, ref whyItIsntValid) == false; failureMessage = null; if (didFailureOccur) { failureMessage = whyItIsntValid; } else if (addVariableWindow != null && NameVerifier.DoesTunneledVariableAlreadyExist(addVariableWindow.TunnelingObject, addVariableWindow.TunnelingVariable, currentElement)) { didFailureOccur = true; failureMessage = "There is already a variable that is modifying " + addVariableWindow.TunnelingVariable + " on " + addVariableWindow.TunnelingObject; } else if (addVariableWindow != null && IsUserTryingToCreateNewWithExposableName(addVariableWindow.ResultName, addVariableWindow.DesiredVariableType == CustomVariableType.Exposed)) { didFailureOccur = true; failureMessage = "The variable\n\n" + resultName + "\n\nis an expoable variable. Please use a different variable name or select the variable through the Expose tab"; } else if (ExposedVariableManager.IsReservedPositionedPositionedObjectMember(resultName) && currentElement is EntitySave) { didFailureOccur = true; failureMessage = "The variable\n\n" + resultName + "\n\nis reserved by FlatRedBall."; } return(didFailureOccur); }
private static void ReactToChangedCustomVariableName(string oldName, CustomVariable customVariable) { string whyItIsntValid = ""; bool isNameValid = NameVerifier.IsCustomVariableNameValid(customVariable.Name, customVariable, GlueState.Self.CurrentElement, ref whyItIsntValid); string newName = EditorLogic.CurrentCustomVariable.Name; if (customVariable.GetIsVariableState() && oldName != newName) { whyItIsntValid += "\nState variables cannot be renamed - they require specific names to function properly."; } if (!string.IsNullOrEmpty(whyItIsntValid)) { MessageBox.Show(whyItIsntValid); customVariable.Name = oldName; // handle invalid names here } else { IElement element = EditorLogic.CurrentElement; List <IElement> elementsToGenerate = new List <IElement>(); List <IElement> elementsToSearchForTunneledVariablesIn = new List <IElement>(); #region Change any states that use this variable foreach (StateSave stateSave in element.AllStates) { foreach (InstructionSave instructionSave in stateSave.InstructionSaves) { if (instructionSave.Member == oldName) { instructionSave.Member = newName; } } } #endregion #region Change any NOS that uses this as its source List <NamedObjectSave> nosList = ObjectFinder.Self.GetAllNamedObjectsThatUseElement(element); foreach (NamedObjectSave nos in nosList) { IElement container = ObjectFinder.Self.GetElementContaining(nos); if (!elementsToSearchForTunneledVariablesIn.Contains(container)) { elementsToSearchForTunneledVariablesIn.Add(container); } if (nos.RenameVariable(oldName, newName)) { if (!elementsToGenerate.Contains(container)) { elementsToGenerate.Add(container); } } } #endregion #region Change any CustomVaribles that tunnel in to this variable foreach (IElement elementToCheck in elementsToSearchForTunneledVariablesIn) { foreach (CustomVariable variableToCheck in elementToCheck.CustomVariables) { if (!string.IsNullOrEmpty(variableToCheck.SourceObject) && !string.IsNullOrEmpty(variableToCheck.SourceObjectProperty) && variableToCheck.SourceObjectProperty == oldName) { NamedObjectSave nos = elementToCheck.GetNamedObjectRecursively(variableToCheck.SourceObject); // just to be safe if (nos != null && nosList.Contains(nos)) { variableToCheck.SourceObjectProperty = newName; if (!elementsToGenerate.Contains(elementToCheck)) { elementsToGenerate.Add(elementToCheck); } } } } } #endregion #region Change all events that reference this variable foreach (var eventResponse in element.Events) { if (eventResponse.SourceVariable == oldName) { eventResponse.SourceVariable = newName; Plugins.PluginManager.ReceiveOutput("Changing event " + eventResponse.EventName + " to use variable " + newName); } } #endregion foreach (IElement toRegenerate in elementsToGenerate) { CodeWriter.GenerateCode(toRegenerate); } } }