Skip to content

Commit

Permalink
Add config option for deserialization warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jul 2, 2022
1 parent 88408c2 commit 514c2c1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,7 @@ public void fromTag(NbtCompound tag) {
}
}

for (String missedKeyId : componentMap.getKeys()) {
Identifier id = Identifier.tryParse(missedKeyId);
String cause;
if (id == null) cause = "invalid identifier";
else if (ComponentRegistry.get(id) == null) cause = "unregistered key";
else cause = "provider does not have ";
ComponentsInternals.LOGGER.warn("Failed to deserialize component: {} {}", cause, missedKeyId);
}
ComponentsInternals.logDeserializationWarnings(componentMap.getKeys());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,49 @@
*/
package dev.onyxstudios.cca.internal.base;

import dev.onyxstudios.cca.api.v3.component.ComponentRegistry;
import dev.onyxstudios.cca.internal.base.asm.StaticComponentLoadingException;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

public final class ComponentsInternals {
public static final Logger LOGGER = LogManager.getLogger("Cardinal Components API");
private static boolean logDeserializationWarnings = true;

public static void init() {
Path path = FabricLoader.getInstance().getConfigDir().resolve("cardinal-components-api.properties");
try(Reader reader = Files.newBufferedReader(path)) {
Properties cfg = new Properties();
cfg.load(reader);
logDeserializationWarnings = Boolean.parseBoolean(cfg.getProperty("log-deserialization-warnings", "true"));
} catch (IOException e) {
try {
Files.writeString(path, """
# If set to false, warnings will not get logged when a component fails to be resolved (typically due to mods being removed)
# Default value: true
log-deserialization-warnings = true
# Internal value, do not edit or your changes may be arbitrarily reset
config-version = 1
""");
} catch (IOException ex) {
LOGGER.error("Failed to write config file at {}", path);
}
}
}

@Nonnull
public static <R> R createFactory(Class<R> factoryClass) {
Expand All @@ -48,4 +80,17 @@ public static <R> R createFactory(Class<R> factoryClass) {
throw new StaticComponentLoadingException("Failed to instantiate generated component factory", e);
}
}

public static void logDeserializationWarnings(Collection<String> missedKeyIds) {
if (logDeserializationWarnings) {
for (String missedKeyId : missedKeyIds) {
Identifier id = Identifier.tryParse(missedKeyId);
String cause;
if (id == null) cause = "invalid identifier";
else if (ComponentRegistry.get(id) == null) cause = "unregistered key";
else cause = "provider does not have ";
LOGGER.warn("Failed to deserialize component: {} {}", cause, missedKeyId);
}
}
}
}
5 changes: 5 additions & 0 deletions cardinal-components-base/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"description": "dynamically exposing components",
"version": "${version}",
"icon": "assets/cardinal-components-base/icon.png",
"entrypoints": {
"main": [
"dev.onyxstudios.cca.internal.base.ComponentsInternals::init"
]
},
"custom": {
"modmenu": {
"badges": [ "library" ],
Expand Down
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
------------------------------------------------------
Version 4.2.0
------------------------------------------------------
**Additions**
- Players can now set the `log-deserialization-warnings` option to toggle warnings when a component fails to be resolved
(typically due to mods being removed)

**Mod Compatibility**
- Fixed incompatibility with Immersive Portals

------------------------------------------------------
Version 4.1.4
------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_api_version=0.47.8+1.18.2
elmendorf_version=0.5.2

#Publishing
mod_version = 4.1.4
mod_version = 4.2.0
curseforge_id = 318449
modrinth_id = K01OU20C
curseforge_versions = 1.18; 1.18.1; 1.18.2
Expand Down

0 comments on commit 514c2c1

Please sign in to comment.