-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
2,483 additions
and
1,268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
458 changes: 202 additions & 256 deletions
458
HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java
Large diffs are not rendered by default.
Oops, something went wrong.
141 changes: 141 additions & 0 deletions
141
HMCL/src/main/java/org/jackhuang/hmcl/java/HMCLJavaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package org.jackhuang.hmcl.java; | ||
|
||
import org.jackhuang.hmcl.download.DownloadProvider; | ||
import org.jackhuang.hmcl.download.java.JavaDownloadTask; | ||
import org.jackhuang.hmcl.download.java.RemoteFiles; | ||
import org.jackhuang.hmcl.game.DownloadInfo; | ||
import org.jackhuang.hmcl.game.GameJavaVersion; | ||
import org.jackhuang.hmcl.task.Task; | ||
import org.jackhuang.hmcl.util.gson.JsonUtils; | ||
import org.jackhuang.hmcl.util.io.FileUtils; | ||
import org.jackhuang.hmcl.util.platform.OperatingSystem; | ||
import org.jackhuang.hmcl.util.platform.Platform; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
|
||
import static org.jackhuang.hmcl.util.logging.Logger.LOG; | ||
|
||
/** | ||
* @author Glavo | ||
*/ | ||
public final class HMCLJavaRepository implements JavaRepository { | ||
private final Path root; | ||
|
||
public HMCLJavaRepository(Path root) { | ||
this.root = root; | ||
} | ||
|
||
public Path getPlatformRoot(Platform platform) { | ||
return root.resolve(platform.toString()); | ||
} | ||
|
||
public Path getJavaDir(Platform platform, String name) { | ||
return getPlatformRoot(platform).resolve(name); | ||
} | ||
|
||
public Path getManifestFile(Platform platform, String name) { | ||
return getPlatformRoot(platform).resolve(name + ".json"); | ||
} | ||
|
||
@Override | ||
public Collection<JavaRuntime> getAllJava(Platform platform) { | ||
Path root = getPlatformRoot(platform); | ||
if (!Files.isDirectory(root)) | ||
return Collections.emptyList(); | ||
|
||
ArrayList<JavaRuntime> list = new ArrayList<>(); | ||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(root)) { | ||
for (Path file : stream) { | ||
try { | ||
String name = file.getFileName().toString(); | ||
if (name.endsWith(".json") && Files.isRegularFile(file)) { | ||
Path javaDir = file.resolveSibling(name.substring(0, name.length() - ".json".length())); | ||
Path executable; | ||
try { | ||
executable = JavaManager.getExecutable(javaDir).toRealPath(); | ||
} catch (IOException e) { | ||
if (platform.getOperatingSystem() == OperatingSystem.OSX) | ||
executable = JavaManager.getMacExecutable(javaDir).toRealPath(); | ||
else | ||
throw e; | ||
} | ||
|
||
if (Files.isDirectory(javaDir)) { | ||
JavaManifest manifest; | ||
try (InputStream input = Files.newInputStream(file)) { | ||
manifest = JsonUtils.fromJsonFully(input, JavaManifest.class); | ||
} | ||
|
||
list.add(new JavaRuntime(executable, manifest.getInfo(), true)); | ||
} | ||
} | ||
} catch (Throwable e) { | ||
LOG.warning("Failed to parse " + file, e); | ||
} | ||
} | ||
|
||
} catch (IOException ignored) { | ||
} | ||
return list; | ||
} | ||
|
||
@Override | ||
public Task<JavaRuntime> getInstallJavaTask(DownloadProvider downloadProvider, Platform platform, GameJavaVersion gameJavaVersion) { | ||
Path javaDir = getJavaDir(platform, gameJavaVersion.getComponent()); | ||
|
||
return new JavaDownloadTask(downloadProvider, javaDir, gameJavaVersion, JavaManager.getJavaPlatform(platform)) | ||
.thenApplyAsync(result -> { | ||
Path executable = null; | ||
try { | ||
executable = JavaManager.getExecutable(javaDir).toRealPath(); | ||
} catch (IOException e) { | ||
if (platform.getOperatingSystem() == OperatingSystem.OSX) { | ||
executable = JavaManager.getMacExecutable(javaDir).toRealPath(); | ||
} | ||
} | ||
|
||
JavaInfo info = new JavaInfo(platform, result.download.getVersion().getName(), null); | ||
|
||
Map<String, Object> update = new LinkedHashMap<>(); | ||
update.put("provider", "mojang"); | ||
update.put("component", gameJavaVersion.getComponent()); | ||
|
||
Map<String, JavaLocalFiles.Local> files = new LinkedHashMap<>(); | ||
result.remoteFiles.getFiles().forEach((path, file) -> { | ||
if (file instanceof RemoteFiles.RemoteFile) { | ||
DownloadInfo downloadInfo = ((RemoteFiles.RemoteFile) file).getDownloads().get("raw"); | ||
if (downloadInfo != null) { | ||
files.put(path, new JavaLocalFiles.LocalFile(downloadInfo.getSha1(), downloadInfo.getSize())); | ||
} | ||
} else if (file instanceof RemoteFiles.RemoteDirectory) { | ||
files.put(path, new JavaLocalFiles.LocalDirectory()); | ||
} else if (file instanceof RemoteFiles.RemoteLink) { | ||
files.put(path, new JavaLocalFiles.LocalLink(((RemoteFiles.RemoteLink) file).getTarget())); | ||
} | ||
}); | ||
|
||
JavaManifest manifest = new JavaManifest(info, update, files); | ||
FileUtils.writeText(getManifestFile(platform, gameJavaVersion.getComponent()), JsonUtils.GSON.toJson(manifest)); | ||
return new JavaRuntime(executable, info, true); | ||
}); | ||
} | ||
|
||
@Override | ||
public Task<Void> getUninstallJavaTask(JavaRuntime java) { | ||
return Task.runAsync(() -> { | ||
Path root = getPlatformRoot(java.getPlatform()); | ||
Path relativized = root.relativize(java.getBinary()); | ||
|
||
if (relativized.getNameCount() > 1) { | ||
String name = relativized.getName(0).toString(); | ||
Files.deleteIfExists(getManifestFile(java.getPlatform(), name)); | ||
FileUtils.deleteDirectory(getJavaDir(java.getPlatform(), name).toFile()); | ||
} | ||
}); | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
HMCL/src/main/java/org/jackhuang/hmcl/java/JavaLocalFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Hello Minecraft! Launcher | ||
* Copyright (C) 2020 huangyuhui <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.jackhuang.hmcl.java; | ||
|
||
import com.google.gson.*; | ||
import com.google.gson.annotations.JsonAdapter; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* @author Glavo | ||
*/ | ||
public final class JavaLocalFiles { | ||
@JsonAdapter(Serializer.class) | ||
public abstract static class Local { | ||
private final String type; | ||
|
||
Local(String type) { | ||
this.type = type; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
} | ||
|
||
public static final class LocalFile extends Local { | ||
private final String sha1; | ||
private final long size; | ||
|
||
public LocalFile(String sha1, long size) { | ||
super("file"); | ||
this.sha1 = sha1; | ||
this.size = size; | ||
} | ||
|
||
public String getSha1() { | ||
return sha1; | ||
} | ||
|
||
public long getSize() { | ||
return size; | ||
} | ||
} | ||
|
||
public static final class LocalDirectory extends Local { | ||
public LocalDirectory() { | ||
super("directory"); | ||
} | ||
} | ||
|
||
public static final class LocalLink extends Local { | ||
private final String target; | ||
|
||
public LocalLink(String target) { | ||
super("link"); | ||
this.target = target; | ||
} | ||
|
||
public String getTarget() { | ||
return target; | ||
} | ||
} | ||
|
||
public static class Serializer implements JsonSerializer<Local>, JsonDeserializer<Local> { | ||
|
||
@Override | ||
public JsonElement serialize(Local src, Type typeOfSrc, JsonSerializationContext context) { | ||
JsonObject obj = new JsonObject(); | ||
obj.addProperty("type", src.getType()); | ||
if (src instanceof LocalFile) { | ||
obj.addProperty("sha1", ((LocalFile) src).getSha1()); | ||
obj.addProperty("size", ((LocalFile) src).getSize()); | ||
} else if (src instanceof LocalLink) { | ||
obj.addProperty("target", ((LocalLink) src).getTarget()); | ||
} | ||
return obj; | ||
} | ||
|
||
@Override | ||
public Local deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
if (!json.isJsonObject()) | ||
throw new JsonParseException(json.toString()); | ||
|
||
JsonObject obj = json.getAsJsonObject(); | ||
if (!obj.has("type")) | ||
throw new JsonParseException(json.toString()); | ||
|
||
String type = obj.getAsJsonPrimitive("type").getAsString(); | ||
|
||
try { | ||
switch (type) { | ||
case "file": { | ||
String sha1 = obj.getAsJsonPrimitive("sha1").getAsString(); | ||
long size = obj.getAsJsonPrimitive("size").getAsLong(); | ||
return new LocalFile(sha1, size); | ||
} | ||
case "directory": { | ||
return new LocalDirectory(); | ||
} | ||
case "link": { | ||
String target = obj.getAsJsonPrimitive("target").getAsString(); | ||
return new LocalLink(target); | ||
} | ||
default: | ||
throw new AssertionError(); | ||
} | ||
} catch (Throwable e) { | ||
throw new JsonParseException(json.toString()); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.