Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MAIN SHIT
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.util.ArrayDeque;
- import java.util.Iterator;
- import java.util.Queue;
- public class Loader {
- public static byte[] loadAsset(String localPath, ObjectReturn<String> retreveUrl, String name){
- return loadAsset(localPath, retreveUrl, name, true);
- }
- public static byte[] loadAsset(String localPath, ObjectReturn<String> retreveUrl, String name, boolean shouldSaveToFile){
- File local=new File(localPath);
- try{
- System.out.println("Loading "+name+"...");
- FileInputStream in=new FileInputStream(local);
- Queue<Byte> buff=new ArrayDeque<>();
- byte[] b={0},actual;
- while(in.read(b)>-1)buff.add(b[0]);
- in.close();
- actual=new byte[buff.size()];
- Iterator<Byte> buf=buff.iterator();
- for(int i=0;i<actual.length;i++){
- actual[i]=buf.next();
- }
- System.out.println(name+" loaded!");
- return actual;
- }catch(IOException e1){
- System.out.println(name+" not found. Loading from internet...");
- try{
- URL src=new URL(retreveUrl.process());
- InputStream in=src.openStream();
- Queue<Byte> buff=new ArrayDeque<>();
- byte[] b={0},actual;
- while(in.read(b)>-1)buff.add(b[0]);
- in.close();
- actual=new byte[buff.size()];
- Iterator<Byte> buf=buff.iterator();
- for(int i=0;i<actual.length;i++){
- actual[i]=buf.next();
- }
- System.out.println(name+" loaded!");
- if(shouldSaveToFile){
- System.out.println("Saving "+name+" to local file...");
- local.createNewFile();
- OutputStream out=new FileOutputStream(local);
- out.write(actual, 0, actual.length);
- out.close();
- System.out.println("Saved!");
- }
- return actual;
- }catch(IOException e2){
- System.out.println(name+" failed to load!");
- return null;
- }
- }
- }
- }
- public interface ObjectReturn<T>{
- public abstract T process();
- }
- //EXAMPLE
- try{
- setIconImage(ImageIO.read(new ByteArrayInputStream(Loader.loadAsset("LapisAnimatorIcon.png", ()->"http://i.imgur.com/idpF9O7.png", "Icon"))));
- }catch(Exception e1){
- e1.printStackTrace();
- }
- try{
- byte[] data=Loader.loadAsset("ComfortaaFont.tff", ()->{
- String src=new String(Loader.loadAsset("ew7futgaw0 ertgwzegfydvfu[cb xa3/", ()->"https://fonts.googleapis.com/css?family=Comfortaa", "Comfortaa-font-src-getter-dummy",false));
- return src.substring(src.lastIndexOf("url(")+4, src.lastIndexOf(") "));
- }, "ComfortaaFont.tff");
- GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
- ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(data)));
- }catch(Exception e1){
- e1.printStackTrace();
- System.err.println("CRITICAL ASSET FAILED TO LOAD!");
- System.out.println("Exiting!");
- System.exit(0);
- }
Add Comment
Please, Sign In to add comment