mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
Merge branch 'master' into feat/meta-pack
This commit is contained in:
@@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of Terra.
|
|
||||||
*
|
|
||||||
* Terra is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Terra is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.dfsek.terra.config.loaders.config;
|
|
||||||
|
|
||||||
import com.dfsek.tectonic.api.depth.DepthTracker;
|
|
||||||
import com.dfsek.tectonic.api.exception.LoadException;
|
|
||||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
|
||||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.AnnotatedType;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
import com.dfsek.terra.api.config.ConfigPack;
|
|
||||||
import com.dfsek.terra.api.properties.Properties;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* @deprecated Use the Image and ImageLoader class provided by the library-image addon instead. This is subject to removal in v7.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public class BufferedImageLoader implements TypeLoader<BufferedImage> {
|
|
||||||
|
|
||||||
private final ConfigPack pack;
|
|
||||||
|
|
||||||
public BufferedImageLoader(ConfigPack pack) {
|
|
||||||
this.pack = pack;
|
|
||||||
if(!pack.getContext().has(ImageCache.class))
|
|
||||||
pack.getContext().put(new ImageCache(new ConcurrentHashMap<>()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BufferedImage load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader, DepthTracker depthTracker)
|
|
||||||
throws LoadException {
|
|
||||||
return pack.getContext().get(ImageCache.class).map.computeIfAbsent((String) c, s -> {
|
|
||||||
try {
|
|
||||||
return ImageIO.read(Files.newInputStream(pack.getRootPath().resolve(s)));
|
|
||||||
} catch(IOException e) {
|
|
||||||
throw new LoadException("Unable to load image", e, depthTracker);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Cache prevents configs from loading the same image multiple times into memory
|
|
||||||
*/
|
|
||||||
private record ImageCache(ConcurrentHashMap<String, BufferedImage> map) implements Properties {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -33,7 +33,6 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
@@ -77,7 +76,6 @@ import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
|||||||
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
|
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
|
||||||
import com.dfsek.terra.api.world.chunk.generation.util.provider.ChunkGeneratorProvider;
|
import com.dfsek.terra.api.world.chunk.generation.util.provider.ChunkGeneratorProvider;
|
||||||
import com.dfsek.terra.config.loaders.GenericTemplateSupplierLoader;
|
import com.dfsek.terra.config.loaders.GenericTemplateSupplierLoader;
|
||||||
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
|
|
||||||
import com.dfsek.terra.config.preprocessor.MetaListLikePreprocessor;
|
import com.dfsek.terra.config.preprocessor.MetaListLikePreprocessor;
|
||||||
import com.dfsek.terra.config.preprocessor.MetaMapPreprocessor;
|
import com.dfsek.terra.config.preprocessor.MetaMapPreprocessor;
|
||||||
import com.dfsek.terra.config.preprocessor.MetaNumberPreprocessor;
|
import com.dfsek.terra.config.preprocessor.MetaNumberPreprocessor;
|
||||||
@@ -273,8 +271,7 @@ public class ConfigPackImpl implements ConfigPack {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void register(TypeRegistry registry) {
|
public void register(TypeRegistry registry) {
|
||||||
registry.registerLoader(ConfigType.class, configTypeRegistry)
|
registry.registerLoader(ConfigType.class, configTypeRegistry);
|
||||||
.registerLoader(BufferedImage.class, new BufferedImageLoader(this));
|
|
||||||
registryMap.forEach(registry::registerLoader);
|
registryMap.forEach(registry::registerLoader);
|
||||||
shortcuts.forEach(registry::registerLoader); // overwrite with delegated shortcuts if present
|
shortcuts.forEach(registry::registerLoader); // overwrite with delegated shortcuts if present
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -51,11 +52,11 @@ import com.dfsek.terra.api.util.reflection.TypeKey;
|
|||||||
public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||||
private static final Entry<?> NULL = new Entry<>(null);
|
private static final Entry<?> NULL = new Entry<>(null);
|
||||||
private final Map<RegistryKey, Entry<T>> objects;
|
private final Map<RegistryKey, Entry<T>> objects;
|
||||||
private final ListMultimap<String, Pair<RegistryKey, Entry<T>>> objectIDs = Multimaps.newListMultimap(new HashMap<>(), ArrayList::new);
|
private final ListMultimap<String, Pair<RegistryKey, Entry<T>>> objectIDs = Multimaps.newListMultimap(new ConcurrentHashMap<>(), ArrayList::new);
|
||||||
private final TypeKey<T> typeKey;
|
private final TypeKey<T> typeKey;
|
||||||
|
|
||||||
public OpenRegistryImpl(TypeKey<T> typeKey) {
|
public OpenRegistryImpl(TypeKey<T> typeKey) {
|
||||||
this(new HashMap<>(), typeKey);
|
this(new ConcurrentHashMap<>(), typeKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected OpenRegistryImpl(Map<RegistryKey, Entry<T>> init, TypeKey<T> typeKey) {
|
protected OpenRegistryImpl(Map<RegistryKey, Entry<T>> init, TypeKey<T> typeKey) {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import com.dfsek.terra.api.Platform;
|
import com.dfsek.terra.api.Platform;
|
||||||
@@ -44,9 +45,9 @@ public class ConfigRegistry extends OpenRegistryImpl<ConfigPack> {
|
|||||||
public synchronized void loadAll(Platform platform) throws IOException, PackLoadFailuresException {
|
public synchronized void loadAll(Platform platform) throws IOException, PackLoadFailuresException {
|
||||||
Path packsDirectory = platform.getDataFolder().toPath().resolve("packs");
|
Path packsDirectory = platform.getDataFolder().toPath().resolve("packs");
|
||||||
Files.createDirectories(packsDirectory);
|
Files.createDirectories(packsDirectory);
|
||||||
List<Exception> failedLoads = new ArrayList<>();
|
List<Exception> failedLoads = new CopyOnWriteArrayList<>();
|
||||||
try(Stream<Path> packs = Files.list(packsDirectory)) {
|
try(Stream<Path> packs = Files.list(packsDirectory)) {
|
||||||
packs.forEach(path -> {
|
packs.parallel().forEach(path -> {
|
||||||
try {
|
try {
|
||||||
ConfigPack pack = new ConfigPackImpl(path, platform);
|
ConfigPack pack = new ConfigPackImpl(path, platform);
|
||||||
registerChecked(pack.getRegistryKey(), pack);
|
registerChecked(pack.getRegistryKey(), pack);
|
||||||
|
|||||||
Reference in New Issue
Block a user