Esempio n. 1
0
        public void DecompressFiles()
        {
            workerThread = new Thread(() =>
            {
                IEnumerable <string> filesList = Directory.EnumerateFiles(localDataPath, "*.*", SearchOption.AllDirectories);
                int total = (from file in filesList select file).Count();
                int index = 0;
                foreach (string file in filesList)
                {
                    if (endSession)
                    {
                        Debug.Log("Ended.");
                        endSession = false;
                        return;
                    }

                    index++;

                    byte[] decompressedData;

                    using (Stream str = System.IO.File.OpenRead(file))
                    {
                        using (BinaryReader br = new BinaryReader(str, Encoding.ASCII))
                        {
                            decompressedData = null;

                            uint ZZZ4 = br.ReadUInt32();
                            if (ZZZ4 != 878336602)
                            {
                                continue;
                            }

                            int uncompressedSize = br.ReadInt32();

                            // Decompress //
                            byte[] compressedData = br.ReadBytes((int)(str.Length - str.Position));
                            LZ4 decompressor      = new LZ4();
                            decompressedData      = new byte[uncompressedSize];
                            decompressor.DecompressKnownSize(compressedData, decompressedData, uncompressedSize);
                        }
                    }

                    if (decompressedData != null)
                    {
                        System.IO.File.Delete(file);
                        System.IO.File.WriteAllBytes(file, decompressedData);
                        Debug.Log("Decompressed : " + file);
                    }

                    this.progress.Report((int)(index / (float)total * 100));
                }
            });

            workerThread.Start();
        }
Esempio n. 2
0
        public ResourceRepository(string repoPath, IProgress <int> progress)
        {
            this.localDataPath = Path.GetDirectoryName(repoPath);
            this.progress      = progress;

            byte[] decompressedData;

            if (!System.IO.File.Exists(repoPath))
            {
                Debug.Log("Repo file doesn't exist : " + repoPath);
                return;
            }

            // Read Repo //
            using (Stream str = System.IO.File.OpenRead(repoPath))
            {
                using (BinaryReader br = new BinaryReader(str, Encoding.ASCII))
                {
                    string CCCC             = new string(br.ReadChars(4));
                    string ZZZ4             = new string(br.ReadChars(4));
                    int    uncompressedSize = br.ReadInt32();

                    // Decompress //
                    byte[] compressedData = br.ReadBytes((int)(str.Length - str.Position));
                    LZ4    decompressor   = new LZ4();
                    decompressedData = new byte[uncompressedSize];
                    decompressor.DecompressKnownSize(compressedData, decompressedData, uncompressedSize);
                }
            }

            // Parse Repo //
            folderFileContents = new Dictionary <string, List <int> >();
            files        = new List <File>();
            hashMap      = new Dictionary <byte[], File>(ByteArrayComparer.Default);
            hashIndexMap = new Dictionary <string, uint>(StringComparer.OrdinalIgnoreCase);
            int fileIndex = 0;

            using (MemoryStream ms = new MemoryStream(decompressedData))
            {
                using (BinaryReader br = new BinaryReader(ms, Encoding.ASCII))
                {
                    uint version = br.ReadUInt32();     // probably version, always 1
                    br.ReadUInt16();                    // unknown
                    br.ReadUInt32();                    // empty

                    // Read File Types //
                    ushort sizeOfFileTypeStrings = br.ReadUInt16();
                    string fileTypeData          = new string(br.ReadChars(sizeOfFileTypeStrings));
                    fileTypes = fileTypeData.Split(';');

                    // Read Folders //
                    ushort sizeOfFolderStrings = br.ReadUInt16();
                    string folderPathData      = new string(br.ReadChars(sizeOfFolderStrings));
                    folderPathData = folderPathData.Replace('/', '\\');
                    folderPaths    = folderPathData.Split(';');

                    // Read Files //
                    while (ms.Position < ms.Length)
                    {
                        File   file       = new File(br);
                        string folderPath = folderPaths[file.GetFolderIndex()];
                        if (folderFileContents.ContainsKey(folderPath))
                        {
                            folderFileContents[folderPath].Add(fileIndex);
                        }
                        else
                        {
                            folderFileContents.Add(folderPath, new List <int>());
                            folderFileContents[folderPath].Add(fileIndex);
                        }
                        var hash = file.GetHash();
                        if (!hashMap.ContainsKey(hash))
                        {
                            files.Add(file);
                            hashMap.Add(file.GetHash(), file);
                            hashIndexMap.Add(file.GetHashName(), (uint)files.Count - 1);
                        }
                        fileIndex++;
                    }
                }
            }
            Debug.Log("Read " + fileIndex + " files in Repository.");
        }