-
Notifications
You must be signed in to change notification settings - Fork 0
/
KTANEComponent.cs
259 lines (244 loc) · 13.1 KB
/
KTANEComponent.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
248
249
250
251
252
253
254
255
256
257
258
259
using LiveSplit.Model;
using LiveSplit.UI;
using LiveSplit.UI.Components;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace LiveSplit.KTANE {
public class KTANEComponent : IComponent {
private static List<string> missions = new List<string>() { "firsttime", "somethingoldsomethingnew", "doubleyourmoney", "onestepup", "pickupthepace", "ahiddenmessage", "somethingsdifferent", "giantleap", "fairgame", "pickupthepacetwo", "noroomforerror", "eightminutes", "asmallwrinkle", "payattention", "theknob", "multitasker", "wireseverywhere", "rtfm", "whosonfirst", "fiendish", "pickupthepacethree", "onewitheverything", "pickupthepacefour", "juggler", "doubletrouble", "iamhardcore", "blinkenlights", "appliedtheory", "amazing", "snipsnap", "rainbowtable", "blinkenlightstwo" };
public string ComponentName { get { return "Keep Talking and Nobody Explodes Autosplitter"; } }
protected TimerModel Model { get; set; }
public IDictionary<string, Action> ContextMenuControls { get { return null; } }
private static string BestTimes = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low", @"Steel Crate Games\Keep Talking and Nobody Explodes\best_times.xml");
private static string Progression = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low", @"Steel Crate Games\Keep Talking and Nobody Explodes\progression.xml");
private static string LogFile = null;
private int currentSplit = 0;
private decimal bestTimeRTA, bestTime;
private DateTime startOfBomb, endOfBomb;
private List<LogLine> logLines = new List<LogLine>();
private long lastPosition = 0;
private InfoTextComponent textInfo;
private Process KTANE;
public KTANEComponent() {
textInfo = new InfoTextComponent("Best Time", "");
textInfo.LongestString = "Best Time 123.456 / 123.456";
textInfo.InformationName = "Best Time";
}
private bool GetKTANE() {
if (KTANE == null || KTANE.HasExited) {
if (KTANE != null) {
if (MessageBox.Show("Do you want to reset progression?", "Progression", MessageBoxButtons.YesNo) == DialogResult.Yes) {
File.WriteAllText(Progression, Properties.Resources.progression, Encoding.UTF8);
}
KTANE = null;
}
Process[] ktanes = Process.GetProcessesByName("ktane");
if (ktanes.Length > 0) {
KTANE = ktanes[0];
LogFile = Path.Combine(Path.GetDirectoryName(KTANE.MainModule.FileName), @"logs\ktane.log");
ReadData();
return true;
}
}
return KTANE != null;
}
private void GetValues(IInvalidator invalidator, LiveSplitState lvstate, float width, float height, LayoutMode mode) {
if (!GetKTANE()) { return; }
if (Model != null && Model.CurrentState != null) {
ReadData();
foreach (LogLine line in logLines) {
if (Model.CurrentState.CurrentPhase != TimerPhase.Running && line.Message.Equals("[BombGenerator] Generator settings: Time: 300, NumStrikes: 3, FrontFaceOnly: True\r3 Pools:\r[Wires] Count: 1\r[BigButton] Count: 1\r[Keypad] Count: 1")) {
Model.Start();
continue;
}
if (currentSplit <= 0 || currentSplit >= 33 || Model.CurrentState.CurrentPhase != TimerPhase.Running || line.Time < DateTime.Now - Model.CurrentState.CurrentTime.RealTime.Value) {
continue;
}
if (line.Message.IndexOf("[Bomb] A winner is you!!") >= 0) {
if (Model.CurrentState.Run.Count < 8) {
switch (currentSplit) {
case 1:
case 5:
case 12:
case 16:
case 22:
case 26:
case 32:
Model.Split();
break;
default:
currentSplit++;
break;
}
} else {
Model.Split();
}
endOfBomb = line.Time;
} else if (line.Message.IndexOf("[BombGenerator] Generator settings: Time: ") >= 0) {
int index = line.Message.IndexOf("Time: ") + 6;
bestTime = decimal.Parse(line.Message.Substring(index, line.Message.IndexOf(",", index) - index));
} else if (line.Message.IndexOf("[BombGenerator] Generating Widgets") >= 0) {
startOfBomb = line.Time;
endOfBomb = DateTime.MinValue;
XDocument x = XDocument.Load(BestTimes);
var xmlList = x.Descendants("dictionary").Elements().ToList();
if (xmlList.Count >= currentSplit) {
foreach (XElement ele in xmlList) {
if (ele.Element("key").Element("string").Value == missions[currentSplit - 1]) {
bestTimeRTA = decimal.Parse(ele.Element("value").Element("GameRecord").Element("RealTimeElapsed").Value);
bestTime -= decimal.Parse(ele.Element("value").Element("GameRecord").Element("TimeElapsed").Value);
break;
}
}
} else {
bestTimeRTA = 0;
}
} else if (line.Message.IndexOf("[Bomb] Boom") >= 0) {
startOfBomb = DateTime.MinValue;
endOfBomb = DateTime.MinValue;
}
}
}
//textInfo.InformationName = "Best Time" + (bestTime > 0 ? " (" + TimeSpan.FromSeconds((double)bestTime).ToString(@"m\:") + (bestTime % 60).ToString("00.000") + ")" : "");
textInfo.InformationName = "Best Time";
textInfo.InformationValue = (startOfBomb > DateTime.MinValue ? ((endOfBomb > DateTime.MinValue ? endOfBomb : DateTime.Now) - startOfBomb).TotalSeconds.ToString("0.000") : "0.000") + " / " + bestTimeRTA.ToString("0.000");
textInfo.Update(invalidator, lvstate, width, height, mode);
if (invalidator != null) {
invalidator.Invalidate(0, 0, width, height);
}
}
private void ReadData() {
try {
string text = null;
using (FileStream fs = File.Open(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
if (fs.Length < lastPosition) {
lastPosition = 0;
} else {
fs.Seek(lastPosition, SeekOrigin.Begin);
}
text = GetText(fs, lastPosition);
}
lastPosition = GenerateData(text, lastPosition);
} catch {
}
}
private string GetText(FileStream fs, long position) {
long currentLength = fs.Length;
byte[] data = new byte[currentLength - position];
fs.Read(data, 0, data.Length);
return Encoding.UTF8.GetString(data);
}
private long GenerateData(string text, long lastPosition) {
logLines.Clear();
int index = text.IndexOf('\x0A');
int lastIndex = 0;
while (index >= 0) {
string line = text.Substring(lastIndex, index - lastIndex);
if (line.Length > 30) {
string lineType = line.Substring(0, 5);
if (lineType == "DEBUG" || lineType == " WARN" || lineType == " INFO") {
try {
DateTime time = DateTime.Parse(line.Substring(6, 23).Replace(',', '.')).ToLocalTime();
string message = line.Substring(30);
logLines.Add(new LogLine() { Message = message, Time = time });
} catch { }
}
} else if (logLines.Count > 0 && !string.IsNullOrWhiteSpace(line)) {
logLines[logLines.Count - 1].Message = string.Concat(logLines[logLines.Count - 1].Message, "\r", line);
}
lastIndex = index + 1;
index = text.IndexOf('\x0A', lastIndex);
}
return lastPosition + lastIndex;
}
public void Update(IInvalidator invalidator, LiveSplitState lvstate, float width, float height, LayoutMode mode) {
if (Model == null) {
Model = new TimerModel() { CurrentState = lvstate };
lvstate.OnReset += OnReset;
lvstate.OnPause += OnPause;
lvstate.OnResume += OnResume;
lvstate.OnStart += OnStart;
lvstate.OnSplit += OnSplit;
lvstate.OnUndoSplit += OnUndoSplit;
lvstate.OnSkipSplit += OnSkipSplit;
}
GetValues(invalidator, lvstate, width, height, mode);
}
public void OnReset(object sender, TimerPhase e) {
currentSplit = 0;
startOfBomb = DateTime.MinValue;
endOfBomb = DateTime.MinValue;
bestTimeRTA = 0;
bestTime = 0;
}
public void OnResume(object sender, EventArgs e) {
}
public void OnPause(object sender, EventArgs e) {
}
public void OnStart(object sender, EventArgs e) {
currentSplit++;
}
public void OnUndoSplit(object sender, EventArgs e) {
currentSplit--;
bestTimeRTA = 0;
bestTime = 0;
}
public void OnSkipSplit(object sender, EventArgs e) {
currentSplit++;
}
public void OnSplit(object sender, EventArgs e) {
currentSplit++;
}
public Control GetSettingsControl(LayoutMode mode) { return null; }
public void SetSettings(XmlNode settings) { }
public XmlNode GetSettings(XmlDocument document) { return null; }
public void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region clipRegion) {
if (state.LayoutSettings.BackgroundColor.ToArgb() != Color.Transparent.ToArgb()) {
g.FillRectangle(new SolidBrush(state.LayoutSettings.BackgroundColor), 0, 0, HorizontalWidth, height);
}
PrepareDraw(state, LayoutMode.Horizontal);
textInfo.DrawHorizontal(g, state, height, clipRegion);
}
public void DrawVertical(Graphics g, LiveSplitState state, float width, Region clipRegion) {
if (state.LayoutSettings.BackgroundColor.ToArgb() != Color.Transparent.ToArgb()) {
g.FillRectangle(new SolidBrush(state.LayoutSettings.BackgroundColor), 0, 0, width, VerticalHeight);
}
PrepareDraw(state, LayoutMode.Vertical);
textInfo.DrawVertical(g, state, width, clipRegion);
}
private void PrepareDraw(LiveSplitState state, LayoutMode mode) {
textInfo.DisplayTwoRows = false;
textInfo.NameLabel.HasShadow = textInfo.ValueLabel.HasShadow = state.LayoutSettings.DropShadows;
textInfo.NameLabel.HorizontalAlignment = StringAlignment.Near;
textInfo.ValueLabel.HorizontalAlignment = StringAlignment.Far;
textInfo.NameLabel.VerticalAlignment = StringAlignment.Near;
textInfo.ValueLabel.VerticalAlignment = StringAlignment.Far;
textInfo.NameLabel.ForeColor = state.LayoutSettings.TextColor;
textInfo.ValueLabel.ForeColor = state.LayoutSettings.TextColor;
}
public float HorizontalWidth { get { return textInfo.HorizontalWidth; } }
public float VerticalHeight { get { return textInfo.VerticalHeight; } }
public float MinimumHeight { get { return textInfo.MinimumHeight; } }
public float MinimumWidth { get { return textInfo.MinimumWidth; } }
public float PaddingBottom { get { return textInfo.PaddingBottom; } }
public float PaddingLeft { get { return textInfo.PaddingLeft; } }
public float PaddingRight { get { return textInfo.PaddingRight; } }
public float PaddingTop { get { return textInfo.PaddingTop; } }
public void Dispose() { }
}
public class LogLine {
public DateTime Time;
public string Message;
public override string ToString() {
return Time.ToLongDateString() + " - " + Message;
}
}
}