コード例 #1
0
        static void PatchAll(string file_name, List <string> mod_files, List <string> mod_paths)
        {
            if (File.Exists(file_name))
            {
                if (file_name.EndsWith(".AMB", StringComparison.OrdinalIgnoreCase))
                {
                    if (ShaChecker.ShaChanged(file_name, mod_files, mod_paths))
                    {
                        if (file_name == mod_files.FirstOrDefault())
                        {
                            var modFull = Path.Combine("mods", mod_paths[0], mod_files[0]);
                            File.Copy(modFull, file_name, true);
                            ShaChecker.ShaWrite(mod_files[0], modFull);
                            return;
                        }

                        AMB amb = new AMB(File.Exists(file_name + ".bkp") ? file_name + ".bkp" : file_name);
                        amb.AmbPath = file_name;

                        for (int i = 0; i < mod_files.Count; i++)
                        {
                            var mod_file_full = Path.Combine("mods", mod_paths[i], mod_files[i]);
                            ProgressBar.PrintProgress(i, mod_files.Count, mod_file_full);
                            amb.Add(mod_file_full);
                            ShaChecker.ShaWrite(mod_files[i], mod_file_full);
                        }

                        amb.Save(file_name);
                    }
                }
                else if (file_name.ToUpper().EndsWith(".CSB"))
                {
                    if (ShaChecker.ShaChanged(file_name.Substring(0, file_name.Length - 4), mod_files, mod_paths))
                    {
                        Recover(file_name);
                        if (file_name.EndsWith(".CSB", StringComparison.OrdinalIgnoreCase))
                        {
                            Recover(file_name.Substring(0, file_name.Length - 4) + ".CPK");
                        }

                        ProgressBar.PrintProgress(0, 100, "Asking CsbEditor to unpack " + file_name);

                        //Needs CSB Editor (from SonicAudioTools) to work
                        //FIXME
                        if (!Launcher.LaunchCsbEditor(file_name))
                        {
                            throw new Exception("CsbEditor not found (PatchAll)");
                        }

                        for (int i = 0; i < mod_files.Count; i++)
                        {
                            string mod_file = Path.Combine("mods", mod_paths[i], mod_files[i]);

                            ProgressBar.PrintProgress(i, mod_files.Count, mod_file);
                            File.Copy(mod_file, mod_files[i], true);

                            ShaChecker.ShaWrite(mod_files[i], mod_file);
                        }

                        ProgressBar.PrintProgress(99, 100, "Asking CsbEditor to repack " + file_name);
                        Launcher.LaunchCsbEditor(file_name.Substring(0, file_name.Length - 4));
                    }
                }
            }
        }
コード例 #2
0
        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();
            }
        }