public static void Replicate(object obj)
        {
            Debug.Assert(Static != null);
            Debug.Assert(Static.m_peer is MyRakNetServer);
            Debug.Assert(Static.m_idToType.Contains(obj.GetType()));

            int typeID = Static.m_typeToId[obj.GetType()];

            uint networkID = Static.GetNetworkUniqueID();

            BitStream bs = new BitStream(null);

            bs.Write((byte)MessageIDEnum.REPLICATION_CREATE);
            bs.WriteCompressed(typeID); // TODO:SK better compression
            bs.WriteCompressed(networkID);

            MySyncedClass sync = GetSyncedClass(obj);

            sync.TypeID = typeID;

            Static.AddNetworkedObject(networkID, obj, sync);

            sync.SerializeDefault(bs);

            ((MyRakNetServer)Static.m_peer).BroadcastMessage(bs, PacketPriorityEnum.LOW_PRIORITY, PacketReliabilityEnum.RELIABLE);
        }
        internal void ProcessReplication(BitStream bs)
        {
            bool success;

            int typeID;

            success = bs.ReadCompressed(out typeID);
            Debug.Assert(success, "Failed to read replication type ID");
            Debug.Assert(typeID < m_idToType.Count, "Invalid replication type ID");

            Type type = m_idToType[typeID];

            long tmpLong;

            success = bs.Read(out tmpLong);
            Debug.Assert(success, "Failed to read entityID");
            ulong entityID = (ulong)tmpLong;

            object obj = Activator.CreateInstance(type);
            // should init here
            MySyncedClass sync = GetSyncedClass(obj);

            sync.entityId = entityID;
            sync.Deserialize(bs);

            m_registered.Add(entityID, sync);

            var handle = OnEntityCreated;

            if (handle != null)
            {
                handle(obj, entityID);
            }
        }
        public static void Replicate(object obj, ulong entityID)
        {
            Debug.Assert(Static != null);
            Debug.Assert(Static.m_peer is MyRakNetServer);
            Debug.Assert(!Static.m_stateData.ContainsKey(entityID));
            Debug.Assert(Static.m_idToType.Contains(obj.GetType()));

            int typeID = Static.m_typeToId[obj.GetType()];

            BitStream bs = new BitStream(null);

            bs.Write((byte)MessageIDEnum.REPLICATION_CREATE);
            bs.WriteCompressed(typeID); // TODO:SK better compression
            bs.Write((long)entityID);

            MySyncedClass syncedClass = GetSyncedClass(obj);

            syncedClass.TypeID   = typeID;
            syncedClass.EntityId = entityID;
            syncedClass.Serialize(bs);

            RegisterSynced(syncedClass);

            ((MyRakNetServer)Static.m_peer).BroadcastMessage(bs, PacketPriorityEnum.LOW_PRIORITY, PacketReliabilityEnum.RELIABLE, 0, RakNetGUID.UNASSIGNED_RAKNET_GUID);
        }
 public MyStateDataEntry(uint networkID, MySyncedClass sync)
 {
     Priority  = 0;
     PacketID  = 0;
     NetworkID = networkID;
     Sync      = sync;
 }
        internal void ProcessReplicationCreate(BitStream bs)
        {
            bool success;

            int typeID;

            success = bs.ReadCompressed(out typeID);
            Debug.Assert(success, "Failed to read replication type ID");
            Debug.Assert(typeID < m_idToType.Count, "Invalid replication type ID");

            Type type = m_idToType[typeID];

            uint networkID;

            success = bs.ReadCompressed(out networkID);
            Debug.Assert(success, "Failed to read networkID");

            object        obj  = Activator.CreateInstance(type);
            MySyncedClass sync = GetSyncedClass(obj);

            sync.TypeID = typeID;
            sync.Deserialize(bs);
            // TODO:SK should init here

            AddNetworkedObject(networkID, obj, sync);

            var handle = OnEntityCreated;

            if (handle != null)
            {
                handle(obj);
            }
        }
 public void Add(MySyncedClass mySyncedObject)
 {
     mySyncedObject.m_parent = this;
     m_syncedObjects.Add(mySyncedObject);
     if (mySyncedObject.IsDirty)
     {
         Invalidate();
     }
 }
 public void Add(MySyncedClass mySyncedObject)
 {
     mySyncedObject.m_parent = this;
     m_syncedObjects.Add(mySyncedObject);
     if (mySyncedObject.IsDirty)
     {
         Invalidate();
     }
 }
        public static void RegisterSynced(MySyncedClass mySyncedClass)
        {
            Debug.Assert(mySyncedClass.entityId != 0);
            Debug.Assert(Static != null);
            Debug.Assert(!Static.m_registered.ContainsKey(mySyncedClass.entityId));
            Debug.Assert(!Static.m_registered.ContainsValue(mySyncedClass));

            Static.m_registered.Add(mySyncedClass.entityId, mySyncedClass);
        }
