AssetConfig.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. package com.jme3.asset;
  33. import java.io.DataInput;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.util.Scanner;
  37. import java.util.logging.Level;
  38. import java.util.logging.Logger;
  39. /**
  40. * <code>AssetConfig</code> loads a config file to configure the asset manager.
  41. * <br/><br/>
  42. * The config file is specified with the following format:
  43. * <code>
  44. * "LOADER" <class> : (<extension> ",")* <extension>
  45. * "LOCATOR" <path> <class> : (<extension> ",")* <extension>
  46. * </code>
  47. *
  48. * @author Kirill Vainer
  49. */
  50. public class AssetConfig {
  51. private AssetManager manager;
  52. public AssetConfig(AssetManager manager){
  53. this.manager = manager;
  54. }
  55. public void loadText(InputStream in) throws IOException{
  56. Scanner scan = new Scanner(in);
  57. while (scan.hasNext()){
  58. String cmd = scan.next();
  59. if (cmd.equals("LOADER")){
  60. String loaderClass = scan.next();
  61. String colon = scan.next();
  62. if (!colon.equals(":")){
  63. throw new IOException("Expected ':', got '"+colon+"'");
  64. }
  65. String extensionsList = scan.nextLine();
  66. String[] extensions = extensionsList.split(",");
  67. for (int i = 0; i < extensions.length; i++){
  68. extensions[i] = extensions[i].trim();
  69. }
  70. Class clazz = acquireClass(loaderClass);
  71. if (clazz != null) {
  72. manager.registerLoader(clazz, extensions);
  73. } else {
  74. Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot find loader {0}", loaderClass);
  75. }
  76. } else if (cmd.equals("LOCATOR")) {
  77. String rootPath = scan.next();
  78. String locatorClass = scan.nextLine().trim();
  79. Class clazz = acquireClass(locatorClass);
  80. if (clazz != null) {
  81. manager.registerLocator(rootPath, clazz);
  82. } else {
  83. Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot find locator {0}", locatorClass);
  84. }
  85. } else {
  86. throw new IOException("Expected command, got '" + cmd + "'");
  87. }
  88. }
  89. }
  90. private Class acquireClass(String name) {
  91. try {
  92. Class clazz = Class.forName(name);
  93. return clazz;
  94. } catch (ClassNotFoundException ex) {
  95. return null;
  96. }
  97. }
  98. /*
  99. private static String readString(DataInput dataIn) throws IOException{
  100. int length = dataIn.readUnsignedShort();
  101. char[] chrs = new char[length];
  102. for (int i = 0; i < length; i++){
  103. chrs[i] = (char) dataIn.readUnsignedByte();
  104. }
  105. return String.valueOf(chrs);
  106. }
  107. public void loadBinary(DataInput dataIn) throws IOException{
  108. // read signature and version
  109. // how many locator entries?
  110. int locatorEntries = dataIn.readUnsignedShort();
  111. for (int i = 0; i < locatorEntries; i++){
  112. String locatorClazz = readString(dataIn);
  113. String rootPath = readString(dataIn);
  114. manager.registerLocator(rootPath, locatorClazz);
  115. }
  116. int loaderEntries = dataIn.readUnsignedShort();
  117. for (int i = 0; i < loaderEntries; i++){
  118. String loaderClazz = readString(dataIn);
  119. int numExtensions = dataIn.readUnsignedByte();
  120. String[] extensions = new String[numExtensions];
  121. for (int j = 0; j < numExtensions; j++){
  122. extensions[j] = readString(dataIn);
  123. }
  124. manager.registerLoader(loaderClazz, extensions);
  125. }
  126. }
  127. */
  128. }