Skip to content

Commit

Permalink
字符串优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakoyu authored and Hakoyu committed Dec 8, 2022
1 parent 03180e3 commit 72497c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
70 changes: 36 additions & 34 deletions Tools/ModManager/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ void CheckEnabledMods()
else
GetEnabledMods(ST.enabledModsJsonPath);
}
void ImportMode()
{
var result = MessageBox.Show(I18n.SelectImportMode, "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
ClearAllEnabledMods();
else if (result == MessageBoxResult.Cancel)
return;
}
void GetEnabledMods(string path, bool importMode = false)
{
string datas = File.ReadAllText(path);
Expand All @@ -155,17 +163,11 @@ void GetEnabledMods(string path, bool importMode = false)
{
string err = null!;
JsonNode enabledModsJson = JsonNode.Parse(datas)!;
if (enabledModsJson.AsObject().Count != 1 || enabledModsJson.AsObject().ElementAt(0).Key != "enabledMods")
if (enabledModsJson.AsObject().Count != 1 || enabledModsJson.AsObject().ElementAt(0).Key != enabledMods)
throw new();
if (importMode)
{
var result = MessageBox.Show(I18n.SelectImportMode, "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
ClearAllEnabledMods();
else if (result == MessageBoxResult.Cancel)
return;
}
JsonArray enabledModsJsonArray = enabledModsJson["enabledMods"]!.AsArray();
ImportMode();
JsonArray enabledModsJsonArray = enabledModsJson[enabledMods]!.AsArray();
STLog.Instance.WriteLine($"{I18n.LoadEnabledModsFile} {I18n.Path}: {path}");
foreach (var mod in enabledModsJsonArray)
{
Expand Down Expand Up @@ -215,8 +217,8 @@ void GetUserGroup(string path)
string group = kv.Key;
if (!allUserGroups.ContainsKey(group))
{
AddUserGroup(kv.Value["Icon"]!, group);
foreach (string id in kv.Value["Mods"].AsTomlArray)
AddUserGroup(kv.Value[strIcon]!, group);
foreach (string id in kv.Value[strMods].AsTomlArray)
{
if (allModsShowInfo.ContainsKey(id))
{
Expand Down Expand Up @@ -287,11 +289,11 @@ void GetUserData(string path)
STLog.Instance.WriteLine($"{I18n.LoadUserCustomData}");
foreach (var dic in kv.Value.AsTomlArray)
{
var id = dic["Id"].AsString;
var id = dic[strId].AsString;
if (allModsShowInfo.ContainsKey(id))
{
var info = allModsShowInfo[id];
info.UserDescription = dic["UserDescription"];
info.UserDescription = dic[strUserDescription];
}
else
{
Expand Down Expand Up @@ -436,13 +438,13 @@ ContextMenu CreateContextMenu(ModShowInfo info)
ContextMenu contextMenu = new();
contextMenu.Style = (Style)Application.Current.Resources["ContextMenu_Style"];
MenuItem menuItem = new();
menuItem.Header = info.Enabled is true ? $"{I18n.DisableSelectedMods}" : $"{I18n.EnabledSelectedMods}";
menuItem.Header = info.Enabled is true ? I18n.DisableSelectedMods : I18n.EnabledSelectedMods;
menuItem.Click += (o, e) => ChangeSelectedModsEnabled(info.Enabled is not true);
contextMenu.Items.Add(menuItem);
STLog.Instance.WriteLine($"{I18n.AddMenuItem} {menuItem.Header}", STLogLevel.DEBUG);

menuItem = new();
menuItem.Header = info.Collected is true ? $"{I18n.CancelCollectionSelectedMods}" : $"{I18n.CollectionSelectedMods}";
menuItem.Header = info.Collected is true ? I18n.CancelCollectionSelectedMods : I18n.CollectionSelectedMods;
menuItem.Click += (o, e) => ChangeSelectedModsCollected(info.Collected is not true);
contextMenu.Items.Add(menuItem);
STLog.Instance.WriteLine($"{I18n.AddMenuItem} {menuItem.Header}", STLogLevel.DEBUG);
Expand Down Expand Up @@ -662,10 +664,10 @@ void SaveEnabledMods(string path)
{
JsonObject keyValues = new()
{
{ "enabledMods", new JsonArray() }
{ enabledMods, new JsonArray() }
};
foreach (var mod in allEnabledModsId)
((JsonArray)keyValues["enabledMods"]!).Add(mod);
((JsonArray)keyValues[enabledMods]!).Add(mod);
File.WriteAllText(path, keyValues.ToJsonString(new() { WriteIndented = true }));
STLog.Instance.WriteLine($"{I18n.SaveEnabledListSuccess} {I18n.Path}: {path}");
}
Expand All @@ -674,50 +676,50 @@ void SaveUserData(string path)
using TomlTable toml = new()
{
{ ModGroupType.Collected, new TomlArray() },
{ "UserCustomData", new TomlArray() }
{ strUserCustomData, new TomlArray() }
};
foreach (var info in allModsShowInfo.Values)
{
if (info.Collected is true)
toml[ModGroupType.Collected].Add(info.Id);
if (info.UserDescription!.Length > 0)
{
toml["UserCustomData"].Add(new TomlTable()
toml[strUserCustomData].Add(new TomlTable()
{
["Id"] = info.Id,
["UserDescription"] = info.UserDescription!.Length > 0 ? info.UserDescription : "",
[strId] = info.Id,
[strUserDescription] = info.UserDescription!.Length > 0 ? info.UserDescription : "",
});
}
}
toml.SaveTo(path);
STLog.Instance.WriteLine($"{I18n.SaveUserDataSuccess} {I18n.Path}: {path}");
}
void SaveUserGroup(string path, string tag = "All")
void SaveUserGroup(string path, string tag = strAll)
{
TomlTable toml = new();
if (tag == "All")
if (tag == strAll)
{
foreach (var kv in allUserGroups)
{
toml.Add(kv.Key, new TomlTable()
{
["Icon"] = ((Emoji.Wpf.TextBlock)ListBoxItemHelper.GetIcon(allListBoxItemsFromGroups[kv.Key])).Text,
["Mods"] = new TomlArray(),
[strIcon] = ((Emoji.Wpf.TextBlock)ListBoxItemHelper.GetIcon(allListBoxItemsFromGroups[kv.Key])).Text,
[strMods] = new TomlArray(),
});
foreach (var id in kv.Value)
toml[kv.Key]["Mods"].Add(id);
toml[kv.Key][strMods].Add(id);
}
}
else
{
var mods = allUserGroups[tag];
toml.Add(tag, new TomlTable()
{
["Icon"] = ((Emoji.Wpf.TextBlock)ListBoxItemHelper.GetIcon(allListBoxItemsFromGroups[tag])).Text,
["Mods"] = new TomlArray(),
[strIcon] = ((Emoji.Wpf.TextBlock)ListBoxItemHelper.GetIcon(allListBoxItemsFromGroups[tag])).Text,
[strMods] = new TomlArray(),
});
foreach (var id in mods)
toml[tag]["Mods"].Add(id);
toml[tag][strMods].Add(id);
}
toml.SaveTo(path);
STLog.Instance.WriteLine($"{I18n.SaveUserGroupSuccess} {I18n.Path}: {path}");
Expand Down Expand Up @@ -784,7 +786,7 @@ void DropFile(string filePath)
return;
}
DirectoryInfo dirs = new(tempPath);
var filesInfo = dirs.GetFiles("mod_info.json", SearchOption.AllDirectories);
var filesInfo = dirs.GetFiles(modInfoJson, SearchOption.AllDirectories);
if (filesInfo.Length > 0 && filesInfo.First() is FileInfo fileInfo && fileInfo.FullName is string jsonPath)
{
var newModInfo = GetModInfo(jsonPath);
Expand Down Expand Up @@ -985,10 +987,10 @@ ObservableCollection<ModShowInfo> GetSearchModsShowInfo(string text, string type
showModsInfo = new(
type switch
{
"Name" => modsShowInfoFromGroup[nowGroup].Where(i => i.Name!.Contains(text, StringComparison.OrdinalIgnoreCase)),
"Id" => modsShowInfoFromGroup[nowGroup].Where(i => i.Id.Contains(text, StringComparison.OrdinalIgnoreCase)),
"Author" => modsShowInfoFromGroup[nowGroup].Where(i => i.Author!.Contains(text, StringComparison.OrdinalIgnoreCase)),
"UserDescription" => modsShowInfoFromGroup[nowGroup].Where(i => i.UserDescription!.Contains(text, StringComparison.OrdinalIgnoreCase)),
strName => modsShowInfoFromGroup[nowGroup].Where(i => i.Name!.Contains(text, StringComparison.OrdinalIgnoreCase)),
strId => modsShowInfoFromGroup[nowGroup].Where(i => i.Id.Contains(text, StringComparison.OrdinalIgnoreCase)),
strAuthor => modsShowInfoFromGroup[nowGroup].Where(i => i.Author!.Contains(text, StringComparison.OrdinalIgnoreCase)),
strUserDescription => modsShowInfoFromGroup[nowGroup].Where(i => i.UserDescription!.Contains(text, StringComparison.OrdinalIgnoreCase)),
_ => throw new NotImplementedException()
});
return showModsInfo;
Expand Down
10 changes: 10 additions & 0 deletions Tools/ModManager/ModManager.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public partial class ModManager : Page
const string userDataPath = "UserData.toml";
const string userGroupPath = "UserGroup.toml";
const string userCustomData = "UserCustomData";
const string enabledMods = "enabledMods";
const string modInfoJson = "mod_info.json";
const string strAll = "All";
const string strId = "Id";
const string strIcon = "Icon";
const string strMods = "Mods";
const string strUserCustomData = "UserCustomData";
const string strUserDescription = "UserDescription";
const string strName = "Name";
const string strAuthor = "Author";
bool groupMenuOpen = false;
bool showModInfo = false;
string? nowSelectedMod = null;
Expand Down

0 comments on commit 72497c2

Please sign in to comment.