-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUnityScript.cs
92 lines (78 loc) · 2.81 KB
/
UnityScript.cs
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
using System;
using System.Reflection;
using UnityEngine;
namespace Oxide.Core.Unity
{
/// <summary>
/// The main MonoBehaviour which calls OxideMod.OnFrame
/// </summary>
public class UnityScript : MonoBehaviour
{
public static GameObject Instance { get; private set; }
public static float RealtimeSinceStartup { get; private set; }
public static void Create()
{
Instance = new GameObject("Oxide.Core.Unity");
DontDestroyOnLoad(Instance);
Instance.AddComponent<UnityScript>();
}
private OxideMod oxideMod;
private void Awake()
{
RealtimeSinceStartup = Time.realtimeSinceStartup;
oxideMod = Interface.Oxide;
EventInfo eventInfo = typeof(Application).GetEvent("logMessageReceived");
if (eventInfo == null)
{
// Unity 4
FieldInfo logCallbackField = typeof(Application).GetField("s_LogCallback", BindingFlags.Static | BindingFlags.NonPublic);
Application.LogCallback logCallback = logCallbackField?.GetValue(null) as Application.LogCallback;
if (logCallback == null)
{
Interface.Oxide.LogWarning("No Unity application log callback is registered");
}
#pragma warning disable 0618
Application.RegisterLogCallback((message, stack_trace, type) =>
{
logCallback?.Invoke(message, stack_trace, type);
LogMessageReceived(message, stack_trace, type);
});
}
else
{
// Unity 5
Delegate handleException = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, "LogMessageReceived");
eventInfo.GetAddMethod().Invoke(null, new object[] { handleException });
}
}
private void Update()
{
RealtimeSinceStartup = Time.realtimeSinceStartup;
oxideMod.OnFrame(Time.deltaTime);
}
private void OnDestroy()
{
if (oxideMod.IsShuttingDown)
{
return;
}
oxideMod.LogWarning("The Oxide Unity Script was destroyed (creating a new instance)");
oxideMod.NextTick(Create);
}
private void OnApplicationQuit()
{
if (!oxideMod.IsShuttingDown)
{
Interface.Call("OnServerShutdown");
Interface.Oxide.OnShutdown();
}
}
private void LogMessageReceived(string message, string stackTrace, LogType type)
{
if (type == LogType.Exception)
{
RemoteLogger.Exception(message, stackTrace);
}
}
}
}