Advertisement
Guest User

Untitled

a guest
Aug 1st, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. package me.kalmanolah.extras;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.net.URL;
  6. import java.nio.channels.Channels;
  7. import java.nio.channels.ReadableByteChannel;
  8. import java.util.logging.Logger;
  9.  
  10. public class OKUpdater{
  11.    
  12.     public static void update(String name, String ver, String checkloc, String dlloc, Logger log, String prefix){
  13.        
  14.         try {
  15.            
  16.             String latestver = null;
  17.            
  18.             log.info(prefix + "Initiating auto-update...");
  19.            
  20.             latestver = OKReader.main(checkloc + "?id=" + name);
  21.            
  22.             if(latestver != null){
  23.            
  24.                 String[] halve = latestver.split("\\&");
  25.                
  26.                 String[] newver = halve[0].split("\\.");
  27.                
  28.                 String[] oldver = ver.split("\\.");
  29.                
  30.                 int g = 0;
  31.                
  32.                 int h = 0;
  33.                
  34.                 for (String s: newver){
  35.                    
  36.                     g++;
  37.                    
  38.                 }
  39.                
  40.                 for (String r: oldver){
  41.                    
  42.                     h++;
  43.                    
  44.                 }
  45.                
  46.                 String isnew = null;
  47.                
  48.                 if(g > h){
  49.                    
  50.                     isnew = "yes";
  51.                    
  52.                 }else if((g == h) || (g < h)){
  53.                    
  54.                     int z = 0;
  55.                    
  56.                     while((z < (g - 1)) || (z == (g - 1)) && isnew == null){
  57.                        
  58.                         if(Integer.parseInt(newver[z]) > Integer.parseInt(oldver[z])){
  59.                            
  60.                             isnew = "yes";
  61.                            
  62.                         }
  63.                        
  64.                         if(Integer.parseInt(newver[z]) < Integer.parseInt(oldver[z])){
  65.                            
  66.                             isnew = "no";
  67.                            
  68.                         }
  69.                        
  70.                         z++;
  71.                        
  72.                     }
  73.                    
  74.                 }
  75.                
  76.                 if(isnew == null){
  77.                    
  78.                     isnew = "no";
  79.                    
  80.                 }
  81.                
  82.                 if(isnew.equals("yes")){
  83.                    
  84.                     log.info(prefix + "A new version of " + name + ", v" + halve[0] + " is available.");
  85.                    
  86.                     new File("plugins" + File.separator + name).mkdir();
  87.                    
  88.                     new File("plugins" + File.separator + name + File.separator + "update").mkdir();
  89.                    
  90.                     File file = new File("plugins" + File.separator + name + File.separator + "update" + File.separator + halve[1]);
  91.                    
  92.                     if(!file.exists()){
  93.                    
  94.                         log.info(prefix + "Starting download of " + name + " v" + halve[0] + "...");
  95.                        
  96.                         FileOutputStream fos;
  97.                            
  98.                         URL url = new URL(dlloc + "?id=" + name + "&ver=" + halve[0] + "&mc=1");
  99.                        
  100.                         ReadableByteChannel rbc = Channels.newChannel(url.openStream());
  101.                        
  102.                         fos = new FileOutputStream("plugins" + File.separator + name + File.separator + "update" + File.separator + halve[1]);
  103.                        
  104.                         fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  105.                        
  106.                         fos.close();
  107.                        
  108.                         log.info(prefix + halve[1] + " downloaded to " + File.separator + "plugins" + File.separator + name + File.separator + "update" + File.separator + ".");
  109.                
  110.                     }else{
  111.                        
  112.                         file.delete();
  113.                            
  114.                         log.info(prefix + "Starting download of " + name + " v" + halve[0] + "...");
  115.                        
  116.                         FileOutputStream fos;
  117.                            
  118.                         URL url = new URL(dlloc + "?id=" + name + "&ver=" + halve[0] + "&mc=1");
  119.                        
  120.                         ReadableByteChannel rbc = Channels.newChannel(url.openStream());
  121.                        
  122.                         fos = new FileOutputStream("plugins" + File.separator + name + File.separator + "update" + File.separator + halve[1]);
  123.                        
  124.                         fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  125.                        
  126.                         fos.close();
  127.                        
  128.                         log.info(prefix + halve[1] + " downloaded to " + File.separator + "plugins" + File.separator + name + File.separator + "update" + File.separator + ".");
  129.                        
  130.                     }
  131.                    
  132.                 }else{
  133.                    
  134.                     log.info(prefix + "You already have the latest version of " + name + ".");
  135.                    
  136.                 }
  137.            
  138.             }
  139.            
  140.         } catch (Exception e) {
  141.             log.info(prefix + "Error while checking for latest version.");
  142.         }
  143.        
  144.        
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement