Skip to content

Commit

Permalink
WOO
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Jul 11, 2024
1 parent 4e34d0d commit 00a41f6
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.electronwill.nightconfig.core.serde.ObjectDeserializer;
import com.electronwill.nightconfig.core.serde.ObjectSerializer;
import com.electronwill.nightconfig.core.serde.SerdeException;
import com.electronwill.nightconfig.core.serde.annotations.SerdeDefault;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.serializer.ConfigSerializer;
import me.shedaniel.autoconfig.util.Utils;
Expand Down Expand Up @@ -76,6 +77,10 @@ public void serialize(@NotNull T t) throws SerializationException {
config.save();
config.close();
} catch (Exception e) {
NightAutoConfig.LOGGER.error(
"Serialization failed for config {}! This will cause the program to stop writing anything to the file.",
type.getRelativeConfigPath(definition)
);
NightAutoConfig.LOGGER.error(e.getMessage(), e);
}
}
Expand All @@ -99,6 +104,11 @@ public T deserialize() throws SerializationException {
return deserializer(configClass).deserializeFields(config, this::createDefault);
}
} catch (Exception e) {
NightAutoConfig.LOGGER.error(
"Deserialization failed for config {}! This will cause the program to ignore the existing modifications and use default values. This might be caused by a missing of {} on restricting value fallbacks.",
type.getRelativeConfigPath(definition),
SerdeDefault.class
);
NightAutoConfig.LOGGER.error(e.getMessage(), e);
return createDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.serializer.ConfigSerializer;
import me.shedaniel.autoconfig.util.Utils;
import net.fabricmc.loader.api.FabricLoader;

import java.nio.file.Path;
import java.util.function.UnaryOperator;
Expand All @@ -38,6 +39,10 @@ public Path getConfigPath(Config definition) {
return Utils.getConfigFolder().resolve(getFileName(definition));
}

public Path getRelativeConfigPath(Config definition) {
return FabricLoader.getInstance().getConfigDir().relativize(getConfigPath(definition));
}

public NightConfigSerializer.Builder builder() {
return new NightConfigSerializer.Builder(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
package band.kessokuteatime.nightautoconfig.serde;

import band.kessokuteatime.nightautoconfig.serde.deserializers.ColorFromIntegerDeserializer;
import band.kessokuteatime.nightautoconfig.serde.deserializers.ColorFromMapDeserializer;
import band.kessokuteatime.nightautoconfig.serde.deserializers.ColorDeserializer;
import band.kessokuteatime.nightautoconfig.serde.deserializers.FloatingPointDeserializer;
import band.kessokuteatime.nightautoconfig.util.TypeUtil;
import com.electronwill.nightconfig.core.serde.*;

import java.awt.*;
import java.util.Map;

public class NightDeserializers {
public static final FloatingPointDeserializer FLOATING_POINT = new FloatingPointDeserializer();
public static final ColorFromIntegerDeserializer COLOR_FROM_INTEGER = new ColorFromIntegerDeserializer();
public static final ColorFromMapDeserializer COLOR_FROM_MAP = new ColorFromMapDeserializer();
public static final ColorDeserializer COLOR = new ColorDeserializer();

public static void provideToBuilder(ObjectDeserializerBuilder builder) {
builder.withDeserializerProvider((valueClass, resultType) -> resultType.getSatisfyingRawType().map(resultClass -> {
if (FloatingPointDeserializer.isNumberTypeSupported(valueClass) && TypeUtil.isPrimitiveOrWrapperNumber(resultClass)) {
return FLOATING_POINT;
}

if ((valueClass == Integer.class || valueClass == int.class) && Color.class.isAssignableFrom(resultClass)) {
return COLOR_FROM_INTEGER;
}

if (Map.class.isAssignableFrom(valueClass) && Color.class.isAssignableFrom(resultClass)) {
return COLOR_FROM_MAP;
if (Color.class.isAssignableFrom(resultClass)) {
return COLOR;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package band.kessokuteatime.nightautoconfig.serde;


import band.kessokuteatime.nightautoconfig.serde.serializers.ColorToIntegerSerializer;
import band.kessokuteatime.nightautoconfig.serde.serializers.ColorToMapSerializer;
import band.kessokuteatime.nightautoconfig.serde.serializers.ColorSerializer;
import com.electronwill.nightconfig.core.serde.ObjectSerializerBuilder;

import java.awt.*;

public class NightSerializers {
public static final ColorToIntegerSerializer COLOR_TO_INTEGER = new ColorToIntegerSerializer();
public static final ColorToMapSerializer COLOR_TO_MAP = new ColorToMapSerializer();
public static final ColorSerializer COLOR = new ColorSerializer();

public static void provideToBuilder(ObjectSerializerBuilder builder) {
builder.withSerializerProvider((valueClass, ctx) -> {
if (Color.class.isAssignableFrom(valueClass)) {
return COLOR_TO_INTEGER;
return COLOR;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package band.kessokuteatime.nightautoconfig.serde.deserializers;

import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.serde.DeserializerContext;
import com.electronwill.nightconfig.core.serde.TypeConstraint;
import com.electronwill.nightconfig.core.serde.ValueDeserializer;

import java.awt.*;
import java.util.Map;
import java.util.Optional;

public class ColorDeserializer implements ValueDeserializer<Config, Color> {
@Override
public Color deserialize(Config value, Optional<TypeConstraint> resultType, DeserializerContext ctx) {
int red = value.get("red");
int green = value.get("green");
int blue = value.get("blue");
int alpha = value.get("alpha");
return new Color(red, green, blue, alpha);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package band.kessokuteatime.nightautoconfig.serde.serializers;

import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.serde.*;

import java.awt.*;
import java.util.Map;

public class ColorSerializer implements ValueSerializer<Color, Config> {
@Override
public Config serialize(Color value, SerializerContext ctx) {
Config config = ctx.createConfig();
config.set("red", value.getRed());
config.set("green", value.getGreen());
config.set("blue", value.getBlue());
config.set("alpha", value.getAlpha());
return config;
}
}

This file was deleted.

This file was deleted.

0 comments on commit 00a41f6

Please sign in to comment.