Jump to content
Cloxygen

Trouble loading local resources

Recommended Posts

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)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...