-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
KspBuildMap.cs
175 lines (158 loc) · 5.38 KB
/
KspBuildMap.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
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using log4net;
using Newtonsoft.Json;
using CKAN.Versioning;
using CKAN.Configuration;
namespace CKAN.GameVersionProviders
{
// <summary>
// THIS IS NOT THE BUILD MAP! If you are trying to access the build map,
// you want to use IKspBuildMap.
//
// This class represents the internal JSON structure of the build map,
// and should only be used by implementations of IKspBuildMap and
// IConfiguration.
// </summary>
public sealed class JBuilds
{
[JsonProperty("builds")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public Dictionary<string, string> Builds { get; set; }
}
// ReSharper disable once ClassNeverInstantiated.Global
public sealed class KspBuildMap : IKspBuildMap
{
// TODO: Need a way for the client to configure this
private static readonly Uri BuildMapUri =
new Uri("https://raw.githubusercontent.com/KSP-CKAN/CKAN-meta/master/builds.json");
private static readonly string cachedBuildMapPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"CKAN", "builds.json");
private static readonly ILog Log = LogManager.GetLogger(typeof(KspBuildMap));
private readonly object _buildMapLock = new object();
private readonly IConfiguration _configuration;
private JBuilds _jBuilds;
public GameVersion this[string buildId]
{
get
{
EnsureBuildMap();
string version;
return _jBuilds.Builds.TryGetValue(buildId, out version) ? GameVersion.Parse(version) : null;
}
}
public List<GameVersion> KnownVersions
{
get
{
EnsureBuildMap();
return _jBuilds.Builds
.Select(b => GameVersion.Parse(b.Value))
.ToList();
}
}
public KspBuildMap(IConfiguration configuration)
{
_configuration = configuration;
}
private void EnsureBuildMap()
{
if (ReferenceEquals(_jBuilds, null))
{
lock (_buildMapLock)
{
if (ReferenceEquals(_jBuilds, null))
{
// Check for a cached copy of the remote build map
if (TrySetCachedBuildMap()) return;
// If that doesn't exist, use the copy from when we were compiled
if (TrySetEmbeddedBuildMap()) return;
Log.Warn("Could not load build map from cached or embedded copy");
}
}
}
}
/// <summary>
/// Download the build map from the server to the cache
/// </summary>
public void Refresh()
{
Log.Debug("Refreshing build map from server");
if (TrySetRemoteBuildMap()) return;
Log.Warn("Could not refresh the build map from remote server");
}
private bool TrySetBuildMap(string buildMapJson)
{
try
{
_jBuilds = JsonConvert.DeserializeObject<JBuilds>(buildMapJson);
return true;
}
catch (Exception e)
{
Log.WarnFormat("Could not parse build map");
Log.DebugFormat("{0}\n{1}", buildMapJson, e);
return false;
}
}
private bool TrySetCachedBuildMap()
{
try
{
Log.Debug("Getting cached build map");
return TrySetBuildMap(File.ReadAllText(cachedBuildMapPath));
}
catch
{
return false;
}
}
private bool TrySetRemoteBuildMap()
{
try
{
Log.Debug("Getting remote build map");
var json = Net.DownloadText(BuildMapUri);
new FileInfo(cachedBuildMapPath).Directory.Create();
File.WriteAllText(cachedBuildMapPath, json);
return TrySetBuildMap(json);
}
catch (Exception e)
{
Log.WarnFormat("Could not retrieve latest build map from: {0}", BuildMapUri);
Log.Debug(e);
return false;
}
}
private bool TrySetEmbeddedBuildMap()
{
try
{
Log.Debug("Getting embedded build map");
var resourceStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("CKAN.builds.json");
if (resourceStream != null)
{
using (var reader = new StreamReader(resourceStream))
{
return TrySetBuildMap(reader.ReadToEnd());
}
}
else
{
return false;
}
}
catch (Exception e)
{
Log.WarnFormat("Could not retrieve build map from embedded resource");
Log.Debug(e);
return false;
}
}
}
}