public Object load(AssetInfo info) throws IOException { this.assetManager = info.getManager(); InputStream in = info.openStream(); try { key = info.getKey(); if (key.getExtension().equals("j3m") && !(key instanceof MaterialKey)) { throw new IOException("Material instances must be loaded via MaterialKey"); } else if (key.getExtension().equals("j3md") && key instanceof MaterialKey) { throw new IOException("Material definitions must be loaded via AssetKey"); } loadFromRoot(BlockLanguageParser.parse(in)); } finally { if (in != null) { in.close(); } } if (material != null) { // material implementation return material; } else { // material definition return materialDef; } }
public static String validateAssetKey(AssetKey assetKey) { if (assetKey != null) { if (assetKey.getAssetId() == null || assetKey.getAssetName() == null) { return RepositoryServiceClientConstants.FAILURE; } return RepositoryServiceClientConstants.SUCCESS; } return RepositoryServiceClientConstants.FAILURE; }
public AssetInfo locate(AssetManager manager, AssetKey key) { String name = key.getName(); File file = (root != null) ? new File(root, name) : new File(name); if (file.exists() && file.isFile()) { try { // Now, check asset name requirements String canonical = file.getCanonicalPath(); String absolute = file.getAbsolutePath(); if (!canonical.endsWith(absolute)) { throw new AssetNotFoundException( "Asset name doesn't match requirements.\n" + "\"" + canonical + "\" doesn't match \"" + absolute + "\""); } } catch (IOException ex) { throw new AssetLoadException("Failed to get file canonical path " + file, ex); } AssetInfo fileAssetInfo = new AssetInfoFile(manager, key, file); return UncompressedAssetInfo.create(fileAssetInfo); } else { return null; } }
@Override public void write(JmeExporter ex) throws IOException { super.write(ex); OutputCapsule oc = ex.getCapsule(this); oc.write(flipY, "flip_y", false); oc.write(generateMips, "generate_mips", false); oc.write(anisotropy, "anisotropy", 0); oc.write(textureTypeHint, "tex_type", Type.TwoDimensional); // Backwards compat oc.write(textureTypeHint == Type.CubeMap, "as_cubemap", false); }
@Override public void read(JmeImporter im) throws IOException { super.read(im); InputCapsule ic = im.getCapsule(this); flipY = ic.readBoolean("flip_y", false); generateMips = ic.readBoolean("generate_mips", false); anisotropy = ic.readInt("anisotropy", 0); boolean asCube = ic.readBoolean("as_cubemap", false); if (asCube) { // Backwards compat textureTypeHint = Type.CubeMap; } else { textureTypeHint = ic.readEnum("tex_type", Texture.Type.class, Type.TwoDimensional); } }
private void loadFromRoot(List<Statement> roots) throws IOException { if (roots.size() == 2) { Statement exception = roots.get(0); String line = exception.getLine(); if (line.startsWith("Exception")) { throw new AssetLoadException(line.substring("Exception ".length())); } else { throw new IOException("In multiroot material, expected first statement to be 'Exception'"); } } else if (roots.size() != 1) { throw new IOException("Too many roots in J3M/J3MD file"); } boolean extending = false; Statement materialStat = roots.get(0); String materialName = materialStat.getLine(); if (materialName.startsWith("MaterialDef")) { materialName = materialName.substring("MaterialDef ".length()).trim(); extending = false; } else if (materialName.startsWith("Material")) { materialName = materialName.substring("Material ".length()).trim(); extending = true; } else { throw new IOException("Specified file is not a Material file"); } String[] split = materialName.split(":", 2); if (materialName.equals("")) { throw new MatParseException("Material name cannot be empty", materialStat); } if (split.length == 2) { if (!extending) { throw new MatParseException("Must use 'Material' when extending.", materialStat); } String extendedMat = split[1].trim(); MaterialDef def = (MaterialDef) assetManager.loadAsset(new AssetKey(extendedMat)); if (def == null) { throw new MatParseException( "Extended material " + extendedMat + " cannot be found.", materialStat); } material = new Material(def); material.setKey(key); material.setName(split[0].trim()); // material.setAssetName(fileName); } else if (split.length == 1) { if (extending) { throw new MatParseException("Expected ':', got '{'", materialStat); } materialDef = new MaterialDef(assetManager, materialName); // NOTE: pass file name for defs so they can be loaded later materialDef.setAssetName(key.getName()); } else { throw new MatParseException("Cannot use colon in material name/path", materialStat); } for (Statement statement : materialStat.getContents()) { split = statement.getLine().split("[ \\{]"); String statType = split[0]; if (extending) { if (statType.equals("MaterialParameters")) { readExtendingMaterialParams(statement.getContents()); } else if (statType.equals("AdditionalRenderState")) { readAdditionalRenderState(statement.getContents()); } else if (statType.equals("Transparent")) { readTransparentStatement(statement.getLine()); } } else { if (statType.equals("Technique")) { readTechnique(statement); } else if (statType.equals("MaterialParameters")) { readMaterialParams(statement.getContents()); } else { throw new MatParseException( "Expected material statement, got '" + statType + "'", statement); } } } }