-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppConfig.cs
83 lines (73 loc) · 2.66 KB
/
AppConfig.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
namespace SAR_Overlay
{
// TODO:
// Scenarious - config/scenarious/*.sarpms (SAR Private Match Scenario
// Locations - config/locations.txt
// What else?
public static class Config
{
private const string locationsFileName = "Locations.txt";
private const string scenariousFolder = "Scenarios";
private static string configDir = "Config/";
private static SARLocation[] teleportLocations = null;
public static SARLocation[] TeleportLocations
{
get
{
if (teleportLocations == null)
ReloadTeleportLocations();
return teleportLocations;
}
}
private static SARScenario[] scenariosList = null;
public static SARScenario[] ScenariosList
{
get
{
if (scenariosList == null)
ReloadScenariosList();
return scenariosList;
}
}
private static T[] LoadFromFile<T>(string filename) where T : SARParseble
{
if (!File.Exists(filename))
return new T[] { };
try
{
return File.ReadAllLines(filename).Select(line => (T)typeof(T).GetMethod("Parse").Invoke(null, new object[] { line })).ToArray();
}
catch
{
MessageBox.Show("Error loading from file " + filename, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return new T[] { };
}
}
private static T[] LoadFromFolder<T>(string path, string fileMask) where T : SARParseble
{
if (!Directory.Exists(path))
return new T[] { };
try
{
var filenames = Directory.EnumerateFiles(path, fileMask, SearchOption.TopDirectoryOnly);
var list = new List<T>();
foreach (var fname in filenames)
{
var scenarioText = File.ReadAllText(fname);
list.Add((T)typeof(T).GetMethod("Parse").Invoke(null, new object[] { scenarioText }));
}
return list.ToArray();
}
catch
{
return new T[] { };
}
}
public static void ReloadScenariosList() => scenariosList = LoadFromFolder<SARScenario>(configDir + scenariousFolder, "*.sarpms");
public static void ReloadTeleportLocations() => teleportLocations = LoadFromFile<SARLocation>(configDir + locationsFileName);
}
}