Created
February 20, 2015 19:08
-
-
Save StillManic/ead1363382823bfd1d59 to your computer and use it in GitHub Desktop.
OBJLoader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OBJLoader implements ICustomModelLoader { | |
public static final Logger logger = LogManager.getLogger(OBJLoader.class); | |
public static final OBJLoader instance = new OBJLoader(); | |
private IResourceManager manager; | |
private final Set<String> enabledDomains = new HashSet<String>(); | |
private final Map<ResourceLocation, OBJModel> cache = new HashMap<ResourceLocation, OBJModel>(); | |
public OBJLoader() | |
{ | |
logger.info("LOADER: constructed"); | |
ModelLoaderRegistry.registerLoader(this); | |
} | |
public void addDomain(String domain) | |
{ | |
logger.printf(Level.INFO, "LOADER: adding domain: %s", domain); | |
enabledDomains.add(domain.toLowerCase()); | |
} | |
public void onResourceManagerReload(IResourceManager resourceManager) | |
{ | |
logger.printf(Level.INFO, "LOADER: Manager reloaded", new Object[0]); | |
this.manager = resourceManager; | |
cache.clear(); | |
} | |
public boolean accepts(ResourceLocation modelLocation) | |
{ | |
return enabledDomains.contains(modelLocation.getResourceDomain()) && modelLocation.getResourcePath().endsWith(".obj"); | |
} | |
@SuppressWarnings("unchecked") | |
public IModel loadModel(ResourceLocation modelLocation) | |
{ | |
boolean isBlockModel = true; | |
String fileName = ""; | |
if (!ModelLoaderRegistry.loaded(modelLocation)) | |
{ | |
ResourceLocation file = new ResourceLocation(modelLocation.getResourceDomain(), modelLocation.getResourcePath()); | |
if (!cache.containsKey(modelLocation)) | |
{ | |
try | |
{ | |
IResource res = null; | |
try | |
{ | |
res = manager.getResource(file); | |
} | |
catch (FileNotFoundException e) | |
{ | |
if (modelLocation.getResourcePath().startsWith("models/block/")) | |
{ | |
res = manager.getResource(new ResourceLocation(modelLocation.getResourceDomain(), "models/item/" + modelLocation.getResourcePath().substring("models/block/".length()))); | |
} | |
else if (modelLocation.getResourcePath().startsWith("models/item/")) | |
{ | |
res = manager.getResource(new ResourceLocation(modelLocation.getResourceDomain(), "models/block/" + modelLocation.getResourcePath().substring("models/item/".length()))); | |
} | |
else throw e; | |
} | |
if (modelLocation.getResourcePath().equalsIgnoreCase("models/item/test_block_wand.obj")) isBlockModel = false; | |
OBJModel.Parser parser = new OBJModel.Parser(res, manager); | |
OBJModel model = parser.parse(); | |
cache.put(modelLocation, model); | |
// TestModel model = new TestModel(); | |
// cache.put(modelLocation, model); | |
} | |
catch (IOException e) | |
{ | |
FMLLog.log(Level.ERROR, e, "LOADER: Exception loading model %s with OBJ loader, skipping", modelLocation); | |
cache.put(modelLocation, null); | |
} | |
} | |
OBJModel model = cache.get(modelLocation); | |
return model == null ? ModelLoaderRegistry.getMissingModel() : model; | |
} | |
else return ModelLoaderRegistry.getModel(modelLocation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment