Example #1
0
        public IEnumerable <byte> Serialize()
        {
            //result = result.Concat(BitConverter.GetBytes((byte)Type));

            var result = CommonSerializer.StringToBytes(Shooter).Concat(CommonSerializer.Vector2ToBytes(Coordinates));

            return(result);
        }
Example #2
0
        public IEnumerable <byte> Serialize()
        {
            var result = CommonSerializer.StringToBytes(Name);

            result = result.Concat(BitConverter.GetBytes(Count));
            result = result.Concat(BitConverter.GetBytes(Weight));

            return(result);
        }
Example #3
0
        public void TestSaveXML()
        {
            var fileName = Path.Combine(tmpDir, "example.xml");
            var test     = new SerializableObject();

            test.ValueA = "Hallo";
            test.ValueB = 1;

            CommonSerializer.SaveAsXMLFile(fileName, test);
            Assert.IsTrue(File.Exists(fileName));
        }
Example #4
0
        public void DeSerialize(ref int position, byte[] dataBytes)
        {
            if (dataBytes[position++] == 0)
            {
                return;
            }

            Rowers         = CommonSerializer.GetInt(ref position, dataBytes);
            Gunners        = CommonSerializer.GetInt(ref position, dataBytes);
            Sailors        = CommonSerializer.GetInt(ref position, dataBytes);
            PirateFighters = CommonSerializer.GetInt(ref position, dataBytes);
        }
Example #5
0
        public void DeSerialize(ref int position, byte[] dataBytes)
        {
            if (dataBytes[position++] == 0)
            {
                return;
            }

            LeftSideCannons  = CommonSerializer.BytesToBools(ref position, dataBytes, LeftSideCannons.Count());
            RightSideCannons = CommonSerializer.BytesToBools(ref position, dataBytes, RightSideCannons.Count());
            ForePartCannons  = CommonSerializer.BytesToBools(ref position, dataBytes, ForePartCannons.Count());
            RearPartCannons  = CommonSerializer.BytesToBools(ref position, dataBytes, RearPartCannons.Count());
        }
Example #6
0
        /// <summary>
        /// Speichert einen beliebigen Wert in der Registry. Einzigste
        /// Bedingung ist, dass der Typ serialisierbar ist (IsSerializable).
        /// </summary>
        /// <param name="ownRootNodeName">Gibt den Hauptknoten an. Kann null oder leer sein, dann wird WW-Ziel + YXZ genommen.</param>
        /// <param name="section">Unterschlüssel (noch unterhalb des Dialogs). Kann null sein, und wird dann ignoriert.</param>
        /// <param name="key">Schlüssel</param>
        /// <param name="value">Wert</param>
        public static void SetValue(string section, string key, object value)
        {
            if (key == null || key == string.Empty)
            {
                throw new ArgumentException("Der Schlüssel darf nicht leer sein.");
            }

            if (value == null)
            {
                m_Provider.SetValue(null, section, key);
                return;
            }

            if (!value.GetType().IsSerializable)
            {
                throw new ArgumentException("Werte müssen serialisierbar sein (typeof(...).IsSerializable == true)");
            }

            if (value.GetType().IsEnum)
            {
                value = Convert.ToInt32(value);
            }

            // Wir unterscheiden verschiedene Arten von Daten
            // - "Einfache" (sog. primitive) Typen: string, bool, int, float (Verarbeitung mit .Parse() und ToString()
            //   - Enum-Werte werden ebenfalls so verarbeitet
            // - Allgemeine Typen: Point, Size, PointF, SizeF (Selbstgeschriebene Funktionen)
            // - Sonstige serialisierbare Typen: Werden mit dem BinarySerializer verarbeitet

            SettingsSerializer serializer = null;

            if (value.GetType().IsPrimitive)
            {
                serializer = new PrimitiveSerializer();
            }
            else if (CommonSerializer.SupportsType(value.GetType()))
            {
                // Allgemeine Typen
                serializer = new CommonSerializer();
            }
            else
            {
                // Binär als letzte Chance
                serializer = new BinarySerializer();
            }

            if (serializer == null)
            {
                throw new NotImplementedException("Kein passender Serialisierer verfügbar.");
            }
            m_Provider.SetValue(serializer.Serialize(value), section, key);
        }
Example #7
0
        public IEnumerable <byte> Serialize()
        {
            //if (!SomethingChanged) return new byte[] { 0 };

            var result = new byte[] { 1 }.Concat(CommonSerializer.BoolArrToBytes(LeftSideCannons));

            result = result.Concat(CommonSerializer.BoolArrToBytes(RightSideCannons));
            result = result.Concat(CommonSerializer.BoolArrToBytes(ForePartCannons));
            result = result.Concat(CommonSerializer.BoolArrToBytes(RearPartCannons));

            SomethingChanged = false;
            return(result);
        }
