Launcher/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java

321 lines
11 KiB
Java
Raw Normal View History

2018-09-17 10:07:32 +03:00
package ru.gravit.launcher.profiles;
import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launcher.hasher.FileNameMatcher;
2018-09-24 19:34:06 +03:00
import ru.gravit.launcher.hasher.HashedDir;
2018-09-17 10:07:32 +03:00
import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.config.ConfigObject;
2018-12-20 18:45:01 +03:00
import ru.gravit.launcher.serialize.config.entry.*;
2018-09-17 10:07:32 +03:00
import ru.gravit.launcher.serialize.stream.StreamObject;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.VerifyHelper;
2018-09-17 10:07:32 +03:00
2018-12-20 18:45:01 +03:00
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.*;
2018-09-17 10:07:32 +03:00
@SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
2018-12-02 15:21:27 +03:00
public final class ClientProfile extends ConfigObject implements Comparable<ClientProfile> {
2018-09-17 10:07:32 +03:00
@LauncherAPI
public enum Version {
MC147("1.4.7", 51),
MC152("1.5.2", 61),
MC164("1.6.4", 78),
MC172("1.7.2", 4),
MC1710("1.7.10", 5),
MC189("1.8.9", 47),
MC194("1.9.4", 110),
MC1102("1.10.2", 210),
MC1112("1.11.2", 316),
MC1122("1.12.2", 340),
MC113("1.13", 393),
2018-11-03 17:35:47 +03:00
MC1131("1.13.1", 401),
MC1132("1.13.2", 402);
2018-09-17 10:07:32 +03:00
private static final Map<String, Version> VERSIONS;
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
static {
Version[] versionsValues = values();
VERSIONS = new HashMap<>(versionsValues.length);
for (Version version : versionsValues)
2018-09-22 17:33:00 +03:00
VERSIONS.put(version.name, version);
2018-09-17 10:07:32 +03:00
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
public static Version byName(String name) {
return VerifyHelper.getMapValue(VERSIONS, name, String.format("Unknown client version: '%s'", name));
}
public final String name;
public final int protocol;
Version(String name, int protocol) {
this.name = name;
this.protocol = protocol;
}
@Override
public String toString() {
return "Minecraft " + name;
}
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
@LauncherAPI
public static final StreamObject.Adapter<ClientProfile> RO_ADAPTER = input -> new ClientProfile(input, true);
2018-12-02 15:21:27 +03:00
public static final boolean profileCaseSensitive = Boolean.getBoolean("launcher.clientProfile.caseSensitive");
2018-12-20 18:45:01 +03:00
2018-09-17 10:07:32 +03:00
private static final FileNameMatcher ASSET_MATCHER = new FileNameMatcher(
new String[0], new String[]{"indexes", "objects"}, new String[0]);
// Version
private String version;
2018-09-17 10:07:32 +03:00
private final String assetIndex;
2018-09-17 10:07:32 +03:00
// Client
private final int sortIndex;
private String title;
private final String serverAddress;
2018-09-17 10:07:32 +03:00
private final int serverPort;
2018-11-08 15:30:16 +03:00
public static class MarkedString {
@LauncherAPI
public String string;
@LauncherAPI
public boolean mark;
public MarkedString(String string, boolean mark) {
this.string = string;
this.mark = mark;
}
2018-11-08 15:30:16 +03:00
public MarkedString(String string) {
this.string = string;
this.mark = false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MarkedString that = (MarkedString) o;
return Objects.equals(string, that.string);
}
@Override
public int hashCode() {
return Objects.hash(string);
}
}
2018-11-08 15:30:16 +03:00
2018-09-17 10:07:32 +03:00
// Updater and client watch service
2018-09-24 19:34:06 +03:00
private final List<String> update = new ArrayList<>();
private final List<String> updateExclusions = new ArrayList<>();
2018-11-08 15:50:24 +03:00
private final List<String> updateShared = new ArrayList<>();
2018-09-24 19:34:06 +03:00
private final List<String> updateVerify = new ArrayList<>();
private final Set<MarkedString> updateOptional = new HashSet<>();
private final boolean updateFastCheck;
2018-09-17 10:07:32 +03:00
private final boolean useWhitelist;
2018-09-17 10:07:32 +03:00
// Client launcher
private final String mainClass;
private final List<String> jvmArgs = new ArrayList<>();
private final List<String> classPath = new ArrayList<>();
private final List<String> clientArgs = new ArrayList<>();
2018-09-17 10:07:32 +03:00
private final List<String> whitelist = new ArrayList<>();
2018-09-17 10:07:32 +03:00
@LauncherAPI
public ClientProfile(BlockConfigEntry block) {
super(block);
// Version
version = block.getEntryValue("version", StringConfigEntry.class);
assetIndex = block.getEntryValue("assetIndex", StringConfigEntry.class);
2018-09-17 10:07:32 +03:00
// Client
sortIndex = block.getEntryValue("sortIndex", IntegerConfigEntry.class);
title = block.getEntryValue("title", StringConfigEntry.class);
serverAddress = block.getEntryValue("serverAddress", StringConfigEntry.class);
serverPort = block.getEntryValue("serverPort", IntegerConfigEntry.class);
2018-09-17 10:07:32 +03:00
// Updater and client watch service
2018-09-24 19:34:06 +03:00
block.getEntry("update", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(update::add);
block.getEntry("updateVerify", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(updateVerify::add);
2018-11-08 16:03:11 +03:00
block.getEntry("updateShared", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(updateShared::add);
block.getEntry("updateOptional", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(e -> updateOptional.add(new MarkedString(e)));
2018-09-24 19:34:06 +03:00
block.getEntry("updateExclusions", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(updateExclusions::add);
2018-11-06 15:14:08 +03:00
block.getEntry("enabledOptional", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(e -> updateOptional.stream().anyMatch(e1 -> e.equals(e1.string) && (e1.mark = true)));
updateFastCheck = block.getEntryValue("updateFastCheck", BooleanConfigEntry.class);
useWhitelist = block.getEntryValue("useWhitelist", BooleanConfigEntry.class);
2018-09-17 10:07:32 +03:00
// Client launcher
mainClass = block.getEntryValue("mainClass", StringConfigEntry.class);
block.getEntry("classPath", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(classPath::add);
block.getEntry("jvmArgs", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(jvmArgs::add);
block.getEntry("clientArgs", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(clientArgs::add);
block.getEntry("whitelist", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(whitelist::add);
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public ClientProfile(HInput input, boolean ro) throws IOException {
this(new BlockConfigEntry(input, ro));
}
@Override
public int compareTo(ClientProfile o) {
return Integer.compare(getSortIndex(), o.getSortIndex());
}
@LauncherAPI
public String getAssetIndex() {
return assetIndex;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public FileNameMatcher getAssetUpdateMatcher() {
return getVersion().compareTo(Version.MC1710) >= 0 ? ASSET_MATCHER : null;
}
@LauncherAPI
public String[] getClassPath() {
return classPath.toArray(new String[0]);
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public String[] getClientArgs() {
return clientArgs.toArray(new String[0]);
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
2018-09-24 19:34:06 +03:00
public FileNameMatcher getClientUpdateMatcher(/*boolean excludeOptional*/) {
String[] updateArray = update.toArray(new String[0]);
String[] verifyArray = updateVerify.toArray(new String[0]);
List<String> excludeList;
//if(excludeOptional)
//{
// excludeList = new ArrayList<>();
// excludeList.addAll(updateExclusions);
// excludeList.addAll(updateOptional);
//}
//else
excludeList = updateExclusions;
String[] exclusionsArray = excludeList.toArray(new String[0]);
2018-09-17 10:07:32 +03:00
return new FileNameMatcher(updateArray, verifyArray, exclusionsArray);
}
@LauncherAPI
public String[] getJvmArgs() {
return jvmArgs.toArray(new String[0]);
2018-09-17 10:07:32 +03:00
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
@LauncherAPI
public String getMainClass() {
return mainClass;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public String getServerAddress() {
return serverAddress;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
2018-11-08 15:30:16 +03:00
public Set<MarkedString> getOptional() {
return updateOptional;
}
2018-11-08 15:30:16 +03:00
@LauncherAPI
2018-12-20 18:45:01 +03:00
public Collection<String> getShared() {
return updateShared;
}
2018-09-24 19:34:06 +03:00
@LauncherAPI
2018-11-08 15:30:16 +03:00
public void markOptional(String opt) {
if (!updateOptional.contains(new MarkedString(opt)))
throw new SecurityException(String.format("Optional mod %s not found in optionalList", opt));
updateOptional.forEach(e -> {
if (e.string.equals(opt)) e.mark = true;
});
2018-09-24 19:34:06 +03:00
}
2018-11-08 15:30:16 +03:00
2018-09-24 19:34:06 +03:00
@LauncherAPI
2018-11-08 15:30:16 +03:00
public void unmarkOptional(String opt) {
if (!updateOptional.contains(new MarkedString(opt)))
throw new SecurityException(String.format("Optional mod %s not found in optionalList", opt));
updateOptional.forEach(e -> {
if (e.string.equals(opt)) e.mark = false;
});
2018-09-24 19:34:06 +03:00
}
2018-11-08 15:30:16 +03:00
public void pushOptional(HashedDir dir, boolean digest) throws IOException {
for (MarkedString opt : updateOptional) {
if (!opt.mark) dir.removeR(opt.string);
2018-09-24 19:34:06 +03:00
}
}
2018-11-08 15:30:16 +03:00
2018-09-17 10:07:32 +03:00
@LauncherAPI
public int getServerPort() {
return serverPort;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public InetSocketAddress getServerSocketAddress() {
return InetSocketAddress.createUnresolved(getServerAddress(), getServerPort());
}
@LauncherAPI
public int getSortIndex() {
return sortIndex;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public String getTitle() {
return title;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public Version getVersion() {
return Version.byName(version);
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public boolean isUpdateFastCheck() {
return updateFastCheck;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
2018-09-22 17:33:00 +03:00
public boolean isWhitelistContains(String username) {
2018-12-02 15:21:27 +03:00
if (!useWhitelist) return true;
return whitelist.stream().anyMatch(profileCaseSensitive ? e -> e.equals(username) : e -> e.equalsIgnoreCase(username));
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public void setTitle(String title) {
this.title = title;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public void setVersion(Version version) {
this.version = version.name;
2018-09-17 10:07:32 +03:00
}
@Override
public String toString() {
return title;
2018-09-17 10:07:32 +03:00
}
@LauncherAPI
public void verify() {
// Version
getVersion();
IOHelper.verifyFileName(getAssetIndex());
// Client
VerifyHelper.verify(getTitle(), VerifyHelper.NOT_EMPTY, "Profile title can't be empty");
VerifyHelper.verify(getServerAddress(), VerifyHelper.NOT_EMPTY, "Server address can't be empty");
VerifyHelper.verifyInt(getServerPort(), VerifyHelper.range(0, 65535), "Illegal server port: " + getServerPort());
// Client launcher
VerifyHelper.verify(getTitle(), VerifyHelper.NOT_EMPTY, "Main class can't be empty");
}
}