forked from drtchops/LiveSplit.Skyrim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkyrimComponent.cs
More file actions
221 lines (184 loc) · 6.33 KB
/
SkyrimComponent.cs
File metadata and controls
221 lines (184 loc) · 6.33 KB
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
using LiveSplit.AutoSplitting;
using LiveSplit.Model;
using LiveSplit.UI;
using LiveSplit.UI.Components;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
namespace LiveSplit.Skyrim
{
public class SkyrimComponent : LogicComponent
{
public override string ComponentName => "Skyrim";
public MediaPlayer MediaPlayer { get; protected set; }
public SkyrimSettings Settings { get; protected set; }
public Time BearCartSplit { get; protected set; }
public AutoSplitManager AutoSplitManager => _gameMemory?.AutoSplitManager;
public string BearCartDefaultSoundPath { get; protected set; }
private TimerModel _timer;
private GameMemory _gameMemory;
private LiveSplitState _state;
private TimerPhase _prevPhase;
public SkyrimComponent(LiveSplitState state)
{
bool debug = false;
#if DEBUG
debug = true;
#endif
Trace.WriteLine($"[NoLoads] Using LiveSplit.Skyrim component version {Assembly.GetExecutingAssembly().GetName().Version} {(debug ? "Debug" : "Release")} build");
_state = state;
try { MediaPlayer = new MediaPlayer(); }
catch { MediaPlayer = null; }
this.Settings = new SkyrimSettings(this, state);
_timer = new TimerModel { CurrentState = state };
this.BearCartSplit = new Time();
this.BearCartDefaultSoundPath = System.IO.Path.GetTempPath() + @"LiveSplit.Skyrim\bearcart.mp3";
//extract embedded sound to temp folder
try
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(BearCartDefaultSoundPath));
System.IO.File.WriteAllBytes(BearCartDefaultSoundPath, Properties.Resources.bearcart_short);
}
catch (System.IO.IOException) { Trace.WriteLine("[NoLoads] Error when extracting bear cart sound to temp folder."); }
_gameMemory = new GameMemory();
_gameMemory.AutoSplitManager.AutoSplitList = Settings.AutoSplitList;
_gameMemory.OnStartSaveLoad += gameMemory_OnStartSaveLoad;
_gameMemory.OnLoadStarted += gameMemory_OnLoadStarted;
_gameMemory.OnLoadFinished += gameMemory_OnLoadFinished;
_gameMemory.OnBearCart += gameMemory_OnBearCart;
_gameMemory.OnSplit += _gameMemory_OnSplit;
state.OnStart += State_OnStart;
state.OnReset += State_OnReset;
_gameMemory.StartMonitoring();
}
private void _gameMemory_OnSplit(object sender, SplitEventArgs e)
{
if (_state.CurrentPhase != TimerPhase.Running)
{
e.AutoSplit.Triggered = false;
return;
}
Trace.WriteLine($"[NoLoads] {e.AutoSplit.Name} Split");
_timer.Split();
}
public override void Dispose()
{
_state.OnStart -= State_OnStart;
_state.OnReset -= State_OnReset;
_gameMemory.Dispose();
MediaPlayer?.Dispose();
Settings.Dispose();
}
void State_OnStart(object sender, EventArgs e)
{
_gameMemory.Reset();
_timer.InitializeGameTime();
BearCartSplit = new Time();
}
void State_OnReset(object sender, TimerPhase e)
{
UpdateBearCartPB();
}
void gameMemory_OnStartSaveLoad(object sender, EventArgs e)
{
Stopwatch s = Stopwatch.StartNew();
if (this.Settings.AutoReset)
{
UpdateBearCartPB(true);
_timer.Reset();
}
if (this.Settings.AutoStart)
{
StartTimer(s.Elapsed);
}
}
void StartTimer(TimeSpan time)
{
TimeSpan originalOffset = _state.Run.Offset;
_state.Run.Offset = time;
_timer.Start();
_state.Run.Offset = originalOffset;
Trace.WriteLine($"[NoLoads] Started timer at {time}");
}
void StartTimer() => StartTimer(TimeSpan.Zero);
void gameMemory_OnLoadStarted(object sender, EventArgs e)
{
_state.IsGameTimePaused = true;
}
void gameMemory_OnLoadFinished(object sender, EventArgs e)
{
_state.IsGameTimePaused = false;
}
void gameMemory_OnBearCart(object sender, EventArgs e)
{
if (BearCartSplit.RealTime == null && _state.CurrentPhase != TimerPhase.NotRunning)
{
BearCartSplit = _state.CurrentTime;
Settings.IsBearCartSecret = false;
Settings.SaveBearCartConfig();
if (Settings.IsBearCartSecret || Settings.PlayBearCartSound) //force play if it isn't unlocked in case the splits were shared
{
if (Settings.IsBearCartSecret || !Settings.PlayBearCartSoundOnlyOnPB || IsBearCartPB(BearCartSplit))
{
if (String.IsNullOrEmpty(Settings.BearCartSoundPath) || !System.IO.File.Exists(Settings.BearCartSoundPath))
MediaPlayer?.PlaySound(BearCartDefaultSoundPath);
else
MediaPlayer?.PlaySound(Settings.BearCartSoundPath);
}
}
}
}
bool IsBearCartPB(Time time)
{
return (time.GameTime < Settings.BearCartPB.GameTime || Settings.BearCartPB.GameTime == new TimeSpan(0));
}
void UpdateBearCartPB(bool silent = false)
{
if (BearCartSplit.RealTime == null)
return;
if (IsBearCartPB(BearCartSplit))
{
DialogResult result = DialogResult.Yes;
if (Settings.BearCartPBNotification && !silent)
{
string newTime = $"New time: Game Time: {BearCartSplit.GameTime.Value.ToString(@"mm\:ss\.fff")}, Real Time: {BearCartSplit.RealTime.Value.ToString(@"mm\:ss\.fff")}\n";
string oldTime = String.Empty;
if (Settings.BearCartPB.GameTime.Value != new TimeSpan(0))
oldTime = $"Previous time: Game Time: {Settings.BearCartPB.GameTime.Value.ToString(@"mm\:ss\.fff")}, Real Time: {Settings.BearCartPB.RealTime.Value.ToString(@"mm\:ss\.fff")}\n";
result = MessageBox.Show(_state.Form, newTime + oldTime + "\nDo you want to save your new Bear Cart Personal Best?",
"New Bear Cart Personal Best", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}
if (result == DialogResult.Yes)
{
Settings.BearCartPB = new Time(BearCartSplit.RealTime, BearCartSplit.GameTime); // give new pb to settings so it can be saved
Settings.SaveBearCartConfig();
}
}
if (!silent)
MediaPlayer?.Stop();
BearCartSplit = new Time();
}
public override XmlNode GetSettings(XmlDocument document)
{
return this.Settings.GetSettings(document);
}
public override Control GetSettingsControl(LayoutMode mode)
{
return this.Settings;
}
public override void SetSettings(XmlNode settings)
{
this.Settings.SetSettings(settings);
}
public override void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
if (state.CurrentPhase != _prevPhase && state.CurrentPhase == TimerPhase.Ended)
{
UpdateBearCartPB(true);
}
_prevPhase = state.CurrentPhase;
}
}
}