-
Notifications
You must be signed in to change notification settings - Fork 742
/
LiveSplitState.cs
220 lines (199 loc) · 8.29 KB
/
LiveSplitState.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
using LiveSplit.Model.Input;
using LiveSplit.Options;
using LiveSplit.UI;
using System;
using System.Linq;
using System.Threading.Tasks;
using Forms = System.Windows.Forms;
namespace LiveSplit.Model
{
public class LiveSplitState : ICloneable
{
public IRun Run { get; set; }
public ILayout Layout { get; set; }
public LayoutSettings LayoutSettings { get; set; }
public ISettings Settings { get; set; }
public Forms.Form Form { get; set; }
public AtomicDateTime AttemptStarted { get; set; }
public AtomicDateTime AttemptEnded { get; set; }
public TimeStamp AdjustedStartTime { get; set; }
public TimeStamp StartTimeWithOffset { get; set; }
public TimeStamp StartTime { get; set; }
public TimeSpan TimePausedAt { get; set; }
public TimeSpan? GameTimePauseTime { get; set; }
public TimerPhase CurrentPhase { get; set; }
public string CurrentComparison { get; set; }
public TimingMethod CurrentTimingMethod { get; set; }
public string CurrentHotkeyProfile { get; set; }
internal TimeSpan? loadingTimes;
public TimeSpan LoadingTimes {
get
{
return loadingTimes ?? TimeSpan.Zero;
}
set
{
loadingTimes = value;
if (IsGameTimePaused)
{
GameTimePauseTime = CurrentTime.RealTime.Value - value;
}
}
}
public bool IsGameTimeInitialized
{
get
{
return loadingTimes.HasValue;
}
set
{
if (value)
{
loadingTimes = LoadingTimes;
}
else
loadingTimes = null;
}
}
private bool isGameTimePaused;
public bool IsGameTimePaused
{
get { return isGameTimePaused; }
set
{
if (!value && isGameTimePaused)
LoadingTimes = CurrentTime.RealTime.Value - (CurrentTime.GameTime ?? CurrentTime.RealTime.Value);
else if (value && !isGameTimePaused)
GameTimePauseTime = (CurrentTime.GameTime ?? CurrentTime.RealTime);
isGameTimePaused = value;
}
}
public event EventHandler OnSplit;
public event EventHandler OnUndoSplit;
public event EventHandler OnSkipSplit;
public event EventHandler OnStart;
public event EventHandlerT<TimerPhase> OnReset;
public event EventHandler OnPause;
public event EventHandler OnUndoAllPauses;
public event EventHandler OnResume;
public event EventHandler OnScrollUp;
public event EventHandler OnScrollDown;
public event EventHandler OnSwitchComparisonPrevious;
public event EventHandler OnSwitchComparisonNext;
public event EventHandler RunManuallyModified;
public event EventHandler ComparisonRenamed;
public Time CurrentTime
{
get
{
var curTime = new Time();
if (CurrentPhase == TimerPhase.NotRunning)
curTime.RealTime = TimeSpan.Zero;
else if (CurrentPhase == TimerPhase.Running)
curTime.RealTime = TimeStamp.Now - AdjustedStartTime;
else if (CurrentPhase == TimerPhase.Paused)
curTime.RealTime = TimePausedAt;
else
curTime.RealTime = Run.Last().SplitTime.RealTime;
if (CurrentPhase == TimerPhase.Ended)
curTime.GameTime = Run.Last().SplitTime.GameTime;
else
curTime.GameTime = IsGameTimePaused
? GameTimePauseTime
: curTime.RealTime - (IsGameTimeInitialized ? (TimeSpan?)LoadingTimes : null);
return curTime;
}
}
public TimeSpan? PauseTime
{
get
{
if (CurrentPhase == TimerPhase.Paused)
return TimeStamp.Now - StartTimeWithOffset - TimePausedAt;
if (CurrentPhase != TimerPhase.NotRunning && StartTimeWithOffset != AdjustedStartTime)
return AdjustedStartTime - StartTimeWithOffset;
return null;
}
}
public TimeSpan CurrentAttemptDuration
{
get
{
if (CurrentPhase == TimerPhase.Paused || CurrentPhase == TimerPhase.Running)
return TimeStamp.Now - StartTime;
if (CurrentPhase == TimerPhase.Ended)
return AttemptEnded - AttemptStarted;
return TimeSpan.Zero;
}
}
public int CurrentSplitIndex { get; set; }
public ISegment CurrentSplit => (CurrentSplitIndex >= 0 && CurrentSplitIndex < Run.Count) ? Run[CurrentSplitIndex] : null;
private LiveSplitState() { }
public LiveSplitState(IRun run, Forms.Form form, ILayout layout, LayoutSettings layoutSettings, ISettings settings)
{
Run = run;
Form = form;
Layout = layout;
Settings = settings;
LayoutSettings = layoutSettings;
AdjustedStartTime = StartTimeWithOffset = StartTime = TimeStamp.Now;
CurrentPhase = TimerPhase.NotRunning;
CurrentSplitIndex = -1;
}
public object Clone()
{
return new LiveSplitState()
{
Run = Run.Clone() as IRun,
Form = Form,
Layout = Layout.Clone() as ILayout,
Settings = Settings.Clone() as ISettings,
LayoutSettings = LayoutSettings.Clone() as LayoutSettings,
AdjustedStartTime = AdjustedStartTime,
StartTimeWithOffset = StartTimeWithOffset,
StartTime = StartTime,
TimePausedAt = TimePausedAt,
GameTimePauseTime = GameTimePauseTime,
isGameTimePaused = isGameTimePaused,
LoadingTimes = LoadingTimes,
CurrentPhase = CurrentPhase,
CurrentSplitIndex = CurrentSplitIndex,
CurrentComparison = CurrentComparison,
CurrentHotkeyProfile = CurrentHotkeyProfile,
CurrentTimingMethod = CurrentTimingMethod,
AttemptStarted = AttemptStarted,
AttemptEnded = AttemptEnded
};
}
public void RegisterTimerModel(ITimerModel model)
{
model.OnSplit += (s, e) => OnSplit?.Invoke(this, e);
model.OnSkipSplit += (s, e) => OnSkipSplit?.Invoke(this, e);
model.OnUndoSplit += (s, e) => OnUndoSplit?.Invoke(this, e);
model.OnStart += (s, e) => OnStart?.Invoke(this, e);
model.OnReset += (s, e) => OnReset?.Invoke(this, e);
model.OnPause += (s, e) => OnPause?.Invoke(this, e);
model.OnUndoAllPauses += (s, e) => OnUndoAllPauses?.Invoke(this, e);
model.OnResume += (s, e) => OnResume?.Invoke(this, e);
model.OnScrollUp += (s, e) => OnScrollUp?.Invoke(this, e);
model.OnScrollDown += (s, e) => OnScrollDown?.Invoke(this, e);
model.OnSwitchComparisonPrevious += (s, e) => OnSwitchComparisonPrevious?.Invoke(this, e);
model.OnSwitchComparisonNext += (s, e) => OnSwitchComparisonNext?.Invoke(this, e);
}
public void SetGameTime(TimeSpan? gameTime)
{
var currentTime = CurrentTime;
if (currentTime.RealTime.HasValue && gameTime.HasValue)
{
LoadingTimes = currentTime.RealTime.Value - gameTime.Value;
if (IsGameTimePaused)
{
GameTimePauseTime = gameTime.Value;
}
}
}
public void CallRunManuallyModified() => RunManuallyModified?.Invoke(this, null);
public void CallComparisonRenamed(EventArgs e) => ComparisonRenamed?.Invoke(this, e);
}
}