Отказ от minimal-json в пользу gson

This commit is contained in:
Gravit 2018-11-14 17:59:55 +07:00
parent d4d4f78387
commit ad924961e6
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
11 changed files with 189 additions and 314 deletions

View file

@ -44,7 +44,6 @@ public static void registerHandlers() {
registerHandler("binaryFile", BinaryFileAuthHandler::new);
registerHandler("textFile", TextFileAuthHandler::new);
registerHandler("mysql", MySQLAuthHandler::new);
registerHandler("json", JsonAuthHandler::new);
registredHandl = true;
}
}

View file

@ -1,123 +0,0 @@
package ru.gravit.launchserver.auth.handler;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
import ru.gravit.utils.HTTPRequest;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.VerifyHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
@SuppressWarnings("unused")
public class JsonAuthHandler extends AuthHandler {
private final URL url;
private final URL urlCheckServer;
private final URL urlJoinServer;
private final URL urlUsernameToUUID;
private final URL urlUUIDToUsername;
private final String userKeyName;
private final String serverIDKeyName;
private final String accessTokenKeyName;
private final String uuidKeyName;
private final String responseErrorKeyName;
private final String responseOKKeyName;
protected JsonAuthHandler(BlockConfigEntry block) {
super(block);
String configUrl = block.getEntryValue("url", StringConfigEntry.class);
String configUrlCheckServer = block.getEntryValue("urlCheckServer", StringConfigEntry.class);
String configUrlJoinServer = block.getEntryValue("urlJoinServer", StringConfigEntry.class);
String configUrlUsernameUUID = block.getEntryValue("urlUsernameToUUID", StringConfigEntry.class);
String configUrlUUIDUsername = block.getEntryValue("urlUUIDToUsername", StringConfigEntry.class);
userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Username key name can't be empty");
serverIDKeyName = VerifyHelper.verify(block.getEntryValue("serverIDKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "ServerID key name can't be empty");
uuidKeyName = VerifyHelper.verify(block.getEntryValue("UUIDKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "UUID key name can't be empty");
accessTokenKeyName = VerifyHelper.verify(block.getEntryValue("accessTokenKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "AccessToken key name can't be empty");
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
responseOKKeyName = VerifyHelper.verify(block.getEntryValue("responseOKKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Response okay key can't be empty");
url = IOHelper.convertToURL(configUrl);
urlCheckServer = IOHelper.convertToURL(configUrlCheckServer);
urlJoinServer = IOHelper.convertToURL(configUrlJoinServer);
urlUsernameToUUID = IOHelper.convertToURL(configUrlUsernameUUID);
urlUUIDToUsername = IOHelper.convertToURL(configUrlUUIDUsername);
}
@Override
public UUID auth(AuthProviderResult authResult) throws IOException {
JsonObject request = Json.object().add(userKeyName, authResult.username).add(accessTokenKeyName, authResult.accessToken);
JsonObject result = jsonRequestChecked(request, url);
String value;
if ((value = result.getString(uuidKeyName, null)) != null)
return UUID.fromString(value);
throw new IOException("Service error");
}
@Override
public UUID checkServer(String username, String serverID) throws IOException {
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID);
JsonObject result = jsonRequestChecked(request, urlCheckServer);
String value;
if ((value = result.getString(uuidKeyName, null)) != null)
return UUID.fromString(value);
throw new IOException("Service error");
}
@Override
public void close() {
}
@Override
public boolean joinServer(String username, String accessToken, String serverID) throws IOException {
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID).add(accessTokenKeyName, accessToken);
HTTPRequest.jsonRequest(request, urlJoinServer);
return request.getString(responseOKKeyName, null).equals("OK");
}
@Override
public UUID usernameToUUID(String username) throws IOException {
JsonObject request = Json.object().add(userKeyName, username);
JsonObject result = jsonRequestChecked(request, urlUsernameToUUID);
String value;
if ((value = result.getString(uuidKeyName, null)) != null)
return UUID.fromString(value);
throw new IOException("Service error");
}
@Override
public String uuidToUsername(UUID uuid) throws IOException {
JsonObject request = Json.object().add(uuidKeyName, uuid.toString());
JsonObject result = jsonRequestChecked(request, urlUUIDToUsername);
String value;
if ((value = result.getString(userKeyName, null)) != null)
return value;
throw new IOException("Service error");
}
public JsonObject jsonRequestChecked(JsonObject object, URL url) throws IOException {
JsonValue result = HTTPRequest.jsonRequest(object, url);
if (!result.isObject())
authError("Authentication server response is malformed");
JsonObject response = result.asObject();
String value;
if ((value = response.getString(responseErrorKeyName, null)) != null)
authError(value);
return response;
}
}

View file

@ -1,38 +1,79 @@
package ru.gravit.launchserver.auth.hwid;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import ru.gravit.utils.HTTPRequest;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.VerifyHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public final class JsonHWIDHandler extends HWIDHandler {
private static final int TIMEOUT = Integer.parseInt(
System.getProperty("launcher.connection.timeout", Integer.toString(1500)));
private static final Gson gson = new Gson();
private final URL url;
private final URL urlBan;
private final URL urlUnBan;
private final URL urlGet;
private final String loginKeyName;
private final String hddKeyName;
private final String cpuKeyName;
private final String biosKeyName;
private final String isBannedKeyName;
public class banRequest
{
long hwid_hdd;
public banRequest(long hwid_hdd, long hwid_cpu, long hwid_bios) {
this.hwid_hdd = hwid_hdd;
this.hwid_cpu = hwid_cpu;
this.hwid_bios = hwid_bios;
}
long hwid_cpu;
long hwid_bios;
}
public class checkRequest
{
public checkRequest(String username, long hwid_hdd, long hwid_cpu, long hwid_bios) {
this.username = username;
this.hwid_hdd = hwid_hdd;
this.hwid_cpu = hwid_cpu;
this.hwid_bios = hwid_bios;
}
String username;
long hwid_hdd;
long hwid_cpu;
long hwid_bios;
}
public class Result
{
String error;
}
public class BannedResult
{
boolean isBanned;
String error;
}
public class HWIDResult
{
long hwid_hdd;
long hwid_cpu;
long hwid_bios;
}
public class HWIDRequest
{
public HWIDRequest(String username) {
this.username = username;
}
String username;
}
JsonHWIDHandler(BlockConfigEntry block) {
super(block);
@ -40,16 +81,6 @@ public final class JsonHWIDHandler extends HWIDHandler {
String configUrlBan = block.getEntryValue("urlBan", StringConfigEntry.class);
String configUrlUnBan = block.getEntryValue("urlUnBan", StringConfigEntry.class);
String configUrlGet = block.getEntryValue("urlGet", StringConfigEntry.class);
loginKeyName = VerifyHelper.verify(block.getEntryValue("loginKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Login key name can't be empty");
hddKeyName = VerifyHelper.verify(block.getEntryValue("hddKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "HDD key name can't be empty");
cpuKeyName = VerifyHelper.verify(block.getEntryValue("cpuKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "CPU key can't be empty");
biosKeyName = VerifyHelper.verify(block.getEntryValue("biosKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Bios key can't be empty");
isBannedKeyName = VerifyHelper.verify(block.getEntryValue("isBannedKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Response username key can't be empty");
url = IOHelper.convertToURL(configUrl);
urlBan = IOHelper.convertToURL(configUrlBan);
urlUnBan = IOHelper.convertToURL(configUrlUnBan);
@ -59,9 +90,11 @@ public final class JsonHWIDHandler extends HWIDHandler {
@Override
public void ban(List<HWID> l_hwid) throws HWIDException {
for (HWID hwid : l_hwid) {
JsonObject request = Json.object().add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios());
banRequest request = new banRequest(hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios());
try {
request(request, urlBan);
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan);
Result r = gson.fromJson(result,Result.class);
if(r.error != null) throw new HWIDException(r.error);
} catch (IOException e) {
LogHelper.error(e);
throw new HWIDException("HWID service error");
@ -69,48 +102,19 @@ public void ban(List<HWID> l_hwid) throws HWIDException {
}
}
public JsonObject request(JsonObject request, URL url) throws HWIDException, IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Accept", "application/json");
if (TIMEOUT > 0)
connection.setConnectTimeout(TIMEOUT);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
writer.write(request.toString());
writer.flush();
writer.close();
InputStreamReader reader;
int statusCode = connection.getResponseCode();
if (200 <= statusCode && statusCode < 300)
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
else
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
JsonValue content = Json.parse(reader);
if (!content.isObject())
throw new HWIDException("HWID server response is malformed");
JsonObject response = content.asObject();
return response;
}
@Override
public void check0(HWID hwid, String username) throws HWIDException {
JsonObject request = Json.object().add(loginKeyName, username).add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios());
JsonObject response;
checkRequest request = new checkRequest(username,hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios());
try {
response = request(request, url);
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan);
BannedResult r = gson.fromJson(result,BannedResult.class);
if(r.error != null) throw new HWIDException(r.error);
boolean isBanned = r.isBanned;
if (isBanned) throw new HWIDException("You will BANNED!");
} catch (IOException e) {
LogHelper.error(e);
throw new HWIDException("HWID service error");
}
boolean isBanned = response.getBoolean(isBannedKeyName, false);
if (isBanned) throw new HWIDException("You will BANNED!");
}
@Override
@ -120,34 +124,30 @@ public void close() {
@Override
public List<HWID> getHwid(String username) throws HWIDException {
JsonObject request = Json.object().add(loginKeyName, username);
JsonObject responce;
ArrayList<HWID> hwids = new ArrayList<>();
HWIDRequest request = new HWIDRequest(username);
try {
responce = request(request, urlGet);
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan);
HWIDResult[] r = gson.fromJson(result,HWIDResult[].class);
for( HWIDResult hw : r)
{
hwids.add(HWID.gen(hw.hwid_hdd,hw.hwid_bios,hw.hwid_cpu));
}
} catch (IOException e) {
LogHelper.error(e);
throw new HWIDException("HWID service error");
}
JsonArray array = responce.get("hwids").asArray();
ArrayList<HWID> hwids = new ArrayList<>();
for (JsonValue i : array) {
long hdd, cpu, bios;
JsonObject object = i.asObject();
hdd = object.getLong(hddKeyName, 0);
cpu = object.getLong(cpuKeyName, 0);
bios = object.getLong(biosKeyName, 0);
HWID hwid = HWID.gen(hdd, cpu, bios);
hwids.add(hwid);
}
return hwids;
}
@Override
public void unban(List<HWID> l_hwid) throws HWIDException {
for (HWID hwid : l_hwid) {
JsonObject request = Json.object().add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios());
banRequest request = new banRequest(hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios());
try {
request(request, urlUnBan);
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlUnBan);
Result r = gson.fromJson(result,Result.class);
if(r.error != null) throw new HWIDException(r.error);
} catch (IOException e) {
LogHelper.error(e);
throw new HWIDException("HWID service error");

View file

@ -3,60 +3,57 @@
import java.io.IOException;
import java.net.URL;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.ClientPermissions;
import ru.gravit.utils.HTTPRequest;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.VerifyHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
public final class JsonAuthProvider extends AuthProvider {
private final Gson gson = new Gson();
private final URL url;
private final String userKeyName;
private final String passKeyName;
private final String ipKeyName;
private final String responseUserKeyName;
private final String responsePermissionKeyName;
private final String responseErrorKeyName;
public class authResult
{
String username;
String error;
long permissions;
}
public class authRequest
{
public authRequest(String username, String password, String ip) {
this.username = username;
this.password = password;
this.ip = ip;
}
String username;
String password;
String ip;
}
JsonAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block, server);
String configUrl = block.getEntryValue("url", StringConfigEntry.class);
userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Username key name can't be empty");
passKeyName = VerifyHelper.verify(block.getEntryValue("passKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Password key name can't be empty");
ipKeyName = VerifyHelper.verify(block.getEntryValue("ipKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "IP key can't be empty");
responseUserKeyName = VerifyHelper.verify(block.getEntryValue("responseUserKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Response username key can't be empty");
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
responsePermissionKeyName = VerifyHelper.verify(block.getEntryValue("responsePermissionKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
url = IOHelper.convertToURL(configUrl);
}
@Override
public AuthProviderResult auth(String login, String password, String ip) throws IOException {
JsonObject request = Json.object().add(userKeyName, login).add(passKeyName, password).add(ipKeyName, ip);
JsonValue content = HTTPRequest.jsonRequest(request, url);
if (!content.isObject())
authRequest authRequest = new authRequest(login,password,ip);
JsonElement request = gson.toJsonTree(authRequest);
JsonElement content = HTTPRequest.jsonRequest(request, url);
if (!content.isJsonObject())
return authError("Authentication server response is malformed");
JsonObject response = content.asObject();
String value;
if ((value = response.getString(responseUserKeyName, null)) != null)
return new AuthProviderResult(value, SecurityHelper.randomStringToken(), new ClientPermissions(response.getLong(responsePermissionKeyName, 0)));
else if ((value = response.getString(responseErrorKeyName, null)) != null)
return authError(value);
authResult result = gson.fromJson(content, authResult.class);
if (result.username != null)
return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions));
else if (result.error != null)
return authError(result.error);
else
return authError("Authentication server response is malformed");
}

View file

@ -11,18 +11,18 @@
import java.util.UUID;
import java.util.regex.Pattern;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.eclipsesource.json.WriterConfig;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.HTTPRequest;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
public final class MojangAuthProvider extends AuthProvider {
private static final Pattern UUID_REGEX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
private static final URL URL;
private final Gson gson = new Gson();
static {
try {
@ -32,52 +32,43 @@ public final class MojangAuthProvider extends AuthProvider {
}
}
public static JsonObject makeJSONRequest(URL url, JsonObject request) throws IOException {
// Make authentication request
HttpURLConnection connection = IOHelper.newConnectionPost(url);
connection.setRequestProperty("Content-Type", "application/json");
try (OutputStream output = connection.getOutputStream()) {
output.write(request.toString(WriterConfig.MINIMAL).getBytes(StandardCharsets.UTF_8));
}
connection.getResponseCode(); // Actually make request
// Read response
InputStream errorInput = connection.getErrorStream();
try (InputStream input = errorInput == null ? connection.getInputStream() : errorInput) {
String charset = connection.getContentEncoding();
Charset charsetObject = charset == null ?
IOHelper.UNICODE_CHARSET : Charset.forName(charset);
// Parse response
String json = new String(IOHelper.read(input), charsetObject);
return json.isEmpty() ? null : Json.parse(json).asObject();
}
}
public MojangAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block, server);
}
public class mojangAuth
{
public mojangAuth(String username, String password) {
this.username = username;
this.password = password;
name = "Minecraft";
version = 1;
}
String name;
int version;
String username;
String password;
}
@Override
public AuthProviderResult auth(String login, String password, String ip) throws Exception {
JsonObject request = Json.object().
add("agent", Json.object().add("name", "Minecraft").add("version", 1)).
add("username", login).add("password", password);
mojangAuth mojangAuth = new mojangAuth(login,password);
JsonElement request = gson.toJsonTree(mojangAuth);
// Verify there's no error
JsonObject response = makeJSONRequest(URL, request);
JsonObject response = HTTPRequest.jsonRequest(request,URL).getAsJsonObject();
if (response == null)
authError("Empty com.mojang response");
JsonValue errorMessage = response.get("errorMessage");
JsonElement errorMessage = response.get("errorMessage");
if (errorMessage != null)
authError(errorMessage.asString());
authError(errorMessage.getAsString());
// Parse JSON data
JsonObject selectedProfile = response.get("selectedProfile").asObject();
String username = selectedProfile.get("name").asString();
String accessToken = response.get("clientToken").asString();
UUID uuid = UUID.fromString(UUID_REGEX.matcher(selectedProfile.get("id").asString()).replaceFirst("$1-$2-$3-$4-$5"));
String launcherToken = response.get("accessToken").asString();
JsonObject selectedProfile = response.get("selectedProfile").getAsJsonObject();
String username = selectedProfile.get("name").getAsString();
String accessToken = response.get("clientToken").getAsString();
UUID uuid = UUID.fromString(UUID_REGEX.matcher(selectedProfile.get("id").getAsString()).replaceFirst("$1-$2-$3-$4-$5"));
String launcherToken = response.get("accessToken").getAsString();
// We're done
return new MojangAuthProviderResult(username, accessToken, uuid, launcherToken);

View file

@ -9,10 +9,9 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.WriterConfig;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.SecurityHelper;
@ -22,12 +21,25 @@
import ru.gravit.launchserver.command.CommandException;
public final class IndexAssetCommand extends Command {
private static Gson gson = new Gson();
private static JsonParser parser = new JsonParser();
public static class IndexObject
{
long size;
public IndexObject(long size, String hash) {
this.size = size;
this.hash = hash;
}
String hash;
}
private static final class IndexAssetVisitor extends SimpleFileVisitor<Path> {
private final JsonObject objects;
private final JsonArray objects;
private final Path inputAssetDir;
private final Path outputAssetDir;
private IndexAssetVisitor(JsonObject objects, Path inputAssetDir, Path outputAssetDir) {
private IndexAssetVisitor(JsonArray objects, Path inputAssetDir, Path outputAssetDir) {
this.objects = objects;
this.inputAssetDir = inputAssetDir;
this.outputAssetDir = outputAssetDir;
@ -40,7 +52,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
// Add to index and copy file
String digest = SecurityHelper.toHex(SecurityHelper.digest(DigestAlgorithm.SHA1, file));
objects.add(name, Json.object().add("size", attrs.size()).add("hash", digest));
IndexObject obj = new IndexObject(attrs.size(),digest);
objects.add(gson.toJsonTree(obj));
IOHelper.copy(file, resolveObjectFile(outputAssetDir, digest));
// Continue visiting
@ -93,14 +106,15 @@ public void invoke(String... args) throws Exception {
Files.createDirectory(outputAssetDir);
// Index objects
JsonObject objects = Json.object();
JsonArray objects = new JsonArray();
LogHelper.subInfo("Indexing objects");
IOHelper.walk(inputAssetDir, new IndexAssetVisitor(objects, inputAssetDir, outputAssetDir), false);
// Write index file
LogHelper.subInfo("Writing asset index file: '%s'", indexFileName);
try (BufferedWriter writer = IOHelper.newWriter(resolveIndexFile(outputAssetDir, indexFileName))) {
Json.object().add(OBJECTS_DIR, objects).writeTo(writer, WriterConfig.MINIMAL);
writer.write(gson.toJson(objects));
}
// Finished

View file

@ -4,11 +4,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonObject.Member;
import com.google.gson.*;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.launchserver.LaunchServer;
@ -16,6 +14,8 @@
import ru.gravit.launchserver.command.CommandException;
public final class UnindexAssetCommand extends Command {
private static Gson gson = new Gson();
private static JsonParser parser = new JsonParser();
public UnindexAssetCommand(LaunchServer server) {
super(server);
}
@ -49,17 +49,17 @@ public void invoke(String... args) throws Exception {
JsonObject objects;
LogHelper.subInfo("Reading asset index file: '%s'", indexFileName);
try (BufferedReader reader = IOHelper.newReader(IndexAssetCommand.resolveIndexFile(inputAssetDir, indexFileName))) {
objects = Json.parse(reader).asObject().get(IndexAssetCommand.OBJECTS_DIR).asObject();
objects = parser.parse(reader).getAsJsonObject().get("objects").getAsJsonObject();
}
// Restore objects
LogHelper.subInfo("Unindexing %d objects", objects.size());
for (Member member : objects) {
String name = member.getName();
for (Map.Entry<String, JsonElement> member : objects.entrySet()) {
String name = member.getKey();
LogHelper.subInfo("Unindexing: '%s'", name);
// Copy hashed file to target
String hash = member.getValue().asObject().get("hash").asString();
String hash = member.getValue().getAsJsonObject().get("hash").getAsString();
Path source = IndexAssetCommand.resolveObjectFile(inputAssetDir, hash);
IOHelper.copy(source, outputAssetDir.resolve(name));
}

View file

@ -17,10 +17,8 @@
import javax.swing.JOptionPane;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.WriterConfig;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import ru.gravit.launcher.*;
import ru.gravit.launcher.hasher.DirWatcher;
import ru.gravit.launcher.hasher.FileNameMatcher;
@ -42,6 +40,7 @@
import ru.gravit.launcher.serialize.stream.StreamObject;
public final class ClientLauncher {
private static Gson gson = new Gson();
private static final class ClassPathFileVisitor extends SimpleFileVisitor<Path> {
private final Collection<Path> result;
@ -170,7 +169,6 @@ public void write(HOutput output) throws IOException {
private static final Path NATIVES_DIR = IOHelper.toPath("natives");
private static final Path RESOURCEPACKS_DIR = IOHelper.toPath("resourcepacks");
private static PublicURLClassLoader classLoader;
private static void addClientArgs(Collection<String> args, ClientProfile profile, Params params) {
PlayerProfile pp = params.pp;
@ -185,16 +183,16 @@ private static void addClientArgs(Collection<String> args, ClientProfile profile
if (version.compareTo(ClientProfile.Version.MC1710) >= 0) {
// Add user properties
Collections.addAll(args, "--userType", "mojang");
JsonObject properties = Json.object();
JsonObject properties = new JsonObject();
if (pp.skin != null) {
properties.add(Launcher.SKIN_URL_PROPERTY, Json.array(pp.skin.url));
properties.add(Launcher.SKIN_DIGEST_PROPERTY, Json.array(SecurityHelper.toHex(pp.skin.digest)));
properties.addProperty(Launcher.SKIN_URL_PROPERTY, pp.skin.url);
properties.addProperty(Launcher.SKIN_DIGEST_PROPERTY, SecurityHelper.toHex(pp.skin.digest));
}
if (pp.cloak != null) {
properties.add(Launcher.CLOAK_URL_PROPERTY, Json.array(pp.cloak.url));
properties.add(Launcher.CLOAK_DIGEST_PROPERTY, Json.array(SecurityHelper.toHex(pp.cloak.digest)));
properties.addProperty(Launcher.CLOAK_URL_PROPERTY, pp.cloak.url);
properties.addProperty(Launcher.CLOAK_DIGEST_PROPERTY, SecurityHelper.toHex(pp.cloak.digest));
}
Collections.addAll(args, "--userProperties", properties.toString(WriterConfig.MINIMAL));
Collections.addAll(args, "--userProperties", ClientLauncher.gson.toJson(properties));
// Add asset index
Collections.addAll(args, "--assetIndex", profile.getAssetIndex());

View file

@ -10,9 +10,8 @@
import java.util.Objects;
import java.util.regex.Pattern;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import ru.gravit.launcher.LauncherAPI;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
@ -22,6 +21,7 @@
import ru.gravit.launcher.profiles.ClientProfile;
public final class ServerPinger {
private JsonParser parser = new JsonParser();
public static final class Result {
@LauncherAPI
public final int onlinePlayers;
@ -184,10 +184,10 @@ private Result modernPing(HInput input, HOutput output) throws IOException {
}
// Parse JSON response
JsonObject object = Json.parse(response).asObject();
JsonObject playersObject = object.get("players").asObject();
int online = playersObject.get("online").asInt();
int max = playersObject.get("max").asInt();
JsonObject object = parser.parse(response).getAsJsonObject();
JsonObject playersObject = object.get("players").getAsJsonObject();
int online = playersObject.get("online").getAsInt();
int max = playersObject.get("max").getAsInt();
// Return ping status
return new Result(online, max, response);

View file

@ -4,6 +4,5 @@
dependencies {
compileOnly 'org.fusesource.jansi:jansi:1.17.1'
compileOnly 'org.javassist:javassist:3.23.1-GA'
compile 'com.eclipsesource.minimal-json:minimal-json:0.9.4'
compile 'com.google.code.gson:gson:2.8.5'
}

View file

@ -9,13 +9,13 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import ru.gravit.utils.helper.IOHelper;
public class HTTPRequest {
private static final int TIMEOUT = 10;
private static final JsonParser parser = new JsonParser();
public static int sendCrashreport(String strurl, byte[] data) throws IOException {
URL url = new URL(strurl);
@ -36,7 +36,7 @@ public static int sendCrashreport(String strurl, String data) throws IOException
return sendCrashreport(strurl, data.getBytes(IOHelper.UNICODE_CHARSET));
}
public static JsonValue jsonRequest(JsonObject request, URL url) throws IOException {
public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
@ -58,7 +58,7 @@ public static JsonValue jsonRequest(JsonObject request, URL url) throws IOExcept
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
else
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
JsonValue content = Json.parse(reader);
JsonElement content = parser.parse(reader);
return content;
}
}