public static void Initialize()
    {
        if (s_initialized)
        {
            return;
        }
        s_initialized = true;

        IEnumerable <Type> gameActionTypes = TypeUtility.GetTypesDerivedFrom(typeof(GameAction));

        ushort id = 1; // 0 is invalid

        foreach (Type gameActionType in gameActionTypes)
        {
            if (gameActionType.IsAbstract)
            {
                continue;
            }

            GameAction instance = (GameAction)Activator.CreateInstance(gameActionType);

            TypeId.Get(gameActionType) = id;

            s_idToGameAction.Add(id, instance);
            s_nameToGameAction.Add(gameActionType.Name, instance);

            id++;
        }
    }
Пример #2
0
        public static byte[] Serialize(object msg)
        {
            var type        = msg.GetType();
            var ser         = new DataContractSerializer(type);
            var typeName    = TypeId.Get(type);
            var nameBytes   = Encoding.UTF8.GetBytes(typeName);
            var lengthBytes = BitConverter.GetBytes(nameBytes.Length);

            using (var ms = new MemoryStream())
            {
                ms.Write(lengthBytes, 0, lengthBytes.Length);
                ms.Write(nameBytes, 0, nameBytes.Length);
                ser.WriteObject(ms, msg);
                return(ms.ToArray());
            }
        }
Пример #3
0
        // Public methods
        ///////////////////////////

        public EntityId RegisterDataType <T>()
        {
            var type      = typeof(T);
            var component = tracker.Create();

            index.Register(component, type);
            var typeIndex = TypeId.Get <T>();

            if (dataTypes.Length <= typeIndex)
            {
                var newSize = dataTypes.Length * 2;
                while (newSize <= typeIndex)
                {
                    newSize *= 2;
                }
                Array.Resize(ref dataTypes, newSize);
            }
            dataTypes[typeIndex] = component;
            return(component);
        }
 public static GameActionId GetActionId(Type gameActionType)
 {
     return(new GameActionId {
         Value = TypeId.Get(gameActionType)
     });
 }