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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package net.minecraft;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Util {
private enum OS {
linux, solaris, windows, macos, unknown
}
private static File workDir = null;
public static File getWorkingDirectory() {
if (workDir == null) workDir = getWorkingDirectory("minecraft");
return workDir;
}
public static File getWorkingDirectory(String applicationName) {
File workingDirectory;
String applicationData, userHome = System.getProperty("user.home", ".");
switch (getPlatform()) {
case solaris:
workingDirectory = new File(userHome, '.' + applicationName + '/');
break;
case windows:
applicationData = System.getenv("APPDATA");
if (applicationData != null) {
workingDirectory = new File(applicationData, "." + applicationName + '/');
break;
}
workingDirectory = new File(userHome, '.' + applicationName + '/');
break;
case macos:
workingDirectory = new File(userHome, "Library/Application Support/" + applicationName);
break;
default:
workingDirectory = new File(userHome, String.valueOf(applicationName) + '/');
break;
}
if (!workingDirectory.exists() && !workingDirectory.mkdirs())
throw new RuntimeException("The working directory could not be created: " + workingDirectory);
return workingDirectory;
}
private static OS getPlatform() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) return OS.windows;
if (osName.contains("mac")) return OS.macos;
if (osName.contains("solaris")) return OS.solaris;
if (osName.contains("sunos")) return OS.solaris;
if (osName.contains("linux")) return OS.linux;
if (osName.contains("unix")) return OS.linux;
return OS.unknown;
}
public static String makeDummyAuthString(String userName) {
String latestVersion = "-1";
String downloadTicket = "none";
String sessionId = "none";
return String.join(":", latestVersion, downloadTicket, userName, sessionId);
}
public static String excutePost(String targetURL, String urlParameters) {
HttpURLConnection connection = null;
try {
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString((urlParameters.getBytes()).length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null)
connection.disconnect();
}
}
}
|