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

424 lines
12 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;
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")
public final class ClientProfile implements Comparable<ClientProfile> {
public ClientProfile(String version, String assetIndex, int sortIndex, String title, String serverAddress, int serverPort, boolean updateFastCheck, boolean useWhitelist, String mainClass) {
this.version = version;
this.assetIndex = assetIndex;
this.sortIndex = sortIndex;
this.title = title;
this.serverAddress = serverAddress;
this.serverPort = serverPort;
this.updateFastCheck = updateFastCheck;
this.useWhitelist = useWhitelist;
this.mainClass = mainClass;
}
public 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-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
@LauncherAPI
private String version;
@LauncherAPI
private String assetIndex;
@LauncherAPI
private String dir;
@LauncherAPI
private String assetDir;
2018-09-17 10:07:32 +03:00
// Client
@LauncherAPI
private int sortIndex;
@LauncherAPI
private String title;
@LauncherAPI
private String serverAddress;
@LauncherAPI
private int serverPort;
2018-11-08 15:30:16 +03:00
public static class OptionalFile {
@LauncherAPI
public String file;
@LauncherAPI
public boolean mark;
@LauncherAPI
public String name;
@LauncherAPI
public String info;
@LauncherAPI
public String[] dependenciesFile;
@LauncherAPI
public String[] conflictFile;
@LauncherAPI
public transient OptionalFile[] dependencies;
@LauncherAPI
public transient OptionalFile[] conflict;
@LauncherAPI
public int subTreeLevel = 1;
@LauncherAPI
public transient Set<OptionalFile> dependenciesCount;
public OptionalFile(String file, boolean mark) {
this.file = file;
this.mark = mark;
}
2018-11-08 15:30:16 +03:00
public OptionalFile(String file) {
this.file = file;
this.mark = false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OptionalFile that = (OptionalFile) o;
return Objects.equals(file, that.file);
}
@Override
public int hashCode() {
return Objects.hash(file);
}
}
2018-11-08 15:30:16 +03:00
2018-09-17 10:07:32 +03:00
// Updater and client watch service
@LauncherAPI
2018-09-24 19:34:06 +03:00
private final List<String> update = new ArrayList<>();
@LauncherAPI
2018-09-24 19:34:06 +03:00
private final List<String> updateExclusions = new ArrayList<>();
@LauncherAPI
2018-11-08 15:50:24 +03:00
private final List<String> updateShared = new ArrayList<>();
@LauncherAPI
2018-09-24 19:34:06 +03:00
private final List<String> updateVerify = new ArrayList<>();
@LauncherAPI
private final Set<OptionalFile> updateOptional = new HashSet<>();
@LauncherAPI
private boolean updateFastCheck;
@LauncherAPI
private boolean useWhitelist;
2018-09-17 10:07:32 +03:00
// Client launcher
@LauncherAPI
private String mainClass;
@LauncherAPI
private final List<String> jvmArgs = new ArrayList<>();
@LauncherAPI
private final List<String> classPath = new ArrayList<>();
@LauncherAPI
private final List<String> clientArgs = new ArrayList<>();
@LauncherAPI
private final List<String> whitelist = new ArrayList<>();
2018-09-17 10:07:32 +03:00
@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
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
@LauncherAPI
public String getAssetDir() {
return assetDir;
}
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
public Set<OptionalFile> getOptional() {
return updateOptional;
}
@LauncherAPI
public void updateOptionalGraph()
{
for(OptionalFile file : updateOptional)
{
if(file.dependenciesFile != null)
{
file.dependencies = new OptionalFile[file.dependenciesFile.length];
for(int i=0;i<file.dependenciesFile.length;++i)
{
file.dependencies[i] = getOptionalFile(file.dependenciesFile[i]);
}
}
if(file.conflictFile != null)
{
file.conflict = new OptionalFile[file.conflictFile.length];
for(int i=0;i<file.conflictFile.length;++i)
{
file.conflict[i] = getOptionalFile(file.conflictFile[i]);
}
}
}
}
@LauncherAPI
public OptionalFile getOptionalFile(String file)
{
for(OptionalFile f : updateOptional)
if(f.file.equals(file)) return f;
return null;
}
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 OptionalFile(opt)))
2018-11-08 15:30:16 +03:00
throw new SecurityException(String.format("Optional mod %s not found in optionalList", opt));
OptionalFile file = getOptionalFile(opt);
markOptional(file);
}
@LauncherAPI
public void markOptional(OptionalFile file)
{
if(file.mark) return;
file.mark = true;
if(file.dependencies != null)
{
for(OptionalFile dep : file.dependencies)
{
if(dep.dependenciesCount == null) dep.dependenciesCount = new HashSet<>();
dep.dependenciesCount.add(file);
markOptional(dep);
}
}
if(file.conflict != null)
{
for(OptionalFile conflict : file.conflict)
{
unmarkOptional(conflict);
}
}
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 OptionalFile(opt)))
2018-11-08 15:30:16 +03:00
throw new SecurityException(String.format("Optional mod %s not found in optionalList", opt));
OptionalFile file = getOptionalFile(opt);
unmarkOptional(file);
}
@LauncherAPI
public void unmarkOptional(OptionalFile file)
{
if(!file.mark) return;
file.mark = false;
if(file.dependenciesCount != null)
{
for(OptionalFile f : file.dependenciesCount)
{
unmarkOptional(f);
}
file.dependenciesCount.clear();
file.dependenciesCount = null;
}
if(file.dependencies != null)
{
for(OptionalFile f : file.dependencies)
{
if(!f.mark) continue;
if(f.dependenciesCount == null)
{
unmarkOptional(f);
}
else if(f.dependenciesCount.size() <= 1)
{
f.dependenciesCount.clear();
f.dependenciesCount = null;
unmarkOptional(f);
}
}
}
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 (OptionalFile opt : updateOptional) {
if (!opt.mark) dir.removeR(opt.file);
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");
}
}