Ver Fonte

Make default extensions statically registrable (#2107)

Riccardo Balbo há 1 ano atrás
pai
commit
a290d0704c

+ 33 - 8
jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2022 jMonkeyEngine
+ * Copyright (c) 2009-2023 jMonkeyEngine
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -36,8 +36,10 @@ import com.jme3.plugins.json.JsonArray;
 import com.jme3.plugins.json.JsonElement;
 
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -51,14 +53,20 @@ public class CustomContentManager {
     private GltfModelKey key;
     private GltfLoader gltfLoader;
 
-    private final Map<String, ExtensionLoader> defaultExtensionLoaders = new HashMap<>();
+    
+    static final Map<String, Class<? extends ExtensionLoader>> defaultExtensionLoaders = new ConcurrentHashMap<>();
+    static {
+        defaultExtensionLoaders.put("KHR_materials_pbrSpecularGlossiness", PBRSpecGlossExtensionLoader.class);
+        defaultExtensionLoaders.put("KHR_lights_punctual", LightsPunctualExtensionLoader.class);
+        defaultExtensionLoaders.put("KHR_materials_unlit", UnlitExtensionLoader.class);
+        defaultExtensionLoaders.put("KHR_texture_transform", TextureTransformExtensionLoader.class);
+        defaultExtensionLoaders.put("KHR_materials_emissive_strength", PBREmissiveStrengthExtensionLoader.class);
+
+    }
+    
+    private final Map<String, ExtensionLoader> loadedExtensionLoaders = new HashMap<>();
 
     public CustomContentManager() {
-        defaultExtensionLoaders.put("KHR_materials_pbrSpecularGlossiness", new PBRSpecGlossExtensionLoader());
-        defaultExtensionLoaders.put("KHR_lights_punctual", new LightsPunctualExtensionLoader());
-        defaultExtensionLoaders.put("KHR_materials_unlit", new UnlitExtensionLoader());
-        defaultExtensionLoaders.put("KHR_texture_transform", new TextureTransformExtensionLoader());
-        defaultExtensionLoaders.put("KHR_materials_emissive_strength", new PBREmissiveStrengthExtensionLoader());
     }
 
     void init(GltfLoader gltfLoader) {
@@ -107,12 +115,29 @@ public class CustomContentManager {
 
         for (Map.Entry<String, JsonElement> ext : extensions.getAsJsonObject().entrySet()) {
             ExtensionLoader loader = null;
+
             if (key != null) {
                 loader = key.getExtensionLoader(ext.getKey());
             }
+
             if (loader == null) {
-                loader = defaultExtensionLoaders.get(ext.getKey());
+                loader = loadedExtensionLoaders.get(ext.getKey());
+                if (loader == null) {
+                    try {
+                        Class<? extends ExtensionLoader> clz = defaultExtensionLoaders.get(ext.getKey());
+                        if (clz != null) {
+                            loader = clz.getDeclaredConstructor().newInstance();
+                        }
+                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
+                        logger.log(Level.WARNING, "Could not instantiate loader", e);
+                    }
+
+                    if (loader != null) {
+                        loadedExtensionLoaders.put(ext.getKey(), loader);
+                    }
+                }
             }
+            
 
             if (loader == null) {
                 logger.log(Level.WARNING, "Could not find loader for extension " + ext.getKey());

+ 10 - 0
jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

@@ -1496,4 +1496,14 @@ public class GltfLoader implements AssetLoader {
             return new SkinBuffers(data, format.getComponentSize());
         }
     }
+    
+
+    public static void registerExtension(String name, Class<? extends ExtensionLoader> ext) {
+        CustomContentManager.defaultExtensionLoaders.put(name, ext);        
+    }
+    
+
+    public static void unregisterExtension(String name) {
+        CustomContentManager.defaultExtensionLoaders.remove(name);
+    }
 }