Esempio n. 1
0
 private static void Decrypt(string input, string output, IResourceCryptographyService crypto)
 {
     using (var outputFs = new FileStream(output, FileMode.Create))
         using (var inputFs = new FileStream(input, FileMode.Open))
             using (var cryptoFs = crypto.Read(inputFs))
             {
                 cryptoFs.CopyTo(outputFs);
             }
 }
        private GameResource GetArchivedResource(string path)
        {
            string original           = path;
            var    encryptedExtension = GetConversionExtension(path);

            do
            {
                path = Path.GetDirectoryName(path);
                if (path.Length != 0)
                {
                    string nonResPath = path;
                    path = path + ".res";
                    string resPath = path;
                    if (!string.IsNullOrWhiteSpace(_rootDir))
                    {
                        resPath = Path.Combine(_rootDir, path);
                    }
                    if (File.Exists(resPath))
                    {
                        var             fileStream = new FileStream(resPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                        var             zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read);
                        var             entryName  = original.Substring(nonResPath.Length).TrimStart('/');
                        ZipArchiveEntry zipEntry;
                        if (encryptedExtension == null)
                        {
                            zipEntry = zipArchive.GetEntry(entryName);
                            return(new GameResource(original, zipEntry.Open(), zipArchive, fileStream));
                        }
                        else
                        {
                            zipEntry = zipArchive.GetEntry(Path.ChangeExtension(entryName, encryptedExtension));
                            if (zipEntry == null)
                            {
                                return(null);
                            }
                            var stream       = zipEntry.Open();
                            var cryptoStream = _resourceCryptographyService.Read(stream);
                            return(new GameResource(original, cryptoStream, zipArchive, fileStream));
                        }
                    }
                }
            } while (!string.IsNullOrWhiteSpace(path));
            return(null);
        }