Esempio n. 1
0
        public override void Serialize(NetworkWriter writer)
        {
            writer.WriteByte(action);
            writer.WriteNetworkInstanceId(netObj.InstanceId);
            switch (action)
            {
            case Action_RpcClient:
            case Action_RpcServer:
                writer.WriteByte(rpcInfo.memberIndex);
                if (rpcInfo.paramCount > 0)
                {
                    for (int i = 0, j = 0, len = rpcInfo.paramCount; i < len; i++)
                    {
                        var pInfo = rpcInfo.parameters[i];

                        if (i == 0 && pInfo.ParameterType == typeof(NetworkConnection))
                        {
                            continue;
                        }
                        object arg = args[j++];
                        SyncVarMessage.Write(writer, pInfo.ParameterType, arg);
                    }
                }
                break;
            }
        }
Esempio n. 2
0
        public override void Deserialize(NetworkReader reader)
        {
            action = reader.ReadByte();

            if (action == 0)
            {
                throw new Exception("action is 0");
            }

            NetworkInstanceId instanceId;

            instanceId = reader.ReadNetworkInstanceId();

            netObj = null;

            netObj = conn.GetObject(instanceId);
            if (netObj == null)
            {
                return;
            }

            switch (action)
            {
            case Action_RpcClient:
            case Action_RpcServer:

                if (action == Action_RpcClient)
                {
                    if (!netObj.IsClient)
                    {
                        return;
                    }
                }
                else if (action == Action_RpcServer)
                {
                    if (!netObj.IsServer)
                    {
                        return;
                    }
                }

                byte memberIndex = reader.ReadByte();
                rpcInfo = RpcInfo.GetRpcInfo(netObj.GetType(), memberIndex);
                object[] args = null;
                if (rpcInfo.paramCount > 0)
                {
                    args = new object[rpcInfo.paramCount];
                    for (int i = 0; i < rpcInfo.paramCount; i++)
                    {
                        var pInfo = rpcInfo.parameters[i];
                        if (i == 0 && pInfo.ParameterType == typeof(NetworkConnection))
                        {
                            args[i] = conn;
                        }
                        else
                        {
                            args[i] = SyncVarMessage.Read(reader, pInfo.ParameterType);
                        }
                    }
                }

                rpcInfo.method.Invoke(netObj, args);
                break;
            }
        }
Esempio n. 3
0
        public static RpcInfo[] GetRpcInfos(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (cachedRpcs == null)
            {
                cachedRpcs = new Dictionary <Type, RpcInfo[]>();
            }
            RpcInfo[] infos;
            if (!cachedRpcs.TryGetValue(type, out infos))
            {
                List <RpcInfo> list = null;
                foreach (var mInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    var attr = (RpcAttribute)(mInfo.GetCustomAttributes(typeof(RpcAttribute), true).FirstOrDefault());
                    if (attr == null)
                    {
                        continue;
                    }

                    if (mInfo.ReturnType != typeof(void))
                    {
                        throw new Exception("method return type only [void]. " + mInfo);
                    }

                    foreach (var pInfo in mInfo.GetParameters())
                    {
                        if (pInfo.ParameterType == typeof(NetworkConnection))
                        {
                        }
                        else
                        {
                            if (!SyncVarMessage.CanSerializeType(pInfo.ParameterType))
                            {
                                throw new Exception("invalid parameter type :" + pInfo.ParameterType + "," + pInfo);
                            }
                        }
                        if (pInfo.IsOut)
                        {
                            throw new Exception("method parameter can't [out]. " + mInfo);
                        }
                    }

                    if (list == null)
                    {
                        list = new List <RpcInfo>();
                    }
                    RpcInfo info = new RpcInfo()
                    {
                        method     = mInfo,
                        parameters = mInfo.GetParameters()
                    };
                    info.paramCount = info.parameters.Length;
                    list.Add(info);
                }

                if (list != null && list.Count > 0)
                {
                    infos = list.OrderBy(o => o.method.Name).ToArray();
                    for (int i = 0; i < infos.Length; i++)
                    {
                        var info = infos[i];
                        info.memberIndex = (byte)i;
                    }
                    cachedRpcs[type] = infos;
                }
            }
            return(infos);
        }
Esempio n. 4
0
        public static SyncVarInfo[] GetSyncVarInfos(Type type)
        {
            if (cachedInfos == null)
            {
                cachedInfos = new Dictionary <Type, SyncVarInfo[]>();
            }

            SyncVarInfo[] infos;
            if (!cachedInfos.TryGetValue(type, out infos))
            {
                List <SyncVarInfo> list = null;
                foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    var syncVarAttr = field.GetCustomAttributes(typeof(SyncVarAttribute), true).FirstOrDefault() as SyncVarAttribute;
                    if (syncVarAttr == null)
                    {
                        continue;
                    }
                    SyncVarInfo info = new SyncVarInfo();
                    info.bits     = syncVarAttr.Bits;
                    info.field    = field;
                    info.typeCode = Type.GetTypeCode(field.FieldType);

                    if (!SyncVarMessage.CanSerializeType(field.FieldType))
                    {
                        throw new Exception("not implment type:" + field.FieldType);
                    }

                    if (!string.IsNullOrEmpty(syncVarAttr.ChangeCallback))
                    {
                        var mInfo = type.GetMethod(syncVarAttr.ChangeCallback, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                        if (mInfo == null)
                        {
                            throw new Exception("not found ChangedCallback method:" + syncVarAttr.ChangeCallback);
                        }
                        info.changeCallback = mInfo;
                    }

                    if (list == null)
                    {
                        list = new List <SyncVarInfo>();
                    }
                    info.index = list.Count();
                    list.Add(info);
                    if (list.Count > 32)
                    {
                        throw new Exception("max 32 sync var");
                    }
                }

                if (list != null)
                {
                    infos = new SyncVarInfo[list.Count];

                    int n = 0;
                    for (int i = 0; i < list.Count; i++)
                    {
                        var info = list[i];
                        if (info.bits == 0)
                        {
                            uint bits = 0, tmp;
                            for (; n < 32; n++)
                            {
                                tmp = (uint)(1 << n);
                                if (list.Where(o => (o.bits & tmp) != 0).Count() == 0)
                                {
                                    bits = tmp;
                                    n++;
                                    break;
                                }
                            }
                            if (bits == 0)
                            {
                                throw new Exception("not avalible bits");
                            }
                            info.bits = bits;
                        }
                        else
                        {
                            for (int j = i - 1; j >= 0; j--)
                            {
                                if (list[j].bits == info.bits)
                                {
                                    throw new Exception("type: " + type.Name + " , field: " + info.field + ", repeat bits " + info.bits);
                                }
                            }
                        }

                        infos[info.index] = info;
                    }
                }
                else
                {
                    infos = null;
                }
                cachedInfos[type] = infos;
            }
            return(infos);
        }