Fixed custom commands not reloading when switching languages

This commit is contained in:
2022-05-11 21:48:24 +08:00
parent da7033395b
commit c089b7ac07

View File

@ -30,6 +30,7 @@ namespace GrasscutterTools
GameData.LoadResources(); GameData.LoadResources();
LoadCustomCommands();
InitArtifactList(); InitArtifactList();
InitGameItemList(); InitGameItemList();
InitWeapons(); InitWeapons();
@ -45,7 +46,6 @@ namespace GrasscutterTools
SaveSettings(); SaveSettings();
} }
private readonly string CustomCommandsFilePath = Path.Combine(Application.LocalUserAppDataPath, "CustomCommands.txt");
private readonly string[] LanguageNames = new string[] { "简体中文", "English" }; private readonly string[] LanguageNames = new string[] { "简体中文", "English" };
private readonly string[] Languages = new string[] { "zh-CN", "en-US" }; private readonly string[] Languages = new string[] { "zh-CN", "en-US" };
@ -60,10 +60,6 @@ namespace GrasscutterTools
CmbLanguage.Items.AddRange(LanguageNames); CmbLanguage.Items.AddRange(LanguageNames);
CmbLanguage.SelectedIndex = Array.IndexOf(Languages, Settings.Default.DefaultLanguage); CmbLanguage.SelectedIndex = Array.IndexOf(Languages, Settings.Default.DefaultLanguage);
if (File.Exists(CustomCommandsFilePath))
LoadCustomCommands(File.ReadAllText(CustomCommandsFilePath));
else
LoadCustomCommands(Resources.CustomCommands);
InitGiveItemRecord(); InitGiveItemRecord();
InitSpawnRecord(); InitSpawnRecord();
} }
@ -80,7 +76,7 @@ namespace GrasscutterTools
Settings.Default.AutoCopy = ChkAutoCopy.Checked; Settings.Default.AutoCopy = ChkAutoCopy.Checked;
Settings.Default.Uid = NUDUid.Value; Settings.Default.Uid = NUDUid.Value;
Settings.Default.Save(); Settings.Default.Save();
File.WriteAllText(CustomCommandsFilePath, SaveCustomCommands()); SaveCustomCommands();
SaveGiveItemRecord(); SaveGiveItemRecord();
SaveSpawnRecord(); SaveSpawnRecord();
} }
@ -134,8 +130,19 @@ namespace GrasscutterTools
#endregion - - #endregion - -
#region - - #region - -
private readonly string CustomCommandsFilePath = Path.Combine(Application.LocalUserAppDataPath, "CustomCommands.txt");
private void LoadCustomCommands(string commands) private bool CustomCommandsChanged;
private void LoadCustomCommands()
{
if (File.Exists(CustomCommandsFilePath))
LoadCustomCommandControls(File.ReadAllText(CustomCommandsFilePath));
else
LoadCustomCommandControls(Resources.CustomCommands);
}
private void LoadCustomCommandControls(string commands)
{ {
FLPCustomCommands.Controls.Clear(); FLPCustomCommands.Controls.Clear();
var lines = commands.Split('\n'); var lines = commands.Split('\n');
@ -143,7 +150,13 @@ namespace GrasscutterTools
AddCustomCommand(lines[i].Trim(), lines[i+1].Trim()); AddCustomCommand(lines[i].Trim(), lines[i+1].Trim());
} }
private string SaveCustomCommands() private void SaveCustomCommands()
{
if (CustomCommandsChanged)
File.WriteAllText(CustomCommandsFilePath, SaveCustomCommandControls());
}
private string SaveCustomCommandControls()
{ {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
foreach (LinkLabel lnk in FLPCustomCommands.Controls) foreach (LinkLabel lnk in FLPCustomCommands.Controls)
@ -183,11 +196,13 @@ namespace GrasscutterTools
if (lnk.Text == name) if (lnk.Text == name)
{ {
lnk.Tag = command; lnk.Tag = command;
CustomCommandsChanged = true;
await ButtonComplete(BtnSaveCustomCommand); await ButtonComplete(BtnSaveCustomCommand);
return; return;
} }
} }
CustomCommandsChanged = true;
AddCustomCommand(name, command); AddCustomCommand(name, command);
await ButtonComplete(BtnSaveCustomCommand); await ButtonComplete(BtnSaveCustomCommand);
} }
@ -218,6 +233,7 @@ namespace GrasscutterTools
if (lnk.Text == name && MessageBox.Show("确认删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) if (lnk.Text == name && MessageBox.Show("确认删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
FLPCustomCommands.Controls.Remove(lnk); FLPCustomCommands.Controls.Remove(lnk);
CustomCommandsChanged = true;
//TxtCustomName.Text = ""; //TxtCustomName.Text = "";
//TxtCommand.Text = ""; //TxtCommand.Text = "";
await ButtonComplete(BtnRemoveCustomCommand); await ButtonComplete(BtnRemoveCustomCommand);
@ -239,7 +255,8 @@ namespace GrasscutterTools
using (var stream = dialog.OpenFile()) using (var stream = dialog.OpenFile())
using (var reader = new StreamReader(stream)) using (var reader = new StreamReader(stream))
{ {
LoadCustomCommands(reader.ReadToEnd()); LoadCustomCommandControls(reader.ReadToEnd());
CustomCommandsChanged = true;
} }
} }
} }
@ -256,7 +273,7 @@ namespace GrasscutterTools
using (var stream = dialog.OpenFile()) using (var stream = dialog.OpenFile())
using (var writer = new StreamWriter(stream)) using (var writer = new StreamWriter(stream))
{ {
writer.Write(SaveCustomCommands()); writer.Write(SaveCustomCommandControls());
} }
} }
} }