Merge pull request #39 from EoD/add-database-compat-flag

add configuration for legacy serialization
This commit is contained in:
mlus 2025-05-02 00:28:27 +08:00 committed by GitHub
commit b6f76a6af2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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

@ -188,6 +188,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);
@ -206,7 +212,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));
}