Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[O] ResetTouchAfterTrack -> ResetTouch, add press key to reset #93

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AquaMai/AquaMai.Config.Interfaces/IConfigView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ public interface IConfigView
public void SetValue(string path, object value);
public T GetValueOrDefault<T>(string path, T defaultValue = default);
public bool TryGetValue<T>(string path, out T resultValue);
public bool Remove(string path);
public string ToToml();
public IConfigView Clone();
}
3 changes: 2 additions & 1 deletion AquaMai/AquaMai.Config/ConfigSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using AquaMai.Config.Attributes;
using AquaMai.Config.Interfaces;
using AquaMai.Config.Migration;
using Tomlet.Models;

namespace AquaMai.Config;
Expand Down Expand Up @@ -60,7 +61,7 @@ public string Serialize(IConfig config)
}

// Version
AppendEntry(sb, null, "Version", "2.0");
AppendEntry(sb, null, "Version", ConfigMigrationManager.Instance.LatestVersion);

foreach (var section in ((Config)config).reflectionManager.SectionValues)
{
Expand Down
37 changes: 37 additions & 0 deletions AquaMai/AquaMai.Config/ConfigView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public void SetValue(string path, object value)
}
current = (TomlTable)next;
}

if (value == null)
{
current.Keys.Remove(pathComponents.Last());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样好像不工作

return;
}
current.Put(pathComponents.Last(), value);
}

Expand All @@ -85,6 +91,11 @@ public bool TryGetValue<T>(string path, out T resultValue)
resultValue = default;
return false;
}
if (typeof(T) == typeof(object))
{
resultValue = (T)(object)value;
return true;
}
try
{
resultValue = Utility.ParseTomlValue<T>(value);
Expand All @@ -98,8 +109,34 @@ public bool TryGetValue<T>(string path, out T resultValue)
}
}

public bool Remove(string path)
{
var pathComponents = path.Split('.');
var current = root;
foreach (var component in pathComponents.Take(pathComponents.Length - 1))
{
if (!Utility.TomlTryGetValueCaseInsensitive(current, component, out var next) || next is not TomlTable nextTable)
{
return false;
}
current = (TomlTable)next;
}
var keyToRemove = pathComponents.Last();
var keysCaseSensitive = current.Keys.Where(k => string.Equals(k, keyToRemove, StringComparison.OrdinalIgnoreCase));
foreach (var key in keysCaseSensitive)
{
current.Entries.Remove(key);
}
return keysCaseSensitive.Any();
}

public string ToToml()
{
return root.SerializedValue;
}

public IConfigView Clone()
{
return new ConfigView(ToToml());
}
}
8 changes: 6 additions & 2 deletions AquaMai/AquaMai.Config/Migration/ConfigMigrationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class ConfigMigrationManager : IConfigMigrationManager
private readonly Dictionary<string, IConfigMigration> migrationMap =
new List<IConfigMigration>
{
new ConfigMigration_V1_0_V2_0()
new ConfigMigration_V1_0_V2_0(),
new ConfigMigration_V2_0_V2_1()
}.ToDictionary(m => m.FromVersion);

public string LatestVersion { get; }
Expand All @@ -39,10 +40,12 @@ public IConfigView Migrate(IConfigView config)
config = migration.Migrate(config);
currentVersion = migration.ToVersion;
}

if (currentVersion != LatestVersion)
{
throw new ArgumentException($"Could not migrate the config from v{currentVersion} to v{LatestVersion}");
}

return config;
}

Expand All @@ -52,7 +55,8 @@ public string GetVersion(IConfigView config)
{
return version;
}

// Assume v1.0 if not found
return "1.0";
}
}
}
44 changes: 44 additions & 0 deletions AquaMai/AquaMai.Config/Migration/ConfigMigration_V2_0_V2_1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using AquaMai.Config.Interfaces;
using Tomlet.Models;

namespace AquaMai.Config.Migration;

public class ConfigMigration_V2_0_V2_1 : IConfigMigration
{
public string FromVersion => "2.0";
public string ToVersion => "2.1";

public IConfigView Migrate(IConfigView src)
{
var dst = src.Clone();
dst.SetValue("Version", ToVersion);

if (IsSectionEnabled(src, "Tweaks.ResetTouchAfterTrack"))
{
dst.Remove("Tweaks.ResetTouchAfterTrack");
dst.SetValue("Tweaks.ResetTouch.AfterTrack", true);
}

return dst;
}

public bool IsSectionEnabled(IConfigView src, string path)
{
if (src.TryGetValue(path, out object section))
{
if (section is bool enabled)
{
return enabled;
}
else if (section is TomlTable table)
{
if (Utility.TomlTryGetValueCaseInsensitive(table, "Disabled", out var disabled))
{
return !Utility.IsTrutyOrDefault(disabled);
}
return true;
}
}
return false;
}
}
10 changes: 10 additions & 0 deletions AquaMai/AquaMai.Config/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public static bool IsTruty(TomlValue value, string path = null)
};
}

