-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.cs
358 lines (329 loc) · 11.6 KB
/
Plugin.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
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.IO;
using System.Text;
using OpenBveApi.Runtime;
using Path = OpenBveApi.Path;
namespace Plugin {
/// <summary>The interface to be implemented by the plugin.</summary>
public class Plugin : IRuntime {
// --- members ---
/// <summary>The train that is simulated by this plugin.</summary>
private Train Train = null;
/// <summary>The random number generator used by the plugin.</summary>
public static Random Random = new Random();
public static string TrainFolder;
internal static bool FolderWriteAccess = true;
/// <summary>Is called when the plugin is loaded.</summary>
/// <param name="properties">The properties supplied to the plugin on loading.</param>
/// <returns>Whether the plugin was loaded successfully.</returns>
public bool Load(LoadProperties properties)
{
properties.Panel = new int[512];
SoundManager.Initialise(properties.PlaySound, 512);
MessageManager.Initialise(properties.AddMessage);
properties.AISupport = AISupport.Basic;
this.Train = new Train(properties.Panel);
//No AI Support
//properties.AISupport = AISupport.None;
this.Train = new Train(properties.Panel);
string configFile = Path.CombineFile(properties.TrainFolder, "BVEC_Ats.cfg");
string OS_ATSDLL = Path.CombineFile(properties.TrainFolder, "OS_ATS1.dll");
string SZ_ATSDLL = Path.CombineFile(properties.TrainFolder, "OS_SZ_ATS1.dll");
string SZ_ATSDLL_2 = Path.CombineFile(properties.TrainFolder, "OS_SZ_Ats2_0.dll");
string OS_ATSconfigFile = Path.CombineFile(properties.TrainFolder, "OS_ATS1.cfg");
string SZ_ATSconfigFile = Path.CombineFile(properties.TrainFolder, "OS_SZ_ATS1.cfg");
string SZ_ATS_2configFile = Path.CombineFile(properties.TrainFolder, "OS_SZ_Ats2_0.cfg");
string ODF_ATSconfigFile = Path.CombineFile(properties.TrainFolder, "OdakyufanAts.cfg");
TrainFolder = properties.TrainFolder;
//Delete error.log from previous run
if (File.Exists(Path.CombineFile(properties.TrainFolder, "error.log")))
{
try
{
File.Delete(Path.CombineFile(properties.TrainFolder, "error.log"));
}
catch
{
FolderWriteAccess = false;
}
}
if (File.Exists(configFile))
{
//Check for the automatic generator version
string generatorversion;
using (var reader = new StreamReader(configFile))
{
//Read in first line
generatorversion = reader.ReadLine();
}
//If it exists
try
{
if (generatorversion != null && generatorversion.StartsWith(";GenVersion="))
{
string versiontext = Regex.Match(generatorversion, @"\d+").Value;
int version = Int32.Parse(versiontext, NumberStyles.Number, CultureInfo.InvariantCulture);
//If we're below the current version, try to upgrade again
if (version < 1)
{
if (File.Exists(OS_ATSDLL) && File.Exists(OS_ATSconfigFile))
{
try
{
string[] Lines = UpgradeOSATS.UpgradeConfigurationFile(OS_ATSconfigFile, properties.TrainFolder);
}
catch (Exception)
{
properties.FailureReason = "An error occured whilst attempting to upgrade the OS_ATS configuration.";
return false;
}
}
}
}
}
catch (Exception)
{
properties.FailureReason = "Empty configuration file detected.";
return false;
}
//Now try loading
try
{
string[] Lines = File.ReadAllLines(configFile, Encoding.UTF8);
this.Train.LoadConfigurationFile(Lines);
return true;
}
catch (Exception ex)
{
properties.FailureReason = "Error loading the configuration file: " + ex.Message;
return false;
}
}
if (!File.Exists(configFile) && File.Exists(OS_ATSDLL) && File.Exists(OS_ATSconfigFile))
{
//The F92_en is blacklisted due to a custom OS_ATS version
if (Regex.IsMatch(properties.TrainFolder, @"\\F92_en(\\)?", RegexOptions.IgnoreCase))
{
properties.FailureReason = "The F92_en is not currently a supported train.";
try
{
using (StreamWriter sw = File.CreateText(Path.CombineFile(properties.TrainFolder, "error.log")))
{
sw.WriteLine("The F92_en is not currently a supported train");
}
return false;
}
catch
{
return false;
}
}
//If there is no existing BVEC_ATS configuration file, but OS_ATS and the appropriate
//configuration files exist, then attempt to upgrade the existing file to BVEC_ATS
try
{
string[] Lines = UpgradeOSATS.UpgradeConfigurationFile(OS_ATSconfigFile, properties.TrainFolder);
try
{
File.WriteAllLines(Path.CombineFile(TrainFolder, "BVEC_ATS.cfg"), Lines);
}
catch
{
//Error writing the new configuration file
}
this.Train.LoadConfigurationFile(Lines);
return true;
}
catch (Exception)
{
properties.FailureReason = "Error upgrading the existing OS_ATS configuration.";
using (StreamWriter sw = File.CreateText(Path.CombineFile(properties.TrainFolder, "error.log")))
{
sw.WriteLine("An existing OS_ATS configuration was found.");
sw.WriteLine("However, an error occurred upgrading the existing OS_ATS configuration.");
}
return false;
}
}
if (File.Exists(SZ_ATSDLL))
{
//We've found an OS_SZ_ATS equipped train
//Upgrade for this is in alpha
try
{
string[] Lines = UpgradeOSSZATS.UpgradeConfigurationFile(SZ_ATSconfigFile, properties.TrainFolder);
try
{
File.WriteAllLines(Path.CombineFile(TrainFolder, "BVEC_ATS.cfg"), Lines);
}
catch
{
//Error writing the new configuration file
}
this.Train.LoadConfigurationFile(Lines);
return true;
}
catch (Exception)
{
properties.FailureReason = "Error upgrading the existing OS_SZ_ATS configuration.";
using (StreamWriter sw = File.CreateText(Path.CombineFile(properties.TrainFolder, "error.log")))
{
sw.WriteLine("An existing OS_SZ_ATS configuration was found.");
sw.WriteLine("However, an error occurred upgrading the existing OS_SZ_ATS configuration.");
}
return false;
}
}
else
{
if (File.Exists(SZ_ATSDLL_2))
{
//We've found an OS_SZ_ATS equipped train
//Upgrade for this is in alpha
try
{
string[] Lines = UpgradeOSSZATS.UpgradeConfigurationFile(SZ_ATS_2configFile, properties.TrainFolder);
try
{
File.WriteAllLines(Path.CombineFile(TrainFolder, "BVEC_ATS.cfg"), Lines);
}
catch
{
//Error writing the new configuration file
}
this.Train.LoadConfigurationFile(Lines);
return true;
}
catch (Exception)
{
properties.FailureReason = "Error upgrading the existing OS_SZ_ATS configuration.";
using (StreamWriter sw = File.CreateText(Path.CombineFile(properties.TrainFolder, "error.log")))
{
sw.WriteLine("An existing OS_SZ_ATS configuration was found.");
sw.WriteLine("However, an error occurred upgrading the existing OS_SZ_ATS configuration.");
}
return false;
}
}
if (File.Exists(ODF_ATSconfigFile))
{
//We've found an OdyakufanATS equipped train
//Upgrade for this is in alpha
try
{
File.Copy(ODF_ATSconfigFile, configFile);
string[] Lines = File.ReadAllLines(configFile);
this.Train.LoadConfigurationFile(Lines);
return true;
}
catch (Exception ex)
{
properties.FailureReason = "Error loading the configuration file: " + ex.Message;
return false;
}
}
else
{
properties.FailureReason = "No supported configuration files exist.";
//Write out error.log with details of what it thinks was found and missing
using (StreamWriter sw = File.CreateText(Path.CombineFile(properties.TrainFolder, "error.log")))
{
sw.WriteLine("Plugin location " + Convert.ToString(properties.TrainFolder));
if (File.Exists(OS_ATSDLL))
{
sw.WriteLine("OS_ATS DLL found");
}
else
{
sw.WriteLine("No OS_ATS DLL found");
}
if (File.Exists(OS_ATSconfigFile))
{
sw.WriteLine("OS_ATS configuration file found");
}
else
{
sw.WriteLine("No OS_ATS configuration file found");
}
}
return false;
}
}
}
/// <summary>Is called when the plugin is unloaded.</summary>
public void Unload() {
// TODO: Your old Dispose code goes here.
}
/// <summary>Is called after loading to inform the plugin about the specifications of the train.</summary>
/// <param name="specs">The specifications of the train.</param>
public void SetVehicleSpecs(VehicleSpecs specs) {
this.Train.Specs = specs;
}
/// <summary>Is called when the plugin should initialize or reinitialize.</summary>
/// <param name="mode">The mode of initialization.</param>
public void Initialize(InitializationModes mode) {
this.Train.Initialize(mode);
}
/// <summary>Is called every frame.</summary>
/// <param name="data">The data passed to the plugin.</param>
public void Elapse(ElapseData data) {
this.Train.Elapse(data);
}
/// <summary>Is called when the driver changes the reverser.</summary>
/// <param name="reverser">The new reverser position.</param>
public void SetReverser(int reverser) {
this.Train.SetReverser(reverser);
}
/// <summary>Is called when the driver changes the power notch.</summary>
/// <param name="powerNotch">The new power notch.</param>
public void SetPower(int powerNotch) {
this.Train.SetPower(powerNotch);
}
/// <summary>Is called when the driver changes the brake notch.</summary>
/// <param name="brakeNotch">The new brake notch.</param>
public void SetBrake(int brakeNotch) {
this.Train.SetBrake(brakeNotch);
}
/// <summary>Is called when a virtual key is pressed.</summary>
/// <param name="key">The virtual key that was pressed.</param>
public void KeyDown(VirtualKeys key) {
this.Train.KeyDown(key);
}
/// <summary>Is called when a virtual key is released.</summary>
/// <param name="key">The virtual key that was released.</param>
public void KeyUp(VirtualKeys key) {
this.Train.KeyUp(key);
}
/// <summary>Is called when a horn is played or when the music horn is stopped.</summary>
/// <param name="type">The type of horn.</param>
public void HornBlow(HornTypes type) {
this.Train.HornBlow(type);
}
/// <summary>Is called when the state of the doors changes.</summary>
/// <param name="oldState">The old state of the doors.</param>
/// <param name="newState">The new state of the doors.</param>
public void DoorChange(DoorStates oldState, DoorStates newState) {
this.Train.Doors = newState;
this.Train.DoorChange(oldState, newState);
}
/// <summary>Is called when the aspect in the current or in any of the upcoming sections changes, or when passing section boundaries.</summary>
/// <param name="signal">Signal information per section. In the array, index 0 is the current section, index 1 the upcoming section, and so on.</param>
/// <remarks>The signal array is guaranteed to have at least one element. When accessing elements other than index 0, you must check the bounds of the array first.</remarks>
public void SetSignal(SignalData[] signal) {
this.Train.SetSignal(signal);
}
/// <summary>Is called when the train passes a beacon.</summary>
/// <param name="beacon">The beacon data.</param>
public void SetBeacon(BeaconData beacon) {
this.Train.SetBeacon(beacon);
}
/// <summary>Is called when the plugin should perform the AI.</summary>
/// <param name="data">The AI data.</param>
public void PerformAI(AIData data)
{
this.Train.PerformAI(data);
}
}
}