Skip to content

Commit

Permalink
More remote fixes, legacy remote import (pastebin) now working
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Jul 7, 2024
1 parent 28fbad0 commit c3d9d3d
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.stream.Collectors;

public class RemoteLayout {
public static final int JSON_VERSION = 3;
public static final int JSON_VERSION = 2;

public static final Codec<RemoteLayout.Versioned> VERSIONED_SAVE_CODEC = RecordCodecBuilder.create(builder -> builder.group(
Codec.INT.fieldOf("version").forGetter(RemoteLayout.Versioned::version),
Expand All @@ -57,6 +57,10 @@ public static RemoteLayout createEmpty() {
}

public static RemoteLayout fromNBT(HolderLookup.Provider provider, CompoundTag tag) {
if (tag.isEmpty()) {
return new RemoteLayout(List.of());
}

RegistryOps<Tag> ops = provider.createSerializationContext(NbtOps.INSTANCE);
var saved = Saved.CODEC.parse(ops, tag)
.resultOrPartial(err -> Log.warning("can't parse remote layout NBT: " + err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void discoverVariables(Set<String> variables, UUID playerId) {
protected record BaseSettings(String enableVariable, BlockPos enablingValue) {
public static final Codec<BaseSettings> CODEC = RecordCodecBuilder.create(builder -> builder.group(
Codec.STRING.optionalFieldOf("enable_var", "").forGetter(BaseSettings::enableVariable),
BlockPos.CODEC.optionalFieldOf("pos", BlockPos.ZERO).forGetter(BaseSettings::enablingValue)
BlockPos.CODEC.optionalFieldOf("enable_pos", BlockPos.ZERO).forGetter(BaseSettings::enablingValue)
).apply(builder, BaseSettings::new));

public static final BaseSettings DEFAULT = new BaseSettings("", BlockPos.ZERO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class ActionWidgetButton extends ActionWidgetVariable<WidgetButtonExtended> implements IActionWidgetLabeled {
public static final MapCodec<ActionWidgetButton> CODEC = RecordCodecBuilder.mapCodec(builder ->
varParts(builder).and(
BlockPos.CODEC.fieldOf("settingPos").forGetter(a -> a.settingPos)
BlockPos.CODEC.fieldOf("set_pos").forGetter(a -> a.settingPos)
).apply(builder, ActionWidgetButton::new));
public static final String ID = "button";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public abstract class ActionWidgetVariable<W extends AbstractWidget> extends ActionWidget<W> {
protected static <P extends ActionWidgetVariable<?>> Products.P3<RecordCodecBuilder.Mu<P>, BaseSettings, WidgetSettings, String> varParts(RecordCodecBuilder.Instance<P> pInstance) {
return baseParts(pInstance).and(Codec.STRING.fieldOf("variableName").forGetter(p -> p.variableName));
return baseParts(pInstance).and(Codec.STRING.fieldOf("var_name").forGetter(p -> p.variableName));
}

protected String variableName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public final class WidgetSettings {
public static final Codec<WidgetSettings> CODEC = RecordCodecBuilder.create(builder -> builder.group(
Codec.INT.fieldOf("x").forGetter(WidgetSettings::getX),
Codec.INT.fieldOf("y").forGetter(WidgetSettings::getY),
Codec.INT.fieldOf("width").forGetter(WidgetSettings::getWidth),
Codec.INT.fieldOf("height").forGetter(WidgetSettings::getHeight),
Codec.INT.optionalFieldOf("width", 0).forGetter(WidgetSettings::getWidth),
Codec.INT.optionalFieldOf("height", 0).forGetter(WidgetSettings::getHeight),
ComponentSerialization.CODEC.fieldOf("title").forGetter(WidgetSettings::getTitle),
ComponentSerialization.CODEC.optionalFieldOf("tooltip", Component.empty()).forGetter(WidgetSettings::getTooltip)
).apply(builder, WidgetSettings::new));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,71 @@
package me.desht.pneumaticcraft.common.util.legacyconv;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import me.desht.pneumaticcraft.client.gui.remote.RemoteLayout;
import me.desht.pneumaticcraft.lib.Log;

import static me.desht.pneumaticcraft.common.util.legacyconv.ConversionUtil.*;

public class ActionWidgetLegacyConv {
public static int determineVersion(JsonObject json) {
return 1;
if (json.has("version")) {
// 1.20.6 and newer
return json.get("version").getAsInt();
} else if (json.has("main")) {
return 1;
} else {
throw new JsonSyntaxException("can't determine saved remote layout version!");
}
}

public static void convertLegacy(JsonObject json, int oldVersion) {
if (oldVersion == 1) {
convertFromV1(json);
}
}

private static void convertFromV1(JsonObject json) {
JsonArray entries = json.getAsJsonObject("main").getAsJsonArray("value");

JsonArray newDoc = new JsonArray();

for (JsonElement entry : entries) {
JsonObject oldEntry = entry.getAsJsonObject();

JsonObject newEntry = new JsonObject();
JsonObject base = new JsonObject();
JsonObject widget = new JsonObject();

getString(oldEntry, "enableVariable").ifPresent(val -> base.addProperty("enable_var", prefixVar(val)));
convXYZ(oldEntry, base, "enabling", "enable_pos");

getInt(oldEntry, "x").ifPresent(val -> widget.addProperty("x", val));
getInt(oldEntry, "y").ifPresent(val -> widget.addProperty("y", val + 8));
getInt(oldEntry, "width").ifPresent(val -> widget.addProperty("width", val));
getInt(oldEntry, "height").ifPresent(val -> widget.addProperty("height", val));
getString(oldEntry, "text").ifPresent(val -> widget.addProperty("title", val));
getString(oldEntry, "tooltip").ifPresent(val -> widget.addProperty("tooltip", val));

newEntry.add("base", base);
newEntry.add("widget", widget);

getString(oldEntry, "id").ifPresent(val -> newEntry.addProperty("type", val));
getString(oldEntry, "variableName").ifPresent(val -> newEntry.addProperty("var_name", prefixVar(val)));
convXYZ(oldEntry, newEntry, "setting", "set_pos");
getString(oldEntry, "dropDownElements").ifPresent(val -> newEntry.addProperty("elements", val));
getBool(oldEntry, "sorted").ifPresent(val -> newEntry.addProperty("sorted", val));

newDoc.add(newEntry);
}

json.add("action_widgets", newDoc);
json.remove("main");
json.addProperty("version", RemoteLayout.JSON_VERSION);

Log.info("Pastebin Remote import (V1 -> V2): converted {} legacy widgets", newDoc.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.desht.pneumaticcraft.common.util.legacyconv;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.Util;
import net.minecraft.nbt.Tag;

import java.util.Arrays;
import java.util.Optional;
import java.util.OptionalInt;

public class ConversionUtil {
static OptionalInt getInt(JsonObject json, String fieldName) {
if (json.has(fieldName)) {
JsonObject sub = json.get(fieldName).getAsJsonObject();
if (sub.has("type") && sub.get("type").getAsInt() == Tag.TAG_INT) {
return OptionalInt.of(sub.get("value").getAsInt());
}
}
return OptionalInt.empty();
}

static Optional<String> getString(JsonObject json, String fieldName) {
if (json.has(fieldName)) {
JsonObject sub = json.get(fieldName).getAsJsonObject();
if (sub.has("type") && sub.get("type").getAsInt() == Tag.TAG_STRING) {
return Optional.of(sub.get("value").getAsString());
}
}
return Optional.empty();
}

static Optional<Boolean> getBool(JsonObject json, String fieldName) {
if (json.has(fieldName)) {
JsonObject sub = json.get(fieldName).getAsJsonObject();
if (sub.has("type") && sub.get("type").getAsInt() == Tag.TAG_BYTE) {
return Optional.of(sub.get("value").getAsBoolean());
}
}
return Optional.empty();
}

static void convXYZ(JsonObject from, JsonObject to, String prefix, String newKey) {
int sx = getInt(from, prefix + "X").orElse(0);
int sy = getInt(from, prefix + "Y").orElse(0);
int sz = getInt(from, prefix + "Z").orElse(0);
to.add(newKey, makeArray(sx, sy, sz));
}

static String prefixVar(String varName) {
return varName.isEmpty() || varName.startsWith("%") || varName.startsWith("#") ? varName : "#" + varName;
}

static JsonArray makeArray(int... vals) {
return Util.make(new JsonArray(), a -> Arrays.stream(vals).forEach(a::add));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,29 @@ public static void convertLegacy(JsonObject json, int oldVersion) {
* @param json the legacy data to convert
*/
private static void convertV1toV2(JsonObject json) {
JsonObject sub = json.getAsJsonObject("widgets");
JsonArray values = sub.getAsJsonArray("value");
JsonArray entries = json.getAsJsonObject("widgets").getAsJsonArray("value");

for (JsonElement el : values) {
JsonObject value = el.getAsJsonObject();
JsonObject nameObj = value.getAsJsonObject("name");
for (JsonElement el : entries) {
JsonObject oldEntry = el.getAsJsonObject();

JsonObject nameObj = oldEntry.getAsJsonObject("name");
String newName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, nameObj.get("value").getAsString());
nameObj.addProperty("value", Names.MOD_ID + ":" + newName);

if (newName.equals("area")) {
JsonObject typeObj = value.getAsJsonObject("type");
// special handling for area widget area types
JsonObject typeObj = oldEntry.getAsJsonObject("type");
EnumOldAreaType oldType = EnumOldAreaType.values()[typeObj.get("value").getAsInt()];
AreaType newType = LegacyAreaWidgetConverter.convertFromLegacyFormat(oldType, value.getAsJsonObject("typeInfo").get("value").getAsInt());
AreaType newType = LegacyAreaWidgetConverter.convertFromLegacyFormat(oldType, oldEntry.getAsJsonObject("typeInfo").get("value").getAsInt());
typeObj.addProperty("type", 8);
typeObj.addProperty("value", newType.getName());
}
}

json.add("pneumaticcraft:progWidgets", values);
json.add("pneumaticcraft:progWidgets", entries);
json.remove("widgets");
}


//
// private void doLegacyConversion(CompoundTag nbt) {
// ListTag l = nbt.getList("widgets", Tag.TAG_COMPOUND);
// int areaConversions = 0;
// for (int i = 0; i < l.size(); i++) {
// CompoundTag subTag = l.getCompound(i);
// String newName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, subTag.getString("name"));
// subTag.putString("name", Names.MOD_ID + ":" + newName);
// if (newName.equals("area")) {
// EnumOldAreaType oldType = EnumOldAreaType.values()[subTag.getInt("type")];
// AreaType newType = LegacyAreaWidgetConverter.convertFromLegacyFormat(oldType, subTag.getInt("typeInfo"));
// subTag.putString("type", newType.getName());
// newType.writeToNBT(subTag);
// areaConversions++;
// }
// }
// nbt.put(IProgrammable.NBT_WIDGETS, l);
// nbt.remove("widgets");
// if (areaConversions > 0) {
// Log.info("Pastebin import: converted {} legacy area widgets", areaConversions);
// }
// }

/**
* Handle version 2 (1.14-1.20) -> version 3 (1.21+) progwidget conversion
* @param json the legacy data to convert
Expand Down

0 comments on commit c3d9d3d

Please sign in to comment.