static bool BenchmarkFile(byte[] compressedBuffer, uint offset, out float compressionRatio)
        {
            compressionRatio = 0f;

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write($"Processing data at 0x{offset:X8}... ");

            byte[] uncompressedBuffer;

            try
            {
                uncompressedBuffer = MPCompression.Decompress(compressedBuffer);
            }
            catch (InvalidDataException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                return(false);
            }

            byte[] compressedBuffer2   = MPCompression.Compress(uncompressedBuffer);
            byte[] uncompressedBuffer2 = MPCompression.Decompress(compressedBuffer2);

            bool areEqual = Utils.CompareArrays(uncompressedBuffer, uncompressedBuffer2);

            if (!areEqual)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Decompression mismatch.");
                return(false);
            }

            compressionRatio = (float)compressedBuffer2.Length / compressedBuffer.Length;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Done! Compression ratio: {compressionRatio}");

            return(true);
        }
        public static void MassExtraction(string romFilePath, string outputPath, bool decompress)
        {
            if (string.IsNullOrEmpty(romFilePath))
            {
                throw new ArgumentNullException(nameof(romFilePath));
            }

            if (string.IsNullOrEmpty(outputPath))
            {
                throw new ArgumentNullException(nameof(outputPath));
            }

            if (!File.Exists(romFilePath))
            {
                throw new FileNotFoundException($"File \"{romFilePath}\" does not exist!", romFilePath);
            }

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            using (FileStream romStream = File.OpenRead(romFilePath))
            {
                ProcessData(romStream, (dataPosition, filePosition, subFilePosition, fileData) =>
                {
                    uint subFileAbsolutePosition = dataPosition + filePosition + subFilePosition;

                    if (decompress)
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.Write($"Decompressing data at 0x{subFileAbsolutePosition:X8}... ");

                        string filePath = Path.Combine(outputPath, filePosition.ToString("X8"));
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }

                        byte[] uncompressedData = null;

                        try
                        {
                            uncompressedData = MPCompression.Decompress(fileData);
                        }
                        catch (InvalidDataException)
                        {
                        }

                        if (uncompressedData != null)
                        {
                            string subFilePath = Path.Combine(filePath, subFilePosition.ToString("X8"));
                            File.WriteAllBytes(subFilePath, uncompressedData);

                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine($"Done!");
                        }
                        else
                        {
                            string subFilePath = Path.Combine(filePath, subFilePosition.ToString("X8"));
                            File.WriteAllBytes(subFilePath, fileData);

                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine($"Not valid compressed data. Extracted as is.");
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.Write($"Extracting data at 0x{subFileAbsolutePosition:X8}... ");

                        string filePath = Path.Combine(outputPath, filePosition.ToString("X8"));
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }

                        string subFilePath = Path.Combine(filePath, subFilePosition.ToString("X8"));
                        File.WriteAllBytes(subFilePath, fileData);

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"Done!");
                    }
                });
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"\nProcess finished!\n");
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            ShowHeader();

            try
            {
                if (args.Length == 2)
                {
                    if (args[0] == "-b")
                    {
                        ROM.Benchmark(args[1]);
                    }
                    else
                    {
                        ShowUsage();
                    }
                }
                else if (args.Length == 3)
                {
                    if (args[0] == "-d")
                    {
                        MPCompression.Decompress(args[1], args[2]);

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Decompression process finished successfully.\n");
                    }
                    else if (args[0] == "-c")
                    {
                        MPCompression.Compress(args[1], args[2]);

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Compression process finished successfully.\n");
                    }
                    else if (args[0] == "-me")
                    {
                        ROM.MassExtraction(args[1], args[2], false);

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Mass extraction process finished successfully.\n");
                    }
                    else if (args[0] == "-md")
                    {
                        ROM.MassExtraction(args[1], args[2], true);

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Mass decompression process finished successfully.\n");
                    }
                    else
                    {
                        ShowUsage();
                    }
                }
                else if (args.Length == 4)
                {
                    if (args[0] == "-et")
                    {
                        Table table = new Table(args[3]);
                        Texts.Extract(args[1], args[2], table);

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Extracted all texts successfully.\n");
                    }
                    else if (args[0] == "-it")
                    {
                        Table table = new Table(args[3]);
                        Texts.Insert(args[1], args[2], table);

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Inserted all texts successfully.\n");
                    }
                    else
                    {
                        ShowUsage();
                    }
                }
                else
                {
                    ShowUsage();
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
            }

            Console.ForegroundColor = ConsoleColor.Gray;

#if DEBUG
            Console.ReadLine();
#endif
        }