public bool Run(string callString) { string[] safeMethods = callString.Split('\r'); foreach (string stringToCall in safeMethods) { _errorMessage = ""; Regex methods = new Regex(@"(?<Client>[a-zA-Z]{1}[a-zA-Z0-9]{0,})(?<dot1>[\.]{1})(?<Class>[a-zA-Z]{1}[a-zA-Z0-9]{0,})(?<dot2>[\.]{1})(?<Function>[a-zA-Z]{1}[a-zA-Z0-9]{0,})(?<lp>[(]{1})(?<Params>.{0,})(?<rp>[)]{1}[;]?)"); MatchCollection mcMethods = methods.Matches(stringToCall); for (int i = 0; i < mcMethods.Count; i++) { bool status = mcMethods[i].Success; if (!status) { continue; } string clientName = mcMethods[i].Groups["Client"].Value.Trim(); string className = mcMethods[i].Groups["Class"].Value.Trim(); string methodName = mcMethods[i].Groups["Function"].Value.Trim(); string param = mcMethods[i].Groups["Params"].Value.Trim(); object[] paramList = ParseFunctionParams(className + "." + methodName, param); object definedClass; SubMember func = null; Definitions.ClassObjects.TryGetValue(clientName + "." + className, out definedClass); if (definedClass != null) { Definitions.ClassMembers.TryGetValue(className + "." + methodName, out func); } try { func.MethodInfo.Invoke(definedClass, paramList); } catch (Exception e) { _errorMessage = e.Message; return(false); } } } return(true); }
public void Add(string className, ref object definedObject, string instanceName) { if (DefinedClasses.Contains(className)) { if (!ClassObjects.ContainsKey(instanceName + "." + className)) { ClassObjects.Add(instanceName + "." + className, definedObject); } return; } DefinedClasses.Add(className); Type t = definedObject.GetType(); ClassObjects[instanceName + "." + className] = definedObject; System.Reflection.MethodInfo[] methods = t.GetMethods(); bool enablePokeinSafety = false; System.Reflection.FieldInfo fi = t.GetField("PokeInSafe"); if (fi != null) { enablePokeinSafety = Convert.ToBoolean(fi.GetValue(definedObject)); } StringBuilder sbJson = new StringBuilder(); sbJson.Append("function "); sbJson.Append(className); sbJson.Append("(){}"); for (int i = 0, ml = methods.Length; i < ml; i++) { if (methods[i].IsPrivate) { continue; } if (methods[i].ReturnParameter.ParameterType != typeof(void)) { continue; } if (enablePokeinSafety) { if (!methods[i].Name.StartsWith("__")) { continue; } } System.Reflection.ParameterInfo[] paramz = methods[i].GetParameters(); bool isCompatible = true; foreach (System.Reflection.ParameterInfo param in paramz) { if (!param.ParameterType.IsSerializable) { isCompatible = false; break; } if (param.ParameterType == typeof(EventArgs)) { isCompatible = false; break; } } if (!isCompatible) { continue; } SubMember sm = new SubMember(); string completeName = className + "." + methods[i].Name; sbJson.Append(completeName); sbJson.Append("=function("); bool isFirst = true; List <string> stringList = new List <string>(); List <string> letterList = new List <string>(); int indexer = 0; StringBuilder letterz = new StringBuilder(); foreach (System.Reflection.ParameterInfo param in paramz) { if (!isFirst) { letterz.Append(","); } sm.ParameterTypes.Add(param.ParameterType); string paramName = "a" + (indexer).ToString(); letterz.Append(paramName); if (param.ParameterType == typeof(String)) { stringList.Add(paramName + "=PokeIn.StrFix(" + paramName + ");"); } letterList.Add(paramName); isFirst = false; indexer++; } sbJson.Append(letterz.ToString(0, letterz.Length)); sbJson.Append("){"); foreach (string str in stringList) { sbJson.Append(str); } sbJson.Append("PokeIn.Send(PokeIn.GetClientId() + \"."); sbJson.Append(completeName + "("); isFirst = true; foreach (string strLetter in letterList) { if (!isFirst) { sbJson.Append("+\","); } sbJson.Append("\"+" + strLetter); isFirst = false; } if (!isFirst) { sbJson.Append("+\""); } sbJson.Append(");\");}\n"); lock (_json) { _json += sbJson.ToString(0, sbJson.Length); } sm.SetMethod(methods[i]); if (!ClassMembers.ContainsKey(completeName)) { ClassMembers.Add(completeName, sm); } } }
public void Add(string className, ref object definedObject, string instanceName) { if (DefinedClasses.Contains(className)) { if (!ClassObjects.ContainsKey(instanceName + "." + className)) { ClassObjects.Add(instanceName + "." + className, definedObject); } return; } DefinedClasses.Add(className); Type t = definedObject.GetType(); ClassObjects[instanceName + "." + className] = definedObject; System.Reflection.MethodInfo[] methods = t.GetMethods(); bool enablePokeinSafety = false; System.Reflection.FieldInfo fi = t.GetField("PokeInSafe") ; if (fi != null) { enablePokeinSafety = Convert.ToBoolean( fi.GetValue(definedObject) ); } StringBuilder sbJson = new StringBuilder(); sbJson.Append("function "); sbJson.Append(className); sbJson.Append("(){}"); for (int i = 0, ml = methods.Length; i < ml; i++) { if (methods[i].IsPrivate) continue; if (methods[i].ReturnParameter.ParameterType != typeof(void)) continue; if (enablePokeinSafety) { if (!methods[i].Name.StartsWith("__")) continue; } System.Reflection.ParameterInfo[] paramz = methods[i].GetParameters(); bool isCompatible = true; foreach (System.Reflection.ParameterInfo param in paramz) { if (!param.ParameterType.IsSerializable) { isCompatible = false; break; } if (param.ParameterType == typeof(EventArgs) ) { isCompatible = false; break; } } if (!isCompatible) continue; SubMember sm = new SubMember(); string completeName = className + "." + methods[i].Name; sbJson.Append(completeName); sbJson.Append("=function("); bool isFirst = true; List<string> stringList = new List<string>(); List<string> letterList = new List<string>(); int indexer = 0; StringBuilder letterz = new StringBuilder(); foreach (System.Reflection.ParameterInfo param in paramz) { if(!isFirst) { letterz.Append(","); } sm.ParameterTypes.Add(param.ParameterType); string paramName = "a"+(indexer).ToString(); letterz.Append(paramName); if (param.ParameterType == typeof(String)) { stringList.Add(paramName + "=PokeIn.StrFix(" + paramName + ");"); } letterList.Add(paramName); isFirst = false; indexer++; } sbJson.Append(letterz.ToString(0,letterz.Length)); sbJson.Append("){"); foreach (string str in stringList) sbJson.Append(str); sbJson.Append("PokeIn.Send(PokeIn.GetClientId() + \"."); sbJson.Append(completeName + "("); isFirst = true; foreach (string strLetter in letterList) { if(!isFirst) { sbJson.Append( "+\"," ); } sbJson.Append( "\"+" + strLetter ); isFirst = false; } if (!isFirst) { sbJson.Append( "+\"" ); } sbJson.Append( ");\");}\n" ); lock (_json) { _json += sbJson.ToString(0,sbJson.Length); } sm.SetMethod(methods[i]); if(!ClassMembers.ContainsKey(completeName)) ClassMembers.Add(completeName, sm); } }