Ejemplo n.º 1
0
        public void Decompress(string filePath, string destFolder)
        {
            using (FileStream fs = File.OpenRead(filePath))
                using (var gzs = new GZipStream(fs, CompressionMode.Decompress))
                {
                    var reader = new RawDataReader(gzs, Encoding.UTF8);

                    foreach (byte b in Signature)
                    {
                        if (b != reader.ReadByte())
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    int nbDir   = reader.ReadInt();
                    var folders = new List <string>(nbDir);

                    for (int i = 0; i < nbDir; ++i)
                    {
                        string dir = Path.Combine(destFolder, reader.ReadString());
                        folders.Add(dir);
                        Directory.CreateDirectory(dir);
                    }


                    int nbFile = reader.ReadInt();

                    for (int i = 0; i < nbFile; ++i)
                    {
                        string file   = reader.ReadString();
                        int    ndxDir = reader.ReadInt();

                        if (ndxDir == NDX_CUR_FOLDER)
                        {
                            file = Path.Combine(destFolder, file);
                        }
                        else
                        {
                            file = Path.Combine(folders[ndxDir], file);
                        }

                        long fileLen = reader.ReadLong();

                        CreateFile(gzs, file, fileLen);

                        FileDecompressed?.Invoke(file);
                    }
                }
        }
Ejemplo n.º 2
0
        public static IEnumerable <Message> ReadConnectionsReq(string filePath)
        {
            byte[] sign = HubCxnSignature;

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.OpenRead(filePath))
                {
                    var reader = new RawDataReader(fs, Encoding.UTF8);

                    foreach (byte b in sign)
                    {
                        if (reader.ReadByte() != b)
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    int count = reader.ReadInt();
                    var cxns  = new List <Message>(count);

                    for (int i = 0; i < count; ++i)
                    {
                        cxns.Add(Message.LoadMessage(reader));
                    }

                    return(cxns);
                }
        }
Ejemplo n.º 3
0
        public static IEnumerable <ProfileInfo> ReadProfiles(string filePath)
        {
            byte[] sign = ProfileSignature;

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.OpenRead(filePath))
                {
                    var reader = new RawDataReader(fs, Encoding.UTF8);

                    foreach (byte b in sign)
                    {
                        if (reader.ReadByte() != b)
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    int count   = reader.ReadInt();
                    var prInfos = new List <ProfileInfo>(count);

                    for (int i = 0; i < count; ++i)
                    {
                        prInfos.Add(ProfileInfo.LoadProfileInfo(reader));
                    }

                    return(prInfos);
                }
        }
Ejemplo n.º 4
0
        public static IEnumerable <Message> ReadHubDialog(string filePath, uint clID)
        {
            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.OpenRead(filePath))
                {
                    var    reader = new RawDataReader(fs, Encoding.UTF8);
                    byte[] sign   = HubDialogSignature;

                    foreach (byte b in sign)
                    {
                        if (reader.ReadByte() != b)
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    uint id = reader.ReadUInt();

                    if (id != clID)
                    {
                        throw new CorruptedFileException(filePath);
                    }

                    int count = reader.ReadInt();
                    var msgs  = new List <Message>(count);

                    for (int i = 0; i < count; ++i)
                    {
                        msgs.Add(Message.LoadMessage(reader));
                    }

                    return(msgs);
                }
        }
Ejemplo n.º 5
0
        public static void AppendConnectionsResp(string filePath, IEnumerable <Message> messages)
        {
            Assert(messages != null);

            using (FileLocker.Lock(filePath))
                using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    var reader   = new RawDataReader(fs, Encoding.UTF8);
                    int countPos = SrvCxnSignature.Length;;
                    fs.Position = countPos;

                    int msgCount = reader.ReadInt();

                    var writer = new RawDataWriter(fs, Encoding.UTF8);
                    fs.Seek(0, SeekOrigin.End);

                    foreach (Message msg in messages)
                    {
                        msg.Write(writer);
                        ++msgCount;
                    }

                    fs.Position = countPos;
                    writer.Write(msgCount);
                }
        }
Ejemplo n.º 6
0
        public IArchiveContent GetArchiveContent(string filePath)
        {
            var archData = new ArchiveContent();

            using (FileStream fs = File.OpenRead(filePath))
            {
                var    reader = new RawDataReader(fs, Encoding.UTF8);
                byte[] sign   = Signature;

                foreach (byte b in sign)
                {
                    if (reader.ReadByte() != b)
                    {
                        throw new CorruptedFileException(filePath);
                    }
                }

                archData.CreationTime = reader.ReadTime();
                int len = reader.ReadInt();
                archData.ArchiveHeader = reader.ReadBytes(len);
                archData.Files         = FilesBag.GetContent(fs);

                return(archData);
            }
        }
