Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. package dk.grp1.tanks.core.internal.managers;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.assets.AssetManager;
  5. import com.badlogic.gdx.audio.Music;
  6. import com.badlogic.gdx.audio.Sound;
  7.  
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14.  
  15. public class CustomAssetManager {
  16.  
  17.     private AssetManager assetManager;
  18.     private Map<String, String> tempFileMap;
  19.     private String localStoragePath;
  20.  
  21.     public CustomAssetManager(String localStoragePath) {
  22.         this.assetManager = new AssetManager();
  23.         this.tempFileMap = new HashMap<>();
  24.         this.localStoragePath = localStoragePath;
  25.     }
  26.  
  27.     /**
  28.      * Creates a new temporary file from the given Class' ClassLoader and FileName.
  29.      * @param clazz
  30.      * @param fileName
  31.      * @return
  32.      */
  33.     private File createTempFileFromBundle(Class clazz, String fileName) {
  34.         InputStream is = clazz.getClassLoader().getResourceAsStream(fileName);
  35.         File tempFile = null;
  36.         String filePath = this.localStoragePath;
  37.         try {
  38.             tempFile = new File(filePath+"/"+ clazz.getSimpleName()+fileName);
  39.             //System.out.println("Writing temp file to " + tempFile.getAbsolutePath());
  40.  
  41.             try (FileOutputStream fos = new FileOutputStream(tempFile)){
  42.                 byte[] buffer = new byte[8 * 1024];
  43.                 int bytesRead;
  44.                 while ((bytesRead = is.read(buffer)) != -1) {
  45.                     fos.write(buffer, 0, bytesRead);
  46.                 }
  47.             }
  48.  
  49.             //System.out.println("TempFile: " + tempFile.getAbsolutePath());
  50.             // Store the file-name/type and the tempFile-path for later lookup
  51.             // Replacing backslashes with forward-slashes for compatibility issues in LibGDX AssetManager.
  52.             tempFileMap.put(clazz.getSimpleName()+fileName, tempFile.getAbsolutePath().replace("\\", "/"));
  53.  
  54.         } catch (IOException ex) {
  55.             ex.printStackTrace();
  56.         }
  57.  
  58.         return tempFile;
  59.     }
  60.  
  61.     public void loadSoundAsset(Class clazz, String fileName) {
  62.         if(tempFileMap.containsKey(clazz.getSimpleName()+fileName))
  63.             return;
  64.        
  65.         File assetFile = createTempFileFromBundle(clazz, fileName);
  66.  
  67.         assetManager.load(assetFile.getAbsolutePath(), Sound.class);
  68.  
  69.         assetManager.finishLoading();
  70.  
  71.         if (assetManager.isLoaded(assetFile.getAbsolutePath(), Sound.class)) {
  72.             //System.out.println("Loaded sound asset: " + assetFile.getAbsolutePath());
  73.         }
  74.  
  75.         for (String ass : assetManager.getAssetNames()) {
  76.             //System.out.println("AssetManager.getAssetNames()" + ass);
  77.         }
  78.     }
  79.  
  80.     public Sound getSoundAsset(Class clazz, String fileName) {
  81.         String tmpFileName = tempFileMap.get(clazz.getSimpleName()+fileName);
  82.  
  83.  
  84.         Sound sound = null;
  85.  
  86.         try {
  87.  
  88.             sound = assetManager.get(tmpFileName);
  89.  
  90.         } catch (NullPointerException ex) {
  91.             System.out.println("The sound asset: " + fileName + ", could not be found.");
  92.         }
  93.  
  94.         return sound;
  95.  
  96.         //return Gdx.audio.newSound(Gdx.files.absolute(tmpFileName.getAbsolutePath()));
  97.     }
  98.  
  99.     public void loadMusicAsset(Class clazz, String fileName) {
  100.         if(tempFileMap.containsKey(clazz.getSimpleName()+fileName))
  101.             return;
  102.  
  103.         File assetFile = createTempFileFromBundle(clazz, fileName);
  104.  
  105.         assetManager.load(assetFile.getAbsolutePath(), Music.class);
  106.  
  107.         assetManager.finishLoading();
  108.  
  109.         if (assetManager.isLoaded(assetFile.getAbsolutePath(), Music.class)) {
  110.             //System.out.println("Loaded sound asset: " + assetFile.getAbsolutePath());
  111.         }
  112.  
  113.         for (String ass : assetManager.getAssetNames()) {
  114.             //System.out.println("AssetManager.getAssetNames()" + ass);
  115.         }
  116.     }
  117.  
  118.     public Music getMusicAsset(Class clazz, String fileName) {
  119.         String tmpFileName = tempFileMap.get(clazz.getSimpleName()+fileName);
  120.  
  121.  
  122.         Music sound = null;
  123.  
  124.         try {
  125.  
  126.             sound = assetManager.get(tmpFileName);
  127.  
  128.         } catch (NullPointerException ex) {
  129.             System.out.println("The sound asset: " + fileName + ", could not be found.");
  130.         }
  131.  
  132.         return sound;
  133.  
  134.         //return Gdx.audio.newSound(Gdx.files.absolute(tmpFileName.getAbsolutePath()));
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement