Esempio n. 1
0
        private void InitializeSyncedValues()
        {
            if (_syncedValues != null)
            {
                throw new InvalidOperationException($"{nameof(SyncedValue)}s on the {nameof(SyncedValueSerializer)} instance have already been initialized.");
            }

            var fields = ReflectionCache.GetSyncedValueFields(_context.GetType());

            _syncedValues = new List <SyncedValue>();
            foreach (var field in fields)
            {
                _syncedValues.Add((SyncedValue)field.GetValue(_context));
            }
        }
Esempio n. 2
0
        private void InitializeRpcs()
        {
            _rpcCodeLookup   = new Dictionary <short, MethodInfo>();
            _rpcMethodLookup = new Dictionary <string, short>();

            var type    = _context.GetType();
            var methods = ReflectionCache.GetRpcMethods(type);

            for (short i = 0; i < methods.Length; i++)
            {
                var method = methods[i];
                if (_rpcMethodLookup.ContainsKey(method.Name))
                {
                    throw new NotImplementedException($"Detected overloaded RPC method '{method.Name}' " +
                                                      $"on component type '{type.FullName}'. Overloads are not supported yet, so please make sure " +
                                                      "that there aren't multiple RPC methods with the same name on the same component.");
                }

                _rpcCodeLookup[i]             = method;
                _rpcMethodLookup[method.Name] = i;
            }

            _rpcSubscription = _context.Entity.Manager.OnEvent(DefaultEvents.Rpc)
                               .Select(evt => (RpcCall)evt.Data)
                               .Where(rpc => rpc.EntityId == _context.Entity.Id && rpc.ComponentId == _context.Id)
                               .Subscribe(rpc =>
            {
                if (_rpcCodeLookup.ContainsKey(rpc.RpcCode))
                {
                    var method = _rpcCodeLookup[rpc.RpcCode];
                    method.Invoke(_context, rpc.Params);
                }
                else
                {
                    Debug.LogWarning($"No RPC method found with code {rpc.RpcCode} " +
                                     $"on component {_context} on entity {_context.Entity}");
                }
            });
        }