[F] CustomCameraId crash when no camera

This commit is contained in:
Clansty
2025-01-25 11:17:40 +08:00
parent bd88d035cc
commit b7cb68e5ac
4 changed files with 30 additions and 3 deletions

View File

@@ -151,6 +151,15 @@ namespace AquaMai.Core.Resources {
}
}
/// <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>

View File

@@ -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>

View File

@@ -115,4 +115,7 @@
<data name="UserGhostAchievement" xml:space="preserve">
<value>友人对战目标: {0}</value>
</data>
<data name="NoCamera" xml:space="preserve">
<value>没有找到任何摄像头</value>
</data>
</root>

View File

@@ -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<string, string> 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<CameraManager.CameraTypeEnum, WebCamTexture> 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}");
}
}
}
}