/
MMPatchRunner.cs
125 lines (99 loc) · 4.47 KB
/
MMPatchRunner.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using ModuleManager.Collections;
using ModuleManager.Extensions;
using ModuleManager.Logging;
using ModuleManager.Threading;
namespace ModuleManager
{
public class MMPatchRunner
{
private const float TIME_TO_WAIT_FOR_LOGS = 0.05f;
private readonly IBasicLogger kspLogger;
public string Status { get; private set; } = "";
public string Errors { get; private set; } = "";
public MMPatchRunner(IBasicLogger kspLogger)
{
this.kspLogger = kspLogger ?? throw new ArgumentNullException(nameof(kspLogger));
}
public IEnumerator Run()
{
PostPatchLoader.Instance.databaseConfigs = null;
string logsDirPath = Path.Combine(KSPUtil.ApplicationRootPath, "Logs");
if (!Directory.Exists(logsDirPath)) Directory.CreateDirectory(logsDirPath);
string logPath = Path.Combine(logsDirPath, "ModuleManager.log");
kspLogger.Info("Patching started on a new thread, all output will be directed to " + logPath);
MessageQueue<ILogMessage> kspLogQueue = new MessageQueue<ILogMessage>();
MessageQueue<ILogMessage> mmLogQueue = new MessageQueue<ILogMessage>();
bool logThreadExitFlag = false;
ITaskStatus loggingThreadStatus = BackgroundTask.Start(delegate
{
QueueLogger kspLogger = new QueueLogger(kspLogQueue);
using (StreamLogger streamLogger = new StreamLogger(new FileStream(logPath, FileMode.Create), kspLogger))
{
while (!logThreadExitFlag)
{
float waitTargetTime = Time.realtimeSinceStartup + TIME_TO_WAIT_FOR_LOGS;
foreach (ILogMessage message in mmLogQueue.TakeAll())
{
message.LogTo(streamLogger);
}
float timeRemaining = waitTargetTime - Time.realtimeSinceStartup;
if (timeRemaining > 0)
System.Threading.Thread.Sleep((int)(timeRemaining * 1000));
}
foreach (ILogMessage message in mmLogQueue.TakeAll())
{
message.LogTo(streamLogger);
}
streamLogger.Info("Done!");
}
});
// Wait for game database to be initialized for the 2nd time
yield return null;
IEnumerable<IProtoUrlConfig> databaseConfigs = null;
MMPatchLoader patchLoader = new MMPatchLoader(new QueueLogger(mmLogQueue));
ITaskStatus patchingThreadStatus = BackgroundTask.Start(delegate
{
databaseConfigs = patchLoader.Run();
});
while(true)
{
yield return null;
if (!patchingThreadStatus.IsRunning)
logThreadExitFlag = true;
Status = patchLoader.status;
Errors = patchLoader.errors;
foreach (ILogMessage message in kspLogQueue.TakeAll())
{
message.LogTo(kspLogger);
}
if (!patchingThreadStatus.IsRunning && !loggingThreadStatus.IsRunning) break;
}
foreach (ILogMessage message in kspLogQueue.TakeAll())
{
message.LogTo(kspLogger);
}
if (patchingThreadStatus.IsExitedWithError)
{
kspLogger.Exception("The patching thread threw an exception", patchingThreadStatus.Exception);
FatalErrorHandler.HandleFatalError("The patching thread threw an exception");
}
if (loggingThreadStatus.IsExitedWithError)
{
kspLogger.Exception("The logging thread threw an exception", loggingThreadStatus.Exception);
FatalErrorHandler.HandleFatalError("The logging thread threw an exception");
}
if (databaseConfigs == null)
{
kspLogger.Error("The patcher returned a null collection of configs");
FatalErrorHandler.HandleFatalError("The patcher returned a null collection of configs");
yield break;
}
PostPatchLoader.Instance.databaseConfigs = databaseConfigs;
}
}
}