forked from fatalis/SourceSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceSplitComponent.cs
434 lines (362 loc) · 16.1 KB
/
SourceSplitComponent.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using System.Diagnostics;
using LiveSplit.Model;
using LiveSplit.Options;
using LiveSplit.TimeFormatters;
using LiveSplit.UI.Components;
using LiveSplit.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Xml;
using System.Windows.Forms;
namespace LiveSplit.SourceSplit
{
class SourceSplitComponent : IComponent
{
public string ComponentName => "SourceSplit";
public SourceSplitSettings Settings { get; set; }
public IDictionary<string, Action> ContextMenuControls { get; protected set; }
protected InfoTimeComponent InternalComponent { get; set; }
public bool Disposed { get; private set; }
public bool IsLayoutComponent { get; private set; }
private TimerModel _timer;
private GraphicsCache _cache;
private GameMemory _gameMemory;
private float _intervalPerTick;
private int _sessionTicks;
private int _totalMapTicks;
private int _totalTicks;
private int _sessionTicksOffset;
private DateTime? _gamePauseTime;
private int _gamePauseTick;
private GameTimingMethod _gameRecommendedTimingMethod;
private bool _waitingForDelay;
private string _currentMap = String.Empty;
private int _splitCount;
private List<string> _mapsVisited;
private GameTimingMethod GameTimingMethod
{
get
{
switch (this.Settings.GameTimingMethod)
{
case GameTimingMethodSetting.EngineTicks:
return GameTimingMethod.EngineTicks;
case GameTimingMethodSetting.EngineTicksWithPauses:
return GameTimingMethod.EngineTicksWithPauses;
default:
return _gameRecommendedTimingMethod;
}
}
}
private TimeSpan GameTime
{
get
{
if (_gamePauseTime != null && this.GameTimingMethod == GameTimingMethod.EngineTicksWithPauses)
{
return TimeSpan.FromSeconds((_totalTicks + _gamePauseTick + FakeTicks(_gamePauseTime.Value, DateTime.Now) - _sessionTicksOffset) * _intervalPerTick);
}
else
{
return TimeSpan.FromSeconds((_totalTicks + _sessionTicks - _sessionTicksOffset) * _intervalPerTick);
}
}
}
public SourceSplitComponent(LiveSplitState state, bool isLayoutComponent)
{
#if DEBUG
// make Debug.WriteLine prepend update count and tick count
Debug.Listeners.Clear();
Debug.Listeners.Add(TimedTraceListener.Instance);
Trace.Listeners.Clear();
Trace.Listeners.Add(TimedTraceListener.Instance);
#endif
this.IsLayoutComponent = isLayoutComponent;
this.Settings = new SourceSplitSettings();
this.InternalComponent = new InfoTimeComponent("Game Time", null, new RegularTimeFormatter(TimeAccuracy.Hundredths));
this.ContextMenuControls = new Dictionary<String, Action>();
this.ContextMenuControls.Add("SourceSplit: Map Times", () => MapTimesForm.Instance.Show());
_cache = new GraphicsCache();
_timer = new TimerModel { CurrentState = state };
state.OnSplit += state_OnSplit;
state.OnReset += state_OnReset;
state.OnStart += state_OnStart;
_mapsVisited = new List<string>();
_intervalPerTick = 0.015f; // will update these when attached to game
_gameRecommendedTimingMethod = GameTimingMethod.EngineTicks;
_gameMemory = new GameMemory(this.Settings);
_gameMemory.OnSetTickRate += gameMemory_OnSetTickRate;
_gameMemory.OnSetTimingMethod += gameMemory_OnSetTimingMethod;
_gameMemory.OnSessionTimeUpdate += gameMemory_OnSessionTimeUpdate;
_gameMemory.OnPlayerGainedControl += gameMemory_OnPlayerGainedControl;
_gameMemory.OnPlayerLostControl += gameMemory_OnPlayerLostControl;
_gameMemory.OnMapChanged += gameMemory_OnMapChanged;
_gameMemory.OnSessionStarted += gameMemory_OnSessionStarted;
_gameMemory.OnSessionEnded += gameMemory_OnSessionEnded;
_gameMemory.OnNewGameStarted += gameMemory_OnNewGameStarted;
_gameMemory.OnGamePaused += gameMemory_OnGamePaused;
_gameMemory.StartReading();
}
#if DEBUG
~SourceSplitComponent()
{
Debug.WriteLine("SourceSplitComponent finalizer");
}
#endif
public void Dispose()
{
this.Disposed = true;
_timer.CurrentState.OnSplit -= state_OnSplit;
_timer.CurrentState.OnReset -= state_OnReset;
_timer.CurrentState.OnStart -= state_OnStart;
_timer.CurrentState.IsGameTimePaused = false; // hack
_timer.CurrentState.LoadingTimes = TimeSpan.Zero;
_gameMemory?.Stop();
}
public void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
// hack to prevent flicker, doesn't actually pause anything
state.IsGameTimePaused = true;
// Update is called every 25ms, so up to 25ms IGT can be lost if using delay and no auto-start
if (_waitingForDelay)
{
if (state.CurrentTime.RealTime >= TimeSpan.Zero)
{
_sessionTicksOffset = _sessionTicks;
_waitingForDelay = false;
}
else
{
state.SetGameTime(state.CurrentTime.RealTime);
}
}
if (!_waitingForDelay)
// update game time, don't show negative time due to tick adjusting
state.SetGameTime(this.GameTime >= TimeSpan.Zero ? this.GameTime : TimeSpan.Zero);
if (!this.Settings.ShowGameTime)
return;
this.InternalComponent.TimeValue =
state.CurrentTime[state.CurrentTimingMethod == TimingMethod.GameTime
? TimingMethod.RealTime : TimingMethod.GameTime];
this.InternalComponent.InformationName = state.CurrentTimingMethod == TimingMethod.GameTime
? "Real Time" : "Game Time";
_cache.Restart();
_cache["TimeValue"] = this.InternalComponent.ValueLabel.Text;
_cache["TimingMethod"] = state.CurrentTimingMethod;
if (invalidator != null && _cache.HasChanged)
invalidator.Invalidate(0f, 0f, width, height);
}
public void DrawVertical(Graphics g, LiveSplitState state, float width, Region region)
{
this.PrepareDraw(state);
this.InternalComponent.DrawVertical(g, state, width, region);
}
public void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region region)
{
this.PrepareDraw(state);
this.InternalComponent.DrawHorizontal(g, state, height, region);
}
void PrepareDraw(LiveSplitState state)
{
this.InternalComponent.NameLabel.ForeColor = state.LayoutSettings.TextColor;
this.InternalComponent.ValueLabel.ForeColor = state.LayoutSettings.TextColor;
this.InternalComponent.NameLabel.HasShadow = this.InternalComponent.ValueLabel.HasShadow = state.LayoutSettings.DropShadows;
}
void state_OnStart(object sender, EventArgs e)
{
_timer.InitializeGameTime();
_totalTicks = 0;
_mapsVisited.Clear();
MapTimesForm.Instance.Reset();
_splitCount = 0;
_totalMapTicks = 0;
_gamePauseTime = null;
// hack to make sure Portal players aren't using manual offset. we handle offset automatically now.
// remove this eventually
if (_timer.CurrentState.TimePausedAt.Seconds == 53 && _timer.CurrentState.TimePausedAt.Milliseconds == 10)
{
_timer.CurrentState.TimePausedAt = TimeSpan.Zero;
_timer.CurrentState.Run.Offset = TimeSpan.Zero;
}
if (_timer.CurrentState.TimePausedAt >= TimeSpan.Zero)
_sessionTicksOffset = _sessionTicks - (int)(_timer.CurrentState.TimePausedAt.TotalSeconds / _intervalPerTick);
else
_waitingForDelay = true;
}
void state_OnReset(object sender, TimerPhase t)
{
MapTimesForm.Instance.Reset();
_waitingForDelay = false;
}
void state_OnSplit(object sender, EventArgs e)
{
Debug.WriteLine("split at time " + this.GameTime);
if (_timer.CurrentState.CurrentPhase == TimerPhase.Ended)
{
this.AddMapTime(_currentMap, TimeSpan.FromSeconds(_totalMapTicks + _sessionTicks - _sessionTicksOffset));
this.AddMapTime("-Total-", this.GameTime);
}
}
// first tick when player is fully in game
void gameMemory_OnSessionStarted(object sender, SessionStartedEventArgs e)
{
_currentMap = e.Map;
}
void gameMemory_OnSetTickRate(object sender, SetTickRateEventArgs e)
{
Debug.WriteLine("tickrate " + e.IntervalPerTick);
_intervalPerTick = e.IntervalPerTick;
}
void gameMemory_OnSetTimingMethod(object sender, SetTimingMethodEventArgs e)
{
_gameRecommendedTimingMethod = e.GameTimingMethod;
}
// called when player is fully in game
void gameMemory_OnSessionTimeUpdate(object sender, SessionTicksUpdateEventArgs e)
{
_sessionTicks = e.SessionTicks;
}
// player is no longer fully in the game
void gameMemory_OnSessionEnded(object sender, EventArgs e)
{
Debug.WriteLine("session ended, total time was " + TimeSpan.FromSeconds((_sessionTicks - _sessionTicksOffset) * _intervalPerTick));
if (_gamePauseTime != null && this.GameTimingMethod == GameTimingMethod.EngineTicksWithPauses)
{
_sessionTicksOffset -= FakeTicks(_gamePauseTime.Value, DateTime.Now);
_gamePauseTime = null;
}
// add up total time and reset session time
_totalTicks += _sessionTicks - _sessionTicksOffset;
_totalMapTicks += _sessionTicks - _sessionTicksOffset;
_sessionTicks = 0;
_sessionTicksOffset = 0;
}
// called immediately after OnSessionEnded if it was a changelevel
void gameMemory_OnMapChanged(object sender, MapChangedEventArgs e)
{
Debug.WriteLine("gameMemory_OnMapChanged " + e.Map + " " + e.PrevMap);
// this is in case they load a save that was made before current map
// fuck time travel
if (!_mapsVisited.Contains(e.PrevMap))
{
_mapsVisited.Add(e.PrevMap);
this.AutoSplit(e.PrevMap);
}
// prevent adding map time twice
if (_timer.CurrentState.CurrentPhase != TimerPhase.Ended && _timer.CurrentState.CurrentPhase != TimerPhase.NotRunning)
this.AddMapTime(e.PrevMap, TimeSpan.FromSeconds(_totalMapTicks * _intervalPerTick));
_totalMapTicks = 0;
}
void gameMemory_OnPlayerGainedControl(object sender, PlayerControlChangedEventArgs e)
{
if (!this.Settings.AutoStartEndResetEnabled)
return;
_timer.Reset(); // make sure to reset for games that start from a quicksave (Aperture Tag)
_timer.Start();
_sessionTicksOffset += e.TicksOffset;
}
void gameMemory_OnPlayerLostControl(object sender, PlayerControlChangedEventArgs e)
{
if (!this.Settings.AutoStartEndResetEnabled)
return;
_sessionTicksOffset += e.TicksOffset;
this.DoSplit();
}
void gameMemory_OnNewGameStarted(object sender, EventArgs e)
{
if (!this.Settings.AutoStartEndResetEnabled)
return;
_timer.Reset();
}
void gameMemory_OnGamePaused(object sender, GamePausedEventArgs e)
{
if (this.GameTimingMethod != GameTimingMethod.EngineTicksWithPauses)
return;
if (e.Paused)
{
_gamePauseTime = DateTime.Now;
_gamePauseTick = _sessionTicks;
}
else
{
if (_gamePauseTime != null)
{
Debug.WriteLine("pause done, adding " + TimeSpan.FromSeconds((DateTime.Now - _gamePauseTime.Value).TotalSeconds));
_sessionTicksOffset -= FakeTicks(_gamePauseTime.Value, DateTime.Now);
}
_gamePauseTime = null;
}
}
int FakeTicks(DateTime start, DateTime end)
{
return (int)((end-start).TotalSeconds / _intervalPerTick);
}
void AutoSplit(string map)
{
if (!this.Settings.AutoSplitEnabled)
return;
Debug.WriteLine("AutoSplit " + map);
map = map.ToLower();
string[] blacklist = this.Settings.MapBlacklist.Select(x => x.ToLower()).ToArray();
if (this.Settings.AutoSplitType == AutoSplitType.Whitelist)
{
string[] whitelist = this.Settings.MapWhitelist.Select(x => x.ToLower()).ToArray();
if (whitelist.Length > 0)
{
if (whitelist.Contains(map))
this.DoSplit();
}
else if (!blacklist.Contains(map))
{
this.DoSplit();
}
}
else if (this.Settings.AutoSplitType == AutoSplitType.Interval)
{
if (!blacklist.Contains(map) && ++_splitCount >= this.Settings.SplitInterval)
{
_splitCount = 0;
this.DoSplit();
}
}
}
void DoSplit()
{
// make split times accurate
_timer.CurrentState.SetGameTime(this.GameTime);
HotkeyProfile profile = _timer.CurrentState.Settings.HotkeyProfiles[_timer.CurrentState.CurrentHotkeyProfile];
bool before = profile.DoubleTapPrevention;
profile.DoubleTapPrevention = false;
_timer.Split();
profile.DoubleTapPrevention = before;
}
// TODO: asterisk for manual start and splits
void AddMapTime(string map, TimeSpan time)
{
string timeStr = time.ToString(time >= TimeSpan.FromHours(1) ? @"hh\:mm\:ss\.fff" : @"mm\:ss\.fff");
MapTimesForm.Instance.AddMapTime(map, timeStr);
}
public XmlNode GetSettings(XmlDocument document)
{
return this.Settings.GetSettings(document);
}
public Control GetSettingsControl(LayoutMode mode)
{
return this.Settings;
}
public void SetSettings(XmlNode settings)
{
this.Settings.SetSettings(settings);
}
public float MinimumWidth => this.InternalComponent.MinimumWidth;
public float MinimumHeight => this.InternalComponent.MinimumHeight;
public float VerticalHeight => this.Settings.ShowGameTime ? this.InternalComponent.VerticalHeight : 0;
public float HorizontalWidth => this.Settings.ShowGameTime ? this.InternalComponent.HorizontalWidth : 0;
public float PaddingLeft => this.Settings.ShowGameTime ? this.InternalComponent.PaddingLeft : 0;
public float PaddingRight => this.Settings.ShowGameTime ? this.InternalComponent.PaddingRight : 0;
public float PaddingTop => this.Settings.ShowGameTime ? this.InternalComponent.PaddingTop : 0;
public float PaddingBottom => this.Settings.ShowGameTime ? this.InternalComponent.PaddingBottom : 0;
}
}