PDA

View Full Version : 317-377... [Client]Version checker/loader + launcher GUI



Halfy
October 29th, 2010, 10:58
Purpose: Add a version checker/loader + launcher GUI to your client
Difficulty: 2/3, not very hard
Assumed Knowledge: c+p, ability to read, creating Java classes
Classes Modified: StartClient.java
Additional Requirements: You must have your own webserver that you have FTP access to.

Procedure

Step 1: First we create the new class
Create a new class called StartClient.java and put the following into it:

import java.io.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Toolkit;

import java.net.URI;
import java.net.URL;
import java.net.MalformedURLException;

public class StartClient extends JFrame implements ActionListener
{
private static final double CLIENT_VERSION = 1.21;
private static final String CLIENT_URL = "Put the URL to your client download here";
private static final String VERSION_URL = "Put the URL of your version.txt on your webserver here";

private JTextArea outputArea;
private JButton okButton;

private boolean outdated;

public StartClient()
{
outdated = false;

int width = 450, height = 200;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width) / 2;
int y = (screen.height - height) / 2;
setBounds(x, y, width, height);

super.setTitle("Version checking...");

okButton = new JButton();
okButton.setText("OK");
okButton.setEnabled(false);

outputArea = new JTextArea(7,30);
outputArea.setEditable(false);

super.add(outputArea, BorderLayout.NORTH);
super.add(okButton, BorderLayout.AFTER_LAST_LINE);

super.setVisible(true);

outputArea.append("Checking for updates...\n");
outputArea.append("Client version: v" + CLIENT_VERSION + "\n");

double newVersion = newVersion();
outputArea.append("Newest client version: v" + newVersion + "\n");

if (newVersion > CLIENT_VERSION)
{
super.setTitle("Out of date!");
okButton.setText("Download update");
okButton.setActionCommand("update");
outputArea.append("There is a new version available! (v" + newVersion + ")\n\nPlease download the newest client.");
}
else
{
super.setTitle("You have the latest version.");
okButton.setText("Open client");
okButton.setActionCommand("continue");
outputArea.append("You have the latest client version! (v" + CLIENT_VERSION + ")\n\nEnjoy playing.");
}

okButton.addActionListener(this);
okButton.setEnabled(true);
}

// read the new version from the website
private double newVersion()
{
try
{
String versionNumber = "";

try
{
// Create a URL for the desired page
URL url = new URL(VERSION_URL);

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line = in.readLine();
while (line != null)
{
if (line.startsWith("version"))
{
versionNumber = line.substring(8);
break;
}
line = in.readLine();
}
in.close();
}
catch (MalformedURLException mue)
{
JOptionPane.showMessageDialog(null, "Error reading new version! Exception:\n\t" + mue.getMessage() + "(Fatal)");
System.exit(0);
}
catch (IOException ioe)
{
JOptionPane.showMessageDialog(null, "Error reading new version! Exception:\n\t" + ioe.getMessage() + "(Fatal)");
System.exit(0);
}

return Double.parseDouble(versionNumber);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Error reading new version! Exception:\n\t" + e.getMessage() + "(Fatal)");
System.exit(0);
}
return 0;
}

public void actionPerformed(ActionEvent event)
{
if ("update".equals(event.getActionCommand()))
{
Desktop desktop = Desktop.getDesktop();

if(!desktop.isSupported(Desktop.Action.BROWSE))
{
JOptionPane.showMessageDialog(null, "Unsupported desktop, can not open URL(fatal)");
System.exit(0);
}

try
{
java.net.URI uri = new java.net.URI(CLIENT_URL);
desktop.browse(uri);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Error opening URL! Exception:\n\t" + e.getMessage() + "(Fatal)");
System.exit(0);
}

try
{
Thread.sleep(1000);
}
catch (Exception e)
{}
this.dispose();
}
else if ("continue".equals(event.getActionCommand()))
{
client c = new client();
try
{
Thread.sleep(1000);
}
catch (Exception e)
{}
this.dispose();
}
}

public static void main(String[] args)
{
StartClient client = new StartClient();
}
}
If you use "bluur's" Gui, then you will need to change
client c = new client(); to
Gui gui = new Gui();

Step 2: Now you need to add a version.txt
On your webserver, create a version.txt with the current version of your client contained like this:

version=1.21
Set the permissions to 755 so people cannot edit it.

Step 3: change the URL constants to what you need them to be
Change
private static final String CLIENT_URL = "Put the URL to your client download here";
private static final String VERSION_URL = "Put the URL of your version.txt on your webserver here"; to the appropriate URLs

Step 3: Now we need to edit your run.bat
Find+edit run.bat or whatever your running bat or cmd file is and change it to this:

@echo off
color 0F
Title PUT A TITLE HERE
"java.exe" -Xmx300m StartClient
pause


Credits: Me, Halfy

Only the registered members can see the link.