Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("mas2extract.exe filename.MAS [-s] [target_directory]");
                Console.WriteLine("MAS2 extractor tool for rFactor 2 MAS game files.");
                Console.WriteLine("");
                Console.WriteLine("Parameter -s will only show all files inside the archive, but not extract them");
                Console.WriteLine("If no extract directory is entered, the location of mas2extract.exe will be used.");
                return;
            }
            var f= args[0];
            if(File.Exists(f) == false)
            {
                Console.WriteLine("Cannot find target MAS file!");
                return;
            }

            string target = "";
            if (args.Length == 2)
                target = args[1];
            else
                target = "./";
            bool unpack = true;
            if (target == "-s")
            {
                unpack = false;
            }else
            {
                if (!Directory.Exists(target))
                {
                    Console.WriteLine("Target extract directory doesn't exist");
                    return;
                }
            }

            MAS2Reader reader;
            try
            {
                reader = new MAS2Reader(f);
                Console.WriteLine(reader.Count + " files in archive");

                Console.WriteLine(
                    "+-----------------------------------------------------------------------------+");
                Console.Write(PatchString("| Name", 38) + " | ");
                Console.Write(PatchString("Compressed", 10) + " | ");
                Console.Write(PatchString("Raw Size", 10) + " | ");
                Console.Write(PatchString("Ratio", 10) + " |");
                Console.WriteLine();
                Console.WriteLine(
                    "+-----------------------------------------------------------------------------+");

                foreach(MAS2File file in reader.Files)
                {
                    double ratio = 1 - file.CompressedSize/file.UncompressedSize;
                    Console.Write(PatchString("| "+file.Filename, 38) + " | ");
                    Console.Write(PatchString(file.CompressedSize.ToString(), 10) + " | ");
                    Console.Write(PatchString(file.UncompressedSize.ToString(), 10) + " | ");
                    Console.Write(PatchString(Math.Round(100*ratio,1).ToString(), 10) + " |");
                    try
                    {
                        if (unpack)
                            reader.ExtractFile(file, Path.Combine(target + "\\", file.Filename));
                    }catch(Exception ex)
                    {
                        Console.Write("FAIL");
                    }
                    Console.WriteLine();
                }

                Console.WriteLine(
                    "+-----------------------------------------------------------------------------+");
                Console.WriteLine("Press ENTER to terminate");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to open MAS archive. Is it a rFactor 2 archive?");
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine(usageText);
                Environment.ExitCode = 1; // Parameter Error
                return;
            }

            // parse the command line for options and other arguments
            var options = new List<string>();
            var arguments = new List<string>();

            bool endOfOptions = false;
            foreach (string arg in args)
            {
                if (endOfOptions) arguments.Add(arg);
                else if (arg[0] == '-') options.Add(arg.TrimStart(new char[] { '-' }));
                else
                {
                    arguments.Add(arg);
                    endOfOptions = true;
                }
            }

            bool optionShowOnly = options.IndexOf("s") >= 0;
            bool optionNotCreate = options.IndexOf("c") >= 0;
            string sourceFile   = arguments.Count > 0 ? arguments[0] : null;
            string targetDir    = arguments.Count > 1 ? arguments[1] : null;

            if(sourceFile == null || !File.Exists(sourceFile))
            {
                Console.Error.WriteLine("Cannot find target MAS file!");
                Environment.ExitCode = 2; // Runtime Error
                return;
            }

            if (!optionShowOnly)
            {
                if(targetDir == null)
                {
                    // build targetDir from filename and create if not already exists
                    targetDir = "./" + Path.GetFileNameWithoutExtension(sourceFile) + ".masdir";
                    optionNotCreate = false; // true makes no sense and will be ignored
                }

                if (!Directory.Exists(targetDir))
                {
                    if (optionNotCreate)
                    {
                        Console.Error.WriteLine("Target extract directory doesn't exist");
                        Environment.ExitCode = 2; // Runtime Error
                        return;
                    }
                    Directory.CreateDirectory(targetDir);
                }
            }

            MAS2Reader reader;
            try
            {
                reader = new MAS2Reader(sourceFile);
                Console.WriteLine(reader.Count + " files in archive");

                Console.WriteLine("+{0}+", new string('-', 77));
                Console.WriteLine("| {0,-33} | {1,-10} | {2,-9} | {3,-6} | {4,-5} |",
                    "Name", "Compressed", "Raw Size", "Ratio", "Type");
                Console.WriteLine("+{0}+", new string('-', 77));

                foreach(MAS2File file in reader.Files)
                {
                    double ratio = (file.UncompressedSize == 0) ? 0.0 : 1.0 - (double)file.CompressedSize / (double)file.UncompressedSize;
                    Console.Write("| {0,-33} | {1,10} | {2,9} | {3,6:P1} | {5,1}{4,4} |",
                        LimitString(file.Filename, 33), file.CompressedSize, file.UncompressedSize, ratio, file.FileType & ~0x10010,
                        // "Z" = Zlib deflate, "M" = MAS2 encoded, " " = plain
                        (file.FileType & 0x10) != 0 ? "Z" : ((file.FileType & 0x10000)!=0 ? "M" : " ")
                    );

                    if (!optionShowOnly)
                    {
                        try
                        {
                            reader.ExtractFile(file, Path.Combine(targetDir, file.Filename));
                        }
                        catch (Exception ex)
                        {
                            Console.Write("FAIL");
                        }
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("+{0}+", new string('-', 77));

                Console.WriteLine("Press ENTER to terminate");
                Console.ReadLine();
                Environment.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Failed to open MAS archive. Is it a rFactor 2 archive?");
                Environment.ExitCode = 2;
            }
        }