1/* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33package com.jme3.asset; 34 35import java.io.DataInput; 36import java.io.IOException; 37import java.io.InputStream; 38import java.util.Scanner; 39import java.util.logging.Level; 40import java.util.logging.Logger; 41 42/** 43 * <code>AssetConfig</code> loads a config file to configure the asset manager. 44 * <br/><br/> 45 * The config file is specified with the following format: 46 * <code> 47 * "LOADER" <class> : (<extension> ",")* <extension> 48 * "LOCATOR" <path> <class> : (<extension> ",")* <extension> 49 * </code> 50 * 51 * @author Kirill Vainer 52 */ 53public class AssetConfig { 54 55 private AssetManager manager; 56 57 public AssetConfig(AssetManager manager){ 58 this.manager = manager; 59 } 60 61 public void loadText(InputStream in) throws IOException{ 62 Scanner scan = new Scanner(in); 63 while (scan.hasNext()){ 64 String cmd = scan.next(); 65 if (cmd.equals("LOADER")){ 66 String loaderClass = scan.next(); 67 String colon = scan.next(); 68 if (!colon.equals(":")){ 69 throw new IOException("Expected ':', got '"+colon+"'"); 70 } 71 String extensionsList = scan.nextLine(); 72 String[] extensions = extensionsList.split(","); 73 for (int i = 0; i < extensions.length; i++){ 74 extensions[i] = extensions[i].trim(); 75 } 76 if (hasClass(loaderClass)) { 77 manager.registerLoader(loaderClass, extensions); 78 } else { 79 Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot find loader {0}", loaderClass); 80 } 81 } else if (cmd.equals("LOCATOR")) { 82 String rootPath = scan.next(); 83 String locatorClass = scan.nextLine().trim(); 84 if (hasClass(locatorClass)) { 85 manager.registerLocator(rootPath, locatorClass); 86 } else { 87 Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot find locator {0}", locatorClass); 88 } 89 } else { 90 throw new IOException("Expected command, got '" + cmd + "'"); 91 } 92 } 93 } 94 95 private boolean hasClass(String name) { 96 try { 97 Class clazz = Class.forName(name); 98 return clazz != null; 99 } catch (ClassNotFoundException ex) { 100 return false; 101 } 102 } 103 104 private static String readString(DataInput dataIn) throws IOException{ 105 int length = dataIn.readUnsignedShort(); 106 char[] chrs = new char[length]; 107 for (int i = 0; i < length; i++){ 108 chrs[i] = (char) dataIn.readUnsignedByte(); 109 } 110 return String.valueOf(chrs); 111 } 112 113 /* 114 public void loadBinary(DataInput dataIn) throws IOException{ 115 // read signature and version 116 117 // how many locator entries? 118 int locatorEntries = dataIn.readUnsignedShort(); 119 for (int i = 0; i < locatorEntries; i++){ 120 String locatorClazz = readString(dataIn); 121 String rootPath = readString(dataIn); 122 manager.registerLocator(rootPath, locatorClazz); 123 } 124 125 int loaderEntries = dataIn.readUnsignedShort(); 126 for (int i = 0; i < loaderEntries; i++){ 127 String loaderClazz = readString(dataIn); 128 int numExtensions = dataIn.readUnsignedByte(); 129 String[] extensions = new String[numExtensions]; 130 for (int j = 0; j < numExtensions; j++){ 131 extensions[j] = readString(dataIn); 132 } 133 134 manager.registerLoader(loaderClazz, extensions); 135 } 136 } 137 */ 138} 139