Jump to content

Trouble loading local resources


Recommended Posts

Posted

Last time I touched this script there was no trouble, that was like 6 months ago. For some reason trying to load local resources returns null no matter what I try. (I've read through the forums and practically every google result, nothing has worked so far.)

projectfiles.PNG.28ed31a6f2b17070219ed48537dd5fa5.PNG

private Image bg;
private Image bankPaintOn;
private Image bankPaintOff;
private Image startButtonimg;
private Image cursor;
@Override
public void onStart() {
    bg = getImage("images/Background.png");
    bankPaintOn = getImage("images/banking_On.png");
    bankPaintOff = getImage("images/banking_Off.png");
    startButtonimg = getImage("images/Start.png");
    cursor = getImage("images/Cursor.png");
}
    private Image getImage(String path)
    {
        Image img;
        try{
            img = ImageIO.read(this.getClass().getResourceAsStream(path));
            return img;
        } catch(IOException e){
            log(e);
            return null;
        }
    }

[ERROR][Bot #1][04/03 07:46:26 PM]: Error in script onStart(): 
java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at Main.getImage(Main.java:604)
    at Main.onStart(Main.java:208)
    at org.osbot.rs07.event.ScriptExecutor.IiIiIiiiiIII(em:105)
    at org.osbot.rs07.event.ScriptExecutor.start(em:234)
    at org.osbot.ub.run(bx:36)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Posted (edited)

embeded resources will not work in osbot you will have to download the images from the web or you can put them in your osbot data folder.

Here is some example code of where I try to load a file from the osbot data directory and if not found I download it from the web and create the cache

https://github.com/battleguard/easyalch/blob/master/src/script/services/PriceLoader.java#L100

Here someones example snippet of a file utility class

 

Edited by battleguard
Posted (edited)
17 hours ago, battleguard said:

embeded resources will not work in osbot you will have to download the images from the web or you can put them in your osbot data folder.

Here is some example code of where I try to load a file from the osbot data directory and if not found I download it from the web and create the cache

https://github.com/battleguard/easyalch/blob/master/src/script/services/PriceLoader.java#L100

Here someones example snippet of a file utility class

 

Thanks for the reply. Is this new that local resources don't work?

Edited by Cloxygen
Posted (edited)

Resources works for the SDN, but not for local scripts.

To get around this, you'll need two separate pieces of interchangeable code. When you're ready to publish your script, make sure to have the script load assets from the resource folder. Otherwise, have the script load the resource from the data folder. For example:

   /**
    * Attempt to load sprites from resource folder.
    * 
    * @param dir
    *            - (e.g. "/resources/sprites/record-blue.png")
    * @return new {@link ImageIcon} instance
    */
   private static ImageIcon loadImageFromResource(String dir) {
       ImageIcon imageIcon = null;
       try {
           imageIcon = new ImageIcon(ImageIO.read(MacroRecorder.class.getResourceAsStream(dir)));
       } catch (IOException e) {
           //Attempt to print to GLOBAL_LOGGER 
           Logger.GLOBAL_LOGGER.error(e);
       }
       return imageIcon;
   }
   /*private static ImageIcon loadImageFromResource(String dir) {
       ImageIcon imageIcon = null;
       try {
           imageIcon = new ImageIcon(
                   ImageIO.read(new File(String.format("%s/OSBot/Data/%s", System.getProperty("user.home"), dir))));
       } catch (IOException e) {
           Logger.GLOBAL_LOGGER.error(e);
       }
       return imageIcon;
   }*/

 

Edited by liverare
Posted
1 hour ago, liverare said:

Resources works for the SDN, but not for local scripts.

To get around this, you'll need two separate pieces of interchangeable code. When you're ready to publish your script, make sure to have the script load assets from the resource folder. Otherwise, have the script load the resource from the data folder. For example:


   /**
    * Attempt to load sprites from resource folder.
    * 
    * @param dir
    *            - (e.g. "/resources/sprites/record-blue.png")
    * @return new {@link ImageIcon} instance
    */
   private static ImageIcon loadImageFromResource(String dir) {
       ImageIcon imageIcon = null;
       try {
           imageIcon = new ImageIcon(ImageIO.read(MacroRecorder.class.getResourceAsStream(dir)));
       } catch (IOException e) {
           //Attempt to print to GLOBAL_LOGGER 
           Logger.GLOBAL_LOGGER.error(e);
       }
       return imageIcon;
   }
   /*private static ImageIcon loadImageFromResource(String dir) {
       ImageIcon imageIcon = null;
       try {
           imageIcon = new ImageIcon(
                   ImageIO.read(new File(String.format("%s/OSBot/Data/%s", System.getProperty("user.home"), dir))));
       } catch (IOException e) {
           Logger.GLOBAL_LOGGER.error(e);
       }
       return imageIcon;
   }*/

 

Use getDirectoryData() from Script

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...