Files
H3VR-TNH-Quality-of-Life-Im…/Assets/MeatKit/MeatKitPlugin.cs
T

331 lines
13 KiB
C#
Raw Normal View History

2022-01-22 20:13:49 -08:00
#if H3VR_IMPORTED
using System.Collections;
using System;
2022-01-26 01:23:39 -08:00
using HarmonyLib;
2022-01-22 20:13:49 -08:00
using System.IO;
using System.Reflection;
using BepInEx;
2022-01-24 00:34:25 -08:00
using BepInEx.Bootstrap;
using BepInEx.Logging;
2022-01-22 20:13:49 -08:00
using BepInEx.Configuration;
using UnityEngine;
2022-01-30 03:16:24 -08:00
using UnityEngine.UI;
2022-01-22 20:13:49 -08:00
using UnityEngine.SceneManagement;
2022-06-06 19:09:31 -07:00
using FistVR;
2022-01-22 20:13:49 -08:00
using TNHQoLImprovements;
2022-06-06 19:09:31 -07:00
using Sodalite.Api;
using Sodalite;
2022-01-22 20:13:49 -08:00
/*
* SUPER LARGE WARNING ABOUT THIS CLASS
* This is the default and fallback class that MeatKit uses as a template to generate a BepInEx plugin
* when building your mod. DO NOT MODIFY THIS FILE AT ALL, IN ANY WAY.
2022-01-22 20:13:49 -08:00
*
* If you want to add custom behavior to your mod, you should make a copy of this class, and put it inside
* the main namespace of your mod (that namespace can be found by opening the 'Allowed Namespaces' list on your build
* profile). MeatKit will then detect and use that class instead of this one, for that one specific profile.
*
* HOWEVER, YOU MUST KEEP ALL OF THE STUFF FROM THIS TEMPLATE, otherwise MeatKit may fail to correctly build
* your plugin, or your mod may fail to correctly load.
2022-01-22 20:13:49 -08:00
*/
// DO NOT REMOVE OR CHANGE ANY OF THESE ATTRIBUTES
[BepInPlugin("MeatKit", "MeatKit Plugin", "1.0.0")]
[BepInProcess("h3vr.exe")]
// DO NOT CHANGE THE NAME OF THIS CLASS OR THE BASE CLASS. If you're making a custom plugin, make sure it extends BaseUnityPlugin.
2022-01-22 20:13:49 -08:00
public class MeatKitPlugin : BaseUnityPlugin
{
// DO NOT CHANGE OR REMOVE THIS FIELD.
#pragma warning disable 414
private static readonly string BasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
internal new static ManualLogSource Logger;
2022-01-22 20:13:49 -08:00
#pragma warning restore 414
2022-01-30 03:16:24 -08:00
public static AssetBundle bundle;
public static Font fontAgencyFB;
2022-01-30 03:16:24 -08:00
public static Font fontBombardier;
public static GameObject playerCamera;
// BepInEx configuration
//--- Health Counter ---//
public static ConfigEntry<bool> cfgHPHiddenWhenAiming;
public static ConfigEntry<float> cfgHPAimOpacity;
public static ConfigEntry<bool> cfgShowHPBackground;
public static ConfigEntry<float> cfgHPBackgroundOpacity;
public static ConfigEntry<HPTextType> cfgHPTextType;
2022-01-30 03:16:24 -08:00
//--- Take and Hold Info ---//
public static ConfigEntry<bool> cfgShowLPC;
2022-02-13 03:17:47 -08:00
public static ConfigEntry<bool> cfgInfoFollowCamera;
public static ConfigEntry<bool> cfgShowTokens;
public static ConfigEntry<bool> cfgShowHolds;
2022-01-30 03:16:24 -08:00
public static ConfigEntry<bool> cfgShowNumbersAtShop;
public static ConfigEntry<bool> cfgShowInfoOnGameOver;
public static ConfigEntry<bool> cfgShowWaves;
//--- Misc. ---//
public static ConfigEntry<HealthExpireIndicationType> cfgHealthCrystalIndicator;
2022-01-30 03:16:24 -08:00
// Take and Hold modifications
2023-07-31 02:32:02 -07:00
private static InPlay playInstance;
2022-06-06 19:09:31 -07:00
2022-01-30 03:16:24 -08:00
// Searching for old leaderboards player count mod to disable
2022-06-06 19:09:31 -07:00
private float lpcSearchTime;
private bool lpcStopSearching = false;
private float lpcModSearchTimeEnd;
2022-01-22 20:13:49 -08:00
2022-06-06 19:29:54 -07:00
public static FVRHealthBar hpDisplay;
2022-06-06 19:09:31 -07:00
// toggle HP visibility from wrist menu
private bool hpDisplayEnabled = true;
private WristMenuButton wmbHPToggle;
2022-01-26 01:23:39 -08:00
private Harmony harmony;
2022-01-22 20:13:49 -08:00
private void SceneChanged(Scene from, Scene to)
{
2023-07-31 02:32:02 -07:00
StartCoroutine("SceneChangedCoroutine");
}
2023-07-31 02:32:02 -07:00
private IEnumerator SceneChangedCoroutine()
{
2023-07-31 02:32:02 -07:00
Destroy(playInstance);
for (int i = 0; i < 11; ++i)
2022-01-22 20:13:49 -08:00
{
// TNH patches
if (GameObject.Find("_GameManager") != null || FindObjectOfType<TNH_Manager>() != null)
{
Logger.LogInfo("We are in a TNH game!");
2023-07-31 02:32:02 -07:00
playInstance = new GameObject().AddComponent<InPlay>();
playerCamera = GameObject.FindGameObjectWithTag("MainCamera");
break;
}
else
{
Logger.LogInfo(String.Format("Couldn't find a TNH game. Trying again...({0}/10)", i));
yield return new WaitForEndOfFrame();
}
2022-01-22 20:13:49 -08:00
}
2023-07-31 02:32:02 -07:00
if (playInstance == null)
2022-01-22 20:13:49 -08:00
{
2022-01-24 03:40:08 -08:00
Logger.LogInfo("We are NOT in a TNH game!");
2022-01-22 20:13:49 -08:00
}
2023-07-31 02:32:02 -07:00
// setup non-TNH specific static objects
// running AFTER searching for TNH_Manager ensures we have everything initialized
playerCamera = GameObject.FindGameObjectWithTag("MainCamera");
hpDisplay = FindObjectOfType<FVRHealthBar>();
// apply health counter tweaks globally
if (hpDisplay != null)
{
HPReadability.ImproveHPTextReadability(hpDisplay.transform.GetChild(0).gameObject);
if (cfgHPHiddenWhenAiming.Value)
hpDisplay.gameObject.AddComponent<HPHideWhenAiming>();
WristMenuAPI.Buttons.Add(wmbHPToggle);
}
else
{
WristMenuAPI.Buttons.Remove(wmbHPToggle);
}
GetFonts();
}
2022-01-30 14:41:03 -08:00
// called on scene change, find fonts from game if they're not set
private void GetFonts()
{
// Agency FB
if (fontAgencyFB == null)
2022-01-30 03:16:24 -08:00
{
2022-06-06 19:29:54 -07:00
if (hpDisplay != null)
2022-01-30 03:16:24 -08:00
{
2022-06-06 19:29:54 -07:00
fontAgencyFB = hpDisplay.transform.GetChild(0).GetChild(0).GetComponent<Text>().font;
2022-01-30 03:16:24 -08:00
}
else
{
var query = FindObjectsOfType<Text>();
foreach (Text itm in query)
{
if (itm.font.name == "AGENCYR")
{
fontAgencyFB = itm.font;
break;
}
}
}
}
2022-01-22 20:13:49 -08:00
}
2022-01-26 01:23:39 -08:00
public MeatKitPlugin(): base()
{
harmony = new Harmony("muskit.TNHQualityOfLifeImprovements");
lpcSearchTime = 30f + 30f * Mathf.Sin(System.DateTime.Today.DayOfYear / 365f); // lolz
2022-01-26 01:23:39 -08:00
}
// You are free to edit this method, however please ensure LoadAssets is still called somewhere inside it.
2022-01-22 20:13:49 -08:00
private void Awake()
{
// ----- BEGIN MEATKIT REQ. CODE -----
// This lets you use your BepInEx-provided logger from other scripts in your project
Logger = base.Logger;
// You may place code before/after this, but do not remove this call to LoadAssets
LoadAssets();
// ----- END MEATKIT REQ. CODE -----
2022-06-06 19:29:54 -07:00
// get Agency FB from system (BAD IDEA, NOT EVERYONE WILL HAVE IT; MAY SET TO DEFAULT FONT)
2022-01-30 03:16:24 -08:00
//fontAgencyFB = Font.CreateDynamicFontFromOSFont("Agency FB", 16);
2022-01-22 20:13:49 -08:00
// load asset bundle
bundle = AssetBundle.LoadFromFile(Path.Combine(BasePath, "tnh_qol_improvements"));
SceneManager.activeSceneChanged += SceneChanged;
2022-02-13 03:17:47 -08:00
fontBombardier = bundle.LoadAsset<Font>("Bombardier");
2022-01-30 03:16:24 -08:00
2022-01-22 20:13:49 -08:00
// setup configuration
2022-01-30 03:16:24 -08:00
//--- Health Counter ---//
cfgHPHiddenWhenAiming = Config.Bind("Health Counter",
"Hide HP Counter When Aiming",
true,
"While aiming around the health counter in view, hide it.");
cfgHPAimOpacity = cfgHPBackgroundOpacity = Config.Bind("Health Counter",
"Aiming opacity",
0f,
"Opacity of Health Counter when aiming around it (if Hide HP is enabled).");
cfgShowHPBackground = Config.Bind("Health Counter",
"Background enabled",
false,
"Apply a background to the Health Counter.");
cfgHPBackgroundOpacity = Config.Bind("Health Counter",
"Background opacity",
0.74f,
"Set opacity of health text's background (if enabled).");
cfgHPTextType = Config.Bind("Health Counter",
"HP Text Type",
HPTextType.Solidify,
"Solidify: Set text to full opacity and give it a drop shadow\n" +
"Untouched: Leave text untouched\n" +
"Hidden: Hide health counter completely (will hide background if enabled)");
2022-01-30 03:16:24 -08:00
//--- Take and Hold Info ---//
cfgShowLPC = Config.Bind("Take and Hold Info",
2022-02-13 03:17:47 -08:00
"Show Player Count in Online Leaderboards",
2022-01-24 03:40:08 -08:00
true,
"Shows the number of players in the currently selected TNH leaderboard.");
2022-02-13 03:17:47 -08:00
cfgInfoFollowCamera = Config.Bind("Take and Hold Info",
"Tilt Wrist Stats Towards Camera",
true,
"Tilt the extra wrist statistics from this mod towards the player's camera, allowing for easier readability.");
cfgShowTokens = Config.Bind("Take and Hold Info",
"Show Tokens",
true,
"Shows how many tokens the player has by their radar hand.");
cfgShowHolds = Config.Bind("Take and Hold Info",
"Show Holds",
true,
"Shows how many holds the player has completed by their radar hand.");
2022-01-30 03:16:24 -08:00
cfgShowWaves = Config.Bind("Take and Hold Info",
"Show Waves",
true,
"Shows how many waves the player has completed on the current hold by their radar hand.");
cfgShowInfoOnGameOver = Config.Bind("Take and Hold Info",
"Show Extra Info at Game Over",
true,
"Show enabled extra game information at the game over area.");
cfgShowNumbersAtShop = Config.Bind("Take and Hold Info",
"Show Numbers for Tokens at Item Station",
true,
"At the item station, add a numberical representation to costs and player's tokens.");
//--- Misc. ---//
cfgHealthCrystalIndicator = Config.Bind("Misc.",
"Show expiration of Health Crystals",
HealthExpireIndicationType.Flashing,
"Add a visual indication on the Health Crystal's despawn timer.");
2022-06-06 19:09:31 -07:00
// calculate end time to search for my deprecated Leaderboard
lpcModSearchTimeEnd = Time.time + lpcSearchTime;
wmbHPToggle = new WristMenuButton("Toggle HP Display", ToggleHPVisibility);
RunPatches();
}
2022-06-06 19:09:31 -07:00
private void RunPatches()
{
if (harmony == null)
2022-06-06 19:29:54 -07:00
{
Logger.LogError("Could not run patches; Harmony didn't initialize correctly!");
return;
2022-06-06 19:29:54 -07:00
}
2022-01-22 20:13:49 -08:00
2022-01-26 01:23:39 -08:00
// patch KillAll code (only acts w/ health crystals)
if (cfgHealthCrystalIndicator.Value != HealthExpireIndicationType.None)
TimedHealthCrystalPatch.Patch(harmony);
2022-01-26 01:23:39 -08:00
// patch leaderboard code
2022-01-24 03:40:08 -08:00
if (cfgShowLPC.Value)
2022-01-26 01:23:39 -08:00
LeaderboardPlayerCountPatch.Patch(harmony);
2022-01-30 03:16:24 -08:00
// for counting wins/loses for TNHInfo.holdCounter
if (cfgShowHolds.Value)
HoldCounterPatch.Patch(harmony);
// stick stats to hand after game over
if (cfgShowHolds.Value || cfgShowTokens.Value)
TNHInfo.Patch(harmony);
2022-01-30 03:16:24 -08:00
// show numerical representation of shop values
if (cfgShowNumbersAtShop.Value)
{
// costs
ShopCostPatch.Patch(harmony);
// player tokens
ShopTokenPatch.Patch(harmony);
}
Logger.LogInfo("Successfully ran patches!");
}
2022-01-24 00:34:25 -08:00
2022-06-06 19:09:31 -07:00
private void ToggleHPVisibility(object sender, ButtonClickEventArgs args)
{
hpDisplayEnabled = !hpDisplayEnabled;
if (hpDisplay != null)
hpDisplay.gameObject.SetActive(hpDisplayEnabled);
}
2022-01-24 00:34:25 -08:00
/// <summary>
/// Its only purpose: to kill the deprecated TNH Leaderboard Player Count mod.
2022-01-24 00:34:25 -08:00
/// </summary>
private void Update()
{
if (lpcStopSearching)
2022-01-24 00:34:25 -08:00
return;
foreach (var plugin in Chainloader.PluginInfos)
{
if (plugin.Key == "me.muskit.tnhLeaderboardPlayerCount")
{
Logger.LogWarning("TNH Leaderboard Player Count mod detected. Destroying it to avoid interference.");
Destroy(plugin.Value.Instance);
lpcStopSearching = true;
2022-01-24 00:34:25 -08:00
}
}
if (Time.realtimeSinceStartup >= lpcModSearchTimeEnd)
{
2022-06-06 19:09:31 -07:00
Logger.LogInfo(string.Format("Stopping search for TNH Leaderboard Player Count mod after {0} seconds.", lpcSearchTime));
lpcStopSearching = true;
}
2022-01-24 00:34:25 -08:00
}
// DO NOT CHANGE OR REMOVE THIS METHOD. It's contents will be overwritten when building your package.
private void LoadAssets()
{
// Code to load your build items will be generated at build-time and inserted here
}
2022-01-22 20:13:49 -08:00
}
#endif