public static void ExtractWP(string fileName, string outputDirectory) { byte[] raw_file = File.ReadAllBytes(fileName); Directory.CreateDirectory(outputDirectory); var files = AMB_old.ReadWP(raw_file); for (int i = 0; i < files.Count; i++) { string outputFile = Path.Combine(outputDirectory, files[i].Name.Replace('\\', Path.DirectorySeparatorChar)); //Copying raw file from the archive into a byte array. byte[] fileBytes = new byte[files[i].Length]; Array.Copy(raw_file, files[i].Pointer, fileBytes, 0, files[i].Length); Directory.CreateDirectory(Path.GetDirectoryName(outputFile)); //And writing that byte array into a file File.WriteAllBytes(outputFile, fileBytes); } }
static void Main(string[] args) { Settings.Load(); if (args.Length == 0) { if (!File.Exists("mods/mods.ini")) { ShowHelpMessage(); return; } var test = GetModFiles(); List <string> mods_prev = new List <string> { }; List <string> modified_files = new List <string> { }; if (File.Exists("mods/mods_prev")) { mods_prev = File.ReadAllLines("mods/mods_prev").ToList <string>(); } ProgressBar.PrintFiller(); ProgressBar.MoveCursorUp(); for (int i = 0; i < test.Count; i++) { modified_files.Add(test[i].OrigFile); ProgressBar.PrintProgress(i, test.Count, "Modifying \"" + test[i].OrigFile + "\"..."); ProgressBar.MoveCursorDown(); Backup(test[i].OrigFile); //Some CSB files may have CPK archive if (File.Exists(test[i].OrigFile.Substring(0, test[i].OrigFile.Length - 4) + ".CPK")) { Backup(test[i].OrigFile.Substring(0, test[i].OrigFile.Length - 4) + ".CPK"); } PatchAll(test[i].OrigFile, test[i].ModFiles, test[i].ModName); mods_prev.Remove(test[i].OrigFile); ProgressBar.MoveCursorUp(); } ProgressBar.PrintProgress(1, 1, ""); ProgressBar.MoveCursorDown(); ProgressBar.PrintProgress(1, 1, ""); for (int i = 0; i < mods_prev.Count; i++) { Recover(mods_prev[i]); //Some CSB files may have CPK archive if (mods_prev[i].EndsWith(".CSB", StringComparison.OrdinalIgnoreCase)) { Recover(mods_prev[i].Substring(0, mods_prev[i].Length - 4) + ".CPK"); } if (mods_prev[i].EndsWith(".CSB", StringComparison.OrdinalIgnoreCase)) { ShaChecker.ShaRemove(mods_prev[i].Substring(0, mods_prev[i].Length - 4)); } else { ShaChecker.ShaRemove(mods_prev[i]); } } if (Directory.Exists("mods")) { File.WriteAllText("mods/mods_prev", string.Join("\n", modified_files.ToArray())); } } else if (args.Length == 1) { if (args[0] == "-h" || args[0] == "--help") { ShowHelpMessage(); } else if (args[0] == "recover") { if (File.Exists("mods/mods_prev")) { string[] mods_prev = File.ReadAllLines("mods/mods_prev"); for (int i = 0; i < mods_prev.Length; i++) { string file = mods_prev[i]; ProgressBar.PrintProgress(i, mods_prev.Length, "Recovering \"" + file + "\" file..."); Recover(file); if (file.EndsWith(".CSB", StringComparison.OrdinalIgnoreCase)) { Recover(file.Substring(0, file.Length - 4) + ".CPK"); ShaChecker.ShaRemove(file.Substring(0, file.Length - 4)); } else { ShaChecker.ShaRemove(file); } } File.Delete("mods/mods_prev"); ProgressBar.PrintProgress(1, 1, ""); } } else { if (File.Exists(args[0])) { new AMB(args[0]).Extract(); } else if (Directory.Exists(args[0])) { AMB amb; if (args[0].EndsWith("_extracted") && (File.Exists(args[0].Substring(0, args[0].Length - 14) + ".AMB") || File.Exists(args[0].Substring(0, args[0].Length - 14) + ".amb"))) { var filePath = args[0].Substring(0, args[0].Length - 14) + ".AMB"; if (!File.Exists(filePath)) { filePath = args[0].Substring(0, args[0].Length - 14) + ".amb"; } amb = new AMB(filePath); } else { amb = new AMB(); } amb.AmbPath = args[0]; var files = Directory.GetFiles(args[0], "*.*", SearchOption.AllDirectories).OrderBy(x => x); foreach (var f in files) { amb.Add(f); } amb.Save(args[0] + ".AMB"); } else { ShowHelpMessage(); } } } else if (args.Length == 2) { if (args[0] == "extract") { if (File.Exists(args[1])) { new AMB(args[1]).Extract(); } else { ShowHelpMessage(); } } else if (args[0] == "read") { if (File.Exists(args[1])) { var amb = new AMB(args[1]); foreach (var o in amb.Objects) { Console.WriteLine("\nFile name: " + o.Name); Console.WriteLine("File pointer: " + o.Pointer + "\t(0x" + o.Pointer.ToString("X") + ")"); Console.WriteLine("File length: " + o.Length + "\t(0x" + o.Length.ToString("X") + ")"); } } else { ShowHelpMessage(); } } else if (File.Exists(args[0]) && Directory.Exists(args[1])) { var files = Directory.GetFiles(args[1], "*", SearchOption.AllDirectories); if (files.Length == 0) { return; } var amb = new AMB(args[0]); foreach (string file in files) { Console.WriteLine("Patching by \"" + file + "\"..."); amb.Add(file); } amb.Save(); Console.WriteLine("Done."); } else if (args[0] == "swap_endianness" && File.Exists(args[1])) { new AMB(args[1]).Save(args[1], true); } else if (args[0] == "endianness" && File.Exists(args[1])) { if (new AMB(args[1]).IsLittleEndian()) { Console.WriteLine("Little endian"); } else { Console.WriteLine("Big endian"); } } else if (args[0] == "create") { new AMB().Save(args[1]); } else if (args[0] == "extract_all") { string[] files = { args[1] }; if (Directory.Exists(args[1])) { files = Directory.GetFiles(args[1], "*.*", SearchOption.AllDirectories); } foreach (var f in files) { new AMB(f).ExtractAll(); } } else if (args[0] == "extract_wp") { if (File.Exists(args[1])) { AMB_old.ExtractWP(args[1], args[1] + "_extracted"); } else { ShowHelpMessage(); } } else if (args[0] == "recreate" && File.Exists(args[1])) { var amb = new AMB(args[1]); amb.Save(args[1] + "_recreated"); } else { ShowHelpMessage(); } } else if (args.Length == 3) { if (args[0] == "find") { var amb = new AMB(args[1]); var a = amb.FindObject(args[2]); foreach (var o in a.amb.Objects) { Console.WriteLine(o.Name); } Console.ReadLine(); } if (args[0] == "extract") { if (File.Exists(args[1])) { new AMB(args[1]).Extract(args[2]); } else { ShowHelpMessage(); } } else if (args[0] == "add") { if (File.Exists(args[1]) && File.Exists(args[2])) { var amb = new AMB(args[1]); amb.Add(args[2]); amb.Save(); } else if (File.Exists(args[1]) && Directory.Exists(args[2])) { var files = Directory.GetFiles(args[2], "*", SearchOption.AllDirectories).OrderBy(x => x); var amb = new AMB(args[1]); foreach (var file in files) { amb.Add(file); } amb.Save(); } else { ShowHelpMessage(); } } else if (args[0] == "delete" || args[0] == "remove") { if (File.Exists(args[1])) { var amb = new AMB(args[1]); amb.Remove(args[2]); amb.Save(); } else { ShowHelpMessage(); } } else { ShowHelpMessage(); } } else if (args.Length == 4) { if (args[0] == "add") { if (File.Exists(args[1]) && File.Exists(args[2])) { var amb = new AMB(args[1]); amb.Add(args[2], args[3]); amb.Save(); } else { ShowHelpMessage(); } } else { ShowHelpMessage(); } } else { ShowHelpMessage(); } }