|
18 | 18 | package com.dfsek.terra;
|
19 | 19 |
|
20 | 20 | import com.dfsek.tectonic.api.TypeRegistry;
|
| 21 | + |
| 22 | +import com.dfsek.terra.api.util.generic.pair.Pair; |
| 23 | + |
21 | 24 | import org.apache.commons.io.FileUtils;
|
22 | 25 | import org.apache.commons.io.IOUtils;
|
23 | 26 | import org.jetbrains.annotations.NotNull;
|
|
33 | 36 | import java.io.UncheckedIOException;
|
34 | 37 | import java.net.URL;
|
35 | 38 | import java.nio.charset.StandardCharsets;
|
| 39 | +import java.nio.file.Files; |
36 | 40 | import java.nio.file.Path;
|
37 | 41 | import java.util.ArrayList;
|
38 | 42 | import java.util.Collections;
|
39 | 43 | import java.util.Comparator;
|
40 | 44 | import java.util.List;
|
41 | 45 | import java.util.Map;
|
| 46 | +import java.util.Set; |
| 47 | +import java.util.stream.Collectors; |
42 | 48 |
|
43 | 49 | import com.dfsek.terra.addon.BootstrapAddonLoader;
|
44 | 50 | import com.dfsek.terra.addon.DependencySorter;
|
@@ -212,13 +218,71 @@ protected void dumpResources() {
|
212 | 218 | logger.info("No resources config found. Skipping resource dumping.");
|
213 | 219 | return;
|
214 | 220 | }
|
| 221 | + |
| 222 | + Path data = getDataFolder().toPath(); |
| 223 | + |
| 224 | + Path addonsPath = data.resolve("addons"); |
| 225 | + Set<Pair<Path, String>> paths = Files |
| 226 | + .walk(addonsPath) |
| 227 | + .map(path -> Pair.of(path, data.relativize(path).toString())) |
| 228 | + |
| 229 | + .map(Pair.mapRight(s -> { |
| 230 | + if(s.contains("+")) { // remove commit hash |
| 231 | + return s.substring(0, s.lastIndexOf('+')); |
| 232 | + } |
| 233 | + return s; |
| 234 | + })) |
| 235 | + |
| 236 | + .filter(Pair.testRight(s -> s.contains("."))) // remove patch version |
| 237 | + .map(Pair.mapRight(s -> s.substring(0, s.lastIndexOf('.')))) |
| 238 | + |
| 239 | + .filter(Pair.testRight(s -> s.contains("."))) // remove minor version |
| 240 | + .map(Pair.mapRight(s -> s.substring(0, s.lastIndexOf('.')))) |
| 241 | + |
| 242 | + .collect(Collectors.toSet()); |
| 243 | + |
| 244 | + Set<String> pathsNoMajor = paths |
| 245 | + .stream() |
| 246 | + .filter(Pair.testRight(s -> s.contains("."))) |
| 247 | + .map(Pair.mapRight(s -> s.substring(0, s.lastIndexOf('.')))) // remove major version |
| 248 | + .map(Pair.unwrapRight()) |
| 249 | + .collect(Collectors.toSet()); |
| 250 | + |
| 251 | + |
| 252 | + // Terra-aaa-aaa-1.2.3-BETA+1e6af8923d.jar |
215 | 253 | String resourceYaml = IOUtils.toString(resourcesConfig, StandardCharsets.UTF_8);
|
216 | 254 | Map<String, List<String>> resources = new Yaml().load(resourceYaml);
|
217 | 255 | resources.forEach((dir, entries) -> entries.forEach(entry -> {
|
218 |
| - String resourcePath = String.format("%s/%s", dir, entry); |
| 256 | + String resourcePath = dir + File.separatorChar + entry; |
219 | 257 | File resource = new File(getDataFolder(), resourcePath);
|
220 | 258 | if(resource.exists())
|
221 | 259 | return; // dont overwrite
|
| 260 | + |
| 261 | + paths |
| 262 | + .stream() |
| 263 | + .filter(Pair.testRight(resourcePath::startsWith)) |
| 264 | + .forEach(Pair.consumeLeft(path -> { |
| 265 | + logger.info("Removing outdated resource {}, replacing with {}", path, resourcePath); |
| 266 | + try { |
| 267 | + Files.delete(path); |
| 268 | + } catch(IOException e) { |
| 269 | + throw new UncheckedIOException(e); |
| 270 | + } |
| 271 | + })); |
| 272 | + |
| 273 | + if(pathsNoMajor |
| 274 | + .stream() |
| 275 | + .anyMatch(resourcePath::startsWith) && // if any share name |
| 276 | + paths |
| 277 | + .stream() |
| 278 | + .map(Pair.unwrapRight()) |
| 279 | + .noneMatch(resourcePath::startsWith)) { // but dont share major version |
| 280 | + logger.warn( |
| 281 | + "Addon {} has a new major version available. It will not be automatically updated; you will need to ensure " + |
| 282 | + "compatibility and update manually.", |
| 283 | + resourcePath); |
| 284 | + } |
| 285 | + |
222 | 286 | logger.info("Dumping resource {}...", resource.getAbsolutePath());
|
223 | 287 | try {
|
224 | 288 | resource.getParentFile().mkdirs();
|
|
0 commit comments