Ejemplo n.º 7
0
        public void Restore(string filePath, string destFolder)
        {
            Initializing?.Invoke();

            using (FileStream fs = File.OpenRead(filePath))
            {
                var    reader = new RawDataReader(fs, Encoding.UTF8);
                byte[] sign   = Signature;

                foreach (byte b in sign)
                {
                    if (reader.ReadByte() != b)
                    {
                        throw new CorruptedFileException(filePath);
                    }
                }

                reader.ReadTime();           //ignore creation time
                int headerLen = reader.ReadInt();
                reader.ReadBytes(headerLen); //ignore header

                Restoring?.Invoke();
                var bag = new FilesBag();

                Clean(destFolder);

                bag.Decompress(fs, destFolder);

                Done?.Invoke();
            }
        }
Ejemplo n.º 8
0
        public static IEnumerable <TableUpdate> LoadTablesUpdate(string filePath, IDatumFactory dataFactory)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                var    reader = new RawDataReader(fs, Encoding.UTF8);
                byte[] sign   = Encoding.UTF8.GetBytes(TABLES_UPDATE_SIGNATURE);

                for (int i = 0; i < sign.Length; ++i)
                {
                    if (reader.ReadByte() != sign[i])
                    {
                        throw new CorruptedFileException(filePath);
                    }
                }

                int tableCount = reader.ReadInt();
                var lst        = new List <TableUpdate>(tableCount);

                for (int i = 0; i < tableCount; ++i)
                {
                    lst.Add(LoadTableUpdate(reader, dataFactory));
                }

                return(lst);
            }
        }
Ejemplo n.º 9
0
        public static IEnumerable <UpdateURI> ReadDataManifest(string filePath, uint startGeneration = 0)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                var reader = new RawDataReader(fs, Encoding.UTF8);

                byte[] sign = Encoding.UTF8.GetBytes(DATA_MANIFEST_SIGNATURE);

                for (int i = 0; i < sign.Length; ++i)
                {
                    if (reader.ReadByte() != sign[i])
                    {
                        throw new CorruptedFileException(filePath);
                    }
                }

                int uriCount = reader.ReadInt();
                var lst      = new List <UpdateURI>();

                for (int i = 0; i < uriCount; ++i)
                {
                    uint   preGen  = reader.ReadUInt();
                    uint   postGen = reader.ReadUInt();
                    string uri     = reader.ReadString();

                    if (preGen >= startGeneration)
                    {
                        lst.Add(new UpdateURI(uri, preGen, postGen));
                    }
                }


                return(lst);
            }
        }
Ejemplo n.º 10
0
        public static Dictionary <AppArchitecture_t, string> ReadAppManifest(string filePath)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                var    reader = new RawDataReader(fs, Encoding.UTF8);
                byte[] sign   = Encoding.UTF8.GetBytes(APP_MANIFEST_SIGNATURE);

                foreach (byte b in sign)
                {
                    if (b != reader.ReadByte())
                    {
                        throw new CorruptedFileException(filePath);
                    }
                }

                int nbKey = reader.ReadInt();
                var dict  = new Dictionary <AppArchitecture_t, string>(nbKey);

                for (int i = 0; i < nbKey; ++i)
                {
                    byte arch = reader.ReadByte();

                    if (!Enum.IsDefined(typeof(AppArchitecture_t), arch))
                    {
                        throw new CorruptedFileException(filePath);
                    }

                    string fileName = reader.ReadString();
                    dict[(AppArchitecture_t)arch] = fileName;
                }

                return(dict);
            }
        }
