static void RunScript(string Filename, string[] parameters) { Console.WriteLine("Loading {0}...", Filename); string[] Lines = File.ReadAllLines(Filename); Console.WriteLine("Script Loaded."); Thread.Sleep(100); RARC CurrentArchive = null; string ErrorMessage = ""; for (int i = 0; i < Lines.Length; i++) { if (Lines[i].StartsWith("//") || string.IsNullOrWhiteSpace(Lines[i])) { continue; } string Line = string.Format(Lines[i], parameters); string[] Params = Line.Split(' '); Console.WriteLine("Executing Line {1}: \"{0}\"", Lines[i], i); switch (Params[0]) { case "new": //new if (CurrentArchive is null) { CurrentArchive = new RARC(); CurrentArchive["Root"] = null; Console.WriteLine("New Archive Created Successfully"); } else { ErrorMessage = string.Format("Could not create a new archive:\n\tAn archive is already loaded in memory!"); goto Error; } break; case "open": //open <filepath> if (CurrentArchive is null) { if (Params.Length < 2) { ErrorMessage = string.Format("Incomplete Syntax - Expected\nopen <filepath>"); goto Error; } if (!File.Exists(Params[1])) { ErrorMessage = string.Format("File {0} could not be found", Params[1]); goto Error; } bool IsYaz0 = YAZ0.Check(Params[1]); bool IsYay0 = YAY0.Check(Params[1]); CurrentArchive = IsYaz0 ? new RARC(YAZ0.DecompressToMemoryStream(Params[1]), Params[1]) : (IsYay0 ? new RARC(YAY0.DecompressToMemoryStream(Params[1]), Params[1]) : new RARC(Params[1])); Console.WriteLine("Archive opened successfully!"); } else { ErrorMessage = string.Format("Could not open the archive:\n\tAn archive is already loaded in memory!"); goto Error; } break; case "save": //save [filepath] if (CurrentArchive is null) { ErrorMessage = string.Format("Save Failed! No Archive Loaded"); goto Error; } CurrentArchive.Save(Params.Length == 1 ? CurrentArchive.FileName : Params[1]); Console.WriteLine("Archive saved successfully!"); break; case "compress": //compress <filepath> [-yz|-yy|-yzf] if (Params.Length < 3) { ErrorMessage = string.Format("Incomplete Syntax - Expected\ncompress <filepath> [-yz|-yy|-yzf]"); goto Error; } if (!File.Exists(Params[1])) { ErrorMessage = string.Format("File {0} could not be found", Params[1]); goto Error; } switch (Params[2]) { case "-yz": case "-yzf": Console.WriteLine("Compressing, please wait... {0}"); YAZ0.Compress(Params[1], Params[2].Contains("f")); break; case "-yy": Console.WriteLine("Compressing, please wait... {0}"); YAY0.Compress(Params[1]); break; default: ErrorMessage = string.Format("Encoding mode {0} doesn't exist", Params[1]); goto Error; } Console.WriteLine("Compress complete!"); break; case "replace": case "add": //add <filepath> <archivepath> string func = Params[0].Equals("replace") ? "replaced" : "added"; if (Params.Length < 3) { ErrorMessage = string.Format("Incomplete Syntax - Expected\n{0} <filepath> <archivepath>", Params[0]); goto Error; } if (CurrentArchive is null) { ErrorMessage = string.Format("Add failed! No archive loaded"); goto Error; } if (!File.Exists(Params[1]) && !Directory.Exists(Params[1])) { ErrorMessage = string.Format("File or Directory {0} could not be found", Params[1]); goto Error; } if (CurrentArchive.ItemExists(Params[1])) { if (Params[0].Equals("replace")) { CurrentArchive[Params[1]] = null; } else { ErrorMessage = string.Format("An item already exists at {0}", Params[1]); goto Error; } } if (File.GetAttributes(Params[1]) == FileAttributes.Directory) { CurrentArchive[Params[2]] = new RARC.Directory(Params[1], CurrentArchive); Console.WriteLine("Folder {0} {1} successfully", Params[1], func); } else { CurrentArchive[Params[2]] = new RARC.File(Params[1]); Console.WriteLine("File {0} {1} successfully", Params[1], func); } break; case "delete": //delete <archivepath> if (Params.Length < 2) { ErrorMessage = string.Format("Incomplete Syntax - Expected\ndelete <archivepath>"); goto Error; } if (CurrentArchive is null) { ErrorMessage = string.Format("Delete failed! No archive loaded"); goto Error; } if (!CurrentArchive.ItemExists(Params[1])) { ErrorMessage = string.Format("Can't delete the non-existant item {0}", Params[1]); goto Error; } CurrentArchive[Params[1]] = null; Console.WriteLine("Deleted {0} successfully", Params[1]); break; case "rename": case "move": //move <archivepath> <archivepath> func = Params[0].Equals("rename") ? "Renamed" : "Moved"; if (Params.Length < 3) { ErrorMessage = string.Format("Incomplete Syntax - Expected\n{0} <archivepath> <archivepath>", Params[0]); goto Error; } if (CurrentArchive is null) { ErrorMessage = string.Format("{0} failed! No archive loaded", Params[0]); goto Error; } if (!CurrentArchive.ItemExists(Params[1])) { ErrorMessage = string.Format("Can't {1} a non-existant item {0}", Params[1], func); goto Error; } if (CurrentArchive.ItemExists(Params[2])) { ErrorMessage = string.Format("An item already exists at {0}", Params[1]); goto Error; } CurrentArchive.MoveItem(Params[1], Params[2]); Console.WriteLine("{2} {0} to {1} successfully", Params[1], Params[2], func); break; case "extract": //extract <archivepath> <filepath> [-o] if (Params.Length < 3) { ErrorMessage = string.Format("Incomplete Syntax - Expected\nextract <archivepath> <filepath>"); goto Error; } if (CurrentArchive is null) { ErrorMessage = string.Format("Extract failed! No archive loaded"); goto Error; } if (!CurrentArchive.ItemExists(Params[1])) { ErrorMessage = string.Format("Can't extract the non-existant item {0}", Params[1]); goto Error; } if (CurrentArchive[Params[1]] is RARC.File efile) { if (File.Exists(Params[2]) && !Params.Contains("-o")) { ErrorMessage = string.Format("There is already a file on your system at {0}, consider adding the -o parameter if you want to overwrite it", Params[2]); goto Error; } efile.Save(Params[2]); } else if (CurrentArchive[Params[1]] is RARC.Directory edir) { if (Directory.Exists(Params[2]) && !Params.Contains("-o")) { ErrorMessage = string.Format("There is already a file on your system at {0}, consider adding the -o parameter if you want to overwrite it", Params[2]); goto Error; } edir.Export(Params[2]); } break; case "edit": //edit <archivepath> <Parameter[]> if (Params.Length < 3) { ErrorMessage = string.Format("Incomplete Syntax - Expected\nedit <archivepath> <parameter[]>\nParameters include:\n-id <new id>\n-setcompressed <-yz>\n-loadmain\n-loadaux\n-loaddvd\n-auto"); goto Error; } if (CurrentArchive is null) { ErrorMessage = string.Format("Edit failed! No archive loaded"); goto Error; } object getitem = CurrentArchive[Params[1]]; if (getitem is RARC.Directory) { ErrorMessage = string.Format("Edit failed! Cannot change properties of folders!"); goto Error; } if (getitem is RARC.File file) { bool mram = true, aram = false, dvd = false, compressed = false, compressedyaz0 = false; for (int x = 2; x < Params.Length; x++) { switch (Params[x]) { case "-id": if (CurrentArchive.KeepFileIDsSynced) { ErrorMessage = string.Format("Edit failed! Cannot change File ID because the Archive is set to Automatically calculate file ID's", Params[x]); goto Error; } if (short.TryParse(Params[++x], out short newid)) { file.ID = newid; } else { ErrorMessage = string.Format("Edit failed! Could not parse {0} as a int16 (short)", Params[x]); goto Error; } break; case "-setcompressed": if (x + 1 != Params.Length - 1 && Params[x + 1].Equals("-yz")) { compressedyaz0 = true; } compressed = true; break; case "-loadmain": mram = true; aram = false; dvd = false; break; case "-loadaux": mram = false; aram = true; dvd = false; break; case "loaddvd": mram = false; aram = false; dvd = true; break; case "auto": if (file.FileData[0] == 0x59 && file.FileData[1] == 0x61 && file.FileData[2] == 0x7A && file.FileData[3] == 0x30) { compressed = true; compressedyaz0 = true; } if (file.Name.Contains(".rel")) { mram = false; aram = true; dvd = false; } else { mram = true; aram = false; dvd = false; } if (!CurrentArchive.KeepFileIDsSynced && file.ID == -1) { file.ID = CurrentArchive.NextFreeFileID; } break; default: ErrorMessage = string.Format("Edit failed! Unknown file property {0}", Params[x]); goto Error; } } RARC.FileAttribute loadattribute = mram ? RARC.FileAttribute.PRELOAD_TO_MRAM : (aram ? RARC.FileAttribute.PRELOAD_TO_ARAM : (dvd ? RARC.FileAttribute.LOAD_FROM_DVD : 0)); file.FileSettings = RARC.FileAttribute.FILE | (compressed ? RARC.FileAttribute.COMPRESSED : 0) | loadattribute | (compressedyaz0 ? RARC.FileAttribute.YAZ0_COMPRESSED : 0); } break; default: ErrorMessage = string.Format("Invalid Command {0}", Params[0]); goto Error; } Console.WriteLine(); } return; Error: Console.WriteLine(ErrorMessage); Thread.Sleep(1000); return; }
private void Save() { SoundPlayer Patience = new SoundPlayer(); Console.WriteLine(); FileInfo fi = new FileInfo(Filename); if (File.Exists(Filename) && fi.IsFileLocked()) { Console.WriteLine("The chosen file cannot be accessed. The file has not been modified, and your changes have not been saved."); goto SaveFailed; } if (Program.CanPlaySfx(Program.WaitSfx)) { Patience.SoundLocation = Program.WaitSfx; Patience.Load(); Patience.PlayLooping(); } if (CameraListBox.SelectedIndex != -1) { ((CameraPanelBase)MainSplitContainer.Panel2.Controls[0]).UnLoadCamera(Cameras[CameraListBox.SelectedIndex]); } if (Settings.Default.IsEnforceCompress) { AdvancedSave(); } if (File.Exists(Filename) && fi.IsFileLocked()) { Console.WriteLine("The chosen file cannot be accessed. The file has not been modified, and your changes have not been saved."); goto SaveFailed; } switch (fi.Extension) { case ".bcam": Console.WriteLine("Saving as a Binary Camera file:"); FileStream fs = new FileStream(Filename, FileMode.Create); Cameras.Save(fs); fs.Close(); break; case ".arc": case ".rarc": if (!File.Exists(Filename)) { MessageBox.Show("The output archive does not exist. Your changes have not been saved."); goto SaveFailed; } Console.WriteLine("Loading the target Archive:"); RARC Archive = YAZ0.Check(Filename) ? new RARC(YAZ0.DecompressToMemoryStream(Filename)) : new RARC(Filename); Console.WriteLine("Archive Loaded. Looking for the .bcam to replace..."); string FinalPath = new string[] { Archive.GetItemKeyFromNoCase("Camera/CameraParam.bcam"), Archive.GetItemKeyFromNoCase("ActorInfo/CameraParam.bcam") }.FirstOrDefault(s => !string.IsNullOrEmpty(s)); if (FinalPath is null) { Console.WriteLine("Error finding a bcam"); DialogResult dr = MessageBox.Show("The archive has no .bcam to replace.\nWould you like to create one?", "Missing .bcam", MessageBoxButtons.YesNo, MessageBoxIcon.Question); Console.WriteLine($"MessageBox Response: {dr.ToString()}"); if (dr != DialogResult.Yes) { Console.WriteLine("The chosen file has not been modified, and your changes have not been saved."); goto SaveFailed; } FinalPath = "Camera/CameraParam.bcam"; } Console.WriteLine(FinalPath is null ? "Injecting..." : ".bcam found. Saving..."); MemoryStream ms = new MemoryStream(); Cameras.Save(ms); Archive[FinalPath] = new RARC.File(((RARC.File)Archive[FinalPath]).Name, ms); Console.WriteLine(".bcam saved into the archive."); Console.WriteLine("Saving the archive..."); Archive.Save(Filename); if (Settings.Default.IsUseYAZ0) { Stopwatch Watch = new Stopwatch(); long UncompressedFilesize = File.ReadAllBytes(Filename).Length; double ETA = UncompressedFilesize * Settings.Default.ElapsedTimeStrong; Watch.Start(); Yaz0BackgroundWorker.RunWorkerAsync(Filename); EnabledContents(this, false); while (Yaz0BackgroundWorker.IsBusy) { Console.Write($"\rYaz0 Encoding: ({Watch.Elapsed.ToString("mm\\:ss\\.fff")} Elapsed, {TimeSpan.FromMilliseconds(ETA).ToString("mm\\:ss\\.fff")} Estimated)"); Application.DoEvents(); } Watch.Stop(); Settings.Default.ElapsedTimeStrong = (double)Watch.ElapsedMilliseconds / (double)UncompressedFilesize; Console.WriteLine("\nYaz0 Encoding Complete!"); EnabledContents(this, true); } break; } Console.WriteLine("Save Complete!"); Console.WriteLine("Current time of Save: " + DateTime.Now.ToString("h:mm tt")); Program.IsUnsavedChanges = false; Console.WriteLine(); Patience.Stop(); if (Program.CanPlaySfx(Program.SuccessSfx)) { Patience.SoundLocation = Program.SuccessSfx; Patience.Load(); Patience.Play(); } return; SaveFailed: Patience.Stop(); if (Program.CanPlaySfx(Program.FailureSfx)) { Patience.SoundLocation = Program.FailureSfx; Patience.Load(); Patience.Play(); } return; }