Example #9
0
 public void Add(MySyncedClass mySyncedObject)
 {
     mySyncedObject.SetParent(this);
     m_syncedClass.Add(mySyncedObject);
     if (!mySyncedObject.IsDefault())
     {
         Invalidate();
     }
 }
Example #10
0
 public void Add(MySyncedClass mySyncedObject)
 {
     mySyncedObject.SetParent(this);
     m_syncedClass.Add(mySyncedObject);
     if (!mySyncedObject.IsDefault())
     {
         Invalidate();
     }
 }
Example #11
0
        public static void RegisterSynced(MySyncedClass mySyncedClass)
        {
            Debug.Assert(mySyncedClass.EntityId != 0);
            Debug.Assert(Static != null);
            Debug.Assert(!Static.m_stateData.ContainsKey(mySyncedClass.EntityId));

            //mySyncedClass.Init

            Static.m_stateData.Add(mySyncedClass.EntityId, mySyncedClass);
        }
        internal void ProcessSync(BitStream bs)
        {
            uint networkID;
            bool success = bs.ReadCompressed(out networkID);

            Debug.Assert(success, "Failed to read networkID");

            MySyncedClass mySyncedObject = GetNetworkedSync(networkID);

            if (mySyncedObject != null)
            {
                mySyncedObject.Deserialize(bs);
            }
        }
        internal static void Sync(MySyncedClass mySyncedClass)
        {
            Debug.Assert(mySyncedClass.entityId != 0);
            Debug.Assert(Static != null);
            Debug.Assert(Static.m_peer is MyRakNetServer);

            BitStream bs = new BitStream(null);

            bs.Write((byte)MessageIDEnum.SYNC_FIELD);
            bs.Write((long)mySyncedClass.entityId);
            mySyncedClass.Serialize(bs);

            ((MyRakNetServer)Static.m_peer).BroadcastMessage(bs, PacketPriorityEnum.LOW_PRIORITY, PacketReliabilityEnum.UNRELIABLE, 0, RakNetGUID.UNASSIGNED_RAKNET_GUID);
        }
        private static void RegisterSynced(uint networkID, MySyncedClass sync)
        {
            Debug.Assert(networkID != 0); // ??
            Debug.Assert(Static != null);
            Debug.Assert(!Static.m_stateData.ContainsKey(networkID));

            Static.m_stateData.Add(networkID, sync);
            foreach (var pair in Static.m_perPlayerStateData)
            {
                var steamID   = pair.Key;
                var stateData = pair.Value;

                stateData.Add(new MyStateDataEntry(networkID, sync));
            }
        }
        internal void ProcessSync(BitStream bs)
        {
            long tmpLong;
            bool success = bs.Read(out tmpLong);

            Debug.Assert(success, "Failed to read entityID");
            ulong entityID = (ulong)tmpLong;

            MySyncedClass mySyncedObject = GetEntitySyncFields(entityID);

            if (mySyncedObject != null)
            {
                mySyncedObject.Deserialize(bs);
            }
        }
        // TODO:sk reflection should be cached if possible
        private static MySyncedClass GetSyncedClass(object obj, Type type = null)
        {
            Debug.Assert(tmpFields.Count == 0);
            MySyncedClass sync = new MySyncedClass();

            type = type ?? obj.GetType();

            Type baseType = type.BaseType;

            if (baseType != null && baseType != typeof(object))
            {
                sync.Add(GetSyncedClass(obj, baseType));
            }

            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (var field in fields)
            {
                //var baseFieldType = field.FieldType.BaseType;
                //if (baseFieldType != null && baseFieldType.IsGenericType && baseFieldType.GetGenericTypeDefinition() == typeof(MySyncedBase<>))

                var fieldAttributes = field.GetCustomAttributes(false);

                foreach (var fieldAttribute in fieldAttributes)
                {
                    StateDataAttribute stateData = fieldAttribute as StateDataAttribute;
                    if (stateData != null)
                    {
                        int order = stateData.Order;
                        Debug.Assert(!tmpFields.ContainsKey(order));

                        object value = field.GetValue(obj);
                        Debug.Assert(value != null, "Uninitialized synced variable");
                        Debug.Assert(!tmpFields.ContainsValue(value));

                        tmpFields.Add(order, value);
                    }
                }
            }

            foreach (var value in tmpFields.Values)
            {
                sync.Add((IMySyncedValue)value);
            }
            tmpFields.Clear();

            return(sync);
        }
        private static void Sync(uint networkID, MySyncedClass sync, int clientIndex)
        {
            Debug.Assert(networkID != 0);
            Debug.Assert(Static != null);
            Debug.Assert(Static.m_peer is MyRakNetServer);

            BitStream bs = new BitStream(null);

            bs.Write((byte)MessageIDEnum.SYNC_FIELD);
            bs.WriteCompressed(networkID);
            sync.Serialize(bs, clientIndex);

            var steamID = Static.m_clientIndexToSteamID[clientIndex];

            // TODO:SK use UNRELIABLE_WITH_ACK_RECEIPT
            var packetID = ((MyRakNetServer)Static.m_peer).SendMessage(bs, steamID, PacketPriorityEnum.LOW_PRIORITY, PacketReliabilityEnum.UNRELIABLE);
        }
