public static Texture getTextureFromJar(String path) {
    boolean wasSandboxed = SpoutClient.isSandboxed();
    SpoutClient.disableSandbox();
    try {
      if (textures.containsKey(path)) {
        return textures.get(path);
      }

      Texture texture = null;
      // Check inside jar
      try {
        InputStream stream = Minecraft.class.getResourceAsStream(path);
        texture = TextureLoader.getTexture("PNG", stream, true, GL11.GL_NEAREST);
        stream.close();
      } catch (Exception e) {
      }
      // Check MCP/Eclipse Path
      if (texture == null) {
        texture =
            getTextureFromPath(
                FileUtil.getSpoutcraftDirectory().getAbsolutePath() + "/../.." + path);
      }

      if (texture != null) {
        textures.put(path, texture);
      }

      return texture;
    } finally {
      SpoutClient.enableSandbox(wasSandboxed);
    }
  }
  @SuppressWarnings("unchecked")
  public void readData(SpoutInputStream input) throws IOException {
    String packetName = input.readString();

    boolean sandboxed = SpoutClient.isSandboxed();
    SpoutClient.enableSandbox();

    try {
      Class<? extends AddonPacket> packetClass = AddonPacket.getPacketFromId(packetName);
      Constructor<? extends AddonPacket> constructor = null;
      Constructor<? extends AddonPacket>[] constructors =
          (Constructor<? extends AddonPacket>[]) packetClass.getConstructors();
      for (Constructor<? extends AddonPacket> c : constructors) {
        if (c.getGenericParameterTypes().length == 0) {
          constructor = c;
          break;
        }
      }
      packet = constructor.newInstance();
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (!sandboxed) {
      SpoutClient.disableSandbox();
    }

    int size = input.readInt();
    compressed = input.readBoolean();
    data = new byte[size];
    input.read(data);
  }
  public static Texture getTextureFromPath(String path) {
    boolean wasSandboxed = SpoutClient.isSandboxed();
    SpoutClient.disableSandbox();
    try {
      if (textures.containsKey(path)) {
        return textures.get(path);
      }
      Texture texture = null;
      try {
        FileInputStream stream = new FileInputStream(path);
        if (stream.available() > 0) {
          texture = TextureLoader.getTexture("PNG", stream, true, GL11.GL_NEAREST);
        }
        stream.close();
      } catch (IOException e) {
      }

      if (texture != null) {
        textures.put(path, texture);
      }
      return texture;
    } finally {
      SpoutClient.enableSandbox(wasSandboxed);
    }
  }
 public static boolean isTextureDownloaded(String addon, String url) {
   boolean wasSandboxed = SpoutClient.isSandboxed();
   SpoutClient.disableSandbox();
   try {
     return getTextureFile(addon, url).exists();
   } finally {
     SpoutClient.enableSandbox(wasSandboxed);
   }
 }
Esempio n. 5
0
 public void download() {
   if (download == null && !installed) {
     downloadFail = null;
     boolean wasSandboxed = SpoutClient.isSandboxed();
     if (wasSandboxed) SpoutClient.disableSandbox();
     download = new Download(this);
     downloads.put(getId(), download);
     download.start();
     if (wasSandboxed) SpoutClient.enableSandbox();
   }
 }
  public void run(int playerId) {
    if (packet != null) {
      SpoutInputStream stream = new SpoutInputStream(ByteBuffer.wrap(data));

      boolean sandboxed = SpoutClient.isSandboxed();
      SpoutClient.enableSandbox();
      try {
        packet.read(stream);
        packet.run();
      } catch (Exception e) {
        e.printStackTrace();
      }

      if (!sandboxed) {
        SpoutClient.disableSandbox();
      }
    }
  }
 public static Texture getTextureFromUrl(String plugin, String url, boolean download) {
   boolean wasSandboxed = SpoutClient.isSandboxed();
   SpoutClient.disableSandbox();
   try {
     File texture = getTextureFile(plugin, url);
     if (!texture.exists()) {
       return null;
     }
     try {
       return getTextureFromPath(texture.getCanonicalPath());
     } catch (IOException e) {
       e.printStackTrace();
       return null;
     }
   } finally {
     SpoutClient.enableSandbox(wasSandboxed);
   }
 }
  public PacketAddonData(AddonPacket packet) {
    this.packet = packet;
    SpoutOutputStream stream = new SpoutOutputStream();

    boolean sandboxed = SpoutClient.isSandboxed();
    SpoutClient.enableSandbox();
    try {
      packet.write(stream);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (!sandboxed) {
      SpoutClient.disableSandbox();
    }
    ByteBuffer buffer = stream.getRawBuffer();
    data = new byte[buffer.capacity() - buffer.remaining()];
    System.arraycopy(buffer.array(), 0, data, 0, data.length);
    needsCompression = data.length > 512;
  }
  public static String getTexturePathFromUrl(String plugin, String url) {
    if (!isTextureDownloaded(plugin, url)) {
      return null;
    }

    boolean wasSandboxed = SpoutClient.isSandboxed();
    SpoutClient.disableSandbox();
    try {
      File download = new File(FileUtil.getTempDirectory(), FileUtil.getFileName(url));
      try {
        return download.getCanonicalPath();
      } catch (IOException e) {
        e.printStackTrace();
      }
    } finally {
      SpoutClient.enableSandbox(wasSandboxed);
    }
    return null;
  }
 public static void downloadTexture(String plugin, String url, boolean ignoreEnding) {
   boolean wasSandboxed = SpoutClient.isSandboxed();
   SpoutClient.disableSandbox();
   try {
     String fileName = FileUtil.getFileName(url);
     if (!ignoreEnding && !FileUtil.isImageFile(fileName)) {
       System.out.println("Rejecting download of invalid texture: " + fileName);
     } else if (!isTextureDownloading(url) && !isTextureDownloaded(plugin, url)) {
       File dir = FileUtil.getTempDirectory();
       if (plugin != null) {
         dir = new File(FileUtil.getCacheDirectory(), plugin);
         dir.mkdir();
       }
       Download download = new Download(fileName, dir, url, null);
       FileDownloadThread.getInstance().addToDownloadQueue(download);
     }
   } finally {
     SpoutClient.enableSandbox(wasSandboxed);
   }
 }
 public static File getTextureFile(String plugin, String url) {
   boolean wasSandboxed = SpoutClient.isSandboxed();
   SpoutClient.disableSandbox();
   try {
     String fileName = FileUtil.getFileName(url);
     File cache = cacheTextureFiles.get(plugin + File.separator + fileName);
     if (cache != null) {
       return cache;
     }
     if (plugin != null) {
       File file = FileUtil.findFile(plugin, fileName);
       if (file != null) {
         cacheTextureFiles.put(plugin + File.separator + fileName, file);
         return file;
       }
     }
     return new File(FileUtil.getTempDirectory(), fileName);
   } finally {
     SpoutClient.enableSandbox(wasSandboxed);
   }
 }