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
3636import com .jme3 .plugins .json .JsonElement ;
3737
3838import java .io .IOException ;
39+ import java .lang .reflect .InvocationTargetException ;
3940import java .util .HashMap ;
4041import java .util .Map ;
42+ import java .util .concurrent .ConcurrentHashMap ;
4143import java .util .logging .Level ;
4244import 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 ());
0 commit comments