Skip to content

Commit 01b736e

Browse files
committed
refactor(DebugLogViewerSettings): change port setting type from string to int and update related logic
- Modified the DebugLogViewerPort setting to use an integer type instead of a string, allowing for better validation and range enforcement. - Updated the UI bindings and localization files to reflect the change in port type, removing the placeholder text and ensuring clarity in descriptions. - Simplified the port value handling by directly clamping the integer value, enhancing the overall robustness of the settings management.
1 parent d148dd9 commit 01b736e

4 files changed

Lines changed: 11 additions & 20 deletions

File tree

Settings/Integration/RitsuLibModSettingsBootstrap.DebugLogViewerPage.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ private static void RegisterDebugLogViewerPage(RitsuLibModSettingsUiBindings ui)
2121
ui.DebugLogViewerAutoOpen,
2222
T("ritsulib.debugLogViewer.autoOpen.description",
2323
"When enabled, RitsuLib waits 3 seconds after starting the viewer. If no browser page is listening, it opens one."))
24-
.AddString(
24+
.AddIntSlider(
2525
"debug_log_viewer_port",
2626
T("ritsulib.debugLogViewer.port.label", "Viewer port"),
2727
ui.DebugLogViewerPort,
28-
T("ritsulib.debugLogViewer.port.placeholder", "18742"),
29-
5,
28+
1,
29+
65535,
30+
1,
31+
value => value.ToString(),
3032
T("ritsulib.debugLogViewer.port.description",
31-
"Loopback HTTP port. If the port is busy, RitsuLib tries the following ports in order. Changes apply on next launch."),
32-
value => RitsuLibModSettingsUiBindings.TryParsePort(value, out _))
33+
"Loopback HTTP port. If the port is busy, RitsuLib tries the following ports in order. Changes apply on next launch."))
3334
.AddButton(
3435
"debug_log_viewer_open_now",
3536
T("ritsulib.debugLogViewer.openNow.label", "Open viewer"),

Settings/Integration/RitsuLibModSettingsUiBindings.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private RitsuLibModSettingsUiBindings()
1515
public IModSettingsValueBinding<bool> DebugCompatLocTable { get; private init; } = null!;
1616
public IModSettingsValueBinding<bool> DebugCompatUnlockEpoch { get; private init; } = null!;
1717
public IModSettingsValueBinding<bool> DebugLogViewerAutoOpen { get; private init; } = null!;
18-
public IModSettingsValueBinding<string> DebugLogViewerPort { get; private init; } = null!;
18+
public IModSettingsValueBinding<int> DebugLogViewerPort { get; private init; } = null!;
1919

2020
public IModSettingsValueBinding<bool> DebugCompatAncientArchitect { get; private init; } =
2121
null!;
@@ -153,16 +153,12 @@ public static RitsuLibModSettingsUiBindings Create()
153153
(settings, value) => settings.DebugLogViewerAutoOpen = value),
154154
() => defaults.DebugLogViewerAutoOpen),
155155
DebugLogViewerPort = ModSettingsBindings.WithDefault(
156-
ModSettingsBindings.Global<RitsuLibSettings, string>(
156+
ModSettingsBindings.Global<RitsuLibSettings, int>(
157157
Const.ModId,
158158
Const.SettingsKey,
159-
settings => Math.Clamp(settings.DebugLogViewerPort, 1, 65535).ToString(),
160-
(settings, value) =>
161-
{
162-
if (TryParsePort(value, out var port))
163-
settings.DebugLogViewerPort = port;
164-
}),
165-
() => defaults.DebugLogViewerPort.ToString()),
159+
settings => Math.Clamp(settings.DebugLogViewerPort, 1, 65535),
160+
(settings, value) => settings.DebugLogViewerPort = Math.Clamp(value, 1, 65535)),
161+
() => defaults.DebugLogViewerPort),
166162
ModSourceHoverTipsEnabled = ModSettingsBindings.WithDefault(
167163
ModSettingsBindings.Global<RitsuLibSettings, bool>(
168164
Const.ModId,
@@ -515,9 +511,5 @@ private static string NormalizeToastAnimation(string? value)
515511
};
516512
}
517513

518-
internal static bool TryParsePort(string? value, out int port)
519-
{
520-
return int.TryParse(value?.Trim(), out port) && port is >= 1 and <= 65535;
521-
}
522514
}
523515
}

Settings/Localization/ModSettingsUi/eng.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@
220220
"ritsulib.debugLogViewer.autoOpen.label": "Open browser automatically",
221221
"ritsulib.debugLogViewer.autoOpen.description": "When enabled, RitsuLib waits 3 seconds after starting the viewer. If no browser page is listening, it opens one.",
222222
"ritsulib.debugLogViewer.port.label": "Viewer port",
223-
"ritsulib.debugLogViewer.port.placeholder": "18742",
224223
"ritsulib.debugLogViewer.port.description": "Loopback HTTP port. If the port is busy, RitsuLib tries the following ports in order. Changes apply on next launch.",
225224
"ritsulib.debugLogViewer.openNow.label": "Open viewer",
226225
"ritsulib.debugLogViewer.openNow.button": "Open log viewer",

Settings/Localization/ModSettingsUi/zhs.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@
220220
"ritsulib.debugLogViewer.autoOpen.label": "自动打开浏览器",
221221
"ritsulib.debugLogViewer.autoOpen.description": "启用后,RitsuLib 会在查看器启动后等待 3 秒;如果没有浏览器页面正在监听,就自动打开一个页面。",
222222
"ritsulib.debugLogViewer.port.label": "查看器端口",
223-
"ritsulib.debugLogViewer.port.placeholder": "18742",
224223
"ritsulib.debugLogViewer.port.description": "本机 HTTP 端口。如果端口被占用,RitsuLib 会按顺序尝试后续端口。修改会在下次启动时生效。",
225224
"ritsulib.debugLogViewer.openNow.label": "打开查看器",
226225
"ritsulib.debugLogViewer.openNow.button": "打开日志查看器",

0 commit comments

Comments
 (0)