-
Notifications
You must be signed in to change notification settings - Fork 54
feat: Display error frame when specific hardware is not available. #51
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
Merged
+294
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
694ef6b
test: complete all essentials for custom hardware display
AdemJensen 55d3b2e
feat: complete all types of hardware error checks
AdemJensen 33b9bb6
fix: add multiple source for TouchSensor check to avoid occational AM…
AdemJensen a07d899
feat: refactored error frame sections into a global helper tool
AdemJensen c5d0d6d
fix: resolve potential weakness
AdemJensen 3d396b8
feat: add an localization string
AdemJensen fc6bd55
fix: camera idx fetch issue
AdemJensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using System; | ||
| using HarmonyLib; | ||
| using Manager; | ||
| using MelonLoader; | ||
| using Monitor.Error; | ||
| using Process; | ||
| using Process.Error; | ||
| using TMPro; | ||
|
|
||
| namespace AquaMai.Core.Helpers; | ||
|
|
||
| public static class ErrorFrame | ||
| { | ||
| private static int _customErrCode; | ||
| private static string _customErrMsg; | ||
| private static DateTime _customErrDate; | ||
|
|
||
| public static void Show(ProcessBase process, int errCode, string errMsg) | ||
| { | ||
| _customErrCode = errCode; | ||
| _customErrMsg = errMsg; | ||
| _customErrDate = DateTime.Now; | ||
| Show(process); | ||
| } | ||
|
|
||
| // Display the error frame with AMDaemon's original error message. | ||
| public static void Show(ProcessBase process) | ||
| { | ||
| var tv = Traverse.Create(process); | ||
| var ctn = tv.Field("container").GetValue<ProcessDataContainer>(); | ||
| ctn.processManager.AddProcess((ProcessBase) new ErrorProcess(ctn)); | ||
| ctn.processManager.ReleaseProcess(process); | ||
| GameManager.IsErrorMode = true; | ||
| } | ||
|
|
||
| // patch the error monitor so that it can display custom error codes and messages. | ||
| [HarmonyPostfix] | ||
| [HarmonyPatch(typeof(ErrorMonitor), "Initialize", typeof(int), typeof(bool))] | ||
| public static void PostInitialize(ErrorMonitor __instance) | ||
| { | ||
| var tv = Traverse.Create(__instance); | ||
| if (_customErrCode == 0) | ||
| { | ||
| MelonLogger.Msg($"Displaying error frame with AMDaemon code {AMDaemon.Error.Number}: {AMDaemon.Error.Message}"); | ||
| return; | ||
| } | ||
| MelonLogger.Msg($"Displaying error frame with custom code {_customErrCode}: {_customErrMsg}"); | ||
| tv.Field("ErrorID").GetValue<TextMeshProUGUI>().text = _customErrCode.ToString().PadLeft(4, '0'); | ||
| tv.Field("ErrorMessage").GetValue<TextMeshProUGUI>().text = _customErrMsg; | ||
| tv.Field("ErrorDate").GetValue<TextMeshProUGUI>().text = _customErrDate.ToString(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,6 +164,7 @@ public static void Initialize(Assembly modsAssembly, HarmonyLib.Harmony harmony) | |
| CollectWantedPatches(wantedPatches, typeof(KeyListener)); | ||
| CollectWantedPatches(wantedPatches, typeof(Shim)); | ||
| CollectWantedPatches(wantedPatches, typeof(NetPacketHook)); | ||
| CollectWantedPatches(wantedPatches, typeof(ErrorFrame)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是不是应该在启用之后才 patch 这个,这个也没有被别的地方调用过
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是调用的它hdd里内置的error frame,感觉是个比较通用的功能,是不是放在这里当通用组件会好一些?这样比如后面要实现CPU温度监控之类的功能,也可以用这个frame还原街机的效果 |
||
| // 使用时才 patch!不要添加这个 | ||
| // CollectWantedPatches(wantedPatches, typeof(GameSettingsManager)); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Globalization; | ||
| using AquaMai.Config.Attributes; | ||
| using AquaMai.Core.Helpers; | ||
| using AquaMai.Core.Resources; | ||
| using AquaMai.Mods.GameSystem; | ||
| using HarmonyLib; | ||
| using IO; | ||
| using MAI2.Util; | ||
| using MAI2System; | ||
| using Main; | ||
| using Manager; | ||
| using MelonLoader; | ||
| using Monitor.Error; | ||
| using Process; | ||
| using Process.Error; | ||
| using TMPro; | ||
| using UnityEngine; | ||
|
|
||
| namespace AquaMai.Mods.UX; | ||
|
|
||
| [ConfigSection( | ||
| en: "Custom hardware alert, you can configure to display an error frame upon hardware failure. Toggle the switches below to define the required hardware.", | ||
| zh: "自定义硬件警告,可配置在指定硬件自检失败时阻止游戏启动并显示报错画面,开启下方的错误类型以配置需要关注的错误。")] | ||
| public class HardwareAlert | ||
| { | ||
| [ConfigEntry( | ||
| en: "If enabled, all the in-game hardware warnings will be displayed in game's original language, like Japanese for SDEZ. If you have used any translation pack, you should disable this setting.", | ||
| zh: "如果启用,所有硬件警告将会使用游戏原本的语言显示,例如 SDEZ 就会用日文显示报错。如果你安装了任何汉化包,你应该关闭这个选项。")] | ||
| private static readonly bool UseOriginalGameLanguage = true; | ||
| [ConfigEntry( | ||
| en: "1P Touch Sensor", | ||
| zh: "1P 触摸屏")] | ||
| private static readonly bool TouchSensor_1P = false; // Error 3300, 3301 | ||
| [ConfigEntry( | ||
| en: "2P Touch Sensor", | ||
| zh: "2P 触摸屏")] | ||
| private static readonly bool TouchSensor_2P = false; // Error 3302, 3303 | ||
| [ConfigEntry( | ||
| en: "1P LED", | ||
| zh: "1P LED")] | ||
| private static readonly bool LED_1P = false; // custom 3400 | ||
| [ConfigEntry( | ||
| en: "2P LED", | ||
| zh: "2P LED")] | ||
| private static readonly bool LED_2P = false; // custom 3401 | ||
| [ConfigEntry( | ||
| en: "Player Camera", | ||
| zh: "玩家摄像机")] | ||
| private static readonly bool PlayerCamera = false; // 3102 | ||
| [ConfigEntry( | ||
| en: "DX Pass 1P", | ||
| zh: "DX Pass 1P")] | ||
| private static readonly bool CodeReader_1P = false; // 3101 | ||
| [ConfigEntry( | ||
| en: "DX Pass 2P", | ||
| zh: "DX Pass 2P")] | ||
| private static readonly bool CodeReader_2P = false; // 3101 | ||
| [ConfigEntry( | ||
| en: "WeChat QRCode Camera", | ||
| zh: "二维码扫描摄像头")] | ||
| public static readonly bool ChimeCamera = false; // 3100 | ||
|
|
||
| private static readonly List<string> CameraTypeList = ["QRLeft", "QRRight", "Photo", "Chime"]; | ||
| private static SortedDictionary<CameraTypeEnumInner, int> _cameraIndex = []; | ||
| private static bool _isInitialized = false; | ||
|
|
||
| private enum CameraTypeEnumInner | ||
| { | ||
| QRLeft, | ||
| QRRight, | ||
| Photo, | ||
| Chime, | ||
| } | ||
|
|
||
| [HarmonyPostfix] | ||
| [HarmonyPatch(typeof(CameraManager), "CameraInitialize")] | ||
| public static void PostCameraInitialize(CameraManager __instance) | ||
| { | ||
| if (_isInitialized) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var curCamIdx = 0; | ||
| foreach (var cameraTypeName in CameraTypeList) | ||
| { | ||
| if (Enum.TryParse<CameraTypeEnumInner>(cameraTypeName, out var cameraType)) | ||
| { | ||
| MelonLogger.Msg($"[HardwareAlert] Identified camera type {cameraType} for current game version on idx {curCamIdx}"); | ||
| _cameraIndex[cameraType] = curCamIdx; | ||
| curCamIdx++; | ||
| } | ||
| } | ||
|
|
||
| _isInitialized = true; | ||
| } | ||
|
|
||
| [HarmonyPostfix] | ||
| [HarmonyPatch(typeof(StartupProcess), "OnUpdate")] | ||
| public static void OnPostUpdate(StartupProcess __instance) | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // get current startup state | ||
| var tv = Traverse.Create(__instance); | ||
| // var state = tv.Field("_state").GetValue<byte>(); | ||
| var statusSubMsg = tv.Field("_statusSubMsg").GetValue<string[]>(); | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Touch sensor check | ||
| // The built-in AMDaemon errors are not stable, and cannot be localized. | ||
| // So we decided to use another approach to check it. | ||
| if (TouchSensor_1P && statusSubMsg[0] == ConstParameter.TestString_Bad) | ||
| { | ||
| ErrorFrame.Show(__instance, 3300, FaultTouchSensor1P[GetLocale()]); | ||
| return; | ||
| } | ||
| if (TouchSensor_2P && statusSubMsg[1] == ConstParameter.TestString_Bad) | ||
| { | ||
| ErrorFrame.Show(__instance, 3302, FaultTouchSensor2P[GetLocale()]); | ||
| return; | ||
| } | ||
|
|
||
| // LED check | ||
| if (LED_1P && statusSubMsg[2] == ConstParameter.TestString_Bad) | ||
| { | ||
| ErrorFrame.Show(__instance, 3400, FaultLED1P[GetLocale()]); | ||
| return; | ||
| } | ||
| if (LED_2P && statusSubMsg[3] == ConstParameter.TestString_Bad) | ||
| { | ||
| ErrorFrame.Show(__instance, 3401, FaultLED2P[GetLocale()]); | ||
| return; | ||
| } | ||
|
|
||
| // Camera Check | ||
| if (CameraManager.IsReady) | ||
| { | ||
| var nCam = CameraManager.IsAvailableCameras.Length; | ||
|
|
||
| var pcIdx = _cameraIndex[CameraTypeEnumInner.Photo]; | ||
| if (PlayerCamera && pcIdx < nCam && !CameraManager.IsAvailableCameras[pcIdx]) | ||
| { | ||
| ErrorFrame.Show(__instance, 3102, FaultPlayerCamera[GetLocale()]); | ||
| return; | ||
| } | ||
|
|
||
| var cr1PIdx = _cameraIndex[CameraTypeEnumInner.QRLeft]; | ||
| if (CodeReader_1P && cr1PIdx < nCam && !CameraManager.IsAvailableCameras[cr1PIdx]) | ||
| { | ||
| ErrorFrame.Show(__instance, 3101, FaultQR1P[GetLocale()]); | ||
| return; | ||
| } | ||
|
|
||
| var cr2PIdx = _cameraIndex[CameraTypeEnumInner.QRRight]; | ||
| if (CodeReader_2P && cr2PIdx < nCam && !CameraManager.IsAvailableCameras[cr2PIdx]) | ||
| { | ||
| ErrorFrame.Show(__instance, 3101, FaultQR2P[GetLocale()]); | ||
| return; | ||
| } | ||
|
|
||
| var chimeIdx = _cameraIndex[CameraTypeEnumInner.Chime]; | ||
| if (ChimeCamera && chimeIdx < nCam && !CameraManager.IsAvailableCameras[chimeIdx]) | ||
| { | ||
| ErrorFrame.Show(__instance, 3100, FaultChime[GetLocale()]); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static string GetLocale() | ||
| { | ||
| if (UseOriginalGameLanguage) | ||
| { | ||
| return GameVersionToLocale[GameInfo.GameId]; | ||
| } | ||
| MelonLogger.Msg($"[HardwareAlert] Using locale '{Locale.Culture.TwoLetterISOLanguageName}'"); | ||
| // MelonLogger.Msg($"[HardwareAlert] Using locale '{Locale.Culture.Name}'"); | ||
| return Locale.Culture.TwoLetterISOLanguageName.Equals("zh", | ||
| StringComparison.OrdinalIgnoreCase) | ||
| ? "zh" | ||
| : "en"; | ||
| } | ||
|
|
||
| private static readonly Dictionary<string, string> GameVersionToLocale = new() | ||
| { | ||
| ["SDEZ"] = "jp", | ||
| ["SDGA"] = "en", | ||
| ["SDGB"] = "zh", | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultTouchSensor1P = new() | ||
| { | ||
| ["jp"] = "タッチセンサ(1P)はご利用いただけません", | ||
| ["en"] = "Touch Sensor (1P) not available", | ||
| ["zh"] = "触摸传感器(1P)不可用", | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultTouchSensor2P = new() | ||
| { | ||
| ["jp"] = "タッチセンサ(2P)はご利用いただけません", | ||
| ["en"] = "Touch Sensor (2P) not available", | ||
| ["zh"] = "触摸传感器(2P)不可用", | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultLED1P = new() | ||
| { | ||
| ["jp"] = "LED(1P)はご利用いただけません", | ||
| ["en"] = "LED(1P) not available", | ||
| ["zh"] = "LED(1P)不可用", | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultLED2P = new() | ||
| { | ||
| ["jp"] = "LED(2P)はご利用いただけません", | ||
| ["en"] = "LED(2P) not available", | ||
| ["zh"] = "LED(2P)不可用", | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultQR1P = new() | ||
| { | ||
| ["jp"] = "コードリーダー(1P)はご利用いただけません", | ||
| ["en"] = "Code Reader (1P) not available", | ||
| ["zh"] = "DX Pass 二维码相机(1P)不可用", // This thing does not exist... | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultQR2P = new() | ||
| { | ||
| ["jp"] = "コードリーダー(2P)はご利用いただけません", | ||
| ["en"] = "Code Reader (2P) not available", | ||
| ["zh"] = "DX Pass 二维码相机(2P)不可用", // This thing does not exist... | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultPlayerCamera = new() | ||
| { | ||
| ["jp"] = "プレイヤーカメラはご利用いただけません", | ||
| ["en"] = "Player Camera not available", | ||
| ["zh"] = "玩家相机不可用", | ||
| }; | ||
| private static readonly Dictionary<string, string> FaultChime = new() | ||
| { | ||
| ["jp"] = "コードリーダーは(Chime)ご利用いただけません", // This thing does not exist... | ||
| ["en"] = "Code Reader (Chime) not available", // This thing does not exist... | ||
| ["zh"] = "二维码相机不可用", | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.