Add 'exists' path check

This commit is contained in:
Roman Zhuravlev 2024-04-26 23:33:22 +05:00
parent 3800678138
commit 56a2d7892e
7 changed files with 72 additions and 53 deletions

View file

@ -10,6 +10,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;
public interface PreparedPlugin {
@ -23,26 +24,35 @@ public interface PreparedPlugin {
Path getDataDirectory();
default Path loadConfig(Config config, String pathname) {
default Path loadConfig(Config config, String pathname, boolean resourcePreload) {
ClassLoader classLoader = getClass().getClassLoader();
InputStream stream = ResourceUtils.getResource(pathname, classLoader);
if (stream != null) {
config.load(stream);
}
Path path = Paths.get(pathname);
try (InputStream stream = ResourceUtils.getResource(pathname, classLoader)) {
if (stream != null) {
if (resourcePreload) {
config.load(stream);
}
Path path = config.load(getDataDirectory().toString() + '/' + pathname);
try {
if (Files.notExists(path) || Files.size(path) == 0) {
if (ResourceUtils.saveResource(pathname, path, classLoader)) {
config.load(path);
if (Files.notExists(path) || Files.size(path) == 0) {
ResourceUtils.saveResource(stream, path);
}
}
if (Files.exists(path)) config.load(path);
} catch (IOException e) {
throw new ConfigException(e);
}
return path;
}
default Path loadConfig(Config config, String pathname) {
return loadConfig(config, pathname, false);
}
default Path loadConfig(Config config, boolean resourcePreload) {
return loadConfig(config, config.getKey(), resourcePreload);
}
default Path loadConfig(Config config) {
return loadConfig(config, config.getKey());
}

View file

@ -6,7 +6,6 @@ import org.zhdev.varioutil.sql.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;