AssetManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2009-2012 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 com.jme3.asset.plugins.ClasspathLocator;
  34. import com.jme3.asset.plugins.FileLocator;
  35. import com.jme3.audio.AudioData;
  36. import com.jme3.audio.AudioKey;
  37. import com.jme3.font.BitmapFont;
  38. import com.jme3.material.Material;
  39. import com.jme3.post.FilterPostProcessor;
  40. import com.jme3.renderer.Caps;
  41. import com.jme3.scene.Spatial;
  42. import com.jme3.scene.plugins.OBJLoader;
  43. import com.jme3.shader.Shader;
  44. import com.jme3.shader.ShaderGenerator;
  45. import com.jme3.shader.ShaderKey;
  46. import com.jme3.texture.Texture;
  47. import com.jme3.texture.plugins.TGALoader;
  48. import java.util.EnumSet;
  49. import java.util.List;
  50. /**
  51. * <code>AssetManager</code> provides an interface for managing the data assets
  52. * of a jME3 application.
  53. * <p>
  54. * The asset manager provides a means to register {@link AssetLocator}s,
  55. * which are used to find asset data on disk, network, or other file system.
  56. * The asset locators are invoked in order of addition to find the asset data.
  57. * Use the {@link #registerLocator(java.lang.String, java.lang.Class) } method
  58. * to add new {@link AssetLocator}s.
  59. * Some examples of locators:
  60. * <ul>
  61. * <li>{@link FileLocator} - Used to find assets on the local file system.</li>
  62. * <li>{@link ClasspathLocator} - Used to find assets in the Java classpath</li>
  63. * </ul>
  64. * <p>
  65. * The asset data is represented by the {@link AssetInfo} class, this
  66. * data is passed into the registered {@link AssetLoader}s in order to
  67. * convert the data into a usable object. Use the
  68. * {@link #registerLoader(java.lang.Class, java.lang.String[]) } method
  69. * to add loaders.
  70. * Some examples of loaders:
  71. * <ul>
  72. * <li>{@link OBJLoader} - Used to load Wavefront .OBJ model files</li>
  73. * <li>{@link TGALoader} - Used to load Targa image files</li>
  74. * </ul>
  75. * <p>
  76. * Once the asset has been loaded,
  77. */
  78. public interface AssetManager {
  79. /**
  80. * Adds a {@link ClassLoader} that is used to load {@link Class classes}
  81. * that are needed for finding and loading Assets.
  82. * This does <strong>not</strong> allow loading assets from that classpath,
  83. * use registerLocator for that.
  84. *
  85. * @param loader A ClassLoader that Classes in asset files can be loaded from.
  86. */
  87. public void addClassLoader(ClassLoader loader);
  88. /**
  89. * Remove a {@link ClassLoader} from the list of registered ClassLoaders
  90. */
  91. public void removeClassLoader(ClassLoader loader);
  92. /**
  93. * Retrieve the list of registered ClassLoaders that are used for loading
  94. * {@link Class classes} from asset files.
  95. */
  96. public List<ClassLoader> getClassLoaders();
  97. /**
  98. * Registers a loader for the given extensions.
  99. *
  100. * @param loaderClassName
  101. * @param extensions
  102. *
  103. * @deprecated Please use {@link #registerLoader(java.lang.Class, java.lang.String[]) }
  104. * together with {@link Class#forName(java.lang.String) } to find a class
  105. * and then register it.
  106. *
  107. * @deprecated Please use {@link #registerLoader(java.lang.Class, java.lang.String[]) }
  108. * with {@link Class#forName(java.lang.String) } instead.
  109. */
  110. @Deprecated
  111. public void registerLoader(String loaderClassName, String ... extensions);
  112. /**
  113. * Registers an {@link AssetLocator} by using a class name.
  114. * See the {@link AssetManager#registerLocator(java.lang.String, java.lang.Class) }
  115. * method for more information.
  116. *
  117. * @param rootPath The root path from which to locate assets, this
  118. * depends on the implementation of the asset locator.
  119. * A URL based locator will expect a url folder such as "http://www.example.com/"
  120. * while a File based locator will expect a file path (OS dependent).
  121. * @param locatorClassName The full class name of the {@link AssetLocator}
  122. * implementation.
  123. *
  124. * @deprecated Please use {@link #registerLocator(java.lang.String, java.lang.Class) }
  125. * together with {@link Class#forName(java.lang.String) } to find a class
  126. * and then register it.
  127. */
  128. @Deprecated
  129. public void registerLocator(String rootPath, String locatorClassName);
  130. /**
  131. * Register an {@link AssetLoader} by using a class object.
  132. *
  133. * @param loaderClass
  134. * @param extensions
  135. */
  136. public void registerLoader(Class<? extends AssetLoader> loaderClass, String ... extensions);
  137. /**
  138. * Unregister a {@link AssetLoader} from loading its assigned extensions.
  139. * This undoes the effect of calling
  140. * {@link #registerLoader(java.lang.Class, java.lang.String[]) }.
  141. *
  142. * @param loaderClass The loader class to unregister.
  143. * @see #registerLoader(java.lang.Class, java.lang.String[])
  144. */
  145. public void unregisterLoader(Class<? extends AssetLoader> loaderClass);
  146. /**
  147. * Registers the given locator class for locating assets with this
  148. * <code>AssetManager</code>. {@link AssetLocator}s are invoked in the order
  149. * they were registered, to locate the asset by the {@link AssetKey}.
  150. * Once an {@link AssetLocator} returns a non-null AssetInfo, it is sent
  151. * to the {@link AssetLoader} to load the asset.
  152. * Once a locator is registered, it can be removed via
  153. * {@link #unregisterLocator(java.lang.String, java.lang.Class) }.
  154. *
  155. * @param rootPath Specifies the root path from which to locate assets
  156. * for the given {@link AssetLocator}. The purpose of this parameter
  157. * depends on the type of the {@link AssetLocator}.
  158. * @param locatorClass The class type of the {@link AssetLocator} to register.
  159. *
  160. * @see AssetLocator#setRootPath(java.lang.String)
  161. * @see AssetLocator#locate(com.jme3.asset.AssetManager, com.jme3.asset.AssetKey)
  162. * @see #unregisterLocator(java.lang.String, java.lang.Class)
  163. */
  164. public void registerLocator(String rootPath, Class<? extends AssetLocator> locatorClass);
  165. /**
  166. * Unregisters the given locator class. This essentially undoes the operation
  167. * done by {@link #registerLocator(java.lang.String, java.lang.Class) }.
  168. *
  169. * @param rootPath Should be the same as the root path specified in {@link
  170. * #registerLocator(java.lang.String, java.lang.Class) }.
  171. * @param locatorClass The locator class to unregister
  172. *
  173. * @see #registerLocator(java.lang.String, java.lang.Class)
  174. */
  175. public void unregisterLocator(String rootPath, Class<? extends AssetLocator> locatorClass);
  176. /**
  177. * Add an {@link AssetEventListener} to receive events from this
  178. * <code>AssetManager</code>.
  179. * @param listener The asset event listener to add
  180. */
  181. public void addAssetEventListener(AssetEventListener listener);
  182. /**
  183. * Remove an {@link AssetEventListener} from receiving events from this
  184. * <code>AssetManager</code>
  185. * @param listener The asset event listener to remove
  186. */
  187. public void removeAssetEventListener(AssetEventListener listener);
  188. /**
  189. * Removes all asset event listeners.
  190. *
  191. * @see #addAssetEventListener(com.jme3.asset.AssetEventListener)
  192. */
  193. public void clearAssetEventListeners();
  194. /**
  195. * Set an {@link AssetEventListener} to receive events from this
  196. * <code>AssetManager</code>. Any currently added listeners are
  197. * cleared and then the given listener is added.
  198. *
  199. * @param listener The listener to set
  200. * @deprecated Please use {@link #addAssetEventListener(com.jme3.asset.AssetEventListener) }
  201. * to listen for asset events.
  202. */
  203. @Deprecated
  204. public void setAssetEventListener(AssetEventListener listener);
  205. /**
  206. * Manually locates an asset with the given {@link AssetKey}. This method
  207. * should be used for debugging or internal uses. <br/>
  208. * The call will attempt to locate the asset by invoking the
  209. * {@link AssetLocator} that are registered with this <code>AssetManager</code>,
  210. * in the same way that the {@link AssetManager#loadAsset(com.jme3.asset.AssetKey) }
  211. * method locates assets.
  212. *
  213. * @param key The {@link AssetKey} to locate.
  214. * @return The {@link AssetInfo} object returned from the {@link AssetLocator}
  215. * that located the asset, or null if the asset cannot be located.
  216. */
  217. public AssetInfo locateAsset(AssetKey<?> key);
  218. /**
  219. * Load an asset from a key, the asset will be located
  220. * by one of the {@link AssetLocator} implementations provided in the
  221. * {@link AssetManager#registerLocator(java.lang.String, java.lang.Class) }
  222. * call. If located successfully, it will be loaded via the the appropriate
  223. * {@link AssetLoader} implementation based on the file's extension, as
  224. * specified in the call
  225. * {@link AssetManager#registerLoader(java.lang.Class, java.lang.String[]) }.
  226. *
  227. * @param <T> The object type that will be loaded from the AssetKey instance.
  228. * @param key The AssetKey
  229. * @return The loaded asset, or null if it was failed to be located
  230. * or loaded.
  231. */
  232. public <T> T loadAsset(AssetKey<T> key);
  233. /**
  234. * Load an asset by name, calling this method
  235. * is the same as calling
  236. * <code>
  237. * loadAsset(new AssetKey(name)).
  238. * </code>
  239. *
  240. * @param name The name of the asset to load.
  241. * @return The loaded asset, or null if failed to be loaded.
  242. *
  243. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  244. */
  245. public Object loadAsset(String name);
  246. /**
  247. * Loads texture file, supported types are BMP, JPG, PNG, GIF,
  248. * TGA and DDS.
  249. *
  250. * @param key The {@link TextureKey} to use for loading.
  251. * @return The loaded texture, or null if failed to be loaded.
  252. *
  253. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  254. */
  255. public Texture loadTexture(TextureKey key);
  256. /**
  257. * Loads texture file, supported types are BMP, JPG, PNG, GIF,
  258. * TGA and DDS.
  259. *
  260. * @param name The name of the texture to load.
  261. * @return The texture that was loaded
  262. *
  263. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  264. */
  265. public Texture loadTexture(String name);
  266. /**
  267. * Load audio file, supported types are WAV or OGG.
  268. * @param key Asset key of the audio file to load
  269. * @return The audio data loaded
  270. *
  271. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  272. */
  273. public AudioData loadAudio(AudioKey key);
  274. /**
  275. * Load audio file, supported types are WAV or OGG.
  276. * The file is loaded without stream-mode.
  277. * @param name Asset name of the audio file to load
  278. * @return The audio data loaded
  279. *
  280. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  281. */
  282. public AudioData loadAudio(String name);
  283. /**
  284. * Loads a 3D model with a ModelKey.
  285. * Models can be jME3 object files (J3O) or OgreXML/OBJ files.
  286. * @param key Asset key of the model to load
  287. * @return The model that was loaded
  288. *
  289. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  290. */
  291. public Spatial loadModel(ModelKey key);
  292. /**
  293. * Loads a 3D model. Models can be jME3 object files (J3O) or
  294. * OgreXML/OBJ files.
  295. * @param name Asset name of the model to load
  296. * @return The model that was loaded
  297. *
  298. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  299. */
  300. public Spatial loadModel(String name);
  301. /**
  302. * Load a material instance (J3M) file.
  303. * @param name Asset name of the material to load
  304. * @return The material that was loaded
  305. *
  306. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  307. */
  308. public Material loadMaterial(String name);
  309. /**
  310. * Loads shader file(s), shouldn't be used by end-user in most cases.
  311. *
  312. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  313. */
  314. public Shader loadShader(ShaderKey key);
  315. /**
  316. * Load a font file. Font files are in AngelCode text format,
  317. * and are with the extension "fnt".
  318. *
  319. * @param name Asset name of the font to load
  320. * @return The font loaded
  321. *
  322. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  323. */
  324. public BitmapFont loadFont(String name);
  325. /**
  326. * Loads a filter *.j3f file with a FilterKey.
  327. * @param key Asset key of the filter file to load
  328. * @return The filter that was loaded
  329. *
  330. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  331. */
  332. public FilterPostProcessor loadFilter(FilterKey key);
  333. /**
  334. * Loads a filter *.j3f file with a FilterKey.
  335. * @param name Asset name of the filter file to load
  336. * @return The filter that was loaded
  337. *
  338. * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
  339. */
  340. public FilterPostProcessor loadFilter(String name);
  341. /**
  342. * Sets the shaderGenerator to generate shaders based on shaderNodes.
  343. * @param generator the shaderGenerator
  344. */
  345. public void setShaderGenerator(ShaderGenerator generator);
  346. /**
  347. * Returns the shaderGenerator responsible for generating the shaders
  348. * @return the shaderGenerator
  349. */
  350. public ShaderGenerator getShaderGenerator(EnumSet<Caps> caps);
  351. }