This repository was archived by the owner on Jul 13, 2020. It is now read-only.
forked from Slaynash/VRCModLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModComponent.cs
More file actions
155 lines (129 loc) · 4.24 KB
/
Copy pathModComponent.cs
File metadata and controls
155 lines (129 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace VRCModLoader
{
public class ModComponent : MonoBehaviour
{
private CompositeModCaller mods;
private bool freshlyLoaded = false;
private bool quitting = false;
internal static GameObject modulesGameObject;
public static ModComponent Instance { get; private set; }
public static void Create()
{
VRCModLogger.Log("[ModComponent] Loading VRLoader.dll");
LoadVRLoader();
VRCModLogger.Log("[ModComponent] VRLoader.dll loaded");
if (!Bootstrapper.loadmods)
return;
try
{
VRCModLogger.Log("[ModComponent] Creating components");
//First create the mod manager GO, so it gets updated before the modules.
GameObject modManagerGO = new GameObject("IPA_ModManager");
modulesGameObject = new GameObject("IPA_VRModules");
modulesGameObject.SetActive(false); // We will enable it when the Ui scene will be loaded.
modManagerGO.AddComponent<ModComponent>();
}
catch(Exception e)
{
VRCModLogger.LogError("[ModComponent] Error while creating instance: " + e);
}
}
void Awake()
{
VRCModLogger.Log("[ModComponent] Awake called");
DontDestroyOnLoad(gameObject);
Instance = this;
try
{
ModManager.LoadMods();
}
catch (Exception e)
{
VRCModLogger.Log("An error occured while loading mods: " + e);
}
mods = new CompositeModCaller(ModManager.ModControllers);
mods.OnApplicationStart();
SceneManager.sceneLoaded += (scene, method) =>
{
VRCModLogger.Log("[ModComponent] Scene Loaded: " + scene.name);
if (scene.name == "ui")
StartCoroutine(StartVRModules());
mods.OnLevelWasLoaded(scene.buildIndex);
};
}
private IEnumerator StartVRModules()
{
yield return null; // Wait for end UI init
modulesGameObject.SetActive(true);
}
private static void LoadVRLoader()
{
try
{
Assembly.Load(Properties.Resources.VRLoader);
}
catch (Exception e)
{
VRCModLogger.LogError("[ModComponent] Error while loading VRLoader.dll: " + e);
}
}
void Start()
{
VRCModLogger.Log("[ModComponent] Start called");
OnLevelWasLoadedNow(Application.loadedLevel);
}
void Update()
{
if (freshlyLoaded)
{
freshlyLoaded = false;
if (mods != null) mods.OnLevelWasInitialized(Application.loadedLevel);
}
if (mods != null) mods.OnUpdate();
}
void LateUpdate()
{
if(mods != null) mods.OnLateUpdate();
}
void FixedUpdate()
{
if (mods != null) mods.OnFixedUpdate();
}
void OnGUI()
{
if (mods != null) mods.OnGUI();
}
void OnDestroy()
{
VRCModLogger.Log("[ModComponent] Component destroyed");
if (!quitting)
{
Create();
}
}
void OnApplicationQuit()
{
VRCModLogger.Log("[ModComponent] OnApplicationQuit called");
if (mods != null) mods.OnApplicationQuit();
quitting = true;
}
void OnLevelWasLoadedNow(int level)
{
VRCModLogger.Log("[ModComponent] OnLevelWasLoaded called (" + level + ")");
transform.SetAsLastSibling();
if (mods != null) mods.OnLevelWasLoaded(level);
freshlyLoaded = true;
}
public static void OnModSettingsApplied()
{
Instance?.mods?.OnModSettingsApplied();
}
}
}