DS-Pokemon-Rom-Editor/DS_Map/ROMFiles/RomFile.cs
AdAstra-LD 56b6473fcb Added an error message to Script Editor for missing usescript references
+ Script save file functions now return true when everything was saved correctly, false otherwise
+ Return value of the script save functions is checked to determine whether or not to mark current script file as changed
2022-08-30 00:07:38 +02:00

48 lines
1.7 KiB
C#

using System;
using System.IO;
using System.Windows.Forms;
using static DSPRE.RomInfo;
namespace DSPRE.ROMFiles {
public abstract class RomFile {
public abstract byte[] ToByteArray();
public bool SaveToFile(string path, bool showSuccessMessage = true) {
byte[] romFileToByteArray = ToByteArray();
if (romFileToByteArray is null) {
Console.WriteLine(GetType().Name + " couldn't be saved!");
return false;
}
File.WriteAllBytes(path, romFileToByteArray);
if (showSuccessMessage) {
MessageBox.Show(GetType().Name + " saved successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return true;
}
protected internal bool SaveToFileDefaultDir(DirNames dir, int IDtoReplace, bool showSuccessMessage = true) {
string path = RomInfo.gameDirs[dir].unpackedDir + "\\" + IDtoReplace.ToString("D4");
return this.SaveToFile(path, showSuccessMessage);
}
protected internal void SaveToFileExplorePath(string fileType, string fileExtension, string suggestedFileName, bool showSuccessMessage = true) {
fileExtension = "*." + fileExtension;
SaveFileDialog sf = new SaveFileDialog {
Filter = $"{fileType} ({fileExtension})|{fileExtension}"
};
if (!string.IsNullOrWhiteSpace(suggestedFileName)) {
sf.FileName = suggestedFileName;
}
if (sf.ShowDialog() != DialogResult.OK) {
return;
}
this.SaveToFile(sf.FileName, showSuccessMessage);
}
}
}