Ejemplo n.º 1
0
        /// <summary>
        /// Stop editing the d2o file, flush the file and dispose ressources
        /// </summary>
        public void EndWriting()
        {
            lock (m_writingSync)
            {
                m_writer.Seek(0, SeekOrigin.Begin);

                m_writing      = false;
                m_needToBeSync = false;

                WriteHeader();

                foreach (var obj in m_objects)
                {
                    if (!m_indexTable.ContainsKey(obj.Key))
                    {
                        m_indexTable.Add(obj.Key, (int)m_writer.BaseStream.Position);
                    }
                    else
                    {
                        m_indexTable[obj.Key] = (int)m_writer.BaseStream.Position;
                    }


                    WriteObject(m_writer, obj.Value, obj.Value.GetType());
                }

                WriteIndexTable();
                WriteClassesDefinition();

                m_writer.Dispose();
            }
        }
Ejemplo n.º 2
0
        public static bool SerializeConfig()
        {
            lock (CheckLock)
            {
                try
                {
                    BigEndianWriter writer = new BigEndianWriter();

                    writer.WriteInt(ConfigsList.Count);

                    foreach (ConfigEntry entry in ConfigsList)
                    {
                        entry.Serialize(writer);
                    }

                    File.WriteAllBytes(Path.Combine(System.Windows.Forms.Application.StartupPath, "config.ap"), writer.Data);
                    writer.Dispose();

                    return(true);
                }
                catch (Exception ex)
                {
                    WindowManager.MainWindow.Logger.Error("Write file error -> " + ex.Message);
                    return(false);
                }
            }
        }
Ejemplo n.º 3
0
        public void Pack(BigEndianWriter header)
        {
            var data = new BigEndianWriter();

            Serialize(data);
            var   size    = data.Data.Length;
            var   compute = ComputeTypeLen(size);
            short val     = (short)((MessageId << 2) | compute);

            header.WriteShort(val);
            switch (compute)
            {
            case 1:
                header.WriteByte((byte)size);
                break;

            case 2:
                header.WriteUShort((ushort)size);
                break;

            case 3:
                header.WriteByte((byte)((size >> 0x10) & 0xff));
                header.WriteUShort((ushort)(size & 0xffff));
                break;
            }
            header.WriteBytes(data.Data);
            data.Dispose();
        }
Ejemplo n.º 4
0
 public void RecoverAccountsInfos()
 {
     if (File.Exists(m_SavingFilePath))
     {
         IFormatter binaryFormatter = new BinaryFormatter();
         using (Stream stream = new FileStream(m_SavingFilePath, FileMode.Open, FileAccess.Read))
         {
             BigEndianWriter writer = (BigEndianWriter)binaryFormatter.Deserialize(stream);
             using (BigEndianReader reader = new BigEndianReader(writer.Content))
             {
                 int limite = reader.ReadInt();
                 m_Accounts = new List <Account>();
                 for (int index = 0; index < limite; index++)
                 {
                     m_Accounts.Add(new Account(reader.ReadUTF(), reader.ReadUTF()));
                 }
             }
             writer.Dispose();
             stream.Close();
         }
     }
     foreach (Account accountObject in m_Accounts)
     {
         accountObject.Password = CryptageBS.DecryptBS(accountObject.Password);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Stop editing the d2o file, flush the file and dispose ressources
        /// </summary>
        public void EndWriting()
        {
            lock (writingSync)
            {
                writer.Seek(0, SeekOrigin.Begin);

                writing      = false;
                needToBeSync = false;

                WriteHeader();

                foreach (var obj in objects)
                {
                    if (!indexTable.ContainsKey(obj.Key))
                    {
                        indexTable.Add(obj.Key, (int)writer.BaseStream.Position);
                    }
                    else
                    {
                        indexTable[obj.Key] = (int)writer.BaseStream.Position;
                    }

                    WriteObject(obj.Value, obj.Value.GetType());
                }

                WriteIndexTable();
                WriteClassesDefinition();

                File.WriteAllBytes(Filename, writer.Data);
                writer.Dispose();
            }
        }
        public void Send(NetworkMessage message, ConnectionDestination destination = ConnectionDestination.Server)
        {
            if (message is BasicStatMessage || message.Cancel)
            {
                return;
            }

            try
            {
                if (Client.Network == null)
                {
                    return;
                }

                Instance += 1;
                BigEndianWriter writer = new BigEndianWriter();

                switch (destination)
                {
                case ConnectionDestination.Client:
                    message.Pack(writer, 0);
                    Client.Network.ClientConnection.Send(writer.Data);
                    SaveMsg(message, ConnectionDestination.Client);
                    if (ServerMessageReceived != null)
                    {
                        OnServerMessageReceived(new MessageReceivedEventArgs(message));
                    }
                    break;

                case ConnectionDestination.Server:
                    message.Pack(writer, Instance);
                    Client.Network.ServerConnection.Send(writer.Data);
                    SaveMsg(message, ConnectionDestination.Server);
                    if (ClientMessageReceived != null)
                    {
                        OnClientMessageReceived(new MessageReceivedEventArgs(message));
                    }
                    break;
                }

                writer.Dispose();
                writer  = null;
                message = null;
            }
            catch (Exception ex)
            {
                if (Client != null && WindowManager.MainWindow.Logger != null)
                {
                    WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message);
                }
                else
                {
                    WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message);
                }
            }
        }
