/// <summary> /// Invoke the function specified by the ID. /// </summary> static public bool ExecuteAll(List <CachedFunc> rfcs, byte funcID, params object[] parameters) { for (int i = 0; i < rfcs.size; ++i) { CachedFunc ent = rfcs[i]; if (ent.id == funcID) { if (ent.parameters == null) { ent.parameters = ent.func.GetParameters(); } try { if (ent.parameters.Length == 1 && ent.parameters[0].ParameterType == typeof(object[])) { ent.func.Invoke(ent.obj, new object[] { parameters }); } else { ent.func.Invoke(ent.obj, parameters); } return(true); } catch (System.Exception ex) { PrintException(ex, ent, funcID, "", parameters); return(false); } } } return(false); }
/// <summary> /// Rebuild the list of known RFC calls. /// </summary> void RebuildMethodList() { rebuildMethodList = false; mDict0.Clear(); mDict1.Clear(); MonoBehaviour[] mbs = GetComponentsInChildren <MonoBehaviour>(true); for (int i = 0, imax = mbs.Length; i < imax; ++i) { MonoBehaviour mb = mbs[i]; System.Type type = mb.GetType(); MethodInfo[] methods = type.GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); for (int b = 0, bmax = methods.Length; b < bmax; ++b) { MethodInfo method = methods[b]; if (method.IsDefined(typeof(RFC), true)) { CachedFunc ent = new CachedFunc(); ent.obj = mb; ent.func = method; RFC tnc = (RFC)ent.func.GetCustomAttributes(typeof(RFC), true)[0]; if (tnc.id > 0) { if (tnc.id < 256) { mDict0[tnc.id] = ent; } else { Debug.LogError("RFC IDs need to be between 1 and 255 (1 byte). If you need more, just don't specify an ID and use the function's name instead."); } } else { mDict1[method.Name] = ent; } } } } }
/// <summary> /// Invoke the function specified by the function name. /// </summary> static public bool ExecuteAll(List <CachedFunc> rfcs, string funcName, params object[] parameters) { bool retVal = false; for (int i = 0; i < rfcs.size; ++i) { CachedFunc ent = rfcs[i]; if (ent.func.Name == funcName) { retVal = true; if (ent.parameters == null) { ent.parameters = ent.func.GetParameters(); } try { if (ent.parameters.Length == 1 && ent.parameters[0].ParameterType == typeof(object[])) { ent.func.Invoke(ent.obj, new object[] { parameters }); } else { ent.func.Invoke(ent.obj, parameters); } return(true); } catch (System.Exception ex) { PrintException(ex, ent, 0, funcName, parameters); } } } return(retVal); }
/// <summary> /// Print out useful information about an exception that occurred when trying to call a function. /// </summary> static public void PrintException(System.Exception ex, CachedFunc ent, int funcID, string funcName, params object[] parameters) { string received = ""; if (parameters != null) { for (int b = 0; b < parameters.Length; ++b) { if (b != 0) { received += ", "; } received += (parameters[b] != null) ? parameters[b].GetType().ToString() : "<null>"; } } string expected = ""; if (ent.parameters != null) { for (int b = 0; b < ent.parameters.Length; ++b) { if (b != 0) { expected += ", "; } expected += ent.parameters[b].ParameterType.ToString(); } } string err = "[TNet] Failed to call "; if (ent.obj != null && ent.obj is TNBehaviour) { TNBehaviour tb = ent.obj as TNBehaviour; err += "TNO #" + tb.tno.uid + " "; } if (string.IsNullOrEmpty(funcName)) { err += "function #" + funcID + " on " + (ent.obj != null ? ent.obj.GetType().ToString() : "<null>"); } else if (ent.obj != null) { err += "function " + ent.obj.GetType() + "." + funcName; } else { err += "function " + funcName; } if (ex.InnerException != null) { err += ": " + ex.InnerException.Message + "\n"; } else { err += ": " + ex.Message + "\n"; } if (received != expected) { err += " Expected args: " + expected + "\n"; err += " Received args: " + received + "\n\n"; } if (ex.InnerException != null) { err += ex.InnerException.StackTrace + "\n"; } else { err += ex.StackTrace + "\n"; } Debug.LogError(err, ent.obj as Object); }
/// <summary> /// Add a new Remote Creation Call. /// </summary> static void AddRCC(object obj, System.Type type) { MethodInfo[] methods = type.GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); for (int b = 0; b < methods.Length; ++b) { if (methods[b].IsDefined(typeof(RCC), true)) { CachedFunc ent = new CachedFunc(); ent.obj = obj; ent.func = methods[b]; RCC tnc = (RCC)ent.func.GetCustomAttributes(typeof(RCC), true)[0]; ent.id = tnc.id; mRCCs.Add(ent); } } }
/// <summary> /// Rebuild the list of known RFC calls. /// </summary> void RebuildMethodList() { rebuildMethodList = false; mRFCs.Clear(); MonoBehaviour[] mbs = GetComponentsInChildren<MonoBehaviour>(true); for (int i = 0, imax = mbs.Length; i < imax; ++i) { MonoBehaviour mb = mbs[i]; System.Type type = mb.GetType(); MethodInfo[] methods = type.GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); for (int b = 0; b < methods.Length; ++b) { if (methods[b].IsDefined(typeof(RFC), true)) { CachedFunc ent = new CachedFunc(); ent.obj = mb; ent.func = methods[b]; RFC tnc = (RFC)ent.func.GetCustomAttributes(typeof(RFC), true)[0]; ent.id = tnc.id; mRFCs.Add(ent); } } } }
/// <summary> /// Print out useful information about an exception that occurred when trying to call a function. /// </summary> static void PrintException(System.Exception ex, CachedFunc ent, params object[] parameters) { string types = ""; if (parameters != null) { for (int b = 0; b < parameters.Length; ++b) { if (b != 0) types += ", "; types += parameters[b].GetType().ToString(); } } Debug.LogError(ex.Message + "\n" + ent.obj.GetType() + "." + ent.func.Name + " (" + types + ")"); }