quantum-space-buddies/QSB/QSBCore.cs

197 lines
6.3 KiB
C#
Raw Normal View History

using HarmonyLib;
using Mirror;
using OWML.Common;
2020-02-10 22:03:28 +00:00
using OWML.ModHelper;
2021-06-04 10:31:09 +00:00
using QSB.Menus;
2020-11-03 21:11:10 +00:00
using QSB.Patches;
2022-01-18 23:50:20 +00:00
using QSB.QuantumSync;
using QSB.Utility;
2021-02-18 15:36:11 +00:00
using QSB.WorldSync;
2022-02-10 12:18:30 +00:00
using System;
2022-01-29 02:37:28 +00:00
using System.IO;
2021-12-07 15:56:08 +00:00
using System.Linq;
2022-01-29 02:37:28 +00:00
using System.Reflection;
2020-02-10 22:03:28 +00:00
using UnityEngine;
2022-01-19 00:19:49 +00:00
using UnityEngine.InputSystem;
2020-02-10 22:03:28 +00:00
2020-12-17 23:18:47 +00:00
/*
2022-01-29 21:26:05 +00:00
Copyright (C) 2020 - 2022
2021-11-10 01:56:45 +00:00
Henry Pointer (_nebula / misternebula),
2021-12-20 05:19:11 +00:00
Will Corby (JohnCorby),
2021-11-10 01:56:45 +00:00
Aleksander Waage (AmazingAlek),
2020-12-19 21:14:05 +00:00
Ricardo Lopes (Raicuparta)
2021-11-10 01:56:45 +00:00
2020-12-19 21:14:05 +00:00
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/.
2020-12-17 23:18:47 +00:00
*/
2022-03-03 03:46:33 +00:00
namespace QSB;
public class QSBCore : ModBehaviour
2020-02-15 19:48:02 +00:00
{
2022-03-03 03:46:33 +00:00
public static IModHelper Helper { get; private set; }
public static string DefaultServerIP;
public static AssetBundle NetworkAssetBundle { 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 =>
// todo: show this in kick message
Helper.Manifest.Version;
public static string GameVersion =>
// ignore the last patch numbers like the title screen does
// todo: show this in kick message
Application.version.Split('.').Take(3).Join(delimiter: ".");
2022-03-03 03:46:33 +00:00
public static bool DLCInstalled => EntitlementsManager.IsDlcOwned() == EntitlementsManager.AsyncOwnershipStatus.Owned;
public static IMenuAPI MenuApi { get; private set; }
public static DebugSettings DebugSettings { get; private set; } = new();
public void Awake()
2020-12-02 21:29:53 +00:00
{
2022-03-03 03:46:33 +00:00
EpicRerouter.ModSide.Interop.Go();
2022-02-07 05:38:14 +00:00
2022-03-03 03:46:33 +00:00
UIHelper.ReplaceUI(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);
2020-02-10 22:03:28 +00:00
2022-03-03 03:46:33 +00:00
DebugSettings = Helper.Storage.Load<DebugSettings>("debugsettings.json") ?? new DebugSettings();
if (DebugSettings.HookDebugLogs)
{
2022-03-03 03:46:33 +00:00
Application.logMessageReceived += (condition, stackTrace, logType) =>
DebugLog.ToConsole(
$"[Debug] {condition}" +
(stackTrace != string.Empty ? $"\nStacktrace: {stackTrace}" : string.Empty),
logType switch
{
LogType.Error => MessageType.Error,
LogType.Assert => MessageType.Error,
LogType.Warning => MessageType.Warning,
LogType.Log => MessageType.Message,
LogType.Exception => MessageType.Error,
_ => throw new ArgumentOutOfRangeException(nameof(logType), logType, null)
}
);
}
2020-12-02 18:40:38 +00:00
2022-03-03 03:46:33 +00:00
InitializeAssemblies();
MenuApi = ModHelper.Interaction.GetModApi<IMenuAPI>(ModHelper.Manifest.Dependencies[0]);
2022-03-04 13:15:54 +00:00
DebugLog.DebugWrite("loading network-big bundle", MessageType.Info);
var path = Path.Combine(ModHelper.Manifest.ModFolderPath, "AssetBundles/network-big");
var request = AssetBundle.LoadFromFileAsync(path);
request.completed += _ => DebugLog.DebugWrite("network-big bundle loaded", MessageType.Success);
2022-03-03 03:46:33 +00:00
NetworkAssetBundle = Helper.Assets.LoadBundle("AssetBundles/network");
ConversationAssetBundle = Helper.Assets.LoadBundle("AssetBundles/conversation");
DebugAssetBundle = Helper.Assets.LoadBundle("AssetBundles/debug");
TextAssetsBundle = Helper.Assets.LoadBundle("AssetBundles/textassets");
QSBPatchManager.Init();
DeterministicManager.Init();
var components = typeof(IAddComponentOnStart).GetDerivedTypes()
.Select(x => gameObject.AddComponent(x))
.ToArray();
QSBWorldSync.Managers = components.OfType<WorldObjectManager>().ToArray();
QSBPatchManager.OnPatchType += OnPatchType;
QSBPatchManager.OnUnpatchType += OnUnpatchType;
QSBPatchManager.DoPatchType(QSBPatchTypes.OnModStart);
2022-03-03 03:46:33 +00:00
}
private static void OnPatchType(QSBPatchTypes type)
{
if (type == QSBPatchTypes.OnClientConnect)
2021-02-18 15:36:11 +00:00
{
2022-03-03 03:46:33 +00:00
Application.runInBackground = true;
}
2022-03-03 03:46:33 +00:00
}
2021-02-21 18:25:25 +00:00
2022-03-03 03:46:33 +00:00
private static void OnUnpatchType(QSBPatchTypes type)
{
if (type == QSBPatchTypes.OnClientConnect)
{
2022-03-03 03:46:33 +00:00
Application.runInBackground = false;
2021-02-18 15:36:11 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-02-18 15:36:11 +00:00
2022-03-03 03:46:33 +00:00
private static void InitializeAssemblies()
{
DebugLog.DebugWrite("Running RuntimeInitializeOnLoad methods for our assemblies", MessageType.Info);
foreach (var path in Directory.EnumerateFiles(Helper.Manifest.ModFolderPath, "*.dll"))
2022-01-29 02:37:28 +00:00
{
2022-03-03 03:46:33 +00:00
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.IsDefined(typeof(RuntimeInitializeOnLoadMethodAttribute)))
.ForEach(x => x.Invoke(null, null));
2022-01-29 02:37:28 +00:00
}
2022-03-03 03:46:33 +00:00
DebugLog.DebugWrite("Assemblies initialized", MessageType.Success);
}
2022-01-18 23:50:20 +00:00
2022-03-03 03:46:33 +00:00
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)
2022-01-18 23:50:20 +00:00
{
2022-03-03 03:46:33 +00:00
DebugSettings.DebugMode = !DebugSettings.DebugMode;
2022-01-18 23:50:20 +00:00
2022-03-03 03:46:33 +00:00
GetComponent<DebugActions>().enabled = DebugSettings.DebugMode;
GetComponent<DebugGUI>().enabled = DebugSettings.DrawGui;
QuantumManager.UpdateFromDebugSetting();
DebugCameraSettings.UpdateFromDebugSetting();
2022-01-18 23:50:20 +00:00
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"DEBUG MODE = {DebugSettings.DebugMode}");
2022-01-18 23:50:20 +00:00
}
2020-12-02 21:29:53 +00:00
}
2021-06-18 20:54:32 +00:00
}
/*
* _nebula's music thanks
* I listen to music constantly while programming/working - here's my thanks to them for keeping me entertained :P
2021-11-10 01:56:45 +00:00
*
2021-06-18 20:54:32 +00:00
* 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
2021-06-23 15:46:08 +00:00
* Daft Punk
2021-07-19 20:01:41 +00:00
* Natalie Holt
2021-08-17 09:02:19 +00:00
* WMD
*/