X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FIndirectGUIComponentCreator.java;h=6ff8adb1468cd2eb3d8c063ad75dc490f6359bc2;hb=64f90ff345bfea80fd61b50bab2752792df76c7b;hp=a7550b8cf9a6f565763337f1b3beb4206b41b4b4;hpb=d6702bcad356a28bc6c2eb0574d5cecdf536aabc;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java index a7550b8c..6ff8adb1 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java @@ -6,10 +6,10 @@ import java.io.UncheckedIOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Set; import com.google.gson.JsonElement; import com.google.gson.JsonNull; +import com.google.gson.JsonObject; import net.mograsim.logic.model.model.ViewModelModifiable; import net.mograsim.logic.model.model.components.GUIComponent; @@ -19,13 +19,13 @@ import net.mograsim.logic.model.util.JsonHandler; public class IndirectGUIComponentCreator { private static final Map standardComponentIDs = new HashMap<>(); - private static final Set standardComponentIDSetUnmodifiable = Collections.unmodifiableSet(standardComponentIDs.keySet()); + private static final Map standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs); private static final Map componentSuppliers = new HashMap<>(); static { - try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json")) + try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json")) { if (s == null) throw new IOException("Resource not found"); @@ -56,9 +56,9 @@ public class IndirectGUIComponentCreator standardComponentIDs.put(standardComponentID, associatedComponentID); } - public static Set getStandardComponentIDs() + public static Map getStandardComponentIDs() { - return standardComponentIDSetUnmodifiable; + return standardComponentIDsUnmodifiable; } public static void setComponentSupplier(String className, ComponentSupplier componentSupplier) @@ -85,11 +85,7 @@ public class IndirectGUIComponentCreator { if (id != null) { - String resolvedID; - if (id.startsWith("class:") || id.startsWith("file:")) - resolvedID = id; - else - resolvedID = standardComponentIDs.get(id); + String resolvedID = resolveID(id); if (resolvedID != null) { if (resolvedID.startsWith("class:")) @@ -99,6 +95,7 @@ public class IndirectGUIComponentCreator ComponentSupplier componentSupplier = componentSuppliers.get(className); if (componentSupplier != null) return componentSupplier.create(model, params, name); + throw new IllegalArgumentException("Component supplier not found for ID " + id + " (resolved: " + resolvedID + ")"); } else // we know id has to start with "file:" here // because standardComponentIDs only contains strings starting with "class:" or "file:" @@ -106,7 +103,14 @@ public class IndirectGUIComponentCreator throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file"); try { - return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null); + String filename = resolvedID.substring(5); + JsonObject jsonContents = JsonHandler.readJson(filename, JsonObject.class); + SerializablePojo jsonContentsAsSerializablePojo = JsonHandler.parser.fromJson(jsonContents, SerializablePojo.class); + if (jsonContentsAsSerializablePojo.version == null) + return LegacySubmodelComponentSerializer.deserialize(model, + JsonHandler.parser.fromJson(jsonContents, LegacySubmodelComponentParams.class), name, id, null); + return SubmodelComponentSerializer.deserialize(model, + JsonHandler.parser.fromJson(jsonContents, SubmodelComponentParams.class), name, id, null); } catch (IOException e) { @@ -117,6 +121,13 @@ public class IndirectGUIComponentCreator throw new RuntimeException("Could not get component supplier for ID " + id); } + public static String resolveID(String id) + { + if (id.startsWith("class:") || id.startsWith("file:")) + return id; + return standardComponentIDs.get(id); + } + private static void tryLoadComponentClass(String componentClassName) { CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");