Ejemplo n.º 11
0
        public static IEnumerable <string> GetContent(string filePath)
        {
            using (FileStream fs = File.OpenRead(filePath))
                using (var gzs = new GZipStream(fs, CompressionMode.Decompress))
                {
                    var reader = new RawDataReader(gzs, Encoding.UTF8);

                    foreach (byte b in Signature)
                    {
                        if (b != reader.ReadByte())
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    int nbDir   = reader.ReadInt();
                    var folders = new List <string>(nbDir);

                    for (int i = 0; i < nbDir; ++i)
                    {
                        string dir = reader.ReadString();
                        folders.Add(dir);
                    }


                    int nbFile = reader.ReadInt();
                    var files  = new List <string>(nbFile);

                    for (int i = 0; i < nbFile; ++i)
                    {
                        string file   = reader.ReadString();
                        int    ndxDir = reader.ReadInt();

                        if (ndxDir != NDX_CUR_FOLDER)
                        {
                            file = Path.Combine(folders[ndxDir], file);
                        }

                        files.Add(file);
                        long fileLen = reader.ReadLong();
                        reader.Skip((int)fileLen);
                    }

                    return(files);
                }
        }
Ejemplo n.º 12
0
        void Load()
        {
            FileStream fs = null;

            try
            {
                fs = File.OpenRead(SettingsManager.RunOnceFilePath);
                var reader = new RawDataReader(fs, Encoding.UTF8);

                byte[] sign = Signature;

                foreach (byte b in sign)
                {
                    if (b != reader.ReadByte())
                    {
                        throw new CorruptedFileException(SettingsManager.RunOnceFilePath);
                    }
                }

                int actCount = reader.ReadInt();
                for (int i = 0; i < actCount; ++i)
                {
                    var             code = (RunOnceAction_t)reader.ReadInt();
                    IRunOnceCommand act  = m_actBuilder[code]();
                    act.Read(reader);
                    m_actions.Add(act);
                }
            }
            catch (Exception ex)
            {
                Dbg.Log(ex.Message);
                m_actions.Clear();
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }
        }
Ejemplo n.º 13
0
        void LoadUserSettings()
        {
            //if (!File.Exists(UserSettingsFilePath))
            //    return;

            using (FileStream fs = File.OpenRead(UserSettingsFilePath))
                using (var xs = new XorStream(fs))
                    using (var gzs = new GZipStream(xs, CompressionMode.Decompress))
                    {
                        var    reader = new RawDataReader(xs, Encoding.UTF8);
                        byte[] sign   = Encoding.UTF8.GetBytes(USER_SETTINGS_SIGNATURE);

                        for (int i = 0; i < sign.Length; ++i)
                        {
                            if (sign[i] != reader.ReadByte())
                            {
                                throw new CorruptedFileException(UserSettingsFilePath);
                            }
                        }

                        IsMaximized = reader.ReadBoolean();
                        int x = reader.ReadInt();
                        int y = reader.ReadInt();
                        int w = reader.ReadInt();
                        int h = reader.ReadInt();

                        FrameRectangle = new Rectangle(x, y, w, h);

                        UseCountryCode = reader.ReadBoolean();

                        int mruSize  = reader.ReadInt();
                        int mruCount = reader.ReadInt();
                        MRUSubHeading = new MRUList <SubHeading>(mruSize);

                        for (int i = 0; i < mruCount; ++i)
                        {
                            MRUSubHeading.Add(new SubHeading(reader.ReadULong()));
                        }

                        AutoDetectProxy = reader.ReadBoolean();
                        EnableProxy     = reader.ReadBoolean();
                        ProxyHost       = reader.ReadString();
                        ProxyPort       = reader.ReadUShort();
                    }
        }
Ejemplo n.º 14
0
        public static void UpdateDataManifest(string filePath, UpdateURI updateURI)
        {
            Assert(updateURI != null);

            using (FileStream fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var    writer = new RawDataWriter(fs, Encoding.UTF8);
                byte[] sign   = Encoding.UTF8.GetBytes(DATA_MANIFEST_SIGNATURE);

                if (fs.Length == 0)
                {
                    writer.Write(sign);
                    writer.Write(1);
                }
                else
                {
                    var reader = new RawDataReader(fs, Encoding.UTF8);

                    for (int i = 0; i < sign.Length; ++i)
                    {
                        if (reader.ReadByte() != sign[i])
                        {
                            throw new CorruptedFileException(filePath);
                        }
                    }

                    int uriCount = reader.ReadInt();
                    fs.Position -= sizeof(int);
                    writer.Write(uriCount + 1);
                    fs.Seek(0, SeekOrigin.End);
                }

                writer.Write(updateURI.DataPreGeneration);
                writer.Write(updateURI.DataPostGeneration);
                writer.Write(updateURI.FileURI);
            }
        }
Ejemplo n.º 15
0
        void LoadData()
        {
            using (FileStream fs = File.OpenRead(m_dataFilePath))
            {
                var    reader = new RawDataReader(fs, Encoding.UTF8);
                byte[] sign   = FileSignature;

                foreach (byte b in sign)
                {
                    if (b != reader.ReadByte())
                    {
                        throw new CorruptedFileException(m_dataFilePath);
                    }
                }


                int count = reader.ReadInt();

                for (int i = 0; i < count; ++i)
                {
                    m_entries.Add(Entry.LoadEntry(reader));
                }
            }
        }