Ejemplo n.º 7
0
        public void ToSwl()
        {
            var writer = new BigEndianWriter();

            writer.WriteByte(76); //Header
            writer.WriteByte(Version);
            writer.WriteUInt(FrameRate);
            writer.WriteInt(Classes.Count);

            foreach (var classe in Classes)
            {
                writer.WriteUTF(classe);
            }

            writer.WriteBytes(SwfData);

            var swlName = FilePath.Replace(".swf", ".swl");
            var file    = File.Create(swlName);

            file.Write(writer.Data, 0, writer.Data.Length);

            file.Dispose();
            writer.Dispose();
        }
        public void Send(MessagePart part, ConnectionDestination destination)
        {
            try
            {
                BigEndianWriter writer = new BigEndianWriter();
                byte            typeLen;
                if (part.Data.Length > 65535)
                {
                    typeLen = 3;
                }
                else if (part.Data.Length > 255)
                {
                    typeLen = 2;
                }
                else if (part.Data.Length > 0)
                {
                    typeLen = 1;
                }
                else
                {
                    typeLen = 0;
                }

                writer.WriteShort((short)(part.MessageId << 2 | typeLen));

                Instance += 1;
                if (destination == ConnectionDestination.Server)
                {
                    writer.WriteUInt(Instance);
                }

                switch (typeLen)
                {
                case 0:
                    break;

                case 1:
                    writer.WriteByte((byte)part.Data.Length);
                    break;

                case 2:
                    writer.WriteShort((short)part.Data.Length);
                    break;

                case 3:
                    writer.WriteByte((byte)(part.Data.Length >> 16 & 255));
                    writer.WriteShort((short)(part.Data.Length & 65535));
                    break;
                }

                writer.WriteBytes(part.Data);

                if (writer == null || writer.Data == null)
                {
                    return;
                }

                switch (destination)
                {
                case ConnectionDestination.Client:
                    ClientConnection.Send(writer.Data);
                    break;

                case ConnectionDestination.Server:
                    ServerConnection.Send(writer.Data);
                    break;
                }

                writer.Dispose();
                writer = null;
                part   = null;
            }
            catch (Exception ex)
            {
                if (Client != null && WindowManager.MainWindow.Logger != null)
                {
                    WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message);
                }
                else
                {
                    WindowManager.MainWindow.Logger.Error("[Network] Send Function -> " + ex.Message);
                }
            }
        }
Ejemplo n.º 9
0
        public void Save(string uri)
        {
            File.Delete(uri);
            var writer = new BigEndianWriter();

            writer.Seek(4, SeekOrigin.Begin);

            var newIndexes     = new Dictionary <int, long>();
            var newTextIndexes = new Dictionary <string, long>();

            foreach (var oldIndex in _indexes)
            {
                newIndexes.Add(oldIndex.Key, writer.Position);
                writer.WriteUTF(GetText(oldIndex.Key));
            }
            foreach (var oldIndex in _textIndexes)
            {
                newTextIndexes.Add(oldIndex.Key, writer.Position);
                writer.WriteUTF(GetText(oldIndex.Key));
            }

            var oldPos = writer.Position;//end of the strings and start of the indexes

            writer.Seek(0, SeekOrigin.Begin);
            writer.WriteInt((int)oldPos);
            writer.Seek((int)oldPos, SeekOrigin.Begin);

            var subWriter = new BigEndianWriter();

            foreach (var newIndex in newIndexes)
            {
                Tuple <bool, int> newUnDiacriticalIndex;
                _unDiacriticalIndexes.TryGetValue(newIndex.Key, out newUnDiacriticalIndex);

                subWriter.WriteInt(newIndex.Key);                    //id
                subWriter.WriteBoolean(newUnDiacriticalIndex.Item1); //dialectCriticalText
                subWriter.WriteInt((int)newIndex.Value);             //position

                if (newUnDiacriticalIndex.Item1)
                {
                    subWriter.WriteInt(newUnDiacriticalIndex.Item2);
                }
            }
            var subData = subWriter.Data;

            writer.WriteInt(subData.Length);
            writer.WriteBytes(subData);

            subWriter.Clear();
            foreach (var textIndex in newTextIndexes)
            {
                subWriter.WriteUTF(textIndex.Key);
                subWriter.WriteInt((int)textIndex.Value);
            }
            subData = subWriter.Data;
            writer.WriteInt(subData.Length);
            writer.WriteBytes(subData);

            subWriter.Clear();
            foreach (var textSortIndex in newIndexes)
            {
                subWriter.WriteInt(textSortIndex.Key);
            }
            subData = subWriter.Data;
            writer.WriteInt(subData.Length);
            writer.WriteBytes(subData);

            File.WriteAllBytes(uri, writer.Data);
            writer.Dispose();
        }