quantum-space-buddies/QSB/QSBCore.cs
2022-01-28 18:38:34 -08:00

203 lines
7.0 KiB
C#

using Mirror;
using OWML.Common;
using OWML.ModHelper;
using QSB.EyeOfTheUniverse.GalaxyMap;
using QSB.EyeOfTheUniverse.MaskSync;
using QSB.Inputs;
using QSB.Menus;
using QSB.Patches;
using QSB.Player;
using QSB.QuantumSync;
using QSB.RespawnSync;
using QSB.SatelliteSync;
using QSB.StatueSync;
using QSB.TimeSync;
using QSB.Utility;
using QSB.WorldSync;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.InputSystem;
/*
Copyright (C) 2020 - 2021
Henry Pointer (_nebula / misternebula),
Will Corby (JohnCorby),
Aleksander Waage (AmazingAlek),
Ricardo Lopes (Raicuparta)
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
*/
namespace QSB
{
public class QSBCore : ModBehaviour
{
public static IModHelper Helper { get; private set; }
public static IModUnityEvents UnityEvents => Helper.Events.Unity;
public static string DefaultServerIP;
public static bool UseKcpTransport => DebugSettings.UseKcpTransport;
public static int OverrideAppId => DebugSettings.OverrideAppId;
public static bool DumpWorldObjects => DebugSettings.DumpWorldObjects;
public static bool DebugMode => DebugSettings.DebugMode;
public static bool ShowLinesInDebug => DebugMode && DebugSettings.DrawLines;
public static bool ShowQuantumVisibilityObjects => DebugMode && DebugSettings.ShowQuantumVisibilityObjects;
public static bool ShowDebugLabels => DebugMode && DebugSettings.ShowDebugLabels;
public static bool AvoidTimeSync => DebugMode && DebugSettings.AvoidTimeSync;
public static bool SkipTitleScreen => DebugMode && DebugSettings.SkipTitleScreen;
public static bool GreySkybox => DebugMode && DebugSettings.GreySkybox;
public static AssetBundle NetworkAssetBundle { get; internal set; }
public static AssetBundle InstrumentAssetBundle { get; private set; }
public static AssetBundle ConversationAssetBundle { get; private set; }
public static AssetBundle DebugAssetBundle { get; private set; }
public static AssetBundle TextAssetsBundle { get; private set; }
public static bool IsHost => NetworkServer.active;
public static bool IsInMultiplayer => QSBNetworkManager.singleton.isNetworkActive;
public static string QSBVersion => Helper.Manifest.Version;
public static string GameVersion => Application.version;
public static bool DLCInstalled => EntitlementsManager.IsDlcOwned() == EntitlementsManager.AsyncOwnershipStatus.Owned;
public static IMenuAPI MenuApi { get; private set; }
private static DebugSettings DebugSettings { get; set; } = new();
public void Awake()
{
var instance = TextTranslation.Get().m_table;
instance.theUITable[(int)UITextType.PleaseUseController] =
"<color=orange>Quantum Space Buddies</color> is best experienced with friends...";
}
public void Start()
{
Helper = ModHelper;
DebugLog.ToConsole($"* Start of QSB version {QSBVersion} - authored by {Helper.Manifest.Author}", MessageType.Info);
MenuApi = ModHelper.Interaction.GetModApi<IMenuAPI>("_nebula.MenuFramework");
NetworkAssetBundle = Helper.Assets.LoadBundle("AssetBundles/network");
InstrumentAssetBundle = Helper.Assets.LoadBundle("AssetBundles/instruments");
ConversationAssetBundle = Helper.Assets.LoadBundle("AssetBundles/conversation");
DebugAssetBundle = Helper.Assets.LoadBundle("AssetBundles/debug");
TextAssetsBundle = Helper.Assets.LoadBundle("AssetBundles/textassets");
DebugSettings = Helper.Storage.Load<DebugSettings>("debugsettings.json") ?? new DebugSettings();
InitializeAssemblies();
QSBPatchManager.Init();
DeterministicManager.Init();
gameObject.AddComponent<QSBNetworkManager>();
gameObject.AddComponent<DebugActions>();
gameObject.AddComponent<QSBInputManager>();
gameObject.AddComponent<TimeSyncUI>();
gameObject.AddComponent<PlayerEntanglementWatcher>();
gameObject.AddComponent<DebugGUI>();
gameObject.AddComponent<MenuManager>();
gameObject.AddComponent<RespawnManager>();
gameObject.AddComponent<SatelliteProjectorManager>();
gameObject.AddComponent<StatueManager>();
gameObject.AddComponent<GalaxyMapManager>();
gameObject.AddComponent<DebugCameraSettings>();
gameObject.AddComponent<MaskManager>();
// WorldObject managers
QSBWorldSync.Managers = typeof(WorldObjectManager).GetDerivedTypes()
.Select(x => (WorldObjectManager)gameObject.AddComponent(x))
.ToArray();
QSBPatchManager.OnPatchType += OnPatchType;
QSBPatchManager.OnUnpatchType += OnUnpatchType;
}
private static void OnPatchType(QSBPatchTypes type)
{
if (type == QSBPatchTypes.OnClientConnect)
{
Application.runInBackground = true;
}
}
private static void OnUnpatchType(QSBPatchTypes type)
{
if (type == QSBPatchTypes.OnClientConnect)
{
Application.runInBackground = false;
}
}
private static void InitializeAssemblies()
{
DebugLog.DebugWrite("Running RuntimeInitializeOnLoad methods for our assemblies", MessageType.Info);
foreach (var path in Directory.EnumerateFiles(Helper.Manifest.ModFolderPath, "*.dll"))
{
var assembly = Assembly.LoadFile(path);
DebugLog.DebugWrite(assembly.ToString());
assembly.GetTypes()
.SelectMany(x => x.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly))
.Where(x => x.GetCustomAttribute<RuntimeInitializeOnLoadMethodAttribute>() != null)
.ForEach(x => x.Invoke(null, null));
}
DebugLog.DebugWrite($"Assemblies initialized", MessageType.Success);
}
public override void Configure(IModConfig config)
{
DefaultServerIP = config.GetSettingsValue<string>("defaultServerIP");
}
private void Update()
{
if (Keyboard.current[Key.Q].isPressed && Keyboard.current[Key.D].wasPressedThisFrame)
{
DebugSettings.DebugMode = !DebugSettings.DebugMode;
GetComponent<DebugActions>().enabled = DebugMode;
GetComponent<DebugGUI>().enabled = DebugMode;
QuantumManager.UpdateFromDebugSetting();
DebugCameraSettings.UpdateFromDebugSetting();
DebugLog.ToConsole($"DEBUG MODE = {DebugMode}");
}
}
}
}
/*
* _nebula's music thanks
* I listen to music constantly while programming/working - here's my thanks to them for keeping me entertained :P
*
* Wintergatan
* HOME
* C418
* Lupus Nocte
* Max Cooper
* Darren Korb
* Harry Callaghan
* Toby Fox
* Andrew Prahlow
* Valve (Mike Morasky, Kelly Bailey)
* Joel Nielsen
* Vulfpeck
* Detektivbyrån
* Ben Prunty
* ConcernedApe
* Jake Chudnow
* Murray Gold
* Teleskärm
* Daft Punk
* Natalie Holt
* WMD
*/