Remove dump-resources and allow for ignoring specific resources

This commit is contained in:
Zoë Gidiere
2024-01-05 17:19:27 -07:00
parent 37641d43d6
commit 921212ccb5
4 changed files with 67 additions and 60 deletions
@@ -9,12 +9,12 @@ package com.dfsek.terra.api.config;
import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.Platform;
import java.util.List;
public interface PluginConfig { public interface PluginConfig {
void load(Platform platform); void load(Platform platform);
boolean dumpDefaultConfig();
boolean isDebugCommands(); boolean isDebugCommands();
boolean isDebugProfiler(); boolean isDebugProfiler();
@@ -31,5 +31,7 @@ public interface PluginConfig {
int getMaxRecursion(); int getMaxRecursion();
List<String> getIgnoredResources();
int getProviderCache(); int getProviderCache();
} }
@@ -143,11 +143,8 @@ public abstract class AbstractPlatform implements Platform {
config.load(this); // load config.yml config.load(this); // load config.yml
if(config.dumpDefaultConfig()) {
dumpResources(); dumpResources(config.getIgnoredResources());
} else {
logger.info("Skipping resource dumping.");
}
if(config.isDebugProfiler()) { // if debug.profiler is enabled, start profiling if(config.isDebugProfiler()) { // if debug.profiler is enabled, start profiling
profiler.start(); profiler.start();
@@ -259,7 +256,7 @@ public abstract class AbstractPlatform implements Platform {
return internalAddon; return internalAddon;
} }
protected void dumpResources() { protected void dumpResources(List<String> ignoredResources) {
try(InputStream resourcesConfig = getClass().getResourceAsStream("/resources.yml")) { try(InputStream resourcesConfig = getClass().getResourceAsStream("/resources.yml")) {
if(resourcesConfig == null) { if(resourcesConfig == null) {
logger.info("No resources config found. Skipping resource dumping."); logger.info("No resources config found. Skipping resource dumping.");
@@ -301,51 +298,55 @@ public abstract class AbstractPlatform implements Platform {
Map<String, List<String>> resources = new Yaml().load(resourceYaml); Map<String, List<String>> resources = new Yaml().load(resourceYaml);
resources.forEach((dir, entries) -> entries.forEach(entry -> { resources.forEach((dir, entries) -> entries.forEach(entry -> {
String resourceClassPath = dir + "/" + entry; String resourceClassPath = dir + "/" + entry;
String resourcePath = resourceClassPath.replace('/', File.separatorChar); if (ignoredResources.contains(dir) || ignoredResources.contains(entry) || ignoredResources.contains(resourceClassPath)) {
File resource = new File(getDataFolder(), resourcePath); logger.info("Not dumping resource {} because it is ignored.", resourceClassPath);
if(resource.exists()) } else {
return; // dont overwrite String resourcePath = resourceClassPath.replace('/', File.separatorChar);
File resource = new File(getDataFolder(), resourcePath);
if(resource.exists())
return; // dont overwrite
try(InputStream is = getClass().getResourceAsStream("/" + resourceClassPath)) { try(InputStream is = getClass().getResourceAsStream("/" + resourceClassPath)) {
if(is == null) { if(is == null) {
logger.error("Resource {} doesn't exist on the classpath!", resourcePath); logger.error("Resource {} doesn't exist on the classpath!", resourcePath);
return; return;
}
paths
.stream()
.filter(Pair.testRight(resourcePath::startsWith))
.forEach(Pair.consumeLeft(path -> {
logger.info("Removing outdated resource {}, replacing with {}", path, resourcePath);
try {
Files.delete(path);
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}));
if(pathsNoMajor
.stream()
.anyMatch(resourcePath::startsWith) && // if any share name
paths
.stream()
.map(Pair.unwrapRight())
.noneMatch(resourcePath::startsWith)) { // but dont share major version
logger.warn(
"Addon {} has a new major version available. It will not be automatically updated; you will need to " +
"ensure " +
"compatibility and update manually.",
resourcePath);
}
logger.info("Dumping resource {}.", resource.getAbsolutePath());
resource.getParentFile().mkdirs();
resource.createNewFile();
try(OutputStream os = new FileOutputStream(resource)) {
IOUtils.copy(is, os);
}
} catch(IOException e) {
throw new UncheckedIOException(e);
} }
paths
.stream()
.filter(Pair.testRight(resourcePath::startsWith))
.forEach(Pair.consumeLeft(path -> {
logger.info("Removing outdated resource {}, replacing with {}", path, resourcePath);
try {
Files.delete(path);
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}));
if(pathsNoMajor
.stream()
.anyMatch(resourcePath::startsWith) && // if any share name
paths
.stream()
.map(Pair.unwrapRight())
.noneMatch(resourcePath::startsWith)) { // but dont share major version
logger.warn(
"Addon {} has a new major version available. It will not be automatically updated; you will need to " +
"ensure " +
"compatibility and update manually.",
resourcePath);
}
logger.info("Dumping resource {}...", resource.getAbsolutePath());
resource.getParentFile().mkdirs();
resource.createNewFile();
try(OutputStream os = new FileOutputStream(resource)) {
IOUtils.copy(is, os);
}
} catch(IOException e) {
throw new UncheckedIOException(e);
} }
})); }));
} catch(IOException e) { } catch(IOException e) {
@@ -30,6 +30,8 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.List;
import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.config.PluginConfig; import com.dfsek.terra.api.config.PluginConfig;
@@ -71,9 +73,9 @@ public class PluginConfigImpl implements ConfigTemplate, PluginConfig {
@Default @Default
private int providerCache = 32; private int providerCache = 32;
@Value("dump-default") @Value("ignored-resources")
@Default @Default
private boolean dumpDefaultData = true; private List<String> ignoredResources = Collections.emptyList();
@Value("script.max-recursion") @Value("script.max-recursion")
@Default @Default
@@ -99,11 +101,6 @@ public class PluginConfigImpl implements ConfigTemplate, PluginConfig {
logger.info("Debug logging enabled."); logger.info("Debug logging enabled.");
} }
@Override
public boolean dumpDefaultConfig() {
return dumpDefaultData;
}
@Override @Override
public boolean isDebugCommands() { public boolean isDebugCommands() {
return debugCommands; return debugCommands;
@@ -139,6 +136,11 @@ public class PluginConfigImpl implements ConfigTemplate, PluginConfig {
return samplerCache; return samplerCache;
} }
@Override
public List<String> getIgnoredResources() {
return ignoredResources;
}
@Override @Override
public int getMaxRecursion() { public int getMaxRecursion() {
return maxRecursion; return maxRecursion;
@@ -10,7 +10,6 @@ debug:
log: false log: false
profiler: false profiler: false
script: false script: false
dump-default: true
biome-search-resolution: 4 biome-search-resolution: 4
cache: cache:
structure: 32 structure: 32
@@ -18,3 +17,6 @@ cache:
biome-provider: 32 biome-provider: 32
script: script:
max-recursion: 1000 max-recursion: 1000
ignored-resources:
# - "addons"
# - "packs"