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

View File

@@ -143,11 +143,8 @@ public abstract class AbstractPlatform implements Platform {
config.load(this); // load config.yml
if(config.dumpDefaultConfig()) {
dumpResources();
} else {
logger.info("Skipping resource dumping.");
}
dumpResources(config.getIgnoredResources());
if(config.isDebugProfiler()) { // if debug.profiler is enabled, start profiling
profiler.start();
@@ -259,7 +256,7 @@ public abstract class AbstractPlatform implements Platform {
return internalAddon;
}
protected void dumpResources() {
protected void dumpResources(List<String> ignoredResources) {
try(InputStream resourcesConfig = getClass().getResourceAsStream("/resources.yml")) {
if(resourcesConfig == null) {
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);
resources.forEach((dir, entries) -> entries.forEach(entry -> {
String resourceClassPath = dir + "/" + entry;
String resourcePath = resourceClassPath.replace('/', File.separatorChar);
File resource = new File(getDataFolder(), resourcePath);
if(resource.exists())
return; // dont overwrite
if (ignoredResources.contains(dir) || ignoredResources.contains(entry) || ignoredResources.contains(resourceClassPath)) {
logger.info("Not dumping resource {} because it is ignored.", resourceClassPath);
} else {
String resourcePath = resourceClassPath.replace('/', File.separatorChar);
File resource = new File(getDataFolder(), resourcePath);
if(resource.exists())
return; // dont overwrite
try(InputStream is = getClass().getResourceAsStream("/" + resourceClassPath)) {
if(is == null) {
logger.error("Resource {} doesn't exist on the classpath!", resourcePath);
return;
try(InputStream is = getClass().getResourceAsStream("/" + resourceClassPath)) {
if(is == null) {
logger.error("Resource {} doesn't exist on the classpath!", resourcePath);
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) {

View File

@@ -30,6 +30,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.List;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.config.PluginConfig;
@@ -71,9 +73,9 @@ public class PluginConfigImpl implements ConfigTemplate, PluginConfig {
@Default
private int providerCache = 32;
@Value("dump-default")
@Value("ignored-resources")
@Default
private boolean dumpDefaultData = true;
private List<String> ignoredResources = Collections.emptyList();
@Value("script.max-recursion")
@Default
@@ -99,11 +101,6 @@ public class PluginConfigImpl implements ConfigTemplate, PluginConfig {
logger.info("Debug logging enabled.");
}
@Override
public boolean dumpDefaultConfig() {
return dumpDefaultData;
}
@Override
public boolean isDebugCommands() {
return debugCommands;
@@ -139,6 +136,11 @@ public class PluginConfigImpl implements ConfigTemplate, PluginConfig {
return samplerCache;
}
@Override
public List<String> getIgnoredResources() {
return ignoredResources;
}
@Override
public int getMaxRecursion() {
return maxRecursion;

View File

@@ -10,11 +10,13 @@ debug:
log: false
profiler: false
script: false
dump-default: true
biome-search-resolution: 4
cache:
structure: 32
sampler: 128
biome-provider: 32
script:
max-recursion: 1000
max-recursion: 1000
ignored-resources:
# - "addons"
# - "packs"