Remove extra configs again and move them into resource-packs and mods

This commit is contained in:
Blue (Lukas Rieger) 2020-01-07 00:48:06 +01:00
parent 41334364b0
commit 1c44bd2ac4
8 changed files with 216 additions and 119 deletions

View File

@ -86,12 +86,12 @@ public class BlueMapCLI {
Preconditions.checkNotNull(resourcePack);
MainConfig config = configManager.getMainConfig();
configManager.loadResourceConfigs(resourcePack);
config.getWebDataPath().toFile().mkdirs();
Map<String, MapType> maps = new HashMap<>();
configManager.getBlockPropertiesConfig().setResourcePack(resourcePack);
for (MapConfig mapConfig : config.getMapConfigs()) {
File mapPath = new File(mapConfig.getWorldPath());
if (!mapPath.exists() || !mapPath.isDirectory()) {
@ -214,11 +214,6 @@ public class BlueMapCLI {
resourceExtensionsFile.delete();
FileUtils.copyURLToFile(BlueMapCLI.class.getResource("/resourceExtensions.zip"), resourceExtensionsFile, 10000, 10000);
File blockColorsConfigFile = new File(configFolder, "blockColors.json");
if (!blockColorsConfigFile.exists()) {
FileUtils.copyURLToFile(BlueMapCLI.class.getResource("/blockColors.json"), blockColorsConfigFile, 10000, 10000);
}
//find more resource packs
File resourcePackFolder = configFolder.toPath().resolve("resourcepacks").toFile();
resourcePackFolder.mkdirs();
@ -233,7 +228,6 @@ public class BlueMapCLI {
resourcePack = new ResourcePack();
if (textureExportFile.exists()) resourcePack.loadTextureFile(textureExportFile);
resourcePack.load(resources);
resourcePack.loadBlockColorConfig(blockColorsConfigFile);
resourcePack.saveTextureFile(textureExportFile);
return true;
@ -281,7 +275,7 @@ public class BlueMapCLI {
ConfigManager config = new ConfigManager(configFolder, cliConfigUrl, cliDefaultsUrl);
boolean configCreated = !config.getMainConfigFile().exists();
config.loadOrCreateConfigs();
config.loadMainConfig();
if (configCreated) {
Logger.global.logInfo("No config file found! Created default configs here: " + configFolder);

View File

@ -53,11 +53,12 @@ public class BlockPropertiesConfig implements BlockPropertiesMapper {
private ResourcePack resourcePack = null;
public BlockPropertiesConfig(ConfigurationNode node) throws IOException {
this(node, null);
public BlockPropertiesConfig(ConfigurationNode node, ResourcePack resourcePack) throws IOException {
this(node, resourcePack, null);
}
public BlockPropertiesConfig(ConfigurationNode node, ConfigurationLoader<? extends ConfigurationNode> autopoulationConfigLoader) throws IOException {
public BlockPropertiesConfig(ConfigurationNode node, ResourcePack resourcePack, ConfigurationLoader<? extends ConfigurationNode> autopoulationConfigLoader) throws IOException {
this.resourcePack = resourcePack;
this.autopoulationConfigLoader = autopoulationConfigLoader;
mappings = MultimapBuilder.hashKeys().arrayListValues().build();
@ -86,14 +87,6 @@ public class BlockPropertiesConfig implements BlockPropertiesMapper {
});
}
/**
* Sets the {@link ResourcePack} of this PropertyMapper, so it can generate better defaults if the mapping is missing
* @param resourcePack the {@link ResourcePack}
*/
public void setResourcePack(ResourcePack resourcePack) {
this.resourcePack = resourcePack;
}
@Override
public BlockProperties get(BlockState from){
try {

View File

@ -27,6 +27,7 @@ package de.bluecolored.bluemap.core.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@ -37,7 +38,9 @@ import java.util.Set;
import java.util.stream.Collectors;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.resourcepack.ResourcePack.Resource;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.gson.GsonConfigurationLoader;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
@ -109,20 +112,103 @@ public class ConfigManager {
return new File(configFolder, "biomes.json");
}
public void loadOrCreateConfigs() throws IOException {
mainConfig = new MainConfig(loadOrCreate(getMainConfigFile(), defaultMainConfig, mainConfigDefaultValues, true));
URL blockIdsConfigUrl = BlueMap.class.getResource("/blockIds.json");
blockIdConfig = new BlockIdConfig(loadOrCreate(getBlockIdConfigFile(), null, blockIdsConfigUrl, false), getLoader(makeAutogen(getBlockIdConfigFile())));
URL blockPropertiesConfigUrl = BlueMap.class.getResource("/blockProperties.json");
blockPropertiesConfig = new BlockPropertiesConfig(loadOrCreate(getBlockPropertiesConfigFile(), null, blockPropertiesConfigUrl, false), getLoader(makeAutogen(getBlockPropertiesConfigFile())));
URL biomeConfigUrl = BlueMap.class.getResource("/biomes.json");
biomeConfig = new BiomeConfig(loadOrCreate(getBiomeConfigFile(), null, biomeConfigUrl, false), getLoader(makeAutogen(getBiomeConfigFile())));
public File getBlockColorConfigFile() {
return new File(configFolder, "blockColors.json");
}
private ConfigurationNode loadOrCreate(File configFile, URL defaultConfig, URL defaultValues, boolean usePlaceholders) throws IOException {
public void loadMainConfig() throws IOException {
mainConfig = new MainConfig(
loadOrCreate(
getMainConfigFile(),
defaultMainConfig,
mainConfigDefaultValues,
true,
true
)
);
}
public void loadResourceConfigs(ResourcePack resourcePack) throws IOException {
//load blockColors.json from resources, config-folder and resourcepack
URL blockColorsConfigUrl = BlueMap.class.getResource("/blockColors.json");
ConfigurationNode blockColorsConfigNode = loadOrCreate(
getBlockColorConfigFile(),
null,
blockColorsConfigUrl,
false,
false
);
blockColorsConfigNode = joinFromResourcePack(resourcePack, "blockColors.json", blockColorsConfigNode);
resourcePack.getBlockColorCalculator().loadColorConfig(blockColorsConfigNode);
//load blockIds.json from resources, config-folder and resourcepack
URL blockIdsConfigUrl = BlueMap.class.getResource("/blockIds.json");
ConfigurationNode blockIdsConfigNode = loadOrCreate(
getBlockIdConfigFile(),
null,
blockIdsConfigUrl,
false,
false
);
blockIdsConfigNode = joinFromResourcePack(resourcePack, "blockIds.json", blockIdsConfigNode);
blockIdConfig = new BlockIdConfig(
blockIdsConfigNode,
null //getLoader(makeAutogen(getBlockIdConfigFile()))
);
//load blockProperties.json from resources, config-folder and resourcepack
URL blockPropertiesConfigUrl = BlueMap.class.getResource("/blockProperties.json");
ConfigurationNode blockPropertiesConfigNode = loadOrCreate(
getBlockPropertiesConfigFile(),
null,
blockPropertiesConfigUrl,
false,
false
);
blockPropertiesConfigNode = joinFromResourcePack(resourcePack, "blockProperties.json", blockPropertiesConfigNode);
blockPropertiesConfig = new BlockPropertiesConfig(
blockPropertiesConfigNode,
resourcePack,
null //getLoader(makeAutogen(getBlockPropertiesConfigFile()))
);
//load biomes.json from resources, config-folder and resourcepack
URL biomeConfigUrl = BlueMap.class.getResource("/biomes.json");
ConfigurationNode biomeConfigNode = loadOrCreate(
getBiomeConfigFile(),
null,
biomeConfigUrl,
false,
false
);
biomeConfigNode = joinFromResourcePack(resourcePack, "biomes.json", biomeConfigNode);
biomeConfig = new BiomeConfig(
biomeConfigNode,
null //getLoader(makeAutogen(getBiomeConfigFile()))
);
}
private ConfigurationNode joinFromResourcePack(ResourcePack resourcePack, String configFileName, ConfigurationNode defaultConfig) {
ConfigurationNode joinedNode = null;
for (Resource resource : resourcePack.getConfigAdditions(configFileName)) {
try {
ConfigurationNode node = getLoader(configFileName, resource.read()).load();
if (joinedNode == null) joinedNode = node;
else joinedNode.mergeValuesFrom(node);
} catch (IOException ex) {
Logger.global.logWarning("Failed to load an additional " + configFileName + " from the resource-pack! " + ex);
}
}
if (joinedNode == null) return defaultConfig;
joinedNode.mergeValuesFrom(defaultConfig);
return joinedNode;
}
private ConfigurationNode loadOrCreate(File configFile, URL defaultConfig, URL defaultValues, boolean usePlaceholders, boolean generateEmptyConfig) throws IOException {
ConfigurationNode configNode;
if (!configFile.exists()) {
@ -153,7 +239,7 @@ public class ConfigManager {
configNode = loader.createEmptyNode();
//save to create file
loader.save(configNode);
if (generateEmptyConfig) loader.save(configNode);
}
} else {
//load config
@ -169,11 +255,21 @@ public class ConfigManager {
return configNode;
}
/*
private File makeAutogen(File file) throws IOException {
File autogenFile = file.getCanonicalFile().toPath().getParent().resolve("generated").resolve(file.getName()).toFile();
autogenFile.getParentFile().mkdirs();
return autogenFile;
}
*/
private ConfigurationLoader<? extends ConfigurationNode> getLoader(String filename, InputStream is){
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
if (filename.endsWith(".json")) return GsonConfigurationLoader.builder().setSource(() -> reader).build();
if (filename.endsWith(".yaml") || filename.endsWith(".yml")) return YAMLConfigurationLoader.builder().setSource(() -> reader).build();
else return HoconConfigurationLoader.builder().setSource(() -> reader).build();
}
private ConfigurationLoader<? extends ConfigurationNode> getLoader(URL url){
if (url.getFile().endsWith(".json")) return GsonConfigurationLoader.builder().setURL(url).build();

View File

@ -26,7 +26,6 @@ package de.bluecolored.bluemap.core.resourcepack;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ -43,7 +42,6 @@ import de.bluecolored.bluemap.core.util.MathUtils;
import de.bluecolored.bluemap.core.world.Biome;
import de.bluecolored.bluemap.core.world.Block;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.gson.GsonConfigurationLoader;
public class BlockColorCalculator {
@ -59,14 +57,9 @@ public class BlockColorCalculator {
this.blockColorMap = new HashMap<>();
}
public void loadColorConfig(File configFile) throws IOException {
public void loadColorConfig(ConfigurationNode colorConfig) throws IOException {
blockColorMap.clear();
ConfigurationNode colorConfig = GsonConfigurationLoader.builder()
.setFile(configFile)
.build()
.load();
for (Entry<Object, ? extends ConfigurationNode> entry : colorConfig.getChildrenMap().entrySet()){
String key = entry.getKey().toString();
String value = entry.getValue().getString();

View File

@ -25,8 +25,11 @@
package de.bluecolored.bluemap.core.resourcepack;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
@ -34,11 +37,16 @@ import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.commons.io.output.ByteArrayOutputStream;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.resourcepack.BlockStateResource.Builder;
import de.bluecolored.bluemap.core.resourcepack.fileaccess.BluemapAssetOverrideFileAccess;
import de.bluecolored.bluemap.core.resourcepack.fileaccess.CombinedFileAccess;
import de.bluecolored.bluemap.core.resourcepack.fileaccess.FileAccess;
import de.bluecolored.bluemap.core.resourcepack.fileaccess.ResourcePackOldFormatFileAccess;
import de.bluecolored.bluemap.core.world.BlockState;
/**
@ -49,6 +57,13 @@ public class ResourcePack {
public static final String MINECRAFT_CLIENT_VERSION = "1.14.4";
public static final String MINECRAFT_CLIENT_URL = "https://launcher.mojang.com/v1/objects/8c325a0c5bd674dd747d6ebaa4c791fd363ad8a9/client.jar";
private static final String[] CONFIG_FILES = {
"blockColors.json",
"blockIds.json",
"blockProperties.json",
"biomes.json"
};
protected Map<String, BlockStateResource> blockStateResources;
protected Map<String, BlockModelResource> blockModelResources;
protected TextureGallery textures;
@ -58,6 +73,8 @@ public class ResourcePack {
private BufferedImage foliageMap;
private BufferedImage grassMap;
private Multimap<String, Resource> configs;
public ResourcePack() {
blockStateResources = new HashMap<>();
blockModelResources = new HashMap<>();
@ -67,10 +84,14 @@ public class ResourcePack {
grassMap = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
grassMap.setRGB(0, 0, 0xFF00FF00);
blockColorCalculator = new BlockColorCalculator(foliageMap, grassMap);
configs = MultimapBuilder.hashKeys().arrayListValues().build();
}
public void loadBlockColorConfig(File file) throws IOException {
blockColorCalculator.loadColorConfig(file);
/**
* Returns all config-files found in the namespaces of the ResourcePack with that filename
*/
public Collection<Resource> getConfigAdditions(String configFileName){
return configs.get(configFileName);
}
/**
@ -110,15 +131,16 @@ public class ResourcePack {
* @param sources The list of {@link File} sources. Each can be a folder or any zip-compressed file. (E.g. .zip or .jar)
*/
public void load(File... sources) {
try (CombinedFileAccess combinedSourcesAccess = new CombinedFileAccess()){
try (CombinedFileAccess combinedSources = new CombinedFileAccess()){
for (File file : sources) {
try {
combinedSourcesAccess.addFileAccess(FileAccess.of(file));
combinedSources.addFileAccess(FileAccess.of(file));
} catch (IOException e) {
Logger.global.logError("Failed to read ResourcePack: " + file, e);
}
}
FileAccess sourcesAccess = ResourcePackOldFormatFileAccess.from(combinedSourcesAccess);
FileAccess sourcesAccess = new BluemapAssetOverrideFileAccess(combinedSources);
textures.reloadAllTextures(sourcesAccess);
@ -126,6 +148,7 @@ public class ResourcePack {
Collection<String> namespaces = sourcesAccess.listFolders("assets");
for (String namespaceRoot : namespaces) {
//load blockstates
String namespace = namespaceRoot.substring("assets/".length());
Collection<String> blockstateFiles = sourcesAccess.listFiles(namespaceRoot + "/blockstates", true);
for (String blockstateFile : blockstateFiles) {
@ -138,16 +161,31 @@ public class ResourcePack {
Logger.global.logError("Failed to load blockstate: " + namespace + ":" + filename.substring(0, filename.length() - 5), ex);
}
}
//load configs
for (String configName : CONFIG_FILES) {
try {
Resource config = new Resource(sourcesAccess.readFile("assets/" + namespace + "/" + configName));
configs.put(configName, config);
} catch (FileNotFoundException ignore) {
} catch (IOException ex) {
Logger.global.logError("Failed to load config for " + namespace + ": " + configName, ex);
}
}
}
try {
foliageMap = ImageIO.read(sourcesAccess.readFile("assets/minecraft/textures/colormap/foliage.png"));
grassMap = ImageIO.read(sourcesAccess.readFile("assets/minecraft/textures/colormap/grass.png"));
blockColorCalculator.setFoliageMap(foliageMap);
blockColorCalculator.setGrassMap(grassMap);
blockColorCalculator.setFoliageMap(foliageMap);
} catch (IOException ex) {
Logger.global.logError("Failed to load foliage- or grass-map!", ex);
Logger.global.logError("Failed to load foliagemap!", ex);
}
try {
grassMap = ImageIO.read(sourcesAccess.readFile("assets/minecraft/textures/colormap/grass.png"));
blockColorCalculator.setGrassMap(grassMap);
} catch (IOException ex) {
Logger.global.logError("Failed to load grassmap!", ex);
}
} catch (IOException ex) {
@ -203,4 +241,29 @@ public class ResourcePack {
return path;
}
/**
* Caches a full InputStream in a byte-array that can be read later
*/
public class Resource {
private byte[] data;
public Resource(InputStream data) throws FileNotFoundException, IOException {
try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
bout.write(data);
this.data = bout.toByteArray();
} finally {
data.close();
}
}
/**
* Creates a new InputStream to read this resource
*/
public InputStream read() {
return new ByteArrayInputStream(this.data);
}
}
}

View File

@ -27,44 +27,37 @@ package de.bluecolored.bluemap.core.resourcepack.fileaccess;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* This {@link FileAccess} tries to make 1.12/1.13/1.14 ResourcePacks compatible with each other
* This {@link FileAccess} maps its parent {@link FileAccess} to first look in assets/[namespace]/bluemap/... instead of assets/[namespace]/...
*/
public class ResourcePackOldFormatFileAccess implements FileAccess {
public class BluemapAssetOverrideFileAccess implements FileAccess {
private FileAccess parent;
public FileAccess parent;
protected ResourcePackOldFormatFileAccess(FileAccess parent) {
public BluemapAssetOverrideFileAccess(FileAccess parent) {
this.parent = parent;
}
@Override
public void close() throws IOException {
parent.close();
}
@Override
public InputStream readFile(String path) throws FileNotFoundException, IOException {
String[] pathParts = StringUtils.split(path, "/");
if (pathParts.length < 3 || !pathParts[0].equals("assets")) return parent.readFile(path);
String[] newParts = new String[pathParts.length + 1];
System.arraycopy(pathParts, 0, newParts, 0, 2);
System.arraycopy(pathParts, 2, newParts, 3, pathParts.length - 2);
newParts[2] = "bluemap";
String newPath = String.join("/", newParts);
try {
return parent.readFile(path);
return parent.readFile(newPath);
} catch (FileNotFoundException ex) {
for (String altPath : otherPathsToTry(path)) {
try {
return parent.readFile(altPath);
} catch (FileNotFoundException ex2) {
ex.addSuppressed(ex2);
} catch (IOException ex2) {
ex.addSuppressed(ex2);
throw ex;
}
}
throw ex;
return parent.readFile(path);
}
}
@ -78,38 +71,9 @@ public class ResourcePackOldFormatFileAccess implements FileAccess {
return parent.listFolders(path);
}
private Collection<String> otherPathsToTry(String path){
path = FileAccess.normalize(path);
List<String> paths = new ArrayList<>();
String[] parts = path.split(Pattern.quote("/"));
@Override
public void close() throws IOException {
parent.close();
}
//handle block/blocks folder-differences
if (parts.length >= 4 && parts[0].equals("assets") && parts[2].equals("models")) {
if (parts[3].equals("block")) {
parts[3] = "blocks";
paths.add(String.join("/", parts));
} else if (parts[3].equals("blocks")) {
parts[3] = "block";
paths.add(String.join("/", parts));
} else {
String[] newParts = new String[parts.length + 1];
System.arraycopy(parts, 0, newParts, 0, 3);
System.arraycopy(parts, 3, newParts, 4, parts.length - 3);
newParts[3] = "blocks";
paths.add(String.join("/", newParts));
newParts[3] = "block";
paths.add(String.join("/", newParts));
}
}
return paths;
}
public static ResourcePackOldFormatFileAccess from(FileAccess source) {
if (source instanceof ResourcePackOldFormatFileAccess) return (ResourcePackOldFormatFileAccess) source;
return new ResourcePackOldFormatFileAccess(source);
}
}

View File

@ -138,14 +138,9 @@ public class SpongePlugin {
URL defaultSpongeConfig = SpongePlugin.class.getResource("/bluemap-sponge.conf");
URL spongeConfigDefaults = SpongePlugin.class.getResource("/bluemap-sponge-defaults.conf");
ConfigManager configManager = new ConfigManager(getConfigPath().toFile(), defaultSpongeConfig, spongeConfigDefaults);
configManager.loadOrCreateConfigs();
configManager.loadMainConfig();
config = configManager.getMainConfig();
File blockColorsConfigFile = getConfigPath().resolve("blockColors.json").toFile();
if (!blockColorsConfigFile.exists()) {
FileUtils.copyURLToFile(SpongePlugin.class.getResource("/blockColors.json"), blockColorsConfigFile, 10000, 10000);
}
//load resources
File defaultResourceFile = config.getDataPath().resolve("minecraft-client-" + ResourcePack.MINECRAFT_CLIENT_VERSION + ".jar").toFile();
File resourceExtensionsFile = config.getDataPath().resolve("resourceExtensions.zip").toFile();
@ -176,10 +171,9 @@ public class SpongePlugin {
resourcePack = new ResourcePack();
if (textureExportFile.exists()) resourcePack.loadTextureFile(textureExportFile);
resourcePack.load(resources);
resourcePack.loadBlockColorConfig(blockColorsConfigFile);
resourcePack.saveTextureFile(textureExportFile);
configManager.getBlockPropertiesConfig().setResourcePack(resourcePack);
configManager.loadResourceConfigs(resourcePack);
//load maps
for (MapConfig mapConfig : config.getMapConfigs()) {

View File

@ -1,8 +1,8 @@
rootProject.name = 'BlueMap'
include ':BlueMapCore'
include ':BlueMapCLI'
include ':BlueMapSponge'
//include ':BlueMapSponge'
project(':BlueMapCore').projectDir = "$rootDir/BlueMapCore" as File
project(':BlueMapCLI').projectDir = "$rootDir/BlueMapCLI" as File
project(':BlueMapSponge').projectDir = "$rootDir/BlueMapSponge" as File
//project(':BlueMapSponge').projectDir = "$rootDir/BlueMapSponge" as File