Skip to content

Commit a290d07

Browse files
authored
Make default extensions statically registrable (#2107)
1 parent 951b430 commit a290d07

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2022 jMonkeyEngine
2+
* Copyright (c) 2009-2023 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -36,8 +36,10 @@
3636
import com.jme3.plugins.json.JsonElement;
3737

3838
import java.io.IOException;
39+
import java.lang.reflect.InvocationTargetException;
3940
import java.util.HashMap;
4041
import java.util.Map;
42+
import java.util.concurrent.ConcurrentHashMap;
4143
import java.util.logging.Level;
4244
import java.util.logging.Logger;
4345

@@ -51,14 +53,20 @@ public class CustomContentManager {
5153
private GltfModelKey key;
5254
private GltfLoader gltfLoader;
5355

54-
private final Map<String, ExtensionLoader> defaultExtensionLoaders = new HashMap<>();
56+
57+
static final Map<String, Class<? extends ExtensionLoader>> defaultExtensionLoaders = new ConcurrentHashMap<>();
58+
static {
59+
defaultExtensionLoaders.put("KHR_materials_pbrSpecularGlossiness", PBRSpecGlossExtensionLoader.class);
60+
defaultExtensionLoaders.put("KHR_lights_punctual", LightsPunctualExtensionLoader.class);
61+
defaultExtensionLoaders.put("KHR_materials_unlit", UnlitExtensionLoader.class);
62+
defaultExtensionLoaders.put("KHR_texture_transform", TextureTransformExtensionLoader.class);
63+
defaultExtensionLoaders.put("KHR_materials_emissive_strength", PBREmissiveStrengthExtensionLoader.class);
64+
65+
}
66+
67+
private final Map<String, ExtensionLoader> loadedExtensionLoaders = new HashMap<>();
5568

5669
public CustomContentManager() {
57-
defaultExtensionLoaders.put("KHR_materials_pbrSpecularGlossiness", new PBRSpecGlossExtensionLoader());
58-
defaultExtensionLoaders.put("KHR_lights_punctual", new LightsPunctualExtensionLoader());
59-
defaultExtensionLoaders.put("KHR_materials_unlit", new UnlitExtensionLoader());
60-
defaultExtensionLoaders.put("KHR_texture_transform", new TextureTransformExtensionLoader());
61-
defaultExtensionLoaders.put("KHR_materials_emissive_strength", new PBREmissiveStrengthExtensionLoader());
6270
}
6371

6472
void init(GltfLoader gltfLoader) {
@@ -107,12 +115,29 @@ private <T> T readExtension(String name, JsonElement el, T input) throws AssetLo
107115

108116
for (Map.Entry<String, JsonElement> ext : extensions.getAsJsonObject().entrySet()) {
109117
ExtensionLoader loader = null;
118+
110119
if (key != null) {
111120
loader = key.getExtensionLoader(ext.getKey());
112121
}
122+
113123
if (loader == null) {
114-
loader = defaultExtensionLoaders.get(ext.getKey());
124+
loader = loadedExtensionLoaders.get(ext.getKey());
125+
if (loader == null) {
126+
try {
127+
Class<? extends ExtensionLoader> clz = defaultExtensionLoaders.get(ext.getKey());
128+
if (clz != null) {
129+
loader = clz.getDeclaredConstructor().newInstance();
130+
}
131+
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
132+
logger.log(Level.WARNING, "Could not instantiate loader", e);
133+
}
134+
135+
if (loader != null) {
136+
loadedExtensionLoaders.put(ext.getKey(), loader);
137+
}
138+
}
115139
}
140+
116141

117142
if (loader == null) {
118143
logger.log(Level.WARNING, "Could not find loader for extension " + ext.getKey());

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,4 +1496,14 @@ public SkinBuffers populate(Integer bufferViewIndex, int componentType, String t
14961496
return new SkinBuffers(data, format.getComponentSize());
14971497
}
14981498
}
1499+
1500+
1501+
public static void registerExtension(String name, Class<? extends ExtensionLoader> ext) {
1502+
CustomContentManager.defaultExtensionLoaders.put(name, ext);
1503+
}
1504+
1505+
1506+
public static void unregisterExtension(String name) {
1507+
CustomContentManager.defaultExtensionLoaders.remove(name);
1508+
}
14991509
}

0 commit comments

Comments
 (0)