Jump to content

Autoupdater (jar)


Ericthecmh

Recommended Posts

This might not be the best way to write an autoupdater, but it's the way I've done it:

 

Setup:

 

On server, have the jar file (CMHEssMiner.jar) and a versioninfo file.

  • CMHEssMiner.jar is the jar file (duh)
  • versioninfo contains one line with the version (eg. "0.8.3 BETA" -- no quotes)

In the code, create a constant:

public final String version = "0.8.3 BETA"

in the onStart method:

URL url = null;
try {
	url = new URL("http://osbot.no-ip.org/distfiles/versioninfo");
} catch (MalformedURLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
InputStream is = null;
try {
	is = url.openConnection().getInputStream();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
	String line = reader.readLine();
	if (!line.equals(currentVersion)) {
		updateScript(line);
		canStart = false;
		return;
	}
} catch (IOException e) {
}

I created a separate method for the updating part:

private void updateScript(String version) {
	try {
		URL url = new URL(
				"http://osbot.no-ip.org/distfiles/CMHEssMiner.jar");
		InputStream in = url.openStream();
		int bytesRead;
		byte[] buf = new byte[4 * 1024];
		String filename;
		if (System.getProperty("os.name").startsWith("Window")) {
			filename = "\\OSBot\\scripts\\CMHEssMiner.jar";
		} else {
			filename = "/OSBot/scripts/CMHEssMiner.jar";
		}
		OutputStream os = (new FileOutputStream(
				System.getProperty("user.home") + filename));
		while ((bytesRead = in.read(buf)) != -1) {
			os.write(buf, 0, bytesRead);
		}
		os.flush();
		os.close();
		in.close();
		JOptionPane.showMessageDialog(null, "Script has been automatically updated to version" + version + ". Please refresh your scripts list and run the script again.");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

If anyone has a better way to do this or suggestions, I'd love to hear them :D

Eric

Link to comment
Share on other sites

Looks nice, only thing I can think to suggest is using getVersion() and update the version in the script manifest instead of making a new variable to hold the version

 

The version variable in the script manifest is a double right? That wouldn't allow me to number my versions as things like 0.8.3 because 0.8.3 can't be stored as a double. But yeah it does get annoying to change the version number in three different places instead of just updating the version in the script manifest :P

 

Eric

Link to comment
Share on other sites

 

This part of the script is useless

		if (System.getProperty("os.name").startsWith("Window")) {
			filename = "\\OSBot\\scripts\\CMHEssMiner.jar";
		} else {
			filename = "/OSBot/scripts/CMHEssMiner.jar";
		}

just use "/" for both.

 

Does windows take both? I didn't know that. Thanks biggrin.png

 

Eric

 

 

If you really wanted it to be cross platform, just use

System.getProperty("file.separator")
  • Like 1
Link to comment
Share on other sites

If you really wanted it to be cross platform, just use

System.getProperty("file.separator")

 

The only correct answer I've seen on this topic smile.png

 

Also "user.dir" will give you OSBot's directory:

String fs = System.getProperty("file.separator");
String dir = System.getProperty("user.dir") + fs + "scripts" + fs + jarName;

All in all quite a good updater though. A bit sad that this isn't already included in the client.

Normally you would write the file to a temp location just in case the existing one is currently being used, but in OSBot's case you don't need to.

Edited by Kronos
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...