Advertisement
GibTreaty

Network Extensions

Dec 22nd, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Reflection;
  4.  
  5. public static class NetworkExtensions {
  6.     /// <summary>
  7.     /// If the server is using RPCMode.Server to call itself it will call 'function', otherwise NetworkView.RPC will be used.
  8.     /// </summary>
  9.     public static void RPC(this NetworkView networkView, Action function, RPCMode mode, params object[] args) {
  10.         if(function == null) return;
  11.  
  12.         if(mode == RPCMode.Server && Network.isServer)
  13.             function();
  14.         else
  15.             networkView.RPC(function.Method.Name, mode, args);
  16.     }
  17.  
  18.     /// <summary>
  19.     /// If the server is using RPCMode.Server to call itself it will manually find the method to call, otherwise NetworkView.RPC will be used.
  20.     /// </summary>
  21.     public static void RPCDirect(this NetworkView networkView, string name, RPCMode mode, params object[] args) {
  22.         if(mode == RPCMode.Server && Network.isServer) {
  23.             object obj = null;
  24.             MethodInfo function = null;
  25.  
  26.             foreach(MonoBehaviour a in networkView.GetComponents<MonoBehaviour>()) {
  27.                 Type type = a.GetType();
  28.  
  29.                 foreach(MethodInfo method in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
  30.                     if(Attribute.IsDefined(method, typeof(RPC)) && method.Name == name) {
  31.                         ParameterInfo[] parameters = method.GetParameters();
  32.                         object[] arguments = (object[])args.Clone();
  33.  
  34.                         Type networkMessageInfoType = typeof(NetworkMessageInfo);
  35.  
  36.                         if(parameters.Length > 0 && parameters[parameters.Length - 1].ParameterType == networkMessageInfoType)
  37.                             if(!(arguments.Length > 0 && args[arguments.Length - 1].GetType() == networkMessageInfoType)) {
  38.                                 Array.Resize<object>(ref arguments, arguments.Length + 1);
  39.                                 arguments[parameters.Length - 1] = new NetworkMessageInfo();
  40.                             }
  41.  
  42.                         if(parameters.Length == arguments.Length) {
  43.                             bool allTypesCorrect = true;
  44.  
  45.                             for(int i = 0; i < parameters.Length; i++)
  46.                                 if(parameters[i].ParameterType != arguments[i].GetType()) {
  47.                                     allTypesCorrect = false;
  48.                                     break;
  49.                                 }
  50.  
  51.                             if(allTypesCorrect) {
  52.                                 obj = a;
  53.                                 function = method;
  54.                                 args = arguments;
  55.                                 break;
  56.                             }
  57.                         }
  58.                     }
  59.                 }
  60.  
  61.                 if(function != null) break;
  62.             }
  63.  
  64.             if(obj != null && function != null)
  65.                 function.Invoke(obj, args);
  66.             else
  67.                 Debug.LogError("Method (" + name + ") not found.", networkView);
  68.         }
  69.         else
  70.             networkView.RPC(name, mode, args);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement