From b7cb68e5ace5626e3634d18ec06c7793335f2e7b Mon Sep 17 00:00:00 2001
From: Clansty <i@gao4.pw>
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 e75e6e4..9faaafc 100644
--- a/AquaMai.Core/Resources/Locale.Designer.cs
+++ b/AquaMai.Core/Resources/Locale.Designer.cs
@@ -151,6 +151,15 @@ public static string NetErrWasDownloadSuccessOnce {
             }
         }
         
+        /// <summary>
+        ///   Looks up a localized string similar to No camera detected.
+        /// </summary>
+        public static string NoCamera {
+            get {
+                return ResourceManager.GetString("NoCamera", resourceCulture);
+            }
+        }
+        
         /// <summary>
         ///   Looks up a localized string similar to Pause.
         /// </summary>
diff --git a/AquaMai.Core/Resources/Locale.resx b/AquaMai.Core/Resources/Locale.resx
index 33f839c..52e301e 100644
--- a/AquaMai.Core/Resources/Locale.resx
+++ b/AquaMai.Core/Resources/Locale.resx
@@ -122,4 +122,7 @@
     <data name="UserGhostAchievement" xml:space="preserve">
         <value>Battle Target: {0}</value>
     </data>
+    <data name="NoCamera" xml:space="preserve">
+        <value>No camera detected</value>
+    </data>
 </root>
diff --git a/AquaMai.Core/Resources/Locale.zh.resx b/AquaMai.Core/Resources/Locale.zh.resx
index 2c8c44f..6cb65be 100644
--- a/AquaMai.Core/Resources/Locale.zh.resx
+++ b/AquaMai.Core/Resources/Locale.zh.resx
@@ -115,4 +115,7 @@
     <data name="UserGhostAchievement" xml:space="preserve">
         <value>友人对战目标: {0}</value>
     </data>
+    <data name="NoCamera" xml:space="preserve">
+        <value>没有找到任何摄像头</value>
+    </data>
 </root>
diff --git a/AquaMai.Mods/GameSystem/CustomCameraId.cs b/AquaMai.Mods/GameSystem/CustomCameraId.cs
index f659d64..2bddfb5 100644
--- a/AquaMai.Mods/GameSystem/CustomCameraId.cs
+++ b/AquaMai.Mods/GameSystem/CustomCameraId.cs
@@ -7,6 +7,8 @@
 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<string, string> cameraTypeMap = new()
     {
         ["LeftQrCamera"] = "QRLeft",
@@ -64,6 +66,14 @@ public static bool CameraInitialize(CameraManager __instance, ref IEnumerator __
 
     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<CameraManager.CameraTypeEnum, WebCamTexture> webCamTextures = [];
         foreach (var (configEntry, cameraTypeName) in cameraTypeMap)
@@ -105,6 +115,7 @@ private static IEnumerator CameraInitialize(CameraManager __instance)
             CameraManager.DeviceId[(int)cameraType] = textureIndex;
             textureIndex++;
         }
+
         Traverse.Create(__instance).Field("_webcamtex").SetValue(webCamTextures.Values.ToArray());
 
         CameraManager.IsReady = true;
@@ -132,11 +143,12 @@ public static void OnBeforePatch()
             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