Thread: Client Auto-Updater

Results 1 to 8 of 8
  1. #1 Client Auto-Updater 
    Registered Member
    Join Date
    Apr 2012
    Posts
    31
    Thanks given
    0
    Thanks received
    0
    Rep Power
    11
    So I've managed to create my own client auto-updater, but I can't seem to get it to download.

    Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
    import javax.swing.*;
    import sign.signlink;
    import java.net.*;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import javax.swing.JOptionPane;
    import java.applet.Applet;
    import java.applet.AppletContext;
    import java.awt.*;
    import java.io.*;
    import java.math.BigInteger;
    import java.net.*;
    import java.util.zip.CRC32;
    import sign.signlink;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.sound.midi.*;
    import javax.sound.sampled.*;
    import javax.swing.JOptionPane;
    import java.applet.Applet;
    import java.applet.AppletContext;
    import java.awt.*;
    import java.io.*;
    import java.math.BigInteger;
    import java.net.*;
    import java.util.zip.CRC32;
    import sign.signlink;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.sound.midi.*;
    import javax.sound.sampled.*;
    import javax.swing.JOptionPane;
    
    public class Updater extends Thread
    {
    
        public String name;
        public String dir;
    
    
        public void get(String url, String name1, String dir1, String type)
        {
            dir = dir1;
            name = name1;
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("New Client Downloader");
            frame.setLocationRelativeTo(null);
            frame.setLayout(new BorderLayout());
            frame.setPreferredSize(new Dimension(300, 80));
            frame.setResizable(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            client client = new client();
            ClassLoader cl = getClass().getClassLoader();
            try
            {
                URLConnection connection = (new URL(url)).openConnection();
                String f[] = url.split("/");
                File file = new File(f[f.length - 1]);
                int length = connection.getContentLength();
                InputStream instream = connection.getInputStream();
                try{new File(signlink.updatedir()+dir).mkdir();}catch(Exception e){}
                FileOutputStream outstream = new FileOutputStream(signlink.updatedir()+dir+file);
                int size = 0;
                int copy = 0;
                JProgressBar bar = new JProgressBar();
                bar.setStringPainted(true);
                bar.setMaximum(length);
                frame.add(bar, "Center");
                frame.pack();
                frame.setVisible(true);
                while((copy = instream.read()) != -1)
                {
                    outstream.write(copy);
                    size++;
                    int percentage = (int)(((double)size / (double)length) * 100D);
                    bar.setValue(size);
                    bar.setString("Downloading "+type+" "+percentage+"%");
                }
                if(length != size)
                {
                    instream.close();
                    outstream.close();
                } else
                {
                    bar.setString("Unpacking files...");
                    instream.close();
                    outstream.close();
                       unZipFile(); 
    deleteFile();  
    System.exit(0);
    
    
                    frame.setVisible(false);
                }
            }
            catch(Exception e)
            {
                System.err.println("Error connecting to update server.");
                e.printStackTrace();
            }
        }
    
        private void writeStream(InputStream In, OutputStream Out) throws IOException
        {
            byte Buffer[] = new byte[1024];
            int Len;
            while((Len = In.read(Buffer)) >= 0)
            {
                Out.write(Buffer, 0, Len);
            }
            In.close();
            Out.close();
        }
    
        private void unZipFile()
        {
            try
            {
                ZipFile ZipFile = new ZipFile(signlink.updatedir()+dir+name);
                for(Enumeration Entries = ZipFile.entries(); Entries.hasMoreElements();)
                {
                    ZipEntry Entry = (ZipEntry)Entries.nextElement();
                    if(Entry.isDirectory())
                    {
                        (new File(signlink.updatedir()+dir+Entry.getName())).mkdir();
                    } else
                    {
                        
                    writeStream(ZipFile.getInputStream(Entry), new BufferedOutputStream(new FileOutputStream(signlink.updatedir()+dir+Entry.getName())));
    }
                }
                ZipFile.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    
        private void deleteFile()
        {
            try
            {
                File file = new File(signlink.updatedir()+dir+name);                
                file.delete();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    
    
    }
    That's my updater class.

    Do you have any idea on how to make it actually download?
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Nov 2011
    Posts
    43
    Thanks given
    0
    Thanks received
    3
    Rep Power
    14
    What the fuck is with all those imports? Are they necessary?

    Heres a much cleaner Updater: Call it UpdateCache.java

    Code:
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.zip.*;
    import sign.signlink;
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class UpdateCache implements Runnable {
    
    	public static final String ZIP_URL = "http://YOUR_SITE.com/cache.zip";
    	public static final String VERSION_URL = "http://YOUR_SITE.com/cacheVersion.txt";
    	public static final String VERSION_FILE = sign.signlink.findcachedir()+"cacheVersion.dat";
    	private client client;
    	client frame;
        public UpdateCache(client client) {
                    this.client = client;
        }
    		
        private void drawLoadingText(int amount, String text) {
                    client.drawLoadingText(amount, text);
        }
    	
    	public double getCurrentVersion(){
    		try {
    			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(VERSION_FILE)));
    			return Double.parseDouble(br.readLine());
    		} catch (Exception e) {
    			return 0.1;
    		}
    	}
    	
    	public double getNewestVersion(){
    		try {
    			URL tmp = new URL(VERSION_URL);
    			BufferedReader br = new BufferedReader(new InputStreamReader(tmp.openStream()));
    			return Double.parseDouble(br.readLine());
    		} catch (Exception e) {
    			handleException(e);
    			return -1;
    		}
    	}
    	
    	private void handleException(Exception e){
    		StringBuilder strBuff = new StringBuilder();
    		strBuff.append("Please Screenshot this message, and send it to an admin!\r\n\r\n");
            StringBuilder append = strBuff.append(e.getClass().getName()).append(" \"").append(e.getMessage()).append("\"\r\n");
    		for(StackTraceElement s : e.getStackTrace())
    			strBuff.append(s.toString()).append("\r\n");
    		alert("Exception [" + e.getClass().getSimpleName() + "]",strBuff.toString(),true);
    	}
    	
    	private void alert(String msg){
    		alert("Message",msg,false);
    	}
    	
    	private void alert(String title,String msg,boolean error){
    		JOptionPane.showMessageDialog(null,
    			   msg,
    			   title,
    			    (error ? JOptionPane.ERROR_MESSAGE : JOptionPane.PLAIN_MESSAGE));
    	}
    	
    	@Override
    	public void run() {
    	drawLoadingText(0, "Checking Versions");
    		try{
    		double newest = getNewestVersion();
    		if(newest > this.getCurrentVersion()){
    			int n = JOptionPane.showConfirmDialog(
    				    null,
    				    "There is an update to version " + newest + "\n" +
    					"Would you like to update?",
    				    "Current version: "+ getCurrentVersion(),
    				    JOptionPane.YES_NO_OPTION);
    			if(n == 0){
    				updateClient();
    				drawLoadingText(0, "Cache has been updated, please restart the client!");
    				alert("Cache has been updated, please restart the client!");
    				OutputStream out = new FileOutputStream(VERSION_FILE);
    				out.write(String.valueOf(newest).getBytes());;
    				System.exit(0);
    			}else{
    				alert(" Your client may not load correct " +
    				getCurrentVersion());
    				//System.exit(0);
    			}
    		}
    		}catch(Exception e){
    			handleException(e);
    		}
    	}
    	
    	private void updateClient() {
    		File clientZip = downloadClient();
    		if(clientZip != null){
    		unZip(clientZip);
    		}
    	}
    	
    	private void unZip(File clientZip) {
    		try {
    			unZipFile(clientZip,new File(sign.signlink.findcachedir()));
    			clientZip.delete();
    		} catch (IOException e) {
    			handleException(e);
    		}
    	}
    	
    	private void unZipFile(File zipFile,File outFile) throws IOException{
    		ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    		ZipEntry e;
    		long max = 0;
    		long curr = 0;
    		while((e = zin.getNextEntry()) != null)
    			max += e.getSize();
    		zin.close();
    		ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    		while((e = in.getNextEntry()) != null){
    			if(e.isDirectory())
    				new File(outFile,e.getName()).mkdirs();
    			else{
    				FileOutputStream out = new FileOutputStream(new File(outFile,e.getName()));
    				byte[] b = new byte[1024];
    				int len;
    				while((len = in.read(b,0,b.length)) > -1){
    					curr += len;
    						out.write(b, 0, len);
    						setUnzipPercent((int)((curr * 100) / max));
    				}
    				out.flush();
    				out.close();
    			}
    		}
    	}
    
    	public int percent = 0;
    	
    	public void setDownloadPercent(int amount){
    	        percent = amount;
    			drawLoadingText(amount, "Downloading Cache" + " - " + amount + "%");
    	}
    	
    	public int percent2 = 0;
    	
    	public void setUnzipPercent(int amount2){
    	        percent2 = amount2;
    			drawLoadingText(amount2, "Extracting Cache" + " - " + amount2 + "%");
    	}
    
    	private File downloadClient(){
    		File ret = new File(signlink.findcachedir()+"cache.zip");
    		try{
    		OutputStream out = new FileOutputStream(ret);
    		URLConnection conn = new URL(ZIP_URL).openConnection();
    		InputStream in = conn.getInputStream();
    		long max = conn.getContentLength();
    		long curr = 0;
    		byte[] b = new byte[1024];
    		int len;
    		while((len = in.read(b, 0, b.length)) > -1){
    			out.write(b,0,len);
    			curr += len;
    			setDownloadPercent((int)((curr * 100) / max));
    		}
    		out.flush();
    		out.close();
    		in.close();
    		return ret;
    		}catch(Exception e){
    			handleException(e);
    				ret.delete();
    			return null;
    		}
    	}
    }
    Open client.java and search
    Code:

    Code:
    void startUp()
    Under the first { add
    Code:
    Code:
    new UpdateCache(this).run();
    On your webhost make sure you have the cacheVersion.txt uploaded with the correct cache version.
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Apr 2012
    Posts
    31
    Thanks given
    0
    Thanks received
    0
    Rep Power
    11
    I'm going to sound like a complete retard here, but how the fuck do I change the cache version?

    I am stupid.
    Reply With Quote  
     

  4. #4  
    we up

    Number 0's Avatar
    Join Date
    Apr 2012
    Posts
    529
    Thanks given
    102
    Thanks received
    148
    Rep Power
    207
    Quote Originally Posted by InsanityLoL View Post
    I'm going to sound like a complete retard here, but how the fuck do I change the cache version?

    I am stupid.
    public static final String VERSION_URL = "http://YOUR_SITE.com/cacheVersion.txt";
    he's loading it from a site.

    EDIT: Oh client auto-updater
    june.
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Apr 2012
    Posts
    31
    Thanks given
    0
    Thanks received
    0
    Rep Power
    11
    Quote Originally Posted by Grievous View Post
    he's loading it from a site.
    Well, I thought there should've been two ways.

    I know it loads from the site, but ok.

    But thanks guys.
    Reply With Quote  
     

  6. #6  
    Registered Member
    Join Date
    Nov 2011
    Posts
    43
    Thanks given
    0
    Thanks received
    3
    Rep Power
    14
    Umm, this is a cache UPDATER not a DOWNLOADER right?
    in your cachedownloader.java you should have the version number in there?
    Just set them both as 1.
    you'll have some files in your cache like "cacheversion1 or 2 or 3 ect" or somthing.
    Delete those it'll create its own when it auto downloads.

    Plus your not stupid dude, i'm here to help you
    Reply With Quote  
     

  7. #7  
    Registered Member
    Join Date
    Nov 2011
    Posts
    43
    Thanks given
    0
    Thanks received
    3
    Rep Power
    14
    Quote Originally Posted by Grievous View Post
    he's loading it from a site.
    Actually no i'm not? it auto updates the first time it's downloaded.
    But you need to set up the autodownloader for it to even get a version number?
    lolol. As I said dude, its not a downloader, its an UPDATER.
    if you want an auto downloader there are plenty of tutorials.
    this will NOT work unless auto cache is set up.
    Reply With Quote  
     

  8. #8  
    Registered Member
    Join Date
    Apr 2012
    Posts
    31
    Thanks given
    0
    Thanks received
    0
    Rep Power
    11
    Quote Originally Posted by Oldskewl View Post
    Actually no i'm not? it auto updates the first time it's downloaded.
    But you need to set up the autodownloader for it to even get a version number?
    lolol. As I said dude, its not a downloader, its an UPDATER.
    if you want an auto downloader there are plenty of tutorials.
    this will NOT work unless auto cache is set up.
    Thanks mate.

    Mind checking my PM?
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Auto Server/Client/Cache Updater.
    By Colby in forum Tools
    Replies: 10
    Last Post: 03-28-2011, 08:54 PM
  2. Auto Client Updater
    By silabgarza in forum Help
    Replies: 12
    Last Post: 03-11-2011, 12:43 AM
  3. Replies: 0
    Last Post: 01-10-2011, 02:18 AM
  4. Bluur's Client File Auto Updater
    By Dudemanscott in forum Help
    Replies: 0
    Last Post: 11-26-2010, 03:40 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •