Skip to content

Commit

Permalink
[F] CustomCameraId crash when no camera
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Jan 25, 2025
1 parent bd88d03 commit b7cb68e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
9 changes: 9 additions & 0 deletions AquaMai.Core/Resources/Locale.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions AquaMai.Core/Resources/Locale.resx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
3 changes: 3 additions & 0 deletions AquaMai.Core/Resources/Locale.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,7 @@
<data name="UserGhostAchievement" xml:space="preserve">
<value>友人对战目标: {0}</value>
</data>
<data name="NoCamera" xml:space="preserve">
<value>没有找到任何摄像头</value>
</data>
</root>
18 changes: 15 additions & 3 deletions AquaMai.Mods/GameSystem/CustomCameraId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using MelonLoader;
using UnityEngine;
using AquaMai.Config.Attributes;
using AquaMai.Core.Helpers;
using AquaMai.Core.Resources;

namespace AquaMai.Mods.GameSystem;

Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}");
}
}
}
}

0 comments on commit b7cb68e

Please sign in to comment.