private static void RefactorFunction(uNodeFunction function, string name, Type returnType, Type[] paramTypes) { var graph = function.owner; Undo.SetCurrentGroupName("Refactor Function: " + function.Name); HashSet <GameObject> referencedGraphs = new HashSet <GameObject>(); if (graph != null) { RuntimeMethod runtime = null; if (graph is IIndependentGraph) { if (GraphUtility.IsTempGraphObject(graph.gameObject)) { var prefab = uNodeEditorUtility.GetGameObjectSource(graph.gameObject, null); if (prefab != null) { var oriGraph = prefab.GetComponent <uNodeRoot>(); if (oriGraph != null) { var methods = ReflectionUtils.GetRuntimeType(oriGraph).GetMethods(); foreach (var m in methods) { if (m is RuntimeMethod && m.Name == name) { var parameters = m.GetParameters(); if (parameters.Length == paramTypes.Length) { if (runtime == null) { runtime = m as RuntimeMethod; } bool isValid = true; for (int i = 0; i < parameters.Length; i++) { if (parameters[i].ParameterType != paramTypes[i]) { isValid = false; break; } } if (isValid) { runtime = m as RuntimeMethod; } } } } } } } else { var methods = ReflectionUtils.GetRuntimeType(graph).GetMethods(); foreach (var m in methods) { if (m is RuntimeGraphMethod runtimeMethod && runtimeMethod.target == function) { runtime = runtimeMethod; break; } } } } MemberInfo nativeMember = null; if (graph.GeneratedTypeName.ToType(false) != null) { var type = graph.GeneratedTypeName.ToType(false); if (paramTypes.Length == 0 && function.GenericParameters.Count == 0) { nativeMember = type.GetMethod(name, MemberData.flags); } var members = type.GetMember(name, MemberData.flags); if (members != null) { var genericLength = function.GenericParameters.Count; foreach (var m in members) { if (m is MethodInfo method) { var mParam = method.GetParameters(); var mGeneric = method.GetGenericArguments(); if (paramTypes.Length == mParam.Length && mGeneric.Length == genericLength) { bool valid = true; for (int i = 0; i < mParam.Length; i++) { if (mParam[i].ParameterType != paramTypes[i]) { valid = false; break; } } if (valid) { nativeMember = method; break; } } } } } } var graphPrefabs = uNodeEditorUtility.FindPrefabsOfType <uNodeRoot>(); foreach (var prefab in graphPrefabs) { var gameObject = prefab; GameObject prefabContent = null; if (GraphUtility.HasTempGraphObject(prefab)) { gameObject = GraphUtility.GetTempGraphObject(prefab); } else if (uNodeEditorUtility.IsPrefab(prefab)) { prefabContent = PrefabUtility.LoadPrefabContents(AssetDatabase.GetAssetPath(prefab)); gameObject = prefabContent; } var scripts = gameObject.GetComponentsInChildren <MonoBehaviour>(true); bool hasUndo = false; Func <object, bool> scriptValidation = (obj) => { MemberData member = obj as MemberData; if (member != null && member.startType is RuntimeType) { var members = member.GetMembers(false); if (members != null) { for (int i = 0; i < members.Length; i++) { var m = members[i]; if (member.namePath.Length > i + 1) { if (m == runtime || m == nativeMember) { if (!hasUndo && prefabContent == null) { uNodeEditorUtility.RegisterFullHierarchyUndo(gameObject); hasUndo = true; } var path = member.namePath; path[i + 1] = function.Name; member.name = string.Join(".", path); { var items = member.Items; if (items.Length > i) { var mVal = MemberData.CreateFromMember(runtime); items[i] = mVal.Items[0]; member.SetItems(items); } } if (m == nativeMember) { referencedGraphs.Add(prefab); } return(true); } } } } } return(false); }; if (runtime != null) { bool hasChanged = false; Array.ForEach(scripts, script => { bool flag = AnalizerUtility.AnalizeObject(script, scriptValidation); if (flag) { hasChanged = true; hasUndo = false; uNodeGUIUtility.GUIChanged(script); uNodeEditorUtility.MarkDirty(script); } }); if (hasChanged) { if (gameObject != prefab) { uNodeEditorUtility.RegisterFullHierarchyUndo(prefab); if (prefabContent == null) { //Save the temporary graph GraphUtility.AutoSaveGraph(gameObject); } else { //Save the prefab contents and unload it uNodeEditorUtility.SavePrefabAsset(gameObject, prefab); } } uNodeEditorUtility.MarkDirty(prefab); } } if (prefabContent != null) { PrefabUtility.UnloadPrefabContents(prefabContent); } } } uNodeEditorUtility.RegisterFullHierarchyUndo(graph.gameObject); graph.Refresh(); Func <object, bool> validation = delegate(object OBJ) { return(CallbackRefactorFunction(OBJ, function, name, paramTypes)); }; Array.ForEach(graph.nodes, item => AnalizerUtility.AnalizeObject(item, validation)); if (GraphUtility.IsTempGraphObject(graph.gameObject)) { var prefab = uNodeEditorUtility.GetGameObjectSource(graph.gameObject, null); uNodeEditorUtility.RegisterFullHierarchyUndo(prefab); GraphUtility.AutoSaveGraph(graph.gameObject); } uNodeEditor.ClearGraphCache(); uNodeEditor.window?.Refresh(true); DoCompileReferences(graph, referencedGraphs); }
public static void RefactorProperty(uNodeProperty property, string name) { name = uNodeUtility.AutoCorrectName(name); var graph = property.owner; bool hasVariable = false; if (graph.Properties != null && graph.Properties.Count > 0) { foreach (var V in graph.Properties) { if (V.Name == name) { hasVariable = true; break; } } } if (graph.Variables != null && graph.Variables.Count > 0) { foreach (var V in graph.Variables) { if (V.Name == name) { hasVariable = true; break; } } } if (hasVariable) { return; } Undo.SetCurrentGroupName("Rename Property: " + property.Name); HashSet <GameObject> referencedGraphs = new HashSet <GameObject>(); if (graph != null) { RuntimeProperty runtime = null; if (graph is IIndependentGraph) { if (GraphUtility.IsTempGraphObject(graph.gameObject)) { var prefab = uNodeEditorUtility.GetGameObjectSource(graph.gameObject, null); if (prefab != null) { var oriGraph = prefab.GetComponent <uNodeRoot>(); if (oriGraph != null) { runtime = ReflectionUtils.GetRuntimeType(oriGraph).GetProperty(property.Name) as RuntimeProperty; } } } else { runtime = ReflectionUtils.GetRuntimeType(graph).GetProperty(property.Name) as RuntimeProperty; } } PropertyInfo nativeMember = null; if (graph.GeneratedTypeName.ToType(false) != null) { var type = graph.GeneratedTypeName.ToType(false); nativeMember = type.GetProperty(property.Name, MemberData.flags); } var graphPrefabs = uNodeEditorUtility.FindPrefabsOfType <uNodeRoot>(); foreach (var prefab in graphPrefabs) { var gameObject = prefab; GameObject prefabContent = null; if (GraphUtility.HasTempGraphObject(prefab)) { gameObject = GraphUtility.GetTempGraphObject(prefab); } else if (uNodeEditorUtility.IsPrefab(prefab)) { prefabContent = PrefabUtility.LoadPrefabContents(AssetDatabase.GetAssetPath(prefab)); gameObject = prefabContent; } var scripts = gameObject.GetComponentsInChildren <MonoBehaviour>(true); bool hasUndo = false; Func <object, bool> scriptValidation = (obj) => { MemberData member = obj as MemberData; if (member != null && member.startType is RuntimeType) { var members = member.GetMembers(false); if (members != null) { for (int i = 0; i < members.Length; i++) { var m = members[i]; if (member.namePath.Length > i + 1) { if (m == runtime || m == nativeMember) { if (!hasUndo && prefabContent == null) { uNodeEditorUtility.RegisterFullHierarchyUndo(gameObject); hasUndo = true; } var path = member.namePath; path[i + 1] = name; member.name = string.Join(".", path); if (m == nativeMember) { referencedGraphs.Add(prefab); } return(true); } } } } } return(false); }; if (runtime != null || nativeMember != null) { bool hasChanged = false; Array.ForEach(scripts, script => { bool flag = AnalizerUtility.AnalizeObject(script, scriptValidation); if (flag) { hasChanged = true; hasUndo = false; uNodeGUIUtility.GUIChanged(script); uNodeEditorUtility.MarkDirty(script); } }); if (hasChanged) { if (gameObject != prefab) { uNodeEditorUtility.RegisterFullHierarchyUndo(prefab); if (prefabContent == null) { //Save the temporary graph GraphUtility.AutoSaveGraph(gameObject); } else { //Save the prefab contents and unload it uNodeEditorUtility.SavePrefabAsset(gameObject, prefab); } } uNodeEditorUtility.MarkDirty(prefab); } } if (prefabContent != null) { PrefabUtility.UnloadPrefabContents(prefabContent); } } } uNodeEditorUtility.RegisterFullHierarchyUndo(graph.gameObject); string oldVarName = property.Name; property.Name = name; graph.Refresh(); Func <object, bool> validation = delegate(object OBJ) { return(CallbackRenameProperty(OBJ, graph, property.name, oldVarName)); }; Array.ForEach(graph.nodes, item => AnalizerUtility.AnalizeObject(item, validation)); if (GraphUtility.IsTempGraphObject(graph.gameObject)) { var prefab = uNodeEditorUtility.GetGameObjectSource(graph.gameObject, null); uNodeEditorUtility.RegisterFullHierarchyUndo(prefab); GraphUtility.AutoSaveGraph(graph.gameObject); } uNodeEditor.ClearGraphCache(); uNodeEditor.window?.Refresh(true); DoCompileReferences(graph, referencedGraphs); }
public object Get() { object obj = null; if (target.isAssigned) { if (parameters != null && parameters.Length > 0) { target.GetMembers(); object[] paramsValue = null; if (!target.hasRefOrOut) { if (parameters.Length > 0) { paramsValue = new object[parameters.Length]; for (int i = 0; i < paramsValue.Length; i++) { paramsValue[i] = parameters[i].Get(); } } obj = target.Invoke(paramsValue); } else { object[] paramsValue2 = null; if (parameters.Length > 0) { paramsValue = new object[parameters.Length]; paramsValue2 = new object[parameters.Length]; for (int i = 0; i < paramsValue.Length; i++) { paramsValue[i] = parameters[i].Get(); paramsValue2[i] = paramsValue[i]; } } obj = target.Invoke(paramsValue); if (paramsValue != null) { for (int i = 0; i < paramsValue.Length; i++) { if (paramsValue2[i] != paramsValue[i] && parameters[i].CanSetValue()) { parameters[i].Set(paramsValue[i]); } } } } } else { obj = target.Get(); } if (target.targetType == MemberData.TargetType.Values || target.targetType == MemberData.TargetType.Constructor) { if (initializer != null && initializer.value != null) { uNode.ConstructorValueData ctor = initializer.value as uNode.ConstructorValueData; if (ctor != null) { ctor.ApplyInitializer(ref obj); } } } } return(obj); }
public static void RefactorVariable(VariableData variable, string name, UnityEngine.Object owner) { bool isLocal = false; uNodeRoot graph = owner as uNodeRoot; if (graph == null) { INode <uNodeRoot> node = owner as INode <uNodeRoot>; if (node != null) { graph = node.GetOwner(); isLocal = true; } } if (graph != null) { HashSet <GameObject> referencedGraphs = new HashSet <GameObject>(); if (!isLocal) { RuntimeField field = null; if (graph is IIndependentGraph) { if (GraphUtility.IsTempGraphObject(graph.gameObject)) { var prefab = uNodeEditorUtility.GetGameObjectSource(graph.gameObject, null); if (prefab != null) { var oriGraph = prefab.GetComponent <uNodeRoot>(); if (oriGraph != null) { field = ReflectionUtils.GetRuntimeType(oriGraph).GetField(variable.Name) as RuntimeField; } } } else { field = ReflectionUtils.GetRuntimeType(graph).GetField(variable.Name) as RuntimeField; } } FieldInfo nativeMember = null; if (graph.GeneratedTypeName.ToType(false) != null) { var type = graph.GeneratedTypeName.ToType(false); nativeMember = type.GetField(variable.Name, MemberData.flags); } var graphPrefabs = uNodeEditorUtility.FindPrefabsOfType <uNodeRoot>(); foreach (var prefab in graphPrefabs) { var gameObject = prefab; GameObject prefabContent = null; if (GraphUtility.HasTempGraphObject(prefab)) { gameObject = GraphUtility.GetTempGraphObject(prefab); } else if (uNodeEditorUtility.IsPrefab(prefab)) { prefabContent = PrefabUtility.LoadPrefabContents(AssetDatabase.GetAssetPath(prefab)); gameObject = prefabContent; } var scripts = gameObject.GetComponentsInChildren <MonoBehaviour>(true); bool hasUndo = false; Func <object, bool> scriptValidation = (obj) => { MemberData member = obj as MemberData; if (member != null) { var members = member.GetMembers(false); if (members != null) { for (int i = 0; i < members.Length; i++) { var m = members[i]; if (member.namePath.Length > i + 1) { if (m == field || m == nativeMember) { if (!hasUndo && prefabContent == null) { uNodeEditorUtility.RegisterFullHierarchyUndo(gameObject, "Rename Variable: " + variable.Name); hasUndo = true; } var path = member.namePath; path[i + 1] = name; member.name = string.Join(".", path); if (m == nativeMember) { referencedGraphs.Add(prefab); } return(true); } } } } } return(false); }; if (field != null || nativeMember != null) { bool hasChanged = false; Array.ForEach(scripts, script => { bool flag = AnalizerUtility.AnalizeObject(script, scriptValidation); if (flag) { hasChanged = true; hasUndo = false; uNodeGUIUtility.GUIChanged(script); uNodeEditorUtility.MarkDirty(script); } }); if (hasChanged) { if (gameObject != prefab) { uNodeEditorUtility.RegisterFullHierarchyUndo(prefab, "Rename Variable: " + variable.Name); if (prefabContent == null) { //Save the temporary graph GraphUtility.AutoSaveGraph(gameObject); } else { //Save the prefab contents uNodeEditorUtility.SavePrefabAsset(gameObject, prefab); } } uNodeEditorUtility.MarkDirty(prefab); } } if (prefabContent != null) { PrefabUtility.UnloadPrefabContents(prefabContent); } } } string oldVarName = variable.Name; variable.Name = name; Func <object, bool> validation = delegate(object OBJ) { return(CallbackRenameVariable(OBJ, owner, variable.Name, oldVarName)); }; graph.Refresh(); Array.ForEach(graph.nodes, item => AnalizerUtility.AnalizeObject(item, validation)); if (GraphUtility.IsTempGraphObject(graph.gameObject)) { var prefab = uNodeEditorUtility.GetGameObjectSource(graph.gameObject, null); uNodeEditorUtility.RegisterFullHierarchyUndo(prefab, "Rename Variable: " + oldVarName); GraphUtility.AutoSaveGraph(graph.gameObject); } uNodeEditor.ClearGraphCache(); uNodeEditor.window?.Refresh(true); DoCompileReferences(graph, referencedGraphs); } }
/// <summary> /// Function for generate code for set a value. /// </summary> /// <param name="left"></param> /// <param name="right"></param> /// <param name="setType"></param> /// <param name="leftType"></param> /// <param name="rightType"></param> /// <returns></returns> public static string GenerateSetCode(object left, object right, SetType setType = SetType.Change, Type leftType = null, Type rightType = null) { if (left == null || right == null) { return(null); } object firstVal = left; string set; if (right is string) { set = right as string; } else if (right is char[]) { set = new string(right as char[]); } else { set = ParseValue(right, autoConvert: true); } string result = null; if (left is MemberData) { MemberData source = left as MemberData; if (source.type != null && source.type.IsValueType) { MemberInfo[] memberInfo = source.GetMembers(false); // if(memberInfo[0] is RuntimeType && memberInfo[memberInfo.Length - 1] is IRuntimeMember){ // throw null; // } else if (memberInfo != null && memberInfo.Length > 1 && ReflectionUtils.GetMemberType(memberInfo[memberInfo.Length - 2]).IsValueType) { string varName = GenerateVariableName("tempVar"); string data = ParseType(ReflectionUtils.GetMemberType(memberInfo[memberInfo.Length - 2])) + " " + varName + " = "; var pVal = ParseValue((object)source); var pVal2 = pVal.Remove(pVal.IndexOf(ParseStartValue(source)), ParseStartValue(source).Length + 1); if (pVal.LastIndexOf(".") >= 0) { pVal = pVal.Remove(pVal.LastIndexOf(".")); } data += pVal + ";\n"; switch (setType) { case SetType.Subtract: data = data + varName + "." + pVal2.SplitMember().Last() + " -= " + set + ";"; break; case SetType.Divide: data = data + varName + "." + pVal2.SplitMember().Last() + " /= " + set + ";"; break; case SetType.Add: data = data + varName + "." + pVal2.SplitMember().Last() + " += " + set + ";"; break; case SetType.Multiply: data = data + varName + "." + pVal2.SplitMember().Last() + " *= " + set + ";"; break; case SetType.Modulo: data = data + varName + "." + pVal2.SplitMember().Last() + " %= " + set + ";"; break; default: data = data + varName + "." + pVal2.SplitMember().Last() + " = " + set + ";"; break; } if (leftType != null && !leftType.IsCastableTo(typeof(Delegate)) && !(leftType is RuntimeType)) { if (rightType == null || !rightType.IsCastableTo(leftType) && !rightType.IsValueType && rightType != typeof(string)) { if (leftType.IsValueType) { varName = varName.Insert(0, "(" + ParseType(leftType) + ")"); } else if (set != "null") { varName = varName + " as " + ParseType(leftType); } } } return(data + "\n" + pVal + " = " + varName + ";"); } else { result = ParseValue(left, setVariable: true); } } else { result = ParseValue(left, setVariable: true); if (source.type is RuntimeGraphType && right is MemberData) { MemberData mVal = right as MemberData; if (mVal.type != source.type) { set = set.Access(GenerateGetGeneratedComponent(null, source.type as RuntimeGraphType)); } } } } else if (left is string) { result = left.ToString(); } if (leftType != null && !leftType.IsCastableTo(typeof(Delegate)) && !(leftType is RuntimeType)) { if (rightType == null || !rightType.IsCastableTo(leftType) && !rightType.IsValueType && rightType != typeof(string)) { if (leftType.IsValueType) { set = set.Insert(0, "(" + ParseType(leftType) + ")"); } else if (set != "null") { set = set + " as " + ParseType(leftType); } } } bool flag = !generatePureScript && result.EndsWith("\")"); if (includeGraphInformation && firstVal is MemberData && !result.EndsWith("*/")) { var member = firstVal as MemberData; var node = member.GetTargetNode(); if (member.targetType == MemberData.TargetType.ValueNode && node != null) { result = WrapWithInformation(result, node); } } if (flag) { var strs = result.SplitMember(); var lastStr = strs[strs.Count - 1]; string setCode = null; if (lastStr.StartsWith(nameof(RuntimeComponent.GetVariable) + "<")) { setCode = nameof(RuntimeComponent.SetVariable); if (set != "null" && leftType.IsCastableTo(typeof(Delegate))) { set = set.Wrap().Insert(0, "(" + ParseType(leftType) + ")"); } } else if (lastStr.StartsWith(nameof(RuntimeComponent.GetProperty) + "<")) { setCode = nameof(RuntimeComponent.SetProperty); if (set != "null" && leftType.IsCastableTo(typeof(Delegate))) { set = set.Wrap().Insert(0, "(" + ParseType(leftType) + ")"); } } if (setCode != null) { string code = null; switch (setType) { case SetType.Subtract: code = "-"; break; case SetType.Divide: code = "/"; break; case SetType.Add: code = "+"; break; case SetType.Multiply: code = "*"; break; case SetType.Modulo: code = "%"; break; } { //for change the operator code int firstIndex = lastStr.IndexOf("\""); string vName = lastStr.Substring(firstIndex, lastStr.LastIndexOf("\"") - firstIndex + 1); if (code != null) { strs[strs.Count - 1] = DoGenerateInvokeCode(setCode, new string[] { vName, set, code.ParseValue() }); } else { strs[strs.Count - 1] = DoGenerateInvokeCode(setCode, new string[] { vName, set }); } } result = string.Join(".", strs); if (debugScript && setting.debugValueNode && firstVal is MemberData) { result += GenerateDebugCode(firstVal as MemberData, result, true).AddLineInFirst(); } return(result); } } switch (setType) { case SetType.Subtract: result = result + " -= " + set + ";"; break; case SetType.Divide: result = result + " /= " + set + ";"; break; case SetType.Add: result = result + " += " + set + ";"; break; case SetType.Multiply: result = result + " *= " + set + ";"; break; case SetType.Modulo: result = result + " %= " + set + ";"; break; default: result = result + " = " + set + ";"; break; } if (debugScript && setting.debugValueNode && firstVal is MemberData) { result += GenerateDebugCode(firstVal as MemberData, result.RemoveLast(), true).AddLineInFirst(); } return(result); }