From b7cb68e5ace5626e3634d18ec06c7793335f2e7b Mon Sep 17 00:00:00 2001 From: Clansty Date: Sat, 25 Jan 2025 11:17:40 +0800 Subject: [PATCH] [F] CustomCameraId crash when no camera --- AquaMai.Core/Resources/Locale.Designer.cs | 9 +++++++++ AquaMai.Core/Resources/Locale.resx | 3 +++ AquaMai.Core/Resources/Locale.zh.resx | 3 +++ AquaMai.Mods/GameSystem/CustomCameraId.cs | 18 +++++++++++++++--- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/AquaMai.Core/Resources/Locale.Designer.cs b/AquaMai.Core/Resources/Locale.Designer.cs index e75e6e46..9faaafc8 100644 --- a/AquaMai.Core/Resources/Locale.Designer.cs +++ b/AquaMai.Core/Resources/Locale.Designer.cs @@ -151,6 +151,15 @@ namespace AquaMai.Core.Resources { } } + /// + /// Looks up a localized string similar to No camera detected. + /// + public static string NoCamera { + get { + return ResourceManager.GetString("NoCamera", resourceCulture); + } + } + /// /// Looks up a localized string similar to Pause. /// diff --git a/AquaMai.Core/Resources/Locale.resx b/AquaMai.Core/Resources/Locale.resx index 33f839c4..52e301e5 100644 --- a/AquaMai.Core/Resources/Locale.resx +++ b/AquaMai.Core/Resources/Locale.resx @@ -122,4 +122,7 @@ Battle Target: {0} + + No camera detected + diff --git a/AquaMai.Core/Resources/Locale.zh.resx b/AquaMai.Core/Resources/Locale.zh.resx index 2c8c44fa..6cb65be1 100644 --- a/AquaMai.Core/Resources/Locale.zh.resx +++ b/AquaMai.Core/Resources/Locale.zh.resx @@ -115,4 +115,7 @@ 友人对战目标: {0} + + 没有找到任何摄像头 + diff --git a/AquaMai.Mods/GameSystem/CustomCameraId.cs b/AquaMai.Mods/GameSystem/CustomCameraId.cs index f659d644..2bddfb54 100644 --- a/AquaMai.Mods/GameSystem/CustomCameraId.cs +++ b/AquaMai.Mods/GameSystem/CustomCameraId.cs @@ -7,6 +7,8 @@ using Manager; using MelonLoader; using UnityEngine; using AquaMai.Config.Attributes; +using AquaMai.Core.Helpers; +using AquaMai.Core.Resources; namespace AquaMai.Mods.GameSystem; @@ -45,7 +47,7 @@ public class CustomCameraId en: "WeChat QRCode Camera.", zh: "二维码扫描摄像头")] public static int chimeCamera; - + private static readonly Dictionary cameraTypeMap = new() { ["LeftQrCamera"] = "QRLeft", @@ -64,6 +66,14 @@ public class CustomCameraId private static IEnumerator CameraInitialize(CameraManager __instance) { + if (WebCamTexture.devices.Length == 0) + { + MelonLogger.Warning("[CustomCameraId] No camera detected, camera initialization failed."); + MessageHelper.ShowMessage(Locale.NoCamera, title: "CustomCameraId"); + CameraManager.IsReady = true; + yield break; + } + var textureCache = new WebCamTexture[WebCamTexture.devices.Length]; SortedDictionary webCamTextures = []; foreach (var (configEntry, cameraTypeName) in cameraTypeMap) @@ -105,6 +115,7 @@ public class CustomCameraId CameraManager.DeviceId[(int)cameraType] = textureIndex; textureIndex++; } + Traverse.Create(__instance).Field("_webcamtex").SetValue(webCamTextures.Values.ToArray()); CameraManager.IsReady = true; @@ -132,11 +143,12 @@ public class CustomCameraId cameraList += $"FPS: {webCamTexture.requestedFPS}\n"; webCamTexture.Stop(); } + cameraList += "=================================================="; - + foreach (var line in cameraList.Split('\n')) { MelonLogger.Msg($"[CustomCameraId] {line}"); } } -} +} \ No newline at end of file