Simple AutoUpdating v2 - Update more efficiently

Discussion in 'Resources' started by oyasunadev, Oct 4, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    oyasunadev

    Hey guys, me again. I realized there were things I forgot to include in my first release here, so I recoded the whole system! Now with YAML integration, I added in the build feature in the version number. For example, major.minor.build. Without any further discussion here it is!

    downloadFile(String site, String destination) - Download a file saved to a set directory.
    site - URL to download from.
    destination - Location to save the file to.
    downloadFile(String site
    PHP:
    public void downloadFile(String siteString destination)
    {
            try {
                    
    BufferedInputStream in = new BufferedInputStream(new URL(site).openStream());
                    
    FileOutputStream fout = new FileOutputStream(destination);
     
                    
    byte data[] = new byte[1024]; //Download 1 KB at a time
                    
    int count;
                    while((
    count in.read(data01024)) != -1)
                    {
                            
    fout.write(data0count);
                    }
     
                    
    in.close();
                    
    fout.close();
            } catch(
    MalformedURLException e) {
                    
    e.printStackTrace();
            } catch(
    IOException e) {
                    
    e.printStackTrace();
            }
    }
    newTempFile(String site) - Download a files and uses it as a temporary file.
    site - URL to download from.
    newTempFile(String site) (open)
    PHP:
    public File newTempFile(String site)
    {
            try {
                    
    File file File.createTempFile("latest""");
                    
    file.deleteOnExit();
     
                    
    BufferedInputStream in = new BufferedInputStream(new URL(site).openStream());
                    
    FileOutputStream fout = new FileOutputStream(file);
     
                    
    byte data[] = new byte[1024]; //Download 1 KB at a time
                    
    int count;
                    while((
    count in.read(data01024)) != -1)
                    {
                            
    fout.write(data0count);
                    }
     
                    
    in.close();
                    
    fout.close();
     
                    return 
    file;
            } catch(
    MalformedURLException e) {
                    
    e.printStackTrace();
            } catch(
    IOException e) {
                    
    e.printStackTrace();
            }
     
            return 
    null;
    }

    Usage
    Usage (open)
    PHP:
    GLOBAL VARIABLES:
    boolean updateAvailable false;
     
    ONENABLE:
    PluginManager pm getServer().getPluginManager();
     
    Configuration latestConfig = new Configuration(newTempFile("http://oyasdev.site40.net/latest.yml"));
    latestConfig.load();
     
    double[] currentVersion = {Double.valueOf(pdfFile.getVersion().split("\\.")[0]),
                    
    Double.valueOf(pdfFile.getVersion().split("\\.")[1]),
                    
    Double.valueOf(pdfFile.getVersion().split("\\.")[2])};
    double[] latestVersion = {latestConfig.getDouble("Major"currentVersion[0]),
                    
    latestConfig.getDouble("Minor"currentVersion[1]),
                    
    latestConfig.getDouble("Build"currentVersion[2])};
    if(
    latestVersion[0] > currentVersion[0] ||
                    
    latestVersion[1] > currentVersion[1] ||
                    
    latestVersion[2] > currentVersion[2])
    {
            
    log.info("[MyPlugin v1.0.0] Update available!");
    updateAvailable true;
     
            
    pm.disablePlugin(this);
    } else {
            
    log.info("[MyPlugin v1.0.0] Up to date!");
    }
     
    if(!
    updateAvailable)
    {
            
    //REGULAR ENABLING STUFF
    }
     
    ONDISABLE:
    if(
    updateAvailable)
    {
            
    log.info("[MyPlugin v1.0.0] Downloading update...");
            
    downloadFile("http://oyasdev.site40.net/GameTime.jar"getDataFolder().getPath() + ".jar");
            
    log.info("[MyPlugin v1.0.0] Downloaded update!");
     
            
    log.info("[MyPlugin v1.0.0] Reloading server...");
            
    getServer().reload();
    } else {
            
    //REGULAR DISABLING STUFF
    }
    [/SPOLIER]
     
    Lolmewn and r3Fuze like this.
  2. Offline

    iPhysX

    Hmm :) looks nice. I'll try it later :)
     
  3. Offline

    dralletje

  4. Offline

    Lolmewn

    Nice!
     
  5. Offline

    garbagemule

    As with your other thread, proper error handling is completely left out, which (like I said in the other post) could render plugins unusable due to simple things like I/O errors or connection issues, which shouldn't be the case. The Configuration will throw a FileNotFoundException if the connection to the .yml can't be established. The version number is rigid and doesn't allow for added sub-versions if need be.

    I think it was mentioned also in your other post (or somewhere else) to simply download to the update-folder, which will make sure to auto-update upon server reload/restart, so you don't have to create unnecessary folders and temp files.
     
    iPhysX likes this.
  6. Offline

    Jayjay110

    pfft This looks very similar to my code :p
     
  7. Offline

    Luloak2

    Yeah, GREAT Tutorial :)
     
Thread Status:
Not open for further replies.

Share This Page