Beispiel #1
0
        public byte[] GetFullState()
        {
            var writer = ByteArrayWriter.Get();

            GetFullState(writer);
            return(writer.ReleaseAndReturnData());
        }
Beispiel #2
0
        protected bool RaiseStateChangeEvent(Action <ByteArrayWriter> eventWriter)
        {
            if (eventWriter == null)
            {
                return(false);
            }

            var writer = ByteArrayWriter.Get();

            eventWriter(writer);

            if (!Game.Authoritative || writer.Size == 0 ||
                writer.Size > Game.Settings.MaxStateChangeSize ||
                (Game.Time - _lastStateChange) < Game.Settings.MaxStateChangeFrequency)
            {
                return(false);
            }

            _args.Gamemode = this;
            _args.State    = writer.Data;
            OnGamemodeStateChanged(this, _args);

            writer.Release();
            return(true);
        }
Beispiel #3
0
        protected bool RaiseStateChangeEvent(Action <ByteArrayWriter> eventWriter)
        {
            if (eventWriter == null)
            {
                return(false);
            }
            var writer = ByteArrayWriter.Get();

            eventWriter(writer);

            if (!Game.Authoritative || writer == null || writer.Size == 0 ||
                writer.Size > Game.Settings.MaxStateChangeSize ||
                (TimeAlive - _lastStateChange) < Game.Settings.MaxStateChangeFrequency ||
                !_eventsEnabled)
            {
                return(false);
            }

            _stateArgs.Object = this;
            _stateArgs.State  = writer.Data;
            Game.EventEngine.RaiseGameObjectStateChanged(_stateArgs);

            writer.Release();

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Stores up to 4 KB of data for a module in the persistent storage of the game.
        /// Can be arbitrary binary data.
        /// Note that this includes the length of the module name.
        /// E.g.
        /// 2 bytes for name length
        /// UTF8 encoded name
        /// 4 bytes major version
        /// 2 bytes contents length
        /// </summary>
        /// <param name="data"></param>
        /// <param name="module"></param>
        public static void SetPersistentData(byte[] data, Module module)
        {
            var offset = _offsetEnd;

            if (_infos.ContainsKey(GetInfo(module)))
            {
                offset = _offsetTable[GetInfo(module)];
            }

            var writer = ByteArrayWriter.Get();

            writer.Write(module.ModInfo.ModMajor);
            writer.Write(module.ModInfo.ModMinor);
            writer.Write(data);
            var serialized = writer.ReleaseAndReturnData();

            if (serialized.Length > 4096)
            {
                throw new Exception("Data length is greater than 4KB");
            }

            _infos[GetInfo(module)]       = data;
            _offsetTable[GetInfo(module)] = offset;

            var fs = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            fs.Seek(offset, SeekOrigin.Begin);
            fs.Write(serialized, 0, serialized.Length);
            fs.Flush();
            fs.Close();
            fs.Dispose();

            //Guard: check if the file's too large (larger than 16MB)
            //If so, clear persistent data
            if (new FileInfo(_fileName).Length > 16 * 1024 * 1024)
            {
                File.Delete(_fileName);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Gets the full state of the object, ergo.
        /// </summary>
        /// <returns></returns>
        public ByteArrayWriter GetFullState()
        {
            //And figure out which guid to print
            ushort playerUid = 0;

            if (GetType().IsSubclassOf(typeof(Tanks.Tank)))
            {
                playerUid = ((Tanks.Tank) this).Player.Id;
            }
            else if (GetType().IsSubclassOf(typeof(Projectiles.Projectile)))
            {
                playerUid = ((Projectiles.Projectile) this).Owner.Player.Id;
            }

            var writer = ByteArrayWriter.Get();

            writer.Write(ObjectId);
            writer.Write(ReflectionName);
            writer.Write((byte)GetSerializationType());
            writer.Write(playerUid);
            writer.Write(IsSensor);
            writer.Write(IsStatic);
            writer.Write(ColorMask);
            writer.Write(TimeAlive);
            writer.Write(Size);
            writer.Write(Position);
            writer.Write(LinearVelocity);
            writer.Write(Rotation);
            writer.Write(AngularVelocity);
            writer.Write(Restitution);
            writer.Write(Health);

            GetTypeStateHeader(writer);

            GetFullStateInternal(writer);
            return(writer);
        }