diff --git a/Managers/AssetManager.cs b/Managers/AssetManager.cs new file mode 100644 index 0000000..4c2b66e --- /dev/null +++ b/Managers/AssetManager.cs @@ -0,0 +1,41 @@ +using System.IO; +using System.Reflection; +using UnityEngine; + +namespace GorillaCaptions.Managers; + +public class AssetManager +{ + private static AssetBundle assetBundle; + + private static void LoadAssetBundle() + { + Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("GorillaCaptions.Resources.caption"); + if (stream != null) + assetBundle = AssetBundle.LoadFromStream(stream); + else + Debug.LogError("Failed to load assetbundle"); + } + + public static T LoadObject(string assetName) where T : Object + { + T gameObject = null; + + if (assetBundle == null) + LoadAssetBundle(); + + gameObject = Object.Instantiate(assetBundle.LoadAsset(assetName)); + return gameObject; + } + + public static T LoadAsset(string assetName) where T : Object + { + T gameObject = null; + + if (assetBundle == null) + LoadAssetBundle(); + + gameObject = assetBundle.LoadAsset(assetName) as T; + return gameObject; + } +} \ No newline at end of file diff --git a/Managers/BubbleManager.cs b/Managers/BubbleManager.cs new file mode 100644 index 0000000..bb991bc --- /dev/null +++ b/Managers/BubbleManager.cs @@ -0,0 +1,46 @@ +using GorillaCaptions.Classes; +using System.Collections.Generic; +using TMPro; +using UnityEngine; + +namespace GorillaCaptions.Managers; + +public class BubbleManager : MonoBehaviour +{ + public static BubbleManager instance { get; private set; } + + private GameObject BubbleAsset; + public void Awake() + { + instance = this; + BubbleAsset = AssetManager.LoadAsset("SpeechBubble"); + } + + private Dictionary SpeechBubbles = new(); + public void SpawnBubble(VRRig Rig, string Text) + { + if (SpeechBubbles.TryGetValue(Rig, out GameObject ExistingBubble)) + DestroyBubble(ExistingBubble); + + GameObject TargetBubble = Instantiate(BubbleAsset); + TargetBubble.transform.SetParent(Rig.headMesh.transform, false); + TargetBubble.transform.Find("Canvas/Background/Text") + .GetComponent().text = Text; + TargetBubble.AddComponent().Owner = Rig; + + SpeechBubbles.Add(Rig, TargetBubble); + } + + public void DestroyBubble(GameObject TargetBubble) + { + foreach (KeyValuePair SpeechBubble in SpeechBubbles) + { + if (SpeechBubble.Value == TargetBubble) + { + SpeechBubbles.Remove(SpeechBubble.Key); + Destroy(SpeechBubble.Value); + break; + } + } + } +} diff --git a/Managers/Extensions.cs b/Managers/Extensions.cs new file mode 100644 index 0000000..9af1804 --- /dev/null +++ b/Managers/Extensions.cs @@ -0,0 +1,41 @@ +using System; + +namespace GorillaCaptions.Managers; + +public static class Extensions +{ + public static string GetPlatform(this VRRig rig) + { + string platformProperty = (string)rig.creator.GetPlayerRef().CustomProperties["platform"]; + + if (!string.IsNullOrEmpty(platformProperty)) + return platformProperty; + + if (HasOwnedCosmetic(rig, "S. FIRST LOGIN")) + return "STEAM"; + + if (HasOwnedCosmetic(rig, "FIRST LOGIN") || + rig.creator != null && rig.creator.GetPlayerRef().CustomProperties.Count > 3 || + rig.currentRankedSubTierPC > 0) + return "PC"; + + return "Quest"; + } + + private static bool HasOwnedCosmetic(VRRig rig, string cosmeticName) + { + if (rig._playerOwnedCosmetics == null) + return false; + + foreach (string ownedCosmetic in rig._playerOwnedCosmetics) + { + if (string.IsNullOrEmpty(ownedCosmetic)) + continue; + + if (ownedCosmetic.IndexOf(cosmeticName, StringComparison.Ordinal) >= 0) + return true; + } + + return false; + } +} diff --git a/Managers/SynthesizerManager.cs b/Managers/SynthesizerManager.cs new file mode 100644 index 0000000..a252a76 --- /dev/null +++ b/Managers/SynthesizerManager.cs @@ -0,0 +1,60 @@ +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Threading.Tasks; +using UnityEngine; + +namespace GorillaCaptions.Managers; + +public class SynthesizerManager : MonoBehaviour +{ + public static SynthesizerManager instance { get; private set; } + public static string voskPath; + + public void Awake() + { + instance = this; + + string resourcePath = "GorillaCaptions.Resources.QuickVosk.exe"; + voskPath = Path.Combine(Path.GetTempPath(), "QuickVosk.exe"); + + if (File.Exists(voskPath)) + File.Delete(voskPath); + + using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath); + using FileStream fs = new(voskPath, FileMode.Create, FileAccess.Write); + stream.CopyTo(fs); + } + + public async Task SynthesizeAudio(string audioPath) + { + ProcessStartInfo psi = new() + { + FileName = voskPath, + Arguments = audioPath, + UseShellExecute = false, + RedirectStandardOutput = true, + CreateNoWindow = true + }; + + using Process proc = new() { StartInfo = psi }; + + string output = await Task.Run(() => + { + proc.Start(); + string outStr = proc.StandardOutput.ReadToEnd(); + proc.WaitForExit(); + return outStr; + }); + + string[] lines = output.Split("\n"); + string finalText = lines[lines.Length - 2]; + + if (finalText.Length <= 1) + return null; + + finalText = char.ToUpper(finalText[0]) + finalText.Substring(1); + + return finalText; + } +} \ No newline at end of file