89 lines
3.0 KiB
C#
Raw Normal View History

2022-08-21 15:05:26 +03:00
using Aki.Reflection.Patching;
using BepInEx;
using BepInEx.Configuration;
using Comfort.Common;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
namespace SamSWAT.SixthSense
{
[BepInPlugin("com.samswat.sixthsense", "SamSWAT.SixthSense", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
internal static AudioClip AudioClip;
internal static ConfigEntry<bool> PluginEnabled;
internal static ConfigEntry<float> Cooldown;
2023-02-16 00:40:05 +03:00
internal static ConfigEntry<int> Volume;
2022-08-21 15:05:26 +03:00
async void Awake()
{
PluginEnabled = Config.Bind(
"Main Settings",
"Plugin on/off",
true,
"");
Cooldown = Config.Bind(
"Main Settings",
"Sound cooldown",
5f,
"Time between sound playback in seconds");
2023-02-16 00:40:05 +03:00
Volume = Config.Bind(
"Main Settings",
"Sound volume",
100,
new ConfigDescription("How loud the sound will be, percents",
new AcceptableValueRange<int>(0, 100)));
2022-08-21 15:05:26 +03:00
new Patch().Enable();
2023-02-16 00:40:05 +03:00
var directory = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string uri = $"file://{directory}/audio.ogg".Replace("\\", "/");
2023-02-06 23:13:20 +03:00
using (var web = UnityWebRequestMultimedia.GetAudioClip(uri, AudioType.OGGVORBIS))
2022-08-21 15:05:26 +03:00
{
var asyncOperation = web.SendWebRequest();
while (!asyncOperation.isDone)
await Task.Yield();
if (!web.isNetworkError && !web.isHttpError)
{
AudioClip = DownloadHandlerAudioClip.GetContent(web);
}
else
{
Debug.LogError($"Can't load audio at path: '{uri}', error: {web.error}");
}
}
}
}
public class Patch : ModulePatch
{
2023-02-06 23:13:20 +03:00
private static float nextTime;
2023-02-16 00:40:05 +03:00
2022-08-21 15:05:26 +03:00
protected override MethodBase GetTargetMethod()
{
2023-02-16 00:40:05 +03:00
var t = typeof(BotMemoryClass).GetProperty("GoalEnemy").PropertyType;
return t.GetMethod("SetVisible");
2022-08-21 15:05:26 +03:00
}
[PatchPostfix]
2023-02-16 00:40:05 +03:00
public static void PatchPostfix(object __instance, bool value, bool ___bool_0)
2022-08-21 15:05:26 +03:00
{
2023-02-06 23:13:20 +03:00
if (!Plugin.PluginEnabled.Value || Plugin.AudioClip == null || Time.time < nextTime) return;
2022-08-21 15:05:26 +03:00
2023-02-16 00:40:05 +03:00
var person = (IAIDetails) __instance.GetType().GetProperty("Person").GetValue(__instance);
2023-02-06 23:13:20 +03:00
2023-02-16 00:40:05 +03:00
if (!value || !person.GetPlayer.IsYourPlayer || !___bool_0) return;
2023-02-06 23:13:20 +03:00
2023-02-16 00:40:05 +03:00
var betterAudio = Singleton<BetterAudio>.Instance;
var audioSourceGroupType = BetterAudio.AudioSourceGroupType.NonspatialBypass;
betterAudio.PlayNonspatial(Plugin.AudioClip, audioSourceGroupType, 0.0f, Plugin.Volume.Value/100f);
2023-02-06 23:13:20 +03:00
nextTime = Time.time + Plugin.Cooldown.Value;
2022-08-21 15:05:26 +03:00
}
}
}