add configuration for legacy serialization

This commit is contained in:
EoD 2025-04-25 23:31:43 +00:00
parent cc687a8ea0
commit 44a3e9ca8c
2 changed files with 30 additions and 1 deletions

View File

@ -21,6 +21,7 @@ public class JdbcConfig {
public static ForgeConfigSpec.BooleanValue IS_CHAT_SERVER;
public static ForgeConfigSpec.ConfigValue<String> CHAT_SERVER_IP;
public static ForgeConfigSpec.IntValue CHAT_SERVER_PORT;
public static ForgeConfigSpec.BooleanValue USE_LEGACY_SERIALIZATION;
public static ForgeConfigSpec.ConfigValue<Integer> SERVER_ID;
@ -40,8 +41,13 @@ public class JdbcConfig {
IS_CHAT_SERVER = COMMON_BUILDER.comment("Whether recieve messages from other servers as host").define("IsChatServer",false);
CHAT_SERVER_IP = COMMON_BUILDER.define("ChatServerIP","127.0.0.1");
CHAT_SERVER_PORT = COMMON_BUILDER.defineInRange("ChatServerPort",7900,0,65535);
USE_LEGACY_SERIALIZATION = COMMON_BUILDER.comment(
"Use the old (pre-Base64) serialization format for writing data to the database.",
"Set to true ONLY if you have older mod versions reading the same database.",
"This only affects writing data, the mod can read both Base64 and pre-Base64 serialization.",
"New installations should leave this as 'false'."
).define("use_legacy_serialization", false);
COMMON_BUILDER.pop();
COMMON_CONFIG = COMMON_BUILDER.build();
}
}

View File

@ -187,6 +187,12 @@ public class VanillaSync {
return ItemStack.of(compoundTag);
}
/**
* Deserializes a string from the database back into an NBT string.
* Handles both the new Base64 format (prefixed with "B64:") and the old custom format.
* @param encoded The string retrieved from the database.
* @return The deserialized NBT string.
*/
public static String deserializeString(String encoded) {
if (encoded.startsWith("B64:")) {
String base64 = encoded.substring(4);
@ -205,7 +211,24 @@ public class VanillaSync {
.replace("~", "'");
}
/**
* Serializes an NBT string for database storage.
* Uses Base64 encoding by default (prefixed with "B64:").
* If USE_LEGACY_SERIALIZATION config is true, uses the old custom replacement format.
* @param object The NBT string to serialize.
* @return The serialized string.
*/
public static String serialize(String object) {
// Check the config option for backwards compatibility during writing
if (JdbcConfig.USE_LEGACY_SERIALIZATION.get()) {
// Use old custom replacement logic
return objeåct.replace(",", "|")
.replace("\"", "^")
.replace("{", "<")
.replace("}", ">")
.replace("'", "~");
}
// Base64 encode with a "B64:" marker for new data
return "B64:" + Base64.getEncoder().encodeToString(object.getBytes(StandardCharsets.UTF_8));
}