Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Oct 20, 2024
1 parent 25f7c33 commit 78ed234
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 73 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.1.1

- Use zip reader for more reliable config reading
- Properly fallback to default when no config is found

# 2.1.0

- Add custom controller support
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx4G
fabric_versions=1.20.1, 1.21.1
neoforge_versions=1.21.1

mod_version=2.1.0
mod_version=2.1.1
mod_group=com.bawnorton
mod_id=configurable
mod_name=Configurable
Expand Down
73 changes: 40 additions & 33 deletions src/main/java/com/bawnorton/configurable/ConfigurableMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class ConfigurableMain {
public static final String MOD_ID = "configurable";
Expand All @@ -45,46 +50,48 @@ public static void init() {
Networking.init();

Platform.forEachJar(path -> {
Path configurable = path.resolve("configurable");
if (!(Files.exists(configurable) && Files.isDirectory(configurable))) return;

List<Path> sourceSetSettings = new ArrayList<>();
try (Stream<Path> stream = Files.list(configurable)) {
stream.filter(Files::isRegularFile).forEach(sourceSetSettings::add);
} catch (IOException e) {
ConfigurableMain.LOGGER.error("Could not find configurable settings", e);
}

for(Path sourceSetSetting : sourceSetSettings) {
ConfigurableSettings settings;
try {
settings = GSON.fromJson(Files.newBufferedReader(sourceSetSetting), ConfigurableSettings.class);
} catch (IOException e) {
LOGGER.error("Could not load configurable settings", e);
return;
}
try (ZipFile zipFile = new ZipFile(path.toFile())) {
zipFile.stream()
.filter(entry -> entry.getName().startsWith("configurable/") && !entry.isDirectory())
.forEach(entry -> {
Path filePath = Paths.get(entry.getName());
sourceSetSettings.add(filePath);
});
for (Path sourceSetSetting : sourceSetSettings) {
ConfigurableSettings settings;
try {
try (InputStream is = zipFile.getInputStream(zipFile.getEntry(sourceSetSetting.toString()))) {
settings = GSON.fromJson(new InputStreamReader(is), ConfigurableSettings.class);
}
} catch (IOException e) {
LOGGER.error("Could not load configurable settings", e);
return;
}

String configName = settings.name();
String sourceSet = settings.sourceSet();
String configName = settings.name();
String sourceSet = settings.sourceSet();

if(WRAPPERS.containsKey(configName)) {
Map<String, ConfigurableWrapper> wrappers = WRAPPERS.get(configName);
if(wrappers.containsKey(sourceSet)) {
throw new IllegalStateException("Conflicting config name \"%s\" for source set \"%s\" found in \"%s\"".formatted(configName, sourceSet, sourceSetSetting));
if(WRAPPERS.containsKey(configName)) {
Map<String, ConfigurableWrapper> wrappers = WRAPPERS.get(configName);
if(wrappers.containsKey(sourceSet)) {
throw new IllegalStateException("Conflicting config name \"%s\" for source set \"%s\" found in \"%s\"".formatted(configName, sourceSet, sourceSetSetting));
}
}
}

try {
ConfigurableWrapper wrapper = new ConfigurableWrapper(ConfigurableApiImplLoader.getImpl(configName));
WRAPPERS.computeIfAbsent(configName, k -> new HashMap<>()).put(sourceSet, wrapper);
addToWrapped(settings::fullyQualifiedLoader, wrapper::setLoader, configName);
if(settings.hasScreenFactory() && !Platform.isServer()) {
addToWrapped(settings::fullyQualifiedScreenFactory, wrapper::setScreenFactory, configName);
try {
ConfigurableWrapper wrapper = new ConfigurableWrapper(ConfigurableApiImplLoader.getImpl(configName));
WRAPPERS.computeIfAbsent(configName, k -> new HashMap<>()).put(sourceSet, wrapper);
addToWrapped(settings::fullyQualifiedLoader, wrapper::setLoader, configName);
if(settings.hasScreenFactory() && !Platform.isServer()) {
addToWrapped(settings::fullyQualifiedScreenFactory, wrapper::setScreenFactory, configName);
}
} catch (IllegalStateException e) {
LOGGER.error("Could not create configurable wrapper for \"%s\"".formatted(configName), e);
}
} catch (IllegalStateException e) {
LOGGER.error("Could not create configurable wrapper for \"%s\"".formatted(configName), e);
}
}
} catch (IOException ignored) {}

});
WRAPPERS.values().forEach(sourceSetWrappers -> sourceSetWrappers.values().forEach(wrapper -> {
wrapper.loadConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public Config loadConfig(UnaryOperator<String> datafixer) {
try {
GsonReader reader = new GsonReader(JsonReader.json5(Files.newBufferedReader(loadingPath)));
JsonObject config = GSON.fromJson(reader, JsonObject.class);
if(config == null) {
ConfigurableMain.LOGGER.warn("No config \\"<file_name>\\" found, using default");
return new Config();
}
Config parsed = parseConfig(config, true);
if(usingLegacyConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//? if fabric {

import com.google.gson.Gson;
/*import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import javax.annotation.processing.Filer;
Expand Down Expand Up @@ -46,4 +46,4 @@ public String getName() {
return fmj.get("id").getAsString();
}
}
//?}
*///?}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//? if neoforge {

/*import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.toml.TomlParser;
import javax.annotation.processing.Filer;
import java.io.IOException;
Expand Down Expand Up @@ -42,4 +42,4 @@ public String getName() {
return config.<ArrayList<Config>>get("mods").getFirst().get("modId");
}
}
*///?}
//?}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public final class SourceProviders {

public static void registerDefaultSourceProviders() {
//? if fabric
registerSourceProvider(FabricSourceProvider::new);
/*registerSourceProvider(FabricSourceProvider::new);*/
//? if neoforge
/*registerSourceProvider(NeoForgeSourceProvider::new);*/
registerSourceProvider(NeoForgeSourceProvider::new);
}

public static void registerSourceProvider(SourceProviderFactory factory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ default FieldNamingStrategy defaultFieldNamingStrategy() {
}

//? if !fabric {
/*/^*
/**
* The name of the config this API impl belongs to, this is necessary for non-fabric implementations
* <br>
* <b>THIS DOES NOT SET THE NAME OF THE CONFIG</b>
^/
*/
default String getConfigName() {
throw new UnsupportedOperationException();
}
*///?}
//?}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.spongepowered.asm.mixin.Mixin;

//? if fabric {
import com.bawnorton.configurable.ConfigurableMain;
/*import com.bawnorton.configurable.ConfigurableMain;
import com.bawnorton.configurable.load.ConfigurableWrapper;
import com.terraformersmc.modmenu.ModMenu;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
Expand All @@ -25,10 +25,10 @@ public abstract class ModMenuMixin {
@Shadow @Final private static List<ModMenuApi> apiImplementations;
//?} else {
/*@Shadow private static Map<String, ConfigScreenFactory<?>> configScreenFactories;
/^@Shadow private static Map<String, ConfigScreenFactory<?>> configScreenFactories;
@Shadow private static List<Map<String, ConfigScreenFactory<?>>> delayedScreenFactoryProviders;
*///?}
^///?}
@Inject(
Expand Down Expand Up @@ -59,14 +59,14 @@ public ConfigScreenFactory<?> getModConfigScreenFactory() {
//? if >=1.21 {
apiImplementations.add(api);
//?} else {
/*delayedScreenFactoryProviders.add(api.getProvidedConfigScreenFactories());
*///?}
/^delayedScreenFactoryProviders.add(api.getProvidedConfigScreenFactories());
^///?}
}));
}
}
//?} else {
/*import com.bawnorton.configurable.client.ConfigurableClient;
*///?} else {
import com.bawnorton.configurable.client.ConfigurableClient;

@Mixin(ConfigurableClient.class)
public abstract class ModMenuMixin {}
*///?}
//?}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import com.bawnorton.configurable.client.ConfigurableClient;

//? if fabric {
import net.fabricmc.api.ClientModInitializer;
/*import net.fabricmc.api.ClientModInitializer;
public final class ConfigurableClientWrapper implements ClientModInitializer {
@Override
public void onInitializeClient() {
ConfigurableClient.init();
}
}
//?} elif neoforge {
/*import com.bawnorton.configurable.ConfigurableMain;
*///?} elif neoforge {
import com.bawnorton.configurable.ConfigurableMain;
import com.bawnorton.configurable.platform.Platform;
import net.minecraft.client.MinecraftClient;
import net.neoforged.api.distmarker.Dist;
Expand Down Expand Up @@ -43,4 +43,4 @@ public ConfigurableClientWrapper() {
}));
}
}
*///?}
//?}
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
import java.util.Map;

//? if fabric {
import net.fabricmc.loader.api.FabricLoader;
//?} elif neoforge {
/*import java.util.ServiceLoader;
*///?}
/*import net.fabricmc.loader.api.FabricLoader;
*///?} elif neoforge {
import java.util.ServiceLoader;
//?}

public final class ConfigurableApiImplLoader {
//? if neoforge
/*private static final ServiceLoader<ConfigurableApi> serviceLoader = ServiceLoader.load(ConfigurableApi.class);*/
private static final ServiceLoader<ConfigurableApi> serviceLoader = ServiceLoader.load(ConfigurableApi.class);
private static final Map<String, ConfigurableApi> impls = new HashMap<>();

public static void load() {
//? if fabric {
FabricLoader.getInstance().getEntrypointContainers("configurable", ConfigurableApi.class).forEach(container -> {
/*FabricLoader.getInstance().getEntrypointContainers("configurable", ConfigurableApi.class).forEach(container -> {
String id = container.getProvider().getMetadata().getId();
try {
impls.put(id, container.getEntrypoint());
} catch (Throwable e) {
ConfigurableMain.LOGGER.error("Mod {} provides a broken ConfigurableApi implemenation", id, e);
}
});
//?} elif neoforge {
/*serviceLoader.forEach(apiImpl -> impls.put(apiImpl.getConfigName(), apiImpl));
*///?}
*///?} elif neoforge {
serviceLoader.forEach(apiImpl -> impls.put(apiImpl.getConfigName(), apiImpl));
//?}
}

public static ConfigurableApi getImpl(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import com.bawnorton.configurable.ConfigurableMain;

//? if fabric {
import net.fabricmc.api.ModInitializer;
/*import net.fabricmc.api.ModInitializer;
public final class ConfigurableMainWrapper implements ModInitializer {
@Override
public void onInitialize() {
ConfigurableMain.init();
}
}
//?} elif neoforge {
/*import net.neoforged.fml.common.Mod;
*///?} elif neoforge {
import net.neoforged.fml.common.Mod;

@Mod(ConfigurableMain.MOD_ID)
public final class ConfigurableMainWrapper {
public ConfigurableMainWrapper() {
ConfigurableMain.init();
}
}
*///?}
//?}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.function.Consumer;

//? if fabric {
import net.fabricmc.api.EnvType;
/*import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import java.util.List;
Expand Down Expand Up @@ -35,8 +35,8 @@ public static boolean isServer() {
}
}
//?} elif neoforge {
/*import net.neoforged.fml.ModContainer;
*///?} elif neoforge {
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.fml.loading.LoadingModList;
Expand Down Expand Up @@ -80,4 +80,4 @@ public static boolean isServer() {
return FMLEnvironment.dist.isDedicatedServer();
}
}
*///?}
//?}
2 changes: 1 addition & 1 deletion stonecutter.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id("dev.kikugie.stonecutter")
}
stonecutter active "1.21.1-fabric-yarn" /* [SC] DO NOT EDIT */
stonecutter active "1.21.1-neoforge-yarn" /* [SC] DO NOT EDIT */

stonecutter registerChiseled tasks.register("chiseledBuildAndCollect", stonecutter.chiseled) {
group = "project"
Expand Down

0 comments on commit 78ed234

Please sign in to comment.