Esempio n. 1
0
        private static void ExploreDirectory(List <ResourceInfo> resources, String directoryPath, String curPath)
        {
            foreach (String file in Directory.EnumerateFiles(directoryPath))
            {
                if (file.Split('\\').Last().Split('.').Length != 2)
                {
                    continue;
                }

                string fileName  = file.Split('\\').Last().Split('.')[0];
                string extension = file.Split('\\').Last().Split('.')[1].ToLower();

                if (curPath == "" && fileName == "info" && extension == "txt")
                {
                    continue;
                }

                RManager manager = Res.GetManager(extension);

                resources.Add(new ResourceInfo {
                    FilePath = file, CurPath = curPath, FileName = fileName, Extension = extension, Manager = manager
                });
            }

            foreach (String dir in Directory.EnumerateDirectories(directoryPath))
            {
                ExploreDirectory(resources, dir, curPath + dir.Split('\\').Last() + "_");
            }
        }
Esempio n. 2
0
        public static void RegisterManager(RManager manager)
        {
            stResourceManagers.Add(manager.ValueType, manager);

            foreach (String ext in manager.FileExtensions)
            {
                stResourceExtensions.Add(ext, manager);
            }

            stResourceDictionaries.Add(manager.ValueType, new Dictionary <string, object>());
        }
Esempio n. 3
0
        internal void SaveToFile(String filePath)
        {
            byte[] bytes;
            byte[] hash;

            using (MemoryStream mstr = new MemoryStream())
            {
                using (BinaryWriter bstr = new BinaryWriter(mstr))
                {
                    foreach (Type type in myDictionaries.Keys)
                    {
                        Dictionary <String, object> dict = myDictionaries[type];

                        if (dict.Count == 0)
                        {
                            continue;
                        }

                        RManager manager = Res.GetManager(type);

                        bstr.Write(type.FullName);
                        long pos = mstr.Position;
                        bstr.Write((uint)0xFFFFFFFF);

                        foreach (String key in dict.Keys)
                        {
                            bstr.Write(key);
                            manager.SaveToArchive(bstr, dict[key]);
                        }

                        uint len = (uint)(mstr.Position);
                        mstr.Seek(pos, SeekOrigin.Begin);
                        bstr.Write(len);
                        mstr.Seek(0, SeekOrigin.End);
                    }
                }

                bytes = mstr.ToArray();
                hash  = new SHA256CryptoServiceProvider().ComputeHash(bytes);
            }

            myHash = Encoding.ASCII.GetString(hash);

            using (MemoryStream mstr = new MemoryStream())
            {
                using (BZip2OutputStream cstr = new BZip2OutputStream(mstr))
                {
                    cstr.Write(bytes, 0, bytes.Length);
                }

                bytes = mstr.ToArray();
            }

            using (FileStream fstr = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriter bstr = new BinaryWriter(fstr))
                {
                    bstr.Write(ArchiverVersion);
                    bstr.Write(Name);
                    bstr.Write(Author);
                    bstr.Write(Version);
                    bstr.Write(AuthorWebsite);
                    bstr.Write(AuthorEmail);
                    bstr.Write(Description);
                    bstr.Write((byte)Destination);

                    bstr.Write(bytes.Length);
                    bstr.Write(bytes);

                    bstr.Write(hash.Length);
                    bstr.Write(hash);
                }
            }
        }
Esempio n. 4
0
        internal Archive(String filePath, bool unpackData = true)
            : this()
        {
            myFilePath = filePath;

            ushort archiverVersion;

            byte[] bytes;
            byte[] hash;

            using (FileStream fstr = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader bstr = new BinaryReader(fstr))
                {
                    archiverVersion = bstr.ReadUInt16();
                    myName          = bstr.ReadString();
                    myAuthor        = bstr.ReadString();
                    myVersion       = bstr.ReadString();
                    myAuthorWebsite = bstr.ReadString();
                    myAuthorEmail   = bstr.ReadString();
                    myDescription   = bstr.ReadString();
                    myDest          = (ArchiveDest)bstr.ReadByte();

                    int len = bstr.ReadInt32();
                    bytes = bstr.ReadBytes(len);
                    len   = bstr.ReadInt32();
                    hash  = bstr.ReadBytes(len);
                }
            }

            myHash = Encoding.ASCII.GetString(hash);

            if (!unpackData)
            {
                return;
            }

            byte[] data;

            using (MemoryStream mstr = new MemoryStream(bytes))
            {
                using (BZip2InputStream cstr = new BZip2InputStream(mstr))
                {
                    using (MemoryStream ostr = new MemoryStream())
                    {
                        int chunkSize = 4096;

                        byte[] buffer = new byte[chunkSize];
                        int    len;

                        while ((len = cstr.Read(buffer, 0, chunkSize)) > 0)
                        {
                            ostr.Write(buffer, 0, len);
                        }

                        ostr.Flush();

                        data = ostr.ToArray();
                    }
                }
            }

            byte[] newHash = new SHA256CryptoServiceProvider().ComputeHash(data);

            if (!Enumerable.SequenceEqual(hash, newHash))
            {
                throw new Exception("The data in archive \"" + Name + "\" has been corrupted!");
            }

            using (MemoryStream mstr = new MemoryStream(data))
            {
                using (BinaryReader bstr = new BinaryReader(mstr))
                {
                    while (mstr.Position < mstr.Length)
                    {
                        String typeName = bstr.ReadString();
                        Type   type     = Assembly.GetExecutingAssembly().GetType(typeName);

                        if (type == null)
                        {
                            foreach (Type t in myDictionaries.Keys)
                            {
                                if (typeName == t.FullName)
                                {
                                    type = t;
                                    break;
                                }
                            }
                        }

                        long pos = bstr.ReadUInt32();

                        if (type != null)
                        {
                            RManager manager = Res.GetManager(type);

                            while (mstr.Position < pos)
                            {
                                String key = bstr.ReadString();
                                object obj = manager.LoadFromArchive(bstr);
                                myDictionaries[type].Add(key, obj);
                            }
                        }

                        mstr.Seek(pos, SeekOrigin.Begin);
                    }
                }
            }
        }