Example #8
0
        public void DeSerialize(ref int position, byte[] dataBytes)
        {
            if (dataBytes[position++] == 0)
            {
                return;
            }

            Player.Name = CommonSerializer.GetString(ref position, dataBytes);
            Coordinates = CommonSerializer.GetVector2(ref position, dataBytes);
            MoveVector  = CommonSerializer.GetVector2(ref position, dataBytes);
            Health      = CommonSerializer.GetFloat(ref position, dataBytes);
            ShipCrew.DeSerialize(ref position, dataBytes);
            ShipSupplies.DeSerialize(ref position, dataBytes);
        }
Example #9
0
        private void CheckMouse(Controller controller)
        {
            var mouse = controller as KeyboardAndMouse;

            if (mouse == null)
            {
                return;
            }

            if (mouse.ShootButtonPressed)
            {
                var coords = CommonSerializer.Vector2ToBytes(Camera2D.RelativePosition(mouse.SightPosition)).ToArray();
                ConnectionManager.Instance.AddClientGameEvent(new GameEvent(0, EventType.Shoot, coords));
            }
        }
Example #10
0
        public IEnumerable <byte> Serialize()
        {
            //if (!SomethingChanged) return new byte[]{0};

            var result = new byte[] { 1 }.Concat(CommonSerializer.StringToBytes(Player.Name));

            result = result.Concat(CommonSerializer.Vector2ToBytes(Coordinates));
            result = result.Concat(CommonSerializer.Vector2ToBytes(MoveVector));
            result = result.Concat(BitConverter.GetBytes(Health));
            result = result.Concat(ShipCrew.Serialize());
            result = result.Concat(ShipSupplies.Serialize());

            SomethingChanged = false;
            return(result);
        }
Example #11
0
        public void Shoot(List <IBullet> bullets, GameEvent gameEvent)
        {
            if (!_isEnableForShoot)
            {
                return;
            }

            _isEnableForShoot     = false;
            _cooldounOfShootTimer = new Timer(ShootingTimer, null, 3000, 10000);
            int pos      = 0;
            var vectorTo = CommonSerializer.GetVector2(ref pos, gameEvent.ExtraData);

            bullets.Add(new Bullet(BulletType.Cannonball, Player.Name, Coordinates, vectorTo));
            Console.WriteLine(vectorTo);
        }
Example #12
0
        public void UpdateWorld(byte[] dataBytes, int step, bool isStarted)
        {
            if (dataBytes == null)
            {
                return;
            }

            if (step == 0)
            {
                int pos = 0;

                // Корабли
                int countOfShips = CommonSerializer.GetInt(ref pos, dataBytes);
                for (int i = 0; i < countOfShips; i++)
                {
                    if (ServerShips[i] == null)
                    {
                        ServerShips[i]  = new ClientShip(new Corvette());
                        CurrentShips[i] = new ClientShip(new Corvette());
                    }
                    ServerShips[i].Ship.DeSerialize(ref pos, dataBytes);
                }

                // Флюгер
                ClientWindVane.WindVane.DeSerialize(ref pos, dataBytes);

                // Ядра
                Bullets = new List <ClientBullet>();
                int countOfBullets = CommonSerializer.GetInt(ref pos, dataBytes);
                for (int i = 0; i < countOfBullets; i++)
                {
                    var bullet = new Bullet();
                    bullet.DeSerialize(ref pos, dataBytes);
                    Bullets.Add(new ClientBullet(bullet));
                }
            }

            if (!isStarted)
            {
                InitCurrentShips();
            }
            else
            {
                UpdateCurrentShips(step);
            }
        }
Example #13
0
        public void DeSerialize(ref int position, byte[] dataBytes)
        {
            if (dataBytes[position++] == 0)
            {
                return;
            }

            var count = CommonSerializer.GetInt(ref position, dataBytes);

            Goods = new List <Good>();

            for (int i = 0; i < count; i++)
            {
                var tmpGood = new Good();
                tmpGood.DeSerialize(ref position, dataBytes);
                Goods.Add(tmpGood);
            }
        }