public static bool IsTrutyOrDefault(TomlValue value, bool defaultValue = false)
{
return value switch
{
TomlBoolean boolean => boolean.Value,
TomlLong @long => @long.Value != 0,
_ => defaultValue
};
}

public static bool IsIntegerType(Type type)
{
return type == typeof(sbyte) || type == typeof(short) || type == typeof(int) || type == typeof(long)
Expand Down
9 changes: 9 additions & 0 deletions AquaMai/AquaMai.Core/Resources/Locale.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions AquaMai/AquaMai.Core/Resources/Locale.resx
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,7 @@
<data name="PlaylogSaveError" xml:space="preserve">
<value>Playlog save error</value>
</data>
<data name="TouchPanelReset" xml:space="preserve">
<value>Touch panel reset</value>
</data>
</root>
3 changes: 3 additions & 0 deletions AquaMai/AquaMai.Core/Resources/Locale.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@
<data name="PlaylogSaveError" xml:space="preserve">
<value>保存 Playlog 失败</value>
</data>
<data name="TouchPanelReset" xml:space="preserve">
<value>触摸面板已重置</value>
</data>
</root>
2 changes: 2 additions & 0 deletions AquaMai/AquaMai.Mods/GameSystem/TestProof.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AquaMai.Core;
using AquaMai.Core.Attributes;
using AquaMai.Core.Helpers;
using AquaMai.Mods.Tweaks;
using AquaMai.Mods.UX;
using AquaMai.Mods.UX.PracticeMode;
using HarmonyLib;
Expand Down Expand Up @@ -35,6 +36,7 @@ public static bool ShouldEnableImplicitly
(typeof(OneKeyRetrySkip), OneKeyRetrySkip.skipKey),
(typeof(HideSelfMadeCharts), HideSelfMadeCharts.key),
(typeof(PracticeMode), PracticeMode.key),
(typeof(ResetTouch), ResetTouch.key),
];
var keyMapEnabled = ConfigLoader.Config.GetSectionState(typeof(KeyMap)).Enabled;
return featureKeys.Any(it =>
Expand Down
43 changes: 43 additions & 0 deletions AquaMai/AquaMai.Mods/Tweaks/ResetTouch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using AquaMai.Config.Attributes;
using AquaMai.Config.Types;
using AquaMai.Core.Helpers;
using AquaMai.Core.Resources;
using HarmonyLib;
using MAI2.Util;
using Main;
using Manager;
using Process;

namespace AquaMai.Mods.Tweaks;

[ConfigSection(
en: "Reset touch panel manually or after playing track.",
zh: "重置触摸面板")]
public class ResetTouch
{
[ConfigEntry(en: "Reset touch panel after playing track.", zh: "玩完一首歌自动重置")]
private static bool afterTrack = false;

[ConfigEntry(en: "Reset manually.", zh: "按键重置")]
public static readonly KeyCodeOrName key = KeyCodeOrName.None;

[ConfigEntry] private static readonly bool longPress = false;

[HarmonyPostfix]
[HarmonyPatch(typeof(ResultProcess), "OnStart")]
public static void ResultProcessOnStart()
{
if (!afterTrack) return;
SingletonStateMachine<AmManager, AmManager.EState>.Instance.StartTouchPanel();
MelonLoader.MelonLogger.Msg("[TouchResetAfterTrack] Touch panel reset");
}

[HarmonyPostfix]
[HarmonyPatch(typeof(GameMainObject), "Update")]
public static void OnGameMainObjectUpdate()
{
if (!KeyListener.GetKeyDownOrLongPress(key, longPress)) return;
SingletonStateMachine<AmManager, AmManager.EState>.Instance.StartTouchPanel();
MessageHelper.ShowMessage(Locale.TouchPanelReset);
}
}
21 changes: 0 additions & 21 deletions AquaMai/AquaMai.Mods/Tweaks/ResetTouchAfterTrack.cs

This file was deleted.