-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGHPluginLogger.cs
247 lines (215 loc) · 7.48 KB
/
GHPluginLogger.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.SceneManagement;
using UnityEngine;
using BepInEx.Logging;
namespace GHPluginCoreLib
{
/// <summary>
/// The GHPluginLogger class allows you to log messages to the BepInEx console. It
/// contains various helper methods to make logging much more convenient.
/// </summary>
public class GHPluginLogger
{
private ManualLogSource logger = null;
/// <summary>
///
/// </summary>
/// <param name="sourceName"></param>
public GHPluginLogger(string sourceName = "GH Plugin Logger")
{
this.logger = BepInEx.Logging.Logger.CreateLogSource(sourceName);
}
/// <summary>
///
/// </summary>
/// <param name="logger"></param>
public GHPluginLogger(ManualLogSource logger)
{
this.logger = logger;
}
/// <summary>
/// Logs a message to the BepInEx debug console.
/// </summary>
/// <param name="msg"></param>
public void LogInfo(string msg)
{
this.logger.LogInfo(msg);
}
/// <summary>
/// Logs all properties of a GameObject by default. It can also be used to log
/// all properties of the GameObject and its children. The third parameter can be
/// used to specify which properties of the GameObjects being logged should not
/// be logged to the BepInEx debug console.
/// </summary>
/// <param name="gameObject"></param>
/// <param name="logDataOfAllChildGameObjects"></param>
/// <param name="propertyNamesFilter"></param>
public void LogAllGameObjectData(GameObject gameObject, bool logDataOfAllChildGameObjects, List<string> propertyNamesFilter = null)
{
this.logger.LogInfo("Current GameObject: " + gameObject);
if (gameObject.transform.parent != null)
{
this.logger.LogInfo("Current GameObject parent: " + gameObject.transform.parent.gameObject);
}
else
{
this.logger.LogInfo("This GameObject does not have a parent.");
}
this.logger.LogInfo("Properties of current GameObject:");
if (propertyNamesFilter == null)
{
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(gameObject))
{
this.logger.LogInfo(propertyDescriptor.Name + " = " + propertyDescriptor.GetValue(gameObject));
}
}
else
{
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(gameObject))
{
if (propertyNamesFilter.Contains(propertyDescriptor.Name) == true)
{
this.logger.LogInfo(propertyDescriptor.Name + " = " + propertyDescriptor.GetValue(gameObject));
}
}
}
this.logger.LogInfo("GameObject transform position: " + gameObject.transform.position.ToString());
if (gameObject.GetComponent<Canvas>() != null)
{
this.logger.LogInfo("GameObject canvas sort-order: " + gameObject.GetComponent<Canvas>().sortingOrder);
}
this.logger.LogInfo("");
if (logDataOfAllChildGameObjects == true)
{
for (int currentChildIndex = 0; currentChildIndex < gameObject.transform.childCount; currentChildIndex++)
{
this.LogAllGameObjectData(gameObject.transform.GetChild(currentChildIndex).gameObject, logDataOfAllChildGameObjects);
}
}
}
/// <summary>
/// Logs all properties for a Scene in the game, the second parameter specifies whether
/// the properties of all GameObjects in the Scene should be logged as well.
/// </summary>
/// <param name="scene"></param>
/// <param name="logAllSceneObjectsData"></param>
public void LogSceneData(Scene scene, bool logAllSceneObjectsData)
{
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(scene))
{
this.logger.LogInfo(propertyDescriptor.Name + " = " + propertyDescriptor.GetValue(scene));
}
this.logger.LogInfo("IsValid = " + scene.IsValid());
if (logAllSceneObjectsData == true)
{
foreach (GameObject rootGameObject in scene.GetRootGameObjects())
{
this.LogAllGameObjectData(rootGameObject, true);
}
}
}
/// <summary>
/// Logs all properties for the currently active Scene in the game, the second
/// parameter specifies whether the properties of all GameObjects in the Scene
/// should be logged as well.
/// </summary>
/// <param name="logAllSceneObjectsData"></param>
public void LogActiveSceneData(bool logAllSceneObjectsData)
{
this.LogSceneData(SceneManager.GetActiveScene(), logAllSceneObjectsData);
}
/// <summary>
/// Logs the name of a GameObject as well as the name of its parent, the second
/// parameter specifies whether the same should be done for the children of the
/// GameObject specified in the first parameter.
/// </summary>
/// <param name="gameObject"></param>
/// <param name="logChildObjects"></param>
public void LogGameObjectNameAndParent(GameObject gameObject, bool logChildObjects)
{
this.logger.LogInfo("Current GameObject: " + gameObject);
this.logger.LogInfo("Current GameObject name: " + gameObject.name);
if (gameObject.transform.parent != null)
{
this.logger.LogInfo("Current GameObject parent: " + gameObject.transform.parent.gameObject);
}
else
{
this.logger.LogInfo("This GameObject does not have a parent.");
}
if (logChildObjects == true)
{
for (int currentChildIndex = 0; currentChildIndex < gameObject.transform.childCount; currentChildIndex++)
{
this.LogGameObjectNameAndParent(gameObject.transform.GetChild(currentChildIndex).gameObject, logChildObjects);
}
}
}
/// <summary>
/// Logs the names of all the GameObjects in a Scene in the game as well as the names
/// of their parents.
/// </summary>
/// <param name="scene"></param>
public void LogSceneGameObjectsNameAndParent(Scene scene)
{
foreach (GameObject rootGameObject in scene.GetRootGameObjects())
{
this.LogGameObjectNameAndParent(rootGameObject, true);
}
}
/// <summary>
/// Logs the names of all the GameObjects in the currently active Scene in the game
/// as well as the names of their parents.
/// </summary>
public void LogActiveSceneGameObjectsNameAndParent()
{
this.LogSceneGameObjectsNameAndParent(SceneManager.GetActiveScene());
}
/// <summary>
/// Logs the names of all the folders and files in a FileSystem instance.
/// </summary>
/// <param name="fileSystem"></param>
public void LogFileSystem(FileSystem fileSystem)
{
FileSystem.Carpeta rootFolder = fileSystem.GetCarpetaRaiz();
this.LogInfo("Folder name: " + rootFolder.nombre);
foreach (FileSystem.Carpeta rootSubFolder in rootFolder.GetCarpetas())
{
this.LogFolder(rootSubFolder);
}
}
/// <summary>
/// Logs the name of the FileSystem.Carpeta instance specified in the first parameter
/// as well as the name of all its subfolders and files.
/// </summary>
/// <param name="folder"></param>
public void LogFolder(FileSystem.Carpeta folder)
{
this.LogInfo("Folder name: " + folder.nombre);
foreach (FileSystem.Archivo file in folder.GetArchivos())
{
this.LogInfo("File name: " + file.nombre);
}
foreach (FileSystem.Carpeta subFolder in folder.GetCarpetas())
{
this.LogFolder(subFolder);
}
}
/// <summary>
/// Logs information about an exception, including its message and its stack trace.
/// </summary>
/// <param name="exception"></param>
public void LogException(Exception exception)
{
this.LogInfo("Exception occurred.");
this.LogInfo("Exception message: " + exception.Message);
this.LogInfo("Exception stack trace:");
this.LogInfo(StackTraceUtility.ExtractStackTrace());
}
}
}