Example #14
0
        public void TestOverrideAndLoadJson()
        {
            var fileName = Path.Combine(tmpDir, "example.json");
            var test     = new SerializableObject();

            test.ValueA = "Hallo";
            test.ValueB = 1;

            CommonSerializer.SaveAsJsonFile(fileName, test);
            Assert.IsTrue(File.Exists(fileName));

            test.ValueA = "Changed";

            CommonSerializer.SaveAsJsonFile(fileName, test);
            Assert.IsTrue(File.Exists(fileName));

            var loaded = CommonSerializer.LoadJsonFile <SerializableObject>(fileName);

            Assert.AreEqual("Changed", loaded.ValueA);
        }
        /// <summary>
        /// 准备数据回传
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="result"></param>
        /// <param name="model"></param>
        /// <param name="client"></param>
        private void ResponseResult(SrvDataSource source, RequestResult result, DBTransfer model, ClientRequest client)
        {
            result.RequestID = client.TaskID;
            result.ID        = client.SessionID;
            if (model.IsJson && result.Error == ErrorCode.Sucess && model.IsQuery)
            {
                CommonSerializer common = new CommonSerializer();
                result.Result = common.JSONObjectToString(result.Result);
            }
            byte[] buffer = null;
            try
            {
                if (result.Result is DataTable)
                {
                    DataTable    dt      = result.Result as DataTable;
                    DataSetModel dataSet = new DataSetModel()
                    {
                        Content = dt.DataSet.GetXml(), Schema = dt.DataSet.GetXmlSchema()
                    };
                    result.Result = dataSet;
                }
                buffer = SerializerFactory <CommonSerializer> .Serializer(result);
            }
            catch (Exception ex)
            {
                result.Error     = ErrorCode.Exception;
                result.ReslutMsg = "序列化失败," + ex.Message;
                result.Result    = null;
                buffer           = SerializerFactory <CommonSerializer> .Serializer(result);
            }

            buffer = CryptoServer.Singleton.EnCrypto(buffer, client.AESKey);//AES 加密回传
            //source.Context.Send(buffer)
            source.Rsponse(buffer);
            Console.WriteLine("回传:" + buffer.Length);
        }
Example #16
0
 public void DeSerialize(ref int position, byte[] dataBytes)
 {
     _angleOfDirection = CommonSerializer.GetDouble(ref position, dataBytes);
     ForceOfWind       = CommonSerializer.GetFloat(ref position, dataBytes);
     UpdateDirection();
 }
Example #17
0
 public bool Save()
 {
     CommonSerializer.SaveObjAsBinaryFile(this, $@".\CustClassManager.xml", out bool bSaveOK, out Exception ex);
     return(bSaveOK);
 }
Example #18
0
 public bool Load()
 {
     instance = CommonSerializer.LoadObjFormBinaryFile <CustClassManager>($@".\CustClassManager.xml", out bool bLoadOK, out Exception ex);
     return(bLoadOK);
 }
Example #19
0
 public void DeSerialize(ref int position, byte[] dataBytes)
 {
     Name   = CommonSerializer.GetString(ref position, dataBytes);
     Count  = CommonSerializer.GetInt(ref position, dataBytes);
     Weight = CommonSerializer.GetFloat(ref position, dataBytes);
 }
Example #20
0
        private void Process(TCPUserToken token)
        {
            if (dBAcess == null)
            {
                lock (obj_lock)
                {
                    if (dBAcess == null)
                    {
                        dBAcess = new DBAcessSrv();
                    }
                }
            }
            Task.Factory.StartNew((req) =>
            {
                TCPUserToken userToken = req as TCPUserToken;
                DBTransfer model       = SerializerFactory <CommonSerializer> .Deserialize <DBTransfer>(userToken.Data);

                RequestResult result = null;
                if (model.TimeOut == 0)
                {
                    //不超时
                    result = dBAcess.Execete(Interlocked.Increment(ref rspID), model);
                }
                else
                {
                    int timeOut    = model.TimeOut < 0 ? TimeOut : model.TimeOut * 1000;
                    var taskResult = Executors.Submit(() =>
                    {
                        return(dBAcess.Execete(Interlocked.Increment(ref rspID), model));
                    }, timeOut);
                    result = taskResult.Result;
                    if (result == null)
                    {
                        result = new RequestResult();
                    }
                    if (ExecuteErrorCode.timeout == taskResult.ResultCode)
                    {
                        result.Error = ResultErrorCode.TimeOut;
                    }
                    else if (ExecuteErrorCode.exception == taskResult.ResultCode)
                    {
                        result.Error = ResultErrorCode.Exception;
                    }
                }
                //
                if (model.IsJson && result.Error == DBModel.ErrorCode.Sucess && model.IsQuery)
                {
                    CommonSerializer common = new CommonSerializer();
                    result.Result           = common.JSONObjectToString(result.Result);
                }
                byte[] buffer = null;
                try
                {
                    if (result.Result is DataTable)
                    {
                        DataTable dt  = result.Result as DataTable;
                        result.Result = dt.DataSet.GetXml();
                    }
                    buffer = SerializerFactory <CommonSerializer> .Serializer(result);
                }
                catch (Exception ex)
                {
                    result.Error     = ResultErrorCode.Exception;
                    result.ReslutMsg = "序列化失败," + ex.Message;
                    result.Result    = null;
                    buffer           = SerializerFactory <CommonSerializer> .Serializer(result);
                }
                userToken.Rsp(buffer);
            }, token);
        }
