void Implement_Registrations(StreamWriter sw, EventMethodsGroupsCollection groups) { WrapIn(sw, "namespace Events", () => { WrapIn(sw, "public partial class Tools", () => { //mehtod 1 WrapIn(sw, "static partial void RegesterUserImplementation(object user) ", () => { WriteToStream(sw, "if(!(user is Tools.IEventMethodBase))return; "); foreach (var @group in groups) { foreach (var method in group) { var argsWithoutNames = method.Args.Select(x => NameValidator.GetGenericTypeName(x.ArgType)).ToArray(); var argTypes_AsName = argsWithoutNames.Length == 0 ? "" : "_" + string.Join("_", argsWithoutNames); argTypes_AsName = Regex.Replace(argTypes_AsName, "[^A-Za-z0-9 _]", ""); WriteToStream(sw, "if(user is Groups.", group.GroupName, ".Methods.", method.Name, argTypes_AsName, ".I", method.Name, argTypes_AsName, ")\n\t"); WriteToStream(sw, "Groups.", group.GroupName, ".Methods.", method.Name, argTypes_AsName, ".RegisterListener(user as Groups.", group.GroupName, ".Methods.", method.Name, argTypes_AsName, ".I", method.Name, argTypes_AsName, ");\n"); } } }); //mehtod 2 WrapIn(sw, "static partial void UnRegesterUserImplementation(object user) ", () => { WriteToStream(sw, "if(!(user is Tools.IEventMethodBase))return; "); foreach (var @group in groups) { foreach (var method in group) { var argsWithoutNames = method.Args.Select(x => NameValidator.GetGenericTypeName(x.ArgType)).ToArray(); var argTypes_AsName = argsWithoutNames.Length == 0 ? "" : "_" + string.Join("_", argsWithoutNames); argTypes_AsName = Regex.Replace(argTypes_AsName, "[^A-Za-z0-9 _]", ""); WriteToStream(sw, "if(user is Groups.", group.GroupName, ".Methods.", method.Name, argTypes_AsName, ".I", method.Name, argTypes_AsName, ")\n\t"); WriteToStream(sw, "Groups.", group.GroupName, ".Methods.", method.Name, argTypes_AsName, ".UnRegisterListener(user as Groups.", group.GroupName, ".Methods.", method.Name, argTypes_AsName, ".I", method.Name, argTypes_AsName, ");\n"); } } }); }); }); }
public override string ToString() { var argsName = _args.Select(x => string.Format("{0} {1}", NameValidator.GetGenericTypeName(x.ArgType), x.ArgName)).ToArray(); return(string.Format("{0}({1})", _name, string.Join(" , ", argsName))); }
void Implement_body(StreamWriter sw, EventMethodsGroupsCollection groups) { WrapIn(sw, "namespace Events.Groups", () => { foreach (var group in groups) { WrapIn(sw, "namespace " + group.GroupName, () => { StringBuilder interfacesNames = new StringBuilder(); foreach (var method in group) { //---------------------------------------------------------------------------------- var args = method.Args.Select(x => string.Format("{0} {1}", NameValidator.GetGenericTypeFullName(x.ArgType), x.ArgName)).ToArray(); var argsAsMethodArgs = args.Length == 0 ? "" : string.Join(",", args); var argsWithoutNames = method.Args.Select(x => NameValidator.GetGenericTypeName(x.ArgType)).ToArray(); var argTypes_AsName = argsWithoutNames.Length == 0 ? "" : string.Join("_", argsWithoutNames); argTypes_AsName = Regex.Replace(argTypes_AsName, "[^A-Za-z0-9 _]", ""); //---------------------------------------------------------------------------------- WrapIn(sw, "namespace Methods", () => { WrapIn(sw, string.Format("public static class {0}{1}", @method.Name, (argsWithoutNames.Length > 0 ? string.Format("_{0}", argTypes_AsName) : "")), // methods arg types () => { string delegateType = string.Format("delegate_{0}_{1}", @group.GroupName, @method.Name); WriteDelegate(sw, delegateName: delegateType, argNamesAndTypes: args); //write inteface if (argsWithoutNames.Length > 0) { WrapIn(sw, string.Format("public interface I{0}_{1} : Tools.IEventMethodBase", @method.Name, argTypes_AsName), () => { WriteToStream(sw, method.MethodFullSigneture); }); interfacesNames.Append(string.Format("Methods.{0}_{1}.I{0}_{1},", @method.Name, argTypes_AsName)); } else { WrapIn(sw, string.Format("public interface I{0} : Tools.IEventMethodBase", @method.Name), () => { WriteToStream(sw, method.MethodFullSigneture); }); interfacesNames.Append(string.Format("Methods.{0}.I{0},", @method.Name)); } //delegate instance WriteToStream(sw, "private static ", delegateType, " _listener;\n"); //register user method WrapIn(sw, string.Format("public static void RegisterListener({0} user)", delegateType), () => { WriteToStream(sw, "if(user == null) return;"); WriteToStream(sw, "if(_listener == null) _listener = user;"); WriteToStream(sw, "else if(!_listener.GetInvocationList().Contains(user)) _listener += user;"); }); //Unregister user method WrapIn(sw, string.Format("public static void UnRegisterListener({0} user)", delegateType), () => { WriteToStream(sw, "if(user == null) return;"); WriteToStream(sw, "if(_listener == null) return;"); WriteToStream(sw, "if(_listener.GetInvocationList().Contains(user)) _listener -= user;"); }); //invoke WrapIn(sw, string.Format("public static void Invoke({0})", argsAsMethodArgs), () => { WriteToStream(sw, "if(_listener != null) _listener(", string.Join(",", method.Args.Select(x => x.ArgName).ToArray()), ");"); }); }); }); //---------------------------------------------------------------------------------- } //interface IAll_Group_Events sw.Write("public interface IAll_Group_Events"); if (!string.IsNullOrEmpty(interfacesNames.ToString())) { interfacesNames = interfacesNames.Remove(interfacesNames.Length - 1, 1); //remove the "," at the end of the string builder WriteToStream(sw, ":", interfacesNames.ToString()); } sw.Write("{ }\n"); }); //end namespace {GroupName} warp } }); //end namespace "Events.Groups" warp }
void Implement_body(StreamWriter sw, EventMethodsGroupsCollection groups) { WrapIn(sw, "namespace Events.Groups", () => { foreach (var group in groups) { WrapIn(sw, "namespace " + group.GroupName, () => { StringBuilder interfacesNames = new StringBuilder(); WrapIn(sw, "namespace Methods", () => { foreach (var method in group) { //---------------------------------------------------------------------------------- var args = method.Args.Select( x => string.Format("{0} {1}", NameValidator.GetGenericTypeFullName(x.ArgType), x.ArgName)).ToArray(); var argsAsMethodArgs = args.Length == 0 ? "" : string.Join(",", args); var argsWithoutNames = method.Args.Select(x => NameValidator.GetGenericTypeName(x.ArgType)).ToArray(); var argTypes_AsName = argsWithoutNames.Length == 0 ? "" : string.Join("_", argsWithoutNames); argTypes_AsName = Regex.Replace(argTypes_AsName, "[^A-Za-z0-9 _]", ""); //---------------------------------------------------------------------------------- var interfaceName = (argsWithoutNames.Length > 0) ? string.Format("I{0}_{1}", @method.Name, argTypes_AsName) : string.Format("I{0}", @method.Name); WrapIn(sw, string.Format("public static class {0}{1}", @method.Name, (argsWithoutNames.Length > 0 ? string.Format("_{0}", argTypes_AsName) : "")), // methods arg types () => { WriteToStream(sw, "static List<", interfaceName, "> _users = new List<", interfaceName, ">();"); //write inteface WrapIn(sw, string.Format("public interface {0} : Tools.IEventMethodBase", interfaceName), () => { WriteToStream(sw, method.MethodFullSigneture); }); interfacesNames.Append(string.Format("Methods.{0}.{1},", interfaceName.Substring(1), interfaceName)); //register user method WrapIn(sw, string.Format("internal static void RegisterListener({0} user)", interfaceName), () => { WriteToStream(sw, "if(user == null) return;"); WriteToStream(sw, "if(!_users.Contains(user)) _users.Add(user);"); }); //Unregister user method WrapIn(sw, string.Format("internal static void UnRegisterListener({0} user)", interfaceName), () => { WriteToStream(sw, "if(user == null) return;"); WriteToStream(sw, "if(_users.Contains(user)) _users.Remove(user);"); }); //invoke WrapIn(sw, string.Format("public static void Invoke({0})", argsAsMethodArgs), () => { WriteToStream(sw, "_users.ForEach(x=> x.", method.Name, "(", string.Join(",", method.Args.Select(x => x.ArgName).ToArray()), "));"); }); }); //---------------------------------------------------------------------------------- } }); //interface IAll_Group_Events sw.Write("public interface IAll_Group_Events"); if (!string.IsNullOrEmpty(interfacesNames.ToString())) { interfacesNames = interfacesNames.Remove(interfacesNames.Length - 1, 1); //remove the "," at the end of the string builder WriteToStream(sw, ":", interfacesNames.ToString()); } sw.Write("{ }\n"); }); //end namespace {GroupName} warp } }); //end namespace "Events.Groups" warp }
void Implement_body(StreamWriter sw, EventMethodsGroupsCollection groups) { WrapIn(sw, "namespace Events.Groups", () => { foreach (var group in groups) { WrapIn(sw, "namespace " + group.GroupName, () => { StringBuilder interfacesNames = new StringBuilder(); StringBuilder interfaceDeclereration = new StringBuilder(); StringBuilder invokerListDecloration = new StringBuilder(); StringBuilder invokerMethodsDecloration = new StringBuilder(); foreach (var method in group) { //---------------------------------------------------------------------------------- var args = method.Args.Select( x => string.Format("{0} {1}", NameValidator.GetGenericTypeFullName(x.ArgType), x.ArgName)).ToArray(); var argsAsMethodArgs = args.Length == 0 ? "" : string.Join(",", args); var argsWithoutNames = method.Args.Select(x => NameValidator.GetGenericTypeName(x.ArgType)).ToArray(); var argTypes_AsName = argsWithoutNames.Length == 0 ? "" : string.Join("_", argsWithoutNames); argTypes_AsName = Regex.Replace(argTypes_AsName, "[^A-Za-z0-9 _]", ""); //---------------------------------------------------------------------------------- var interfaceName = (argsWithoutNames.Length > 0) ? string.Format("I{0}_{1}", @method.Name, argTypes_AsName) : string.Format("I{0}", @method.Name); interfaceDeclereration.AppendLine(string.Format("public interface {0} : Tools.IEventMethodBase{{ {1} }}", interfaceName, method.MethodFullSigneture)); invokerListDecloration.AppendLine(string.Format("static List<Methods.{0}> _users_{0} = new List<Methods.{0}>();", interfaceName)); invokerMethodsDecloration.AppendLine(string.Format("internal static void RegisterUser(Methods.{0} user){{", interfaceName)); invokerMethodsDecloration.AppendLine("if(user == null) return;"); invokerMethodsDecloration.AppendLine(string.Format("if(!_users_{0}.Contains(user)) _users_{0}.Add(user);\n}}", interfaceName)); invokerMethodsDecloration.AppendLine(string.Format("internal static void UnRegisterUser(Methods.{0} user){{", interfaceName)); invokerMethodsDecloration.AppendLine("if(user == null) return;"); invokerMethodsDecloration.AppendLine(string.Format("if(_users_{0}.Contains(user)) _users_{0}.Remove(user);\n}}", interfaceName)); invokerMethodsDecloration.AppendLine(string.Format("public static void {0}({1}){{", method.Name, argsAsMethodArgs)); invokerMethodsDecloration.AppendLine(string.Format("_users_{0}.ForEach(x=> x.{1}({2})); \n}}", interfaceName, method.Name, string.Join(",", method.Args.Select(x => x.ArgName).ToArray()))); // is used for the IAll_Group_Events interface interfacesNames.Append(string.Format("Methods.{0},", interfaceName)); //---------------------------------------------------------------------------------- } WrapIn(sw, "namespace Methods", () => { WriteToStream(sw, interfaceDeclereration.ToString()); }); //static WrapIn(sw, "public static class Invoke", () => { WriteToStream(sw, invokerListDecloration.ToString()); WriteToStream(sw, invokerMethodsDecloration.ToString()); }); //interface IAll_Group_Events sw.Write("public interface IAll_Group_Events"); if (!string.IsNullOrEmpty(interfacesNames.ToString())) { interfacesNames = interfacesNames.Remove(interfacesNames.Length - 1, 1); //remove the "," at the end of the string builder WriteToStream(sw, ":", interfacesNames.ToString()); } sw.Write("{ }\n"); }); //end namespace {GroupName} warp } }); //end namespace "Events.Groups" warp }
void DrawEvetArgsList() { ArgsScrollerPos = GUILayout.BeginScrollView(ArgsScrollerPos, false, true); if (!string.IsNullOrEmpty(ContainingGroup)) { for (int i = 0; i < Args.Count; i++) { GUILayout.BeginHorizontal(GUI.skin.FindStyle("Toolbar")); //move arg up button if (i == 0) { GUI.enabled = false; } if (GUILayout.Button("", GUI.skin.verticalScrollbarUpButton)) { var temparg = Args[i - 1]; Args[i - 1] = Args[i]; Args[i] = temparg; } if (i == 0) { GUI.enabled = true; } //move arg down button if (i == Args.Count - 1) { GUI.enabled = false; } if (GUILayout.Button("", GUI.skin.verticalScrollbarDownButton)) { var temparg = Args[i + 1]; Args[i + 1] = Args[i]; Args[i] = temparg; } if (i == Args.Count - 1) { GUI.enabled = true; } //write arg number EditorGUILayout.LabelField(i.ToString(), GUILayout.Width(ArgsListWidth_number_presentage * position.width)); //write the arg type var argTypeString = NameValidator.GetGenericTypeName(Args[i].ArgType); EditorGUILayout.LabelField( new GUIContent(argTypeString, argTypeString), GUILayout.Width(ArgsListWidth_type_presentage * position.width)); //write the arg name EditorGUILayout.LabelField(new GUIContent(Args[i].ArgName, Args[i].ArgName), GUILayout.Width(ArgsListWidth_name_presentage * position.width)); //remove arg button if (GUILayout.Button("", GUI.skin.FindStyle("ToolbarSeachCancelButton"))) { Args.RemoveAt(i); } GUILayout.EndHorizontal(); } } EditorGUILayout.EndScrollView(); }