Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TimeLogger.cs
Go to the documentation of this file.
1using System;
3using System.IO;
5using System.Text;
6
7namespace Terraria;
8
9public static class TimeLogger
10{
11 private struct TimeLogData
12 {
13 public float time;
14
15 public float timeMax;
16
17 public int resetMaxTime;
18
19 public bool usedLastDraw;
20
21 public string logText;
22 }
23
24 private static StreamWriter logWriter;
25
26 private static StringBuilder logBuilder;
27
28 private static int framesToLog;
29
30 private static int currentFrame;
31
32 private static bool startLoggingNextFrame;
33
34 private static bool endLoggingThisFrame;
35
36 private static bool currentlyLogging;
37
39
40 private static double lastDetailedDrawTime;
41
42 private static TimeLogData[] renderTimes;
43
44 private static TimeLogData[] drawTimes;
45
46 private static TimeLogData[] lightingTimes;
47
49
50 private const int maxTimeDelay = 100;
51
52 public static void Initialize()
53 {
54 currentFrame = 0;
55 framesToLog = -1;
57 renderTimes = new TimeLogData[10];
58 drawTimes = new TimeLogData[10];
61 for (int i = 0; i < renderTimes.Length; i++)
62 {
63 renderTimes[i].logText = $"Render #{i}";
64 }
65 drawTimes[0].logText = "Drawing Solid Tiles";
66 drawTimes[1].logText = "Drawing Non-Solid Tiles";
67 drawTimes[2].logText = "Drawing Wall Tiles";
68 drawTimes[3].logText = "Drawing Underground Background";
69 drawTimes[4].logText = "Drawing Water Tiles";
70 drawTimes[5].logText = "Drawing Black Tiles";
71 lightingTimes[0].logText = "Lighting Initialization";
72 for (int j = 1; j < lightingTimes.Length; j++)
73 {
74 lightingTimes[j].logText = $"Lighting Pass #{j}";
75 }
76 detailedDrawTimes[0].logText = "Finding color tiles";
77 detailedDrawTimes[1].logText = "Initial Map Update";
78 detailedDrawTimes[2].logText = "Finding Waterfalls";
79 detailedDrawTimes[3].logText = "Map Section Update";
80 detailedDrawTimes[4].logText = "Map Update";
81 detailedDrawTimes[5].logText = "Section Framing";
82 detailedDrawTimes[6].logText = "Sky Background";
83 detailedDrawTimes[7].logText = "Sun, Moon & Stars";
84 detailedDrawTimes[8].logText = "Surface Background";
85 detailedDrawTimes[9].logText = "Map";
86 detailedDrawTimes[10].logText = "Player Chat";
87 detailedDrawTimes[11].logText = "Water Target";
88 detailedDrawTimes[12].logText = "Background Target";
89 detailedDrawTimes[13].logText = "Black Tile Target";
90 detailedDrawTimes[14].logText = "Wall Target";
91 detailedDrawTimes[15].logText = "Non Solid Tile Target";
92 detailedDrawTimes[16].logText = "Waterfalls";
93 detailedDrawTimes[17].logText = "Solid Tile Target";
94 detailedDrawTimes[18].logText = "NPCs (Behind Tiles)";
95 detailedDrawTimes[19].logText = "NPC";
96 detailedDrawTimes[20].logText = "Projectiles";
97 detailedDrawTimes[21].logText = "Players";
98 detailedDrawTimes[22].logText = "Items";
99 detailedDrawTimes[23].logText = "Rain";
100 detailedDrawTimes[24].logText = "Gore";
101 detailedDrawTimes[25].logText = "Dust";
102 detailedDrawTimes[26].logText = "Water Target";
103 detailedDrawTimes[27].logText = "Interface";
104 detailedDrawTimes[28].logText = "Render Solid Tiles";
105 detailedDrawTimes[29].logText = "Render Non Solid Tiles";
106 detailedDrawTimes[30].logText = "Render Black Tiles";
107 detailedDrawTimes[31].logText = "Render Water/Wires";
108 detailedDrawTimes[32].logText = "Render Walls";
109 detailedDrawTimes[33].logText = "Render Backgrounds";
110 detailedDrawTimes[34].logText = "Drawing Wires";
111 detailedDrawTimes[35].logText = "Render layers up to Players";
112 detailedDrawTimes[36].logText = "Render Items/Rain/Gore/Dust/Water/Map";
113 detailedDrawTimes[37].logText = "Render Interface";
114 for (int k = 0; k < detailedDrawTimes.Length; k++)
115 {
116 if (string.IsNullOrEmpty(detailedDrawTimes[k].logText))
117 {
118 detailedDrawTimes[k].logText = $"Unnamed detailed draw #{k}";
119 }
120 }
121 }
122
123 public static void Start()
124 {
126 {
127 endLoggingThisFrame = true;
128 startLoggingNextFrame = false;
129 }
130 else
131 {
133 endLoggingThisFrame = false;
134 Main.NewText("Detailed logging started", 250, 250, 0);
135 }
136 }
137
138 public static void NewDrawFrame()
139 {
140 for (int i = 0; i < renderTimes.Length; i++)
141 {
142 renderTimes[i].usedLastDraw = false;
143 }
144 for (int j = 0; j < drawTimes.Length; j++)
145 {
146 drawTimes[j].usedLastDraw = false;
147 }
148 for (int k = 0; k < lightingTimes.Length; k++)
149 {
150 lightingTimes[k].usedLastDraw = false;
151 }
153 {
154 startLoggingNextFrame = false;
155 _ = DateTime.Now;
156 string path = Main.SavePath + Path.DirectorySeparatorChar + "TerrariaDrawLog.7z";
157 try
158 {
159 logWriter = new StreamWriter(new GZipStream(new FileStream(path, FileMode.Create), CompressionMode.Compress));
160 logBuilder = new StringBuilder(5000);
161 framesToLog = 600;
162 currentFrame = 1;
163 currentlyLogging = true;
164 }
165 catch
166 {
167 Main.NewText("Detailed logging could not be started.", 250, 250, 0);
168 }
169 }
171 {
172 logBuilder.AppendLine($"Start of Frame #{currentFrame}");
173 }
174 detailedDrawTimer.Restart();
175 lastDetailedDrawTime = detailedDrawTimer.Elapsed.TotalMilliseconds;
176 }
177
178 public static void EndDrawFrame()
179 {
181 {
182 logBuilder.AppendLine($"End of Frame #{currentFrame}");
183 logBuilder.AppendLine();
185 {
186 endLoggingThisFrame = false;
187 logBuilder.AppendLine("Logging ended early");
189 }
190 if (logBuilder.Length > 4000)
191 {
192 logWriter.Write(logBuilder.ToString());
193 logBuilder.Clear();
194 }
195 currentFrame++;
197 {
198 Main.NewText("Detailed logging ended.", 250, 250, 0);
199 logWriter.Write(logBuilder.ToString());
200 logBuilder.Clear();
201 logBuilder = null;
202 logWriter.Flush();
203 logWriter.Close();
204 logWriter = null;
205 framesToLog = -1;
206 currentFrame = 0;
207 currentlyLogging = false;
208 }
209 }
210 detailedDrawTimer.Stop();
211 }
212
213 private static void UpdateTime(TimeLogData[] times, int type, double time)
214 {
215 bool flag = false;
216 if (times[type].resetMaxTime > 0)
217 {
218 times[type].resetMaxTime--;
219 }
220 else
221 {
222 times[type].timeMax = 0f;
223 }
224 times[type].time = (float)time;
225 if ((double)times[type].timeMax < time)
226 {
227 flag = true;
228 times[type].timeMax = (float)time;
229 times[type].resetMaxTime = 100;
230 }
231 times[type].usedLastDraw = true;
232 if (currentFrame != 0)
233 {
234 logBuilder.AppendLine(string.Format(" {0} : {1:F4}ms {2}", times[type].logText, time, flag ? " - New Maximum" : string.Empty));
235 }
236 }
237
238 public static void RenderTime(int renderType, double timeElapsed)
239 {
240 if (renderType >= 0 && renderType < renderTimes.Length)
241 {
242 UpdateTime(renderTimes, renderType, timeElapsed);
243 }
244 }
245
246 public static float GetRenderTime(int renderType)
247 {
248 return renderTimes[renderType].time;
249 }
250
251 public static float GetRenderMax(int renderType)
252 {
253 return renderTimes[renderType].timeMax;
254 }
255
256 public static void DrawTime(int drawType, double timeElapsed)
257 {
258 if (drawType >= 0 && drawType < drawTimes.Length)
259 {
260 UpdateTime(drawTimes, drawType, timeElapsed);
261 }
262 }
263
264 public static float GetDrawTime(int drawType)
265 {
266 return drawTimes[drawType].time;
267 }
268
269 public static float GetDrawTotal()
270 {
271 float num = 0f;
272 for (int i = 0; i < drawTimes.Length; i++)
273 {
274 num += drawTimes[i].time;
275 }
276 return num;
277 }
278
279 public static void LightingTime(int lightingType, double timeElapsed)
280 {
281 if (lightingType >= 0 && lightingType < lightingTimes.Length)
282 {
283 UpdateTime(lightingTimes, lightingType, timeElapsed);
284 }
285 }
286
287 public static float GetLightingTime(int lightingType)
288 {
289 return lightingTimes[lightingType].time;
290 }
291
292 public static float GetLightingTotal()
293 {
294 float num = 0f;
295 for (int i = 0; i < lightingTimes.Length; i++)
296 {
297 num += lightingTimes[i].time;
298 }
299 return num;
300 }
301
302 public static void DetailedDrawReset()
303 {
304 lastDetailedDrawTime = detailedDrawTimer.Elapsed.TotalMilliseconds;
305 }
306
307 public static void DetailedDrawTime(int detailedDrawType)
308 {
309 if (detailedDrawType >= 0 && detailedDrawType < detailedDrawTimes.Length)
310 {
311 double totalMilliseconds = detailedDrawTimer.Elapsed.TotalMilliseconds;
312 double time = totalMilliseconds - lastDetailedDrawTime;
313 lastDetailedDrawTime = totalMilliseconds;
314 UpdateTime(detailedDrawTimes, detailedDrawType, time);
315 }
316 }
317
318 public static float GetDetailedDrawTime(int detailedDrawType)
319 {
320 return detailedDrawTimes[detailedDrawType].time;
321 }
322
323 public static float GetDetailedDrawTotal()
324 {
325 float num = 0f;
326 for (int i = 0; i < detailedDrawTimes.Length; i++)
327 {
328 if (detailedDrawTimes[i].usedLastDraw)
329 {
330 num += detailedDrawTimes[i].time;
331 }
332 }
333 return num;
334 }
335
336 public static void MenuDrawTime(double timeElapsed)
337 {
339 {
340 logBuilder.AppendLine($"Menu Render Time : {timeElapsed:F4}");
341 }
342 }
343
344 public static void SplashDrawTime(double timeElapsed)
345 {
347 {
348 logBuilder.AppendLine($"Splash Render Time : {timeElapsed:F4}");
349 }
350 }
351
352 public static void MapDrawTime(double timeElapsed)
353 {
355 {
356 logBuilder.AppendLine($"Full Screen Map Render Time : {timeElapsed:F4}");
357 }
358 }
359
360 public static void DrawException(Exception e)
361 {
363 {
364 logBuilder.AppendLine(e.ToString());
365 }
366 }
367}
override string ToString()
Definition Exception.cs:384
static void NewText(string newText, byte R=byte.MaxValue, byte G=byte.MaxValue, byte B=byte.MaxValue)
Definition Main.cs:61429
static float GetRenderMax(int renderType)
static float GetDrawTime(int drawType)
static float GetDetailedDrawTotal()
static TimeLogData[] detailedDrawTimes
Definition TimeLogger.cs:48
static void Initialize()
Definition TimeLogger.cs:52
static void EndDrawFrame()
static void MapDrawTime(double timeElapsed)
static void DetailedDrawReset()
static float GetDrawTotal()
static void DetailedDrawTime(int detailedDrawType)
static void UpdateTime(TimeLogData[] times, int type, double time)
static StringBuilder logBuilder
Definition TimeLogger.cs:26
static float GetDetailedDrawTime(int detailedDrawType)
static float GetLightingTime(int lightingType)
static void LightingTime(int lightingType, double timeElapsed)
static void NewDrawFrame()
static bool currentlyLogging
Definition TimeLogger.cs:36
static int framesToLog
Definition TimeLogger.cs:28
static void RenderTime(int renderType, double timeElapsed)
static float GetLightingTotal()
static void DrawTime(int drawType, double timeElapsed)
static StreamWriter logWriter
Definition TimeLogger.cs:24
static bool startLoggingNextFrame
Definition TimeLogger.cs:32
static void MenuDrawTime(double timeElapsed)
static bool endLoggingThisFrame
Definition TimeLogger.cs:34
static float GetRenderTime(int renderType)
static TimeLogData[] lightingTimes
Definition TimeLogger.cs:46
static void Start()
static void SplashDrawTime(double timeElapsed)
static double lastDetailedDrawTime
Definition TimeLogger.cs:40
static Stopwatch detailedDrawTimer
Definition TimeLogger.cs:38
static TimeLogData[] drawTimes
Definition TimeLogger.cs:44
static int currentFrame
Definition TimeLogger.cs:30
static TimeLogData[] renderTimes
Definition TimeLogger.cs:42
static void DrawException(Exception e)
const int maxTimeDelay
Definition TimeLogger.cs:50
static DateTime Now
Definition DateTime.cs:103