Example #21
0
 /// <summary>
 /// 保存内存数据
 /// </summary>
 /// <returns></returns>
 public bool Save()
 {
     CommonSerializer.SaveObjAsBinaryFile(instance, $@"D:\Fi.Data\ItemManager.xml", out bool bSaveOK, out Exception ex);
     return(bSaveOK);
 }
Example #22
0
        /// <summary>
        /// Speichert einen beliebigen Wert in der Registry. Einzigste
        /// Bedingung ist, dass der Typ serialisierbar ist (IsSerializable).
        /// </summary>
        /// <param name="ownRootNodeName">Gibt den Hauptknoten an. Kann null oder leer sein, dann wird WW-Ziel + YXZ genommen.</param>
        /// <param name="section">Unterschlüssel (noch unterhalb des Dialogs). Kann null sein, und wird dann ignoriert.</param>
        /// <param name="key">Schlüssel</param>
        /// <param name="value">Wert</param>
        public static void SetValue (string section, string key, object value) {
            if (key == null || key == string.Empty) {
                throw new ArgumentException ("Der Schlüssel darf nicht leer sein.");
            }

            if (value == null)
            {
                m_Provider.SetValue(null, section, key);
                return;
            }

            if (!value.GetType ().IsSerializable) {
                throw new ArgumentException ("Werte müssen serialisierbar sein (typeof(...).IsSerializable == true)");
            }

            if (value.GetType().IsEnum)
            {
                value = Convert.ToInt32(value);
            }

            // Wir unterscheiden verschiedene Arten von Daten
            // - "Einfache" (sog. primitive) Typen: string, bool, int, float (Verarbeitung mit .Parse() und ToString()
            //   - Enum-Werte werden ebenfalls so verarbeitet
            // - Allgemeine Typen: Point, Size, PointF, SizeF (Selbstgeschriebene Funktionen)
            // - Sonstige serialisierbare Typen: Werden mit dem BinarySerializer verarbeitet

            SettingsSerializer serializer = null;

            if (value.GetType ().IsPrimitive) {
                serializer = new PrimitiveSerializer ();
            }
            else if (CommonSerializer.SupportsType (value.GetType ())) {
                // Allgemeine Typen
                serializer = new CommonSerializer ();
            }
            else {
                // Binär als letzte Chance
                serializer = new BinarySerializer ();
            }

            if (serializer == null) {
                throw new NotImplementedException ("Kein passender Serialisierer verfügbar.");
            }
            m_Provider.SetValue(serializer.Serialize(value), section, key);
        }
Example #23
0
 public static bool Save()
 {
     CommonSerializer.SaveObjAsBinaryFile(Server, $@"D:\FastAutomation\Server.xml", out bool bSaveOK, out Exception ex);
     return(bSaveOK);
 }
Example #24
0
 public void DeSerialize(ref int position, byte[] dataBytes)
 {
     Coordinates = CommonSerializer.GetVector2(ref position, dataBytes);
 }
Example #25
0
 /// <summary>
 /// 保存内存数据
 /// </summary>
 /// <returns></returns>
 public bool Save()
 {
     CommonSerializer.SaveObjAsBinaryFile(this.MachineItems, $@"D:\FastAutomation\MachineItems.xml", out bool bSaveOK, out Exception ex);
     return(bSaveOK);
 }
Example #26
0
 /// <summary>
 /// 加载内存数据
 /// </summary>
 /// <returns></returns>
 public bool Load()
 {
     this.MachineItems = CommonSerializer.LoadObjFormBinaryFile <List <MachineItem> >($@"D:\FastAutomation\MachineItems.xml", out bool bLoadOK, out Exception ex);
     return(bLoadOK);
 }
Example #27
0
 public IEnumerable <byte> Serialize()
 {
     return(CommonSerializer.Vector2ToBytes(Coordinates));
 }
Example #28
0
 /// <summary>
 /// 加载内存数据
 /// </summary>
 /// <returns></returns>
 public bool Load()
 {
     instance = CommonSerializer.LoadObjFromBinaryFile <ItemManager>($@"D:\Fi.Data\ItemManager.xml", out bool bLoadOK, out Exception ex);
     return(bLoadOK);
 }
Example #29
0
 public static bool Load()
 {
     Server = CommonSerializer.LoadObjFormBinaryFile <AsyncTcpServer>($@"D:\FastAutomation\Server.xml", out bool bLoadOK, out Exception ex);
     return(bLoadOK);
 }
Example #30
0
 public void DeSerialize(ref int position, byte[] dataBytes)
 {
     //Type = (BulletType)dataBytes[position++];
     Shooter     = CommonSerializer.GetString(ref position, dataBytes);
     Coordinates = CommonSerializer.GetVector2(ref position, dataBytes);
 }