Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Reflection;
- public static class NetworkExtensions {
- /// <summary>
- /// If the server is using RPCMode.Server to call itself it will call 'function', otherwise NetworkView.RPC will be used.
- /// </summary>
- public static void RPC(this NetworkView networkView, Action function, RPCMode mode, params object[] args) {
- if(function == null) return;
- if(mode == RPCMode.Server && Network.isServer)
- function();
- else
- networkView.RPC(function.Method.Name, mode, args);
- }
- /// <summary>
- /// If the server is using RPCMode.Server to call itself it will manually find the method to call, otherwise NetworkView.RPC will be used.
- /// </summary>
- public static void RPCDirect(this NetworkView networkView, string name, RPCMode mode, params object[] args) {
- if(mode == RPCMode.Server && Network.isServer) {
- object obj = null;
- MethodInfo function = null;
- foreach(MonoBehaviour a in networkView.GetComponents<MonoBehaviour>()) {
- Type type = a.GetType();
- foreach(MethodInfo method in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
- if(Attribute.IsDefined(method, typeof(RPC)) && method.Name == name) {
- ParameterInfo[] parameters = method.GetParameters();
- object[] arguments = (object[])args.Clone();
- Type networkMessageInfoType = typeof(NetworkMessageInfo);
- if(parameters.Length > 0 && parameters[parameters.Length - 1].ParameterType == networkMessageInfoType)
- if(!(arguments.Length > 0 && args[arguments.Length - 1].GetType() == networkMessageInfoType)) {
- Array.Resize<object>(ref arguments, arguments.Length + 1);
- arguments[parameters.Length - 1] = new NetworkMessageInfo();
- }
- if(parameters.Length == arguments.Length) {
- bool allTypesCorrect = true;
- for(int i = 0; i < parameters.Length; i++)
- if(parameters[i].ParameterType != arguments[i].GetType()) {
- allTypesCorrect = false;
- break;
- }
- if(allTypesCorrect) {
- obj = a;
- function = method;
- args = arguments;
- break;
- }
- }
- }
- }
- if(function != null) break;
- }
- if(obj != null && function != null)
- function.Invoke(obj, args);
- else
- Debug.LogError("Method (" + name + ") not found.", networkView);
- }
- else
- networkView.RPC(name, mode, args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement