-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMod.cs
More file actions
147 lines (129 loc) · 4.79 KB
/
Copy pathMod.cs
File metadata and controls
147 lines (129 loc) · 4.79 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
using Colossal.IO.AssetDatabase;
using Colossal.Logging;
using Game;
using Game.Modding;
using System;
using Unity.Entities;
namespace PerformanceMonitor
{
/// <summary>
/// The mod.
/// </summary>
public class Mod : IMod
{
// Create a new log just for this mod.
// This mod will have its own log file in the game's Logs folder.
public static readonly ILog log = LogManager.GetLogger(ModAssemblyInfo.Name)
.SetShowsErrorsInUI(true) // Show message in UI for severity level Error and above.
.SetShowsStackTraceAboveLevels(Level.Error); // Include stack trace for severity level Error and above.
// The one and only global settings for this mod.
public static ModSettings ModSettings { get; private set;}
// Whether or not to show data values.
public static bool ShowGPUUsage = false;
public static bool ShowCPUUsage = false;
public static bool ShowMemoryUsage = false;
/// <summary>
/// One-time mod loading.
/// </summary>
public void OnLoad(UpdateSystem updateSystem)
{
log.Info($"{nameof(Mod)}.{nameof(OnLoad)} Version {ModAssemblyInfo.Version}");
try
{
// Register and load mod settings.
ModSettings = new ModSettings(this);
ModSettings.RegisterInOptionsUI();
ModSettings.RegisterKeyBindings();
AssetDatabase.global.LoadSettings(nameof(PerformanceMonitor), ModSettings, new ModSettings(this));
ModSettings.Loaded();
// Initialize translations.
Translation.Initialize();
// Check if operating system is Windows.
if (IsWindows())
{
// CPU and memory usage are shown only for Windows.
ShowCPUUsage = true;
ShowMemoryUsage = true;
// GPU usage is shown only for Windows and only if successfully initialized.
ShowGPUUsage = InitializeGPUUsage();
}
// Create and activate this mod's systems.
updateSystem.UpdateAt<UISystem>(SystemUpdatePhase.UIUpdate);
#if DEBUG
// Create UI constant files.
// Uncomment this only when the UI constant files need to be created or recreated.
// Then run the mod once in the game to create the constant files.
// Then comment this again. The UI constants are now available to use.
//UIConstantFiles.Create();
#endif
}
catch(Exception ex)
{
log.Error(ex);
}
log.Info($"{nameof(Mod)}.{nameof(OnLoad)} complete.");
}
/// <summary>
/// One-time mod disposing.
/// </summary>
public void OnDispose()
{
log.Info($"{nameof(Mod)}.{nameof(OnDispose)}");
// Stop listening for events to prevent null reference exception when accessing ModSettings.
World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<UISystem>()?.StopListeningForEvents();
// Unregister mod settings.
ModSettings?.UnregisterInOptionsUI();
ModSettings = null;
// Shutdown GPU usage.
if (IsWindows())
{
ShutdownGPUUsage();
}
}
/// <summary>
/// Get whether or not operating system is Windows.
/// This is a separate function to avoid a reference to the Windows-only CheckWindows class in the OnLoad and OnDispose functions.
/// </summary>
private bool IsWindows()
{
try
{
return CheckWindows.IsWindows();
}
catch
{
return false;
}
}
/// <summary>
/// Initialize GPU usage.
/// This is a separate function to avoid a reference to the Windows-only GPUUsage class in the OnLoad function.
/// </summary>
private bool InitializeGPUUsage()
{
try
{
return GPUUsage.Initialize();
}
catch
{
return false;
}
}
/// <summary>
/// Shut down GPU usage.
/// This is a separate function to avoid a reference to the Windows-only GPUUsage class in the OnDispose function.
/// </summary>
private void ShutdownGPUUsage()
{
try
{
GPUUsage.Shutdown();
}
catch
{
// Ignore exception.
}
}
}
}