|
2 | 2 |
|
3 | 3 | import java.io.BufferedReader; |
4 | 4 | import java.io.BufferedWriter; |
| 5 | +import java.io.IOException; |
5 | 6 | import java.io.InputStreamReader; |
6 | 7 | import java.net.URI; |
7 | 8 | import java.nio.file.Files; |
@@ -101,23 +102,62 @@ public String getOslModuleBaseVersion(String version, String module) { |
101 | 102 | private Map<String, String> getModuleBaseVersions(String version) { |
102 | 103 | Map<String, String> baseVersions = null; |
103 | 104 |
|
104 | | - try { |
105 | | - baseVersions = getModuleBaseVersionsFromMeta(version); |
106 | | - } catch (Exception me) { |
| 105 | + Exception me = null; |
| 106 | + Exception ce = null; |
| 107 | + |
| 108 | + boolean prioritizeCache = shouldPrioritizeCacheForModuleBaseVersions(); |
| 109 | + |
| 110 | + if (prioritizeCache) { |
107 | 111 | try { |
108 | 112 | baseVersions = getModuleBaseVersionsFromCache(version); |
109 | | - } catch (Exception ce) { |
110 | | - project.getLogger().warn("unable to read OSL module base versions from cache for gen" + intermediaryGeneration() + " " + version, ce); |
| 113 | + } catch (Exception e) { |
| 114 | + ce = e; |
111 | 115 | } |
| 116 | + } |
112 | 117 |
|
113 | | - if (baseVersions == null) { |
114 | | - throw new IllegalStateException("unable to fetch OSL module base versions from meta for gen" + intermediaryGeneration() + " " + version + ", and it is not in the cache", me); |
| 118 | + if (baseVersions == null) { |
| 119 | + try { |
| 120 | + baseVersions = getModuleBaseVersionsFromMeta(version); |
| 121 | + } catch (Exception e) { |
| 122 | + me = e; |
115 | 123 | } |
116 | 124 | } |
117 | 125 |
|
| 126 | + if (baseVersions == null) { |
| 127 | + try { |
| 128 | + baseVersions = getModuleBaseVersionsFromCache(version); |
| 129 | + } catch (Exception e) { |
| 130 | + ce = e; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (baseVersions == null) { |
| 135 | + if (ce != null) { |
| 136 | + project.getLogger().warn("unable to read OSL module base versions from cache for gen" + intermediaryGeneration() + " " + version, ce); |
| 137 | + } |
| 138 | + |
| 139 | + throw new IllegalStateException("unable to fetch OSL module base versions from meta for gen" + intermediaryGeneration() + " " + version + ", and it is not in the cache", me); |
| 140 | + } |
| 141 | + |
118 | 142 | return baseVersions; |
119 | 143 | } |
120 | 144 |
|
| 145 | + private boolean shouldPrioritizeCacheForModuleBaseVersions() { |
| 146 | + Path baseVersionsCache = moduleBaseVersionsCache(); |
| 147 | + |
| 148 | + try { |
| 149 | + return Files.exists(baseVersionsCache) && !CacheFiles.isStale(baseVersionsCache, CacheFiles.ONE_DAY); |
| 150 | + } catch (IOException e) { |
| 151 | + try { |
| 152 | + Files.deleteIfExists(baseVersionsCache); |
| 153 | + } catch (IOException de) { |
| 154 | + throw new IllegalStateException("unable to delete corrupt cache file", e); |
| 155 | + } |
| 156 | + |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
| 160 | + |
121 | 161 | private Map<String, String> getModuleBaseVersionsFromMeta(String version) throws Exception { |
122 | 162 | String metaUrl = Constants.META_URL + Constants.oslVersionMetaEndpoint(intermediaryGeneration(), version); |
123 | 163 |
|
@@ -237,23 +277,62 @@ public String getOslModuleVersion(String module, String version, GameSide side) |
237 | 277 | private String getModuleVersion(String module, String version, GameSide side) { |
238 | 278 | String moduleVersion = null; |
239 | 279 |
|
240 | | - try { |
241 | | - moduleVersion = getModuleVersionFromMeta(module, version, side); |
242 | | - } catch (Exception me) { |
| 280 | + Exception me = null; |
| 281 | + Exception ce = null; |
| 282 | + |
| 283 | + boolean prioritizeCache = shouldPrioritizeCacheForModuleVersions(); |
| 284 | + |
| 285 | + if (prioritizeCache) { |
| 286 | + try { |
| 287 | + moduleVersion = getModuleVersionFromCache(module, version, side); |
| 288 | + } catch (Exception e) { |
| 289 | + ce = e; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + if (moduleVersion == null) { |
| 294 | + try { |
| 295 | + moduleVersion = getModuleVersionFromMeta(module, version, side); |
| 296 | + } catch (Exception e) { |
| 297 | + me = e; |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + if (moduleVersion == null) { |
243 | 302 | try { |
244 | 303 | moduleVersion = getModuleVersionFromCache(module, version, side); |
245 | | - } catch (Exception ce) { |
246 | | - project.getLogger().warn("unable to read OSL module version from cache for gen" + intermediaryGeneration() + " " + module + " " + version, ce); |
| 304 | + } catch (Exception e) { |
| 305 | + ce = e; |
247 | 306 | } |
| 307 | + } |
248 | 308 |
|
249 | | - if (moduleVersion == null) { |
250 | | - throw new IllegalStateException("unable to fetch OSL module version from meta for gen" + intermediaryGeneration() + " " + module + " " + version + ", and it is not in the cache", me); |
| 309 | + if (moduleVersion == null) { |
| 310 | + if (ce != null) { |
| 311 | + project.getLogger().warn("unable to read OSL module version from cache for gen" + intermediaryGeneration() + " " + version, ce); |
251 | 312 | } |
| 313 | + |
| 314 | + throw new IllegalStateException("unable to fetch OSL module version from meta for gen" + intermediaryGeneration() + " " + version + ", and it is not in the cache", me); |
252 | 315 | } |
253 | 316 |
|
254 | 317 | return moduleVersion; |
255 | 318 | } |
256 | 319 |
|
| 320 | + private boolean shouldPrioritizeCacheForModuleVersions() { |
| 321 | + Path versionsCache = moduleVersionsCache(); |
| 322 | + |
| 323 | + try { |
| 324 | + return Files.exists(versionsCache) && !CacheFiles.isStale(versionsCache, CacheFiles.ONE_DAY); |
| 325 | + } catch (IOException e) { |
| 326 | + try { |
| 327 | + Files.deleteIfExists(versionsCache); |
| 328 | + } catch (IOException de) { |
| 329 | + throw new IllegalStateException("unable to delete corrupt cache file", e); |
| 330 | + } |
| 331 | + |
| 332 | + return false; |
| 333 | + } |
| 334 | + } |
| 335 | + |
257 | 336 | private String getModuleVersionFromMeta(String module, String version, GameSide side) throws Exception { |
258 | 337 | String metaUrl = Constants.META_URL + Constants.oslModuleVersionMetaEndpoint(intermediaryGeneration(), module, minecraftVersion(), version); |
259 | 338 |
|
|
0 commit comments