LapisSea

Untitled

Sep 9th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. //MAIN SHIT
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.URL;
  9. import java.util.ArrayDeque;
  10. import java.util.Iterator;
  11. import java.util.Queue;
  12.  
  13. public class Loader {
  14.     public static byte[] loadAsset(String localPath, ObjectReturn<String> retreveUrl, String name){
  15.         return loadAsset(localPath, retreveUrl, name, true);
  16.     }
  17.     public static byte[] loadAsset(String localPath, ObjectReturn<String> retreveUrl, String name, boolean shouldSaveToFile){
  18.         File local=new File(localPath);
  19.         try{
  20.             System.out.println("Loading "+name+"...");
  21.            
  22.             FileInputStream in=new FileInputStream(local);
  23.             Queue<Byte> buff=new ArrayDeque<>();
  24.             byte[] b={0},actual;
  25.             while(in.read(b)>-1)buff.add(b[0]);
  26.             in.close();
  27.             actual=new byte[buff.size()];
  28.             Iterator<Byte> buf=buff.iterator();
  29.             for(int i=0;i<actual.length;i++){
  30.                 actual[i]=buf.next();
  31.             }
  32.            
  33.            
  34.             System.out.println(name+" loaded!");
  35.             return actual;
  36.         }catch(IOException e1){
  37.             System.out.println(name+" not found. Loading from internet...");
  38.             try{
  39.                 URL src=new URL(retreveUrl.process());
  40.                 InputStream in=src.openStream();
  41.                
  42.                 Queue<Byte> buff=new ArrayDeque<>();
  43.                 byte[] b={0},actual;
  44.                 while(in.read(b)>-1)buff.add(b[0]);
  45.                 in.close();
  46.                 actual=new byte[buff.size()];
  47.                 Iterator<Byte> buf=buff.iterator();
  48.                 for(int i=0;i<actual.length;i++){
  49.                     actual[i]=buf.next();
  50.                 }
  51.                 System.out.println(name+" loaded!");
  52.                 if(shouldSaveToFile){
  53.                     System.out.println("Saving "+name+" to local file...");
  54.                     local.createNewFile();
  55.                     OutputStream out=new FileOutputStream(local);
  56.                     out.write(actual, 0, actual.length);
  57.                     out.close();
  58.                     System.out.println("Saved!");
  59.                 }
  60.                 return actual;
  61.             }catch(IOException e2){
  62.                 System.out.println(name+" failed to load!");
  63.                 return null;
  64.             }
  65.         }
  66.        
  67.     }
  68. }
  69.  
  70.  
  71. public interface ObjectReturn<T>{
  72.     public abstract T process();
  73. }
  74.  
  75. //EXAMPLE
  76. try{
  77.     setIconImage(ImageIO.read(new ByteArrayInputStream(Loader.loadAsset("LapisAnimatorIcon.png", ()->"http://i.imgur.com/idpF9O7.png", "Icon"))));
  78. }catch(Exception e1){
  79.     e1.printStackTrace();
  80. }
  81. try{
  82.     byte[] data=Loader.loadAsset("ComfortaaFont.tff", ()->{
  83.         String src=new String(Loader.loadAsset("ew7futgaw0 ertgwzegfydvfu[cb xa3/", ()->"https://fonts.googleapis.com/css?family=Comfortaa", "Comfortaa-font-src-getter-dummy",false));
  84.         return src.substring(src.lastIndexOf("url(")+4, src.lastIndexOf(") "));
  85.     }, "ComfortaaFont.tff");
  86.    
  87.     GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
  88.     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(data)));
  89. }catch(Exception e1){
  90.     e1.printStackTrace();
  91.     System.err.println("CRITICAL ASSET FAILED TO LOAD!");
  92.     System.out.println("Exiting!");
  93.     System.exit(0);
  94. }
Add Comment
Please, Sign In to add comment