示例#1
0
        public static void PackTCDFile(string directoryPath, string outputPrefix, Action <string, double> progressCallback)
        {
            var fullPath     = new DirectoryInfo(Path.GetFullPath(directoryPath));
            var patternFile  = Path.Combine(fullPath.Parent.FullName, $"{outputPrefix}{TCDEncodeDecode.PatternSuffix}");
            var resourceFile = Path.Combine(fullPath.Parent.FullName, $"{outputPrefix}{TCDEncodeDecode.ResourceSuffix}");

            var files = Directory.EnumerateFiles(directoryPath).ToList();

            files.Sort();

            var packThread = new Thread(() =>
            {
                try
                {
                    using (var sw = new StreamWriter(new FileStream(patternFile, FileMode.Create)))
                    {
                        using (var bw = new BinaryWriter(new FileStream(resourceFile, FileMode.Create)))
                        {
                            var fileCount  = files.Count;
                            var fileNumber = 0;
                            foreach (var file in files)
                            {
                                using (var br = new BinaryReader(File.OpenRead(file), Encoding.GetEncoding("Shift_JIS")))
                                {
                                    var size     = br.BaseStream.Length;
                                    var fileName = Path.GetFileName(file);
                                    var pattern  = $"{fileName}_{size}";

                                    progressCallback(fileName, (double)fileNumber / fileCount);

                                    var rawBuffer = br.ReadBytes((int)size);
                                    var buffer    = rawBuffer.Select((b, i) => i > 1024 ? (byte)b : (byte)(~b & 0xff)).ToArray();
                                    bw.Write(buffer);

                                    var encryptedPattern = TCDEncodeDecode.EncryptString(new StringBuilder(pattern), TCDEncodeDecode.DefaultPassword);
                                    sw.Write($"{encryptedPattern}\r\n");

                                    fileNumber += 1;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    progressCallback(null, 0);
                }
            });

            packThread.Start();
        }
示例#2
0
        public static void UnpackTCDFile(string directoryPath, string inputPrefix, Action <string, double> progressCallback)
        {
            var fullPath     = new DirectoryInfo(Path.GetFullPath(directoryPath));
            var patternFile  = Path.Combine(fullPath.Parent.FullName, $"{inputPrefix}{TCDEncodeDecode.PatternSuffix}");
            var resourceFile = Path.Combine(fullPath.Parent.FullName, $"{inputPrefix}{TCDEncodeDecode.ResourceSuffix}");

            Directory.CreateDirectory(directoryPath);

            var unpackThread = new Thread(() =>
            {
                try
                {
                    using (var rsr = new BinaryReader(File.OpenRead(resourceFile), Encoding.GetEncoding("Shift_JIS")))
                    {
                        using (var psr = new StreamReader(File.OpenRead(patternFile), Encoding.GetEncoding("Shift_JIS")))
                        {
                            var files      = psr.ReadToEnd().Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            var fileCount  = files.Length;
                            var fileNumber = 0;
                            foreach (var file in files)
                            {
                                var pattern = TCDEncodeDecode.DecryptString(file, TCDEncodeDecode.DefaultPassword);

                                var parts    = pattern.Split('_');
                                var fileName = string.Join("_", parts.Take(parts.Length - 1));
                                var newFile  = Path.Combine(directoryPath, fileName);
                                var size     = int.Parse(parts.Last());

                                progressCallback(fileName, (double)fileNumber / fileCount);

                                var rawBuffer = rsr.ReadBytes(size);

                                var buffer = rawBuffer.Select((b, i) => i > 1024 ? (byte)b : (byte)(~b & 0xff)).ToArray();
                                using (var bw = new BinaryWriter(new FileStream(newFile, FileMode.Create)))
                                {
                                    bw.Write(buffer);
                                }

                                fileNumber += 1;
                            }
                        }
                    }
                }
                finally
                {
                    progressCallback(null, 0);
                }
            });

            unpackThread.Start();
        }
示例#3
0
        public static string EncryptString(StringBuilder sourceString, string password)
        {
            var rijndaelManaged = new RijndaelManaged();

            TCDEncodeDecode.GenerateKeyFromPassword(password, rijndaelManaged.KeySize, out byte[] key, rijndaelManaged.BlockSize, out byte[] iv);
            rijndaelManaged.Key = key;
            rijndaelManaged.IV  = iv;
            var bytes     = Encoding.UTF8.GetBytes(sourceString.ToString());
            var encryptor = rijndaelManaged.CreateEncryptor();
            var inArray   = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);

            encryptor.Dispose();
            return(Convert.ToBase64String(inArray));
        }
示例#4
0
        public static string DecryptString(string sourceString, string password)
        {
            var rijndaelManaged = new RijndaelManaged();

            TCDEncodeDecode.GenerateKeyFromPassword(password, rijndaelManaged.KeySize, out byte[] key, rijndaelManaged.BlockSize, out byte[] iv);
            rijndaelManaged.Key = key;
            rijndaelManaged.IV  = iv;
            var inputBuffer = Convert.FromBase64String(sourceString);
            var decryptor   = rijndaelManaged.CreateDecryptor();
            var bytes       = decryptor.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);

            decryptor.Dispose();
            return(Encoding.UTF8.GetString(bytes));
        }
示例#5
0
 public static string ReadTextFile(string path, bool decode)
 {
     try
     {
         using (var stream = new FileStream(path, FileMode.Open))
         {
             var fileReader   = new StreamReader(stream);
             var fileContents = fileReader.ReadToEnd();
             return(decode ? TCDEncodeDecode.DecodeMap(fileContents) : fileContents);
         }
     }
     catch (Exception e)
     {
         MessageBox.Show($"File could not be read.\n{e.GetType().ToString()}: {e.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         return(null);
     }
 }
示例#6
0
        public void Load()
        {
            string resourceFile, patternFile, fileBase;

            resourceFile = patternFile = fileBase = string.Empty;
            if (!File.Exists(this.selectedFile))
            {
                throw new InvalidOperationException($"Missing file \"{this.selectedFile}\".");
            }
            else if (this.selectedFile.EndsWith(TCDLoadStrategy.ResourceSuffix, StringComparison.InvariantCultureIgnoreCase))
            {
                fileBase     = Path.GetFileName(this.selectedFile.Substring(0, this.selectedFile.Length - TCDLoadStrategy.ResourceSuffix.Length));
                resourceFile = this.selectedFile;
                patternFile  = Path.Combine(Path.GetDirectoryName(this.selectedFile), fileBase + TCDLoadStrategy.PatternSuffix);
                if (!File.Exists(patternFile))
                {
                    throw new InvalidOperationException($"Missing associated pattern file \"{patternFile}\".");
                }
            }
            else if (this.selectedFile.EndsWith(TCDLoadStrategy.PatternSuffix, StringComparison.InvariantCultureIgnoreCase))
            {
                fileBase     = Path.GetFileName(this.selectedFile.Substring(0, this.selectedFile.Length - TCDLoadStrategy.PatternSuffix.Length));
                resourceFile = Path.Combine(Path.GetDirectoryName(this.selectedFile), fileBase + TCDLoadStrategy.ResourceSuffix);
                patternFile  = this.selectedFile;
                if (!File.Exists(resourceFile))
                {
                    throw new InvalidOperationException($"Missing associated resource file \"{resourceFile}\".");
                }
            }
            else
            {
                throw new InvalidOperationException($"Invalid file \"{this.selectedFile}\".{Environment.NewLine}Expected a \"<name>Resource\" or \"<name>Pattern\" .tcd file.");
            }

            var unpackThread = new Thread(() =>
            {
                try
                {
                    using (var rsr = new BinaryReader(File.OpenRead(resourceFile), Encoding.GetEncoding("Shift_JIS")))
                    {
                        using (var psr = new StreamReader(File.OpenRead(patternFile), Encoding.GetEncoding("Shift_JIS")))
                        {
                            var files      = psr.ReadToEnd().Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            var fileCount  = files.Length;
                            var fileNumber = 0;
                            foreach (var file in files)
                            {
                                var pattern = TCDEncodeDecode.DecryptString(file, this.password);

                                var parts    = pattern.Split('_');
                                var fileName = string.Join("_", parts.Take(parts.Length - 1));
                                var size     = int.Parse(parts.Last());

                                var rawBuffer = rsr.ReadBytes(size);

                                var buffer = rawBuffer.Select((b, i) => i > 1024 ? (byte)b : (byte)(~b & 0xff)).ToArray();
                                this.fileBytes[fileName] = buffer;

                                fileNumber += 1;

                                this.UpdateProgress(fileName, (double)fileNumber / fileCount);
                            }
                        }
                    }
                }
                catch (CryptographicException)
                {
                    MessageBox.Show("Resource file decryption error. Check the password in the Settings.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    this.UpdateProgressComplete();
                }
            });

            unpackThread.Start();
        }