Example #18
0
        // TODO:SK reflection should be cached if possible
        private static MySyncedClass GetSyncedClass(object obj, Type type = null)
        {
            Debug.Assert(tmpFields.Count == 0);
            MySyncedClass sync = null;
            MySyncedClass baseSync = null;

            type = type ?? obj.GetType();

            Type baseType = type.BaseType;

            if (baseType != null && baseType != typeof(object))
            {
                baseSync = GetSyncedClass(obj, baseType);
            }

            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (var field in fields)
            {
                //var baseFieldType = field.FieldType.BaseType;
                //if (baseFieldType != null && baseFieldType.IsGenericType && baseFieldType.GetGenericTypeDefinition() == typeof(MySyncedBase<>))

                var fieldAttributes = field.GetCustomAttributes(false);

                foreach (var fieldAttribute in fieldAttributes)
                {
                    StateDataAttribute stateData = fieldAttribute as StateDataAttribute;
                    if (stateData != null)
                    {
                        int order = stateData.Order;
                        Debug.Assert(!tmpFields.ContainsKey(order));

                        object value = field.GetValue(obj);
                        Debug.Assert(value != null, "Uninitialized synced variable");
                        Debug.Assert(!tmpFields.ContainsValue(value));

                        tmpFields.Add(order, value);
                    }
                }
            }

            if (baseSync != null && tmpFields.Count == 0)
            {
                return baseSync;
            }
            else
            {
                sync = new MySyncedClass();
                if (baseSync != null)
                {
                    sync.Add(baseSync);
                }
                foreach (var value in tmpFields.Values)
                {
                    sync.Add((IMySyncedValue)value);
                }
            }
            tmpFields.Clear();

            return sync;
        }
 private void AddNetworkedObject(uint networkID, object obj, MySyncedClass sync)
 {
     m_objectToNetworkID.Add(obj, networkID);
     m_networkIDToObject.Add(networkID, obj);
     RegisterSynced(networkID, sync);
 }
Example #20
0
 public void SetParent(MySyncedClass parent)
 {
     Debug.Assert(m_parent == null);
     m_parent = parent;
 }
Example #21
0
 public void SetParent(MySyncedClass parent)
 {
     m_parent = parent;
 }
Example #22
0
        public static void RegisterSynced(MySyncedClass mySyncedClass)
        {
            Debug.Assert(mySyncedClass.EntityId != 0);
            Debug.Assert(Static != null);
            Debug.Assert(!Static.m_stateData.ContainsKey(mySyncedClass.EntityId));

            //mySyncedClass.Init

            Static.m_stateData.Add(mySyncedClass.EntityId, mySyncedClass);
        }
 public MySyncedClass(MySyncedClass parent = null)
 {
     m_parent = parent;
     Invalidate();
 }
Example #24
0
        internal static void Sync(MySyncedClass mySyncedClass)
        {
            Debug.Assert(mySyncedClass.EntityId != 0);
            Debug.Assert(Static != null);
            Debug.Assert(Static.m_peer is MyRakNetServer);

            BitStream bs = new BitStream(null);
            bs.Write((byte)MessageIDEnum.SYNC_FIELD);
            bs.Write((long)mySyncedClass.EntityId);
            mySyncedClass.Serialize(bs);

            // TODO:SK use UNRELIABLE_WITH_ACK_RECEIPT
            ((MyRakNetServer)Static.m_peer).BroadcastMessage(bs, PacketPriorityEnum.LOW_PRIORITY, PacketReliabilityEnum.UNRELIABLE, 0, RakNetGUID.UNASSIGNED_RAKNET_GUID);
        }
Example #25
0
 public void SetParent(MySyncedClass parent)
 {
     m_parent = parent;
 }
 public MySyncedClass(MySyncedClass parent = null)
 {
     m_parent = parent;
     Invalidate();
 }
        public static void RegisterSynced(MySyncedClass mySyncedClass)
        {
            Debug.Assert(mySyncedClass.entityId != 0);
            Debug.Assert(Static != null);
            Debug.Assert(!Static.m_registered.ContainsKey(mySyncedClass.entityId));
            Debug.Assert(!Static.m_registered.ContainsValue(mySyncedClass));

            Static.m_registered.Add(mySyncedClass.entityId, mySyncedClass);
        }