summaryrefslogtreecommitdiff
path: root/src/net/minecraft/GameVersion.java
blob: 9de8f4d3807ae2a384bd1c4fa635d3a858040857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package net.minecraft;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.time.Instant;
import java.util.Scanner;

public class GameVersion {
	public String name;
	public String desc;
	public Instant compiledOn;
	public Instant releasedOn;

	public URL gameJarUrl;
	public int proxyPort;

	private boolean infoDownloaded = false;

	public GameVersion(String name, String desc, long compileTimestamp, long releaseTimestamp) {
		this.name = name;
		this.desc = desc;
		this.compiledOn = Instant.ofEpochMilli(compileTimestamp);
		this.releasedOn = Instant.ofEpochMilli(releaseTimestamp);
	}

	public void downloadInfo() throws IOException {
		URL infoUrl = new URL("http://files.betacraft.uk/launcher/assets/jsons/" + name + ".info");
		Scanner scanner = new Scanner(infoUrl.openStream(), "UTF-8");
		while (scanner.hasNextLine()) {
			String[] keyValueList = scanner.nextLine().split(":", 2);

			if (keyValueList.length < 2)
				continue;

			String key = keyValueList[0];
			String value = keyValueList[1];

			switch (key) {
				case "url":
					gameJarUrl = new URL(value);
					break;
				case "proxy-args":
					int portIndex = value.indexOf("http.proxyPort=") + "http.proxyPort=".length();
					proxyPort = Integer.parseInt(value.substring(portIndex));
					break;
			}
		}
		infoDownloaded = true;
	}

	public boolean hasFullInfo() {
		return infoDownloaded;
	}

	public boolean isPreleaseOrDemo() {
		boolean isDemo = name.contains("demo");
		boolean isPrerelease = name.contains("pre") || name.contains("test") || name.contains("w");
		boolean isClassic = name.startsWith("c");
		return isDemo || isPrerelease || isClassic;
	}

	public boolean isSameVersion(File versionFile) throws IOException {
		DataInputStream dis = new DataInputStream(Files.newInputStream(versionFile.toPath()));
		String version = dis.readUTF();
		dis.close();

		long versionTimestamp = Long.parseLong(version);
		return releasedOn.getEpochSecond() == versionTimestamp;
	}

	public void writeVersionFile(File file) throws IOException {
		DataOutputStream dos = new DataOutputStream(Files.newOutputStream(file.toPath()));
		dos.writeUTF(String.valueOf(releasedOn.getEpochSecond()));
		dos.close();
	}
}