From aec0f47b9c5a15f4df63b5e7a8ad19536a249fea Mon Sep 17 00:00:00 2001 From: akanyan Date: Tue, 17 Dec 2024 19:03:52 +0000 Subject: [PATCH] feat(SortByInternal): store selection in the fs --- .../MU3.User/patch_UserDetail.cs | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/SortByInternalDifficulty/MU3.User/patch_UserDetail.cs b/SortByInternalDifficulty/MU3.User/patch_UserDetail.cs index 11cd792..0ea428c 100644 --- a/SortByInternalDifficulty/MU3.User/patch_UserDetail.cs +++ b/SortByInternalDifficulty/MU3.User/patch_UserDetail.cs @@ -1,5 +1,9 @@ using MU3.Client; using MU3.DB; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; namespace MU3.User; class patch_UserDetail: UserDetail { @@ -9,12 +13,62 @@ class patch_UserDetail: UserDetail { // Attempting to use a profile with InternalLevel sorting enabled // causes unpatched clients to crash thanks to enormous incompetence - // of Sxga's interns. So, unfortunately, this value has to be discarded. + // of Sxga's interns. // See: // * MusicSelectViewDataList._sort1 set // * UserDetail.copyFrom() if(userDetail.tabSetting == (int)patch_MusicSort1ID.InternalLevel) { userDetail.tabSetting = (int)patch_MusicSort1ID.Level; + stashTabSelection(userDetail.userName, true); + } else { + stashTabSelection(userDetail.userName, false); } } + + public extern void orig_copyFrom(UserData userDetail); + public new void copyFrom(UserData userDetail) { + orig_copyFrom(userDetail); + + if(userDetail.tabSetting == (int)patch_MusicSort1ID.Level && isStashedTabSelection(userDetail.userName)) { + MusicSort1 = (MusicSort1ID)patch_MusicSort1ID.InternalLevel; + } + } + + private void stashTabSelection(string user, bool value) { + var path = getStashPath(); + List entries; + try { + entries = File.ReadAllText(path).Split('\n').ToList(); + entries = entries.Where((val) => val.Length > 0 && val != user).ToList(); + } catch(Exception) { + entries = []; + } + + if(value) { + entries.Add(user); + } + + try { + File.WriteAllText(path, entries.Aggregate("", (acc, s) => acc + s + "\n").Trim()); + } catch(Exception e) { + UnityEngine.Debug.Log("Unable to write tab selection: " + e.ToString()); + } + } + + private bool isStashedTabSelection(string user) { + try { + var entries = File.ReadAllText(getStashPath()).Split('\n').ToList(); + return entries.Contains(user); + } catch(Exception) { + // Whatever, this is not mission critical + return false; + } + } + + private string getStashPath() { + using IniFile iniFile = new("mu3.ini"); + var dir = iniFile.getValue("Extra", "CacheDir", "."); + Directory.CreateDirectory(dir); + return Path.Combine(dir, "data_tab_cache.txt"); + } }