mirror of
https://github.com/packwiz/packwiz-installer.git
synced 2025-10-16 16:04:32 +02:00
Compare commits
51 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3d1d6db9b4 | ||
|
c6e304bc7f | ||
|
92d6f68f1d | ||
|
07af6046c1 | ||
|
89bdfd9c98 | ||
|
f4dd4fa866 | ||
|
6db8422c87 | ||
|
7d6346c088 | ||
|
aff921f67e | ||
|
afb574d82d | ||
|
8635906b1c | ||
|
bf95f03a18 | ||
|
bca2d758e1 | ||
|
46771ce870 | ||
|
b143f67acd | ||
|
03b0f1b09b | ||
|
6c6a0100fd | ||
|
6d47c0d61f | ||
|
226e754547 | ||
|
2c02703101 | ||
|
81a60cc759 | ||
|
92afa93fd7 | ||
|
0858c90079 | ||
|
1d4c94f5b6 | ||
|
74ddca5d54 | ||
|
0df48d19a9 | ||
|
f5b22f37a4 | ||
|
f52cd19ad4 | ||
|
60887a4312 | ||
|
a368268038 | ||
|
8beded7b41 | ||
|
91060dcd54 | ||
|
e06ee21f3b | ||
|
b3370739a5 | ||
|
ecc6f0440a | ||
|
92b44352b3 | ||
|
1d5a787b02 | ||
|
b5983800e8 | ||
|
4b3c279e71 | ||
|
b413371306 | ||
|
1d2ec61232 | ||
|
a0da889a02 | ||
|
432bb4e25f | ||
|
c89d3b1e47 | ||
|
e8538c22bc | ||
|
a15489f5e4 | ||
|
9d3587c72e | ||
|
bead683b7c | ||
|
0770029dc6 | ||
|
ecaab219c2 | ||
|
b45a2983e7 |
30
.gitignore
vendored
30
.gitignore
vendored
@@ -1,9 +1,9 @@
|
|||||||
|
|
||||||
# Created by https://www.gitignore.io/api/java,gradle,intellij
|
# Created by https://www.toptal.com/developers/gitignore/api/java,gradle,intellij+all
|
||||||
# Edit at https://www.gitignore.io/?templates=java,gradle,intellij
|
# Edit at https://www.toptal.com/developers/gitignore?templates=java,gradle,intellij+all
|
||||||
|
|
||||||
### Intellij ###
|
### Intellij+all ###
|
||||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
# User-specific stuff
|
# User-specific stuff
|
||||||
@@ -33,6 +33,9 @@
|
|||||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||||
# since they will be recreated, and may cause churn. Uncomment if using
|
# since they will be recreated, and may cause churn. Uncomment if using
|
||||||
# auto-import.
|
# auto-import.
|
||||||
|
# .idea/artifacts
|
||||||
|
# .idea/compiler.xml
|
||||||
|
# .idea/jarRepositories.xml
|
||||||
# .idea/modules.xml
|
# .idea/modules.xml
|
||||||
# .idea/*.iml
|
# .idea/*.iml
|
||||||
# .idea/modules
|
# .idea/modules
|
||||||
@@ -72,13 +75,18 @@ fabric.properties
|
|||||||
# Android studio 3.1+ serialized cache file
|
# Android studio 3.1+ serialized cache file
|
||||||
.idea/caches/build_file_checksums.ser
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
||||||
### Intellij Patch ###
|
### Intellij+all Patch ###
|
||||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
# Ignores the whole .idea folder and all .iml files
|
||||||
|
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
|
||||||
|
|
||||||
# *.iml
|
.idea/
|
||||||
# modules.xml
|
|
||||||
# .idea/misc.xml
|
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
|
||||||
# *.ipr
|
|
||||||
|
*.iml
|
||||||
|
modules.xml
|
||||||
|
.idea/misc.xml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
# Sonarlint plugin
|
# Sonarlint plugin
|
||||||
.idea/sonarlint
|
.idea/sonarlint
|
||||||
@@ -127,4 +135,4 @@ gradle-app.setting
|
|||||||
### Gradle Patch ###
|
### Gradle Patch ###
|
||||||
**/build/
|
**/build/
|
||||||
|
|
||||||
# End of https://www.gitignore.io/api/java,gradle,intellij
|
# End of https://www.toptal.com/developers/gitignore/api/java,gradle,intellij+all
|
||||||
|
36
.idea/inspectionProfiles/Project_Default.xml
generated
36
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -1,36 +0,0 @@
|
|||||||
<component name="InspectionProjectProfileManager">
|
|
||||||
<profile version="1.0">
|
|
||||||
<option name="myName" value="Project Default" />
|
|
||||||
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
|
|
||||||
<option name="TOP_LEVEL_CLASS_OPTIONS">
|
|
||||||
<value>
|
|
||||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
|
||||||
<option name="REQUIRED_TAGS" value="" />
|
|
||||||
</value>
|
|
||||||
</option>
|
|
||||||
<option name="INNER_CLASS_OPTIONS">
|
|
||||||
<value>
|
|
||||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
|
||||||
<option name="REQUIRED_TAGS" value="" />
|
|
||||||
</value>
|
|
||||||
</option>
|
|
||||||
<option name="METHOD_OPTIONS">
|
|
||||||
<value>
|
|
||||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
|
||||||
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
|
|
||||||
</value>
|
|
||||||
</option>
|
|
||||||
<option name="FIELD_OPTIONS">
|
|
||||||
<value>
|
|
||||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
|
||||||
<option name="REQUIRED_TAGS" value="" />
|
|
||||||
</value>
|
|
||||||
</option>
|
|
||||||
<option name="IGNORE_DEPRECATED" value="false" />
|
|
||||||
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
|
|
||||||
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
|
|
||||||
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
|
|
||||||
<option name="myAdditionalJavadocTags" value="wbp.parser.entryPoint" />
|
|
||||||
</inspection_tool>
|
|
||||||
</profile>
|
|
||||||
</component>
|
|
10
.idea/misc.xml
generated
10
.idea/misc.xml
generated
@@ -1,10 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="EntryPointsManager">
|
|
||||||
<list size="1">
|
|
||||||
<item index="0" class="java.lang.String" itemvalue="com.google.gson.annotations.SerializedName" />
|
|
||||||
</list>
|
|
||||||
</component>
|
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="11" project-jdk-type="JavaSDK" />
|
|
||||||
</project>
|
|
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2019
|
Copyright (c) 2021 comp500
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@@ -1,2 +1,2 @@
|
|||||||
# packwiz-installer
|
# packwiz-installer
|
||||||
An installer for launching packwiz modpacks with MultiMC.
|
An installer for launching packwiz modpacks with MultiMC. You'll need [the bootstrapper](https://github.com/comp500/packwiz-installer-bootstrap/releases) to actually use this.
|
||||||
|
72
build.gradle
72
build.gradle
@@ -1,72 +0,0 @@
|
|||||||
plugins {
|
|
||||||
id 'java'
|
|
||||||
id 'application'
|
|
||||||
id 'com.github.johnrengelman.shadow' version '5.0.0'
|
|
||||||
id 'com.palantir.git-version' version '0.11.0'
|
|
||||||
id 'com.github.breadmoirai.github-release' version '2.2.9'
|
|
||||||
}
|
|
||||||
|
|
||||||
sourceCompatibility = 1.8
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation 'commons-cli:commons-cli:1.4'
|
|
||||||
implementation 'com.moandjiezana.toml:toml4j:0.7.2'
|
|
||||||
// TODO: Implement tests
|
|
||||||
//testImplementation 'junit:junit:4.12'
|
|
||||||
implementation 'com.google.code.gson:gson:2.8.1'
|
|
||||||
implementation 'com.squareup.okio:okio:2.2.2'
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
jcenter()
|
|
||||||
}
|
|
||||||
|
|
||||||
mainClassName = 'link.infra.packwiz.installer.RequiresBootstrap'
|
|
||||||
version gitVersion()
|
|
||||||
|
|
||||||
jar {
|
|
||||||
manifest {
|
|
||||||
attributes(
|
|
||||||
'Main-Class': 'link.infra.packwiz.installer.RequiresBootstrap',
|
|
||||||
'Implementation-Version': project.version
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commons CLI and Minimal JSON are already included in packwiz-installer-bootstrap
|
|
||||||
shadowJar {
|
|
||||||
dependencies {
|
|
||||||
exclude(dependency('commons-cli:commons-cli:1.4'))
|
|
||||||
exclude(dependency('com.eclipsesource.minimal-json:minimal-json:0.9.5'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used for vscode launch.json
|
|
||||||
task copyJar(type: Copy) {
|
|
||||||
from shadowJar
|
|
||||||
rename "packwiz-installer-(.*)\\.jar", "packwiz-installer.jar"
|
|
||||||
into "build/libs/"
|
|
||||||
}
|
|
||||||
|
|
||||||
build.dependsOn copyJar
|
|
||||||
|
|
||||||
if (project.hasProperty("github.token")) {
|
|
||||||
githubRelease {
|
|
||||||
// IntelliJ u ok?
|
|
||||||
//noinspection GroovyAssignabilityCheck
|
|
||||||
owner "comp500"
|
|
||||||
//noinspection GroovyAssignabilityCheck
|
|
||||||
repo "packwiz-installer"
|
|
||||||
//noinspection GroovyAssignabilityCheck
|
|
||||||
tagName "${project.version}"
|
|
||||||
//noinspection GroovyAssignabilityCheck
|
|
||||||
releaseName "Release ${project.version}"
|
|
||||||
//noinspection GroovyAssignabilityCheck
|
|
||||||
draft true
|
|
||||||
//noinspection GroovyAssignabilityCheck
|
|
||||||
token findProperty("github.token") ?: ""
|
|
||||||
releaseAssets = [jar.destinationDirectory.file("packwiz-installer.jar").get()]
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.githubRelease.dependsOn(build)
|
|
||||||
}
|
|
169
build.gradle.kts
Normal file
169
build.gradle.kts
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
dependencies {
|
||||||
|
classpath("com.guardsquare:proguard-gradle:7.1.0") {
|
||||||
|
exclude("com.android.tools.build")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
java
|
||||||
|
application
|
||||||
|
id("com.github.johnrengelman.shadow") version "7.1.2"
|
||||||
|
id("com.palantir.git-version") version "0.13.0"
|
||||||
|
id("com.github.breadmoirai.github-release") version "2.2.12"
|
||||||
|
kotlin("jvm") version "1.6.10"
|
||||||
|
id("com.github.jk1.dependency-license-report") version "2.0"
|
||||||
|
`maven-publish`
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("commons-cli:commons-cli:1.5.0")
|
||||||
|
implementation("com.moandjiezana.toml:toml4j:0.7.2")
|
||||||
|
implementation("com.google.code.gson:gson:2.8.9")
|
||||||
|
implementation("com.squareup.okio:okio:3.0.0")
|
||||||
|
implementation(kotlin("stdlib-jdk8"))
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.9.3")
|
||||||
|
implementation("com.michael-bull.kotlin-result:kotlin-result:1.1.14")
|
||||||
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
mainClassName = "link.infra.packwiz.installer.RequiresBootstrap"
|
||||||
|
}
|
||||||
|
|
||||||
|
val gitVersion: groovy.lang.Closure<*> by extra
|
||||||
|
version = gitVersion()
|
||||||
|
|
||||||
|
tasks.jar {
|
||||||
|
manifest {
|
||||||
|
attributes["Main-Class"] = "link.infra.packwiz.installer.RequiresBootstrap"
|
||||||
|
attributes["Implementation-Version"] = project.version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
licenseReport {
|
||||||
|
renderers = arrayOf<com.github.jk1.license.render.ReportRenderer>(
|
||||||
|
com.github.jk1.license.render.InventoryMarkdownReportRenderer("licenses.md", "packwiz-installer")
|
||||||
|
)
|
||||||
|
filters = arrayOf<com.github.jk1.license.filter.DependencyFilter>(com.github.jk1.license.filter.LicenseBundleNormalizer())
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.shadowJar {
|
||||||
|
exclude("**/*.kotlin_metadata")
|
||||||
|
exclude("**/*.kotlin_builtins")
|
||||||
|
exclude("META-INF/maven/**/*")
|
||||||
|
exclude("META-INF/proguard/**/*")
|
||||||
|
|
||||||
|
// Relocate Commons CLI, so that it doesn't clash with old packwiz-installer-bootstrap (that shades it)
|
||||||
|
relocate("org.apache.commons.cli", "link.infra.packwiz.installer.deps.commons-cli")
|
||||||
|
|
||||||
|
// from Commons CLI
|
||||||
|
exclude("META-INF/LICENSE.txt")
|
||||||
|
exclude("META-INF/NOTICE.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.register<proguard.gradle.ProGuardTask>("shrinkJar") {
|
||||||
|
injars(tasks.shadowJar)
|
||||||
|
outjars("build/libs/" + tasks.shadowJar.get().outputs.files.first().name.removeSuffix(".jar") + "-shrink.jar")
|
||||||
|
if (System.getProperty("java.version").startsWith("1.")) {
|
||||||
|
libraryjars("${System.getProperty("java.home")}/lib/rt.jar")
|
||||||
|
libraryjars("${System.getProperty("java.home")}/lib/jce.jar")
|
||||||
|
} else {
|
||||||
|
// Use jmods for Java 9+
|
||||||
|
val mods = listOf("java.base", "java.logging", "java.desktop", "java.sql")
|
||||||
|
for (mod in mods) {
|
||||||
|
libraryjars(mapOf(
|
||||||
|
"jarfilter" to "!**.jar",
|
||||||
|
"filter" to "!module-info.class"
|
||||||
|
), "${System.getProperty("java.home")}/jmods/$mod.jmod")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
keep("class link.infra.packwiz.installer.** { *; }")
|
||||||
|
dontoptimize()
|
||||||
|
dontobfuscate()
|
||||||
|
|
||||||
|
// Used by Okio and OkHttp
|
||||||
|
dontwarn("org.codehaus.mojo.animal_sniffer.*")
|
||||||
|
dontwarn("okhttp3.internal.platform.**")
|
||||||
|
dontwarn("org.conscrypt.**")
|
||||||
|
dontwarn("org.bouncycastle.**")
|
||||||
|
dontwarn("org.openjsse.**")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Used for vscode launch.json
|
||||||
|
tasks.register<Copy>("copyJar") {
|
||||||
|
from(tasks.named("shrinkJar"))
|
||||||
|
rename("packwiz-installer-(.*)\\.jar", "packwiz-installer.jar")
|
||||||
|
into("build/libs/")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.build {
|
||||||
|
dependsOn("copyJar")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.hasProperty("github.token")) {
|
||||||
|
githubRelease {
|
||||||
|
owner("comp500")
|
||||||
|
repo("packwiz-installer")
|
||||||
|
tagName("${project.version}")
|
||||||
|
releaseName("Release ${project.version}")
|
||||||
|
draft(true)
|
||||||
|
token(findProperty("github.token") as String? ?: "")
|
||||||
|
releaseAssets(tasks.jar.get().destinationDirectory.file("packwiz-installer.jar").get())
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.githubRelease {
|
||||||
|
dependsOn(tasks.build)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.compileKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
freeCompilerArgs = listOf("-Xjvm-default=enable", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tasks.compileTestKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
freeCompilerArgs = listOf("-Xjvm-default=enable", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (project.hasProperty("bunnycdn.token")) {
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
create<MavenPublication>("maven") {
|
||||||
|
groupId = "link.infra.packwiz"
|
||||||
|
artifactId = "packwiz-installer"
|
||||||
|
|
||||||
|
from(components["java"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
repositories {
|
||||||
|
maven {
|
||||||
|
url = uri("https://storage.bunnycdn.com/comp-maven")
|
||||||
|
credentials(HttpHeaderCredentials::class) {
|
||||||
|
name = "AccessKey"
|
||||||
|
value = findProperty("bunnycdn.token") as String?
|
||||||
|
}
|
||||||
|
authentication {
|
||||||
|
create<HttpHeaderAuthentication>("header")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
@@ -1,245 +0,0 @@
|
|||||||
package link.infra.packwiz.installer;
|
|
||||||
|
|
||||||
import link.infra.packwiz.installer.metadata.IndexFile;
|
|
||||||
import link.infra.packwiz.installer.metadata.ManifestFile;
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.GeneralHashingSource;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.Hash;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.HashUtils;
|
|
||||||
import link.infra.packwiz.installer.ui.IExceptionDetails;
|
|
||||||
import link.infra.packwiz.installer.ui.IOptionDetails;
|
|
||||||
import okio.Buffer;
|
|
||||||
import okio.Okio;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
class DownloadTask implements IOptionDetails, IExceptionDetails {
|
|
||||||
final IndexFile.File metadata;
|
|
||||||
ManifestFile.File cachedFile = null;
|
|
||||||
private Exception failure = null;
|
|
||||||
private boolean alreadyUpToDate = false;
|
|
||||||
private boolean metadataRequired = true;
|
|
||||||
private boolean invalidated = false;
|
|
||||||
// If file is new or isOptional changed to true, the option needs to be presented again
|
|
||||||
private boolean newOptional = true;
|
|
||||||
private final UpdateManager.Options.Side downloadSide;
|
|
||||||
|
|
||||||
private DownloadTask(IndexFile.File metadata, String defaultFormat, UpdateManager.Options.Side downloadSide) {
|
|
||||||
this.metadata = metadata;
|
|
||||||
if (metadata.hashFormat == null || metadata.hashFormat.length() == 0) {
|
|
||||||
metadata.hashFormat = defaultFormat;
|
|
||||||
}
|
|
||||||
this.downloadSide = downloadSide;
|
|
||||||
}
|
|
||||||
|
|
||||||
void invalidate() {
|
|
||||||
invalidated = true;
|
|
||||||
alreadyUpToDate = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void updateFromCache(ManifestFile.File cachedFile) {
|
|
||||||
if (failure != null) return;
|
|
||||||
if (cachedFile == null) {
|
|
||||||
this.cachedFile = new ManifestFile.File();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.cachedFile = cachedFile;
|
|
||||||
|
|
||||||
if (!invalidated) {
|
|
||||||
Hash currHash;
|
|
||||||
try {
|
|
||||||
currHash = HashUtils.getHash(metadata.hashFormat, metadata.hash);
|
|
||||||
} catch (Exception e) {
|
|
||||||
failure = e;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (currHash != null && currHash.equals(cachedFile.hash)) {
|
|
||||||
// Already up to date
|
|
||||||
alreadyUpToDate = true;
|
|
||||||
metadataRequired = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (cachedFile.isOptional) {
|
|
||||||
// Because option selection dialog might set this task to true/false, metadata is always needed to download
|
|
||||||
// the file, and to show the description and name
|
|
||||||
metadataRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void downloadMetadata(IndexFile parentIndexFile, SpaceSafeURI indexUri) {
|
|
||||||
if (failure != null) return;
|
|
||||||
if (metadataRequired) {
|
|
||||||
try {
|
|
||||||
metadata.downloadMeta(parentIndexFile, indexUri);
|
|
||||||
} catch (Exception e) {
|
|
||||||
failure = e;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (metadata.linkedFile != null) {
|
|
||||||
if (metadata.linkedFile.option != null) {
|
|
||||||
if (metadata.linkedFile.option.optional) {
|
|
||||||
if (cachedFile.isOptional) {
|
|
||||||
// isOptional didn't change
|
|
||||||
newOptional = false;
|
|
||||||
} else {
|
|
||||||
// isOptional false -> true, set option to it's default value
|
|
||||||
// TODO: preserve previous option value, somehow??
|
|
||||||
cachedFile.optionValue = this.metadata.linkedFile.option.defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cachedFile.isOptional = isOptional();
|
|
||||||
cachedFile.onlyOtherSide = !correctSide();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void download(String packFolder, SpaceSafeURI indexUri) {
|
|
||||||
if (failure != null) return;
|
|
||||||
|
|
||||||
// Ensure it is removed
|
|
||||||
if (!cachedFile.optionValue || !correctSide()) {
|
|
||||||
if (cachedFile.cachedLocation == null) return;
|
|
||||||
try {
|
|
||||||
Files.deleteIfExists(Paths.get(packFolder, cachedFile.cachedLocation));
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO: how much of a problem is this? use log4j/other log library to show warning?
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
cachedFile.cachedLocation = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (alreadyUpToDate) return;
|
|
||||||
|
|
||||||
Path destPath = Paths.get(packFolder, metadata.getDestURI().toString());
|
|
||||||
|
|
||||||
// Don't update files marked with preserve if they already exist on disk
|
|
||||||
if (metadata.preserve) {
|
|
||||||
if (destPath.toFile().exists()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Hash hash;
|
|
||||||
String fileHashFormat;
|
|
||||||
if (metadata.linkedFile != null) {
|
|
||||||
hash = metadata.linkedFile.getHash();
|
|
||||||
fileHashFormat = metadata.linkedFile.download.hashFormat;
|
|
||||||
} else {
|
|
||||||
hash = metadata.getHash();
|
|
||||||
fileHashFormat = metadata.hashFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
Source src = metadata.getSource(indexUri);
|
|
||||||
GeneralHashingSource fileSource = HashUtils.getHasher(fileHashFormat).getHashingSource(src);
|
|
||||||
Buffer data = new Buffer();
|
|
||||||
Okio.buffer(fileSource).readAll(data);
|
|
||||||
|
|
||||||
if (fileSource.hashIsEqual(hash)) {
|
|
||||||
Files.createDirectories(destPath.getParent());
|
|
||||||
Files.copy(data.inputStream(), destPath, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
} else {
|
|
||||||
// TODO: no more SYSOUT!!!!!!!!!
|
|
||||||
System.out.println("Invalid hash for " + metadata.getDestURI().toString());
|
|
||||||
System.out.println("Calculated: " + fileSource.getHash());
|
|
||||||
System.out.println("Expected: " + hash);
|
|
||||||
failure = new Exception("Hash invalid!");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cachedFile.cachedLocation != null && !destPath.equals(Paths.get(packFolder, cachedFile.cachedLocation))) {
|
|
||||||
// Delete old file if location changes
|
|
||||||
Files.delete(Paths.get(packFolder, cachedFile.cachedLocation));
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
failure = e;
|
|
||||||
}
|
|
||||||
if (failure == null) {
|
|
||||||
if (cachedFile == null) {
|
|
||||||
cachedFile = new ManifestFile.File();
|
|
||||||
}
|
|
||||||
// Update the manifest file
|
|
||||||
try {
|
|
||||||
cachedFile.hash = metadata.getHash();
|
|
||||||
} catch (Exception e) {
|
|
||||||
failure = e;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cachedFile.isOptional = isOptional();
|
|
||||||
cachedFile.cachedLocation = metadata.getDestURI().toString();
|
|
||||||
if (metadata.linkedFile != null) {
|
|
||||||
try {
|
|
||||||
cachedFile.linkedFileHash = metadata.linkedFile.getHash();
|
|
||||||
} catch (Exception e) {
|
|
||||||
failure = e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Exception getException() {
|
|
||||||
return failure;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isOptional() {
|
|
||||||
if (metadata.linkedFile != null) {
|
|
||||||
return metadata.linkedFile.isOptional();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isNewOptional() {
|
|
||||||
return isOptional() && this.newOptional;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean correctSide() {
|
|
||||||
if (metadata.linkedFile != null) {
|
|
||||||
return metadata.linkedFile.side.hasSide(downloadSide);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return metadata.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getOptionValue() {
|
|
||||||
return cachedFile.optionValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getOptionDescription() {
|
|
||||||
if (metadata.linkedFile != null) {
|
|
||||||
return metadata.linkedFile.option.description;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOptionValue(boolean value) {
|
|
||||||
if (value && !cachedFile.optionValue) {
|
|
||||||
// Ensure that an update is done if it changes from false to true, or from true to false
|
|
||||||
alreadyUpToDate = false;
|
|
||||||
}
|
|
||||||
cachedFile.optionValue = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static List<DownloadTask> createTasksFromIndex(IndexFile index, String defaultFormat, UpdateManager.Options.Side downloadSide) {
|
|
||||||
ArrayList<DownloadTask> tasks = new ArrayList<>();
|
|
||||||
for (IndexFile.File file : index.files) {
|
|
||||||
tasks.add(new DownloadTask(file, defaultFormat, downloadSide));
|
|
||||||
}
|
|
||||||
return tasks;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@@ -1,148 +0,0 @@
|
|||||||
package link.infra.packwiz.installer;
|
|
||||||
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
|
||||||
import link.infra.packwiz.installer.ui.CLIHandler;
|
|
||||||
import link.infra.packwiz.installer.ui.IUserInterface;
|
|
||||||
import link.infra.packwiz.installer.ui.InputStateHandler;
|
|
||||||
import link.infra.packwiz.installer.ui.InstallWindow;
|
|
||||||
import org.apache.commons.cli.*;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
// Actual main() is in RequiresBootstrap!
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public Main(String[] args) {
|
|
||||||
// Big overarching try/catch just in case everything breaks
|
|
||||||
try {
|
|
||||||
this.startup(args);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
JOptionPane.showMessageDialog(null,
|
|
||||||
"A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(),
|
|
||||||
"packwiz-installer", JOptionPane.ERROR_MESSAGE);
|
|
||||||
System.exit(1);
|
|
||||||
});
|
|
||||||
// In case the eventqueue is broken, exit after 1 minute
|
|
||||||
try {
|
|
||||||
Thread.sleep(60 * 1000);
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
// Good, it was already called?
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startup(String[] args) {
|
|
||||||
Options options = new Options();
|
|
||||||
addNonBootstrapOptions(options);
|
|
||||||
addBootstrapOptions(options);
|
|
||||||
|
|
||||||
CommandLineParser parser = new DefaultParser();
|
|
||||||
CommandLine cmd = null;
|
|
||||||
try {
|
|
||||||
cmd = parser.parse(options, args);
|
|
||||||
} catch (ParseException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
try {
|
|
||||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
||||||
} catch (Exception e1) {
|
|
||||||
// Ignore the exceptions, just continue using the ugly L&F
|
|
||||||
}
|
|
||||||
JOptionPane.showMessageDialog(null, e.getMessage(), "packwiz-installer", JOptionPane.ERROR_MESSAGE);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
IUserInterface ui;
|
|
||||||
// if "headless", GUI creation will fail anyway!
|
|
||||||
if (cmd.hasOption("no-gui") || GraphicsEnvironment.isHeadless()) {
|
|
||||||
ui = new CLIHandler();
|
|
||||||
} else {
|
|
||||||
ui = new InstallWindow();
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] unparsedArgs = cmd.getArgs();
|
|
||||||
if (unparsedArgs.length > 1) {
|
|
||||||
ui.handleExceptionAndExit(new RuntimeException("Too many arguments specified!"));
|
|
||||||
return;
|
|
||||||
} else if (unparsedArgs.length < 1) {
|
|
||||||
ui.handleExceptionAndExit(new RuntimeException("URI to install from must be specified!"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String title = cmd.getOptionValue("title");
|
|
||||||
if (title != null) {
|
|
||||||
ui.setTitle(title);
|
|
||||||
}
|
|
||||||
|
|
||||||
InputStateHandler inputStateHandler = new InputStateHandler();
|
|
||||||
ui.show(inputStateHandler);
|
|
||||||
|
|
||||||
UpdateManager.Options uOptions = new UpdateManager.Options();
|
|
||||||
|
|
||||||
String side = cmd.getOptionValue("side");
|
|
||||||
if (side != null) {
|
|
||||||
uOptions.side = UpdateManager.Options.Side.from(side);
|
|
||||||
}
|
|
||||||
|
|
||||||
String packFolder = cmd.getOptionValue("pack-folder");
|
|
||||||
if (packFolder != null) {
|
|
||||||
uOptions.packFolder = packFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
String metaFile = cmd.getOptionValue("meta-file");
|
|
||||||
if (metaFile != null) {
|
|
||||||
uOptions.manifestFile = metaFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
uOptions.downloadURI = new SpaceSafeURI(unparsedArgs[0]);
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
// TODO: better error message?
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start update process!
|
|
||||||
// TODO: start in SwingWorker?
|
|
||||||
try {
|
|
||||||
ui.executeManager(() -> {
|
|
||||||
try {
|
|
||||||
new UpdateManager(uOptions, ui, inputStateHandler);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: better error message?
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: better error message?
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Called by packwiz-installer-bootstrap to set up the help command
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
|
||||||
public static void addNonBootstrapOptions(Options options) {
|
|
||||||
options.addOption("s", "side", true, "Side to install mods from (client/server, defaults to client)");
|
|
||||||
options.addOption(null, "title", true, "Title of the installer window");
|
|
||||||
options.addOption(null, "pack-folder", true, "Folder to install the pack to (defaults to the JAR directory)");
|
|
||||||
options.addOption(null, "meta-file", true, "JSON file to store pack metadata, relative to the pack folder (defaults to packwiz.json)");
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: link these somehow so they're only defined once?
|
|
||||||
private static void addBootstrapOptions(Options options) {
|
|
||||||
options.addOption(null, "bootstrap-update-url", true, "Github API URL for checking for updates");
|
|
||||||
options.addOption(null, "bootstrap-update-token", true, "Github API Access Token, for private repositories");
|
|
||||||
options.addOption(null, "bootstrap-no-update", false, "Don't update packwiz-installer");
|
|
||||||
options.addOption(null, "bootstrap-main-jar", true, "Location of the packwiz-installer JAR file");
|
|
||||||
options.addOption("g", "no-gui", false, "Don't display a GUI to show update progress");
|
|
||||||
options.addOption("h", "help", false, "Display this message"); // Implemented in packwiz-installer-bootstrap!
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -8,8 +8,6 @@ public class RequiresBootstrap {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Very small CLI implementation, because Commons CLI complains on unexpected
|
// Very small CLI implementation, because Commons CLI complains on unexpected
|
||||||
// options
|
// options
|
||||||
// Also so that Commons CLI can be excluded from the shaded JAR, as it is
|
|
||||||
// included in the bootstrap
|
|
||||||
if (Arrays.stream(args).map(str -> {
|
if (Arrays.stream(args).map(str -> {
|
||||||
if (str == null) return "";
|
if (str == null) return "";
|
||||||
if (str.startsWith("--")) {
|
if (str.startsWith("--")) {
|
||||||
|
@@ -1,500 +0,0 @@
|
|||||||
package link.infra.packwiz.installer;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.google.gson.JsonIOException;
|
|
||||||
import com.google.gson.JsonSyntaxException;
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
|
||||||
import com.moandjiezana.toml.Toml;
|
|
||||||
import link.infra.packwiz.installer.metadata.IndexFile;
|
|
||||||
import link.infra.packwiz.installer.metadata.ManifestFile;
|
|
||||||
import link.infra.packwiz.installer.metadata.PackFile;
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.GeneralHashingSource;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.Hash;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.HashUtils;
|
|
||||||
import link.infra.packwiz.installer.request.HandlerManager;
|
|
||||||
import link.infra.packwiz.installer.ui.IExceptionDetails;
|
|
||||||
import link.infra.packwiz.installer.ui.IUserInterface;
|
|
||||||
import link.infra.packwiz.installer.ui.InputStateHandler;
|
|
||||||
import link.infra.packwiz.installer.ui.InstallProgress;
|
|
||||||
import okio.Okio;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class UpdateManager {
|
|
||||||
|
|
||||||
private final Options opts;
|
|
||||||
public final IUserInterface ui;
|
|
||||||
private boolean cancelled;
|
|
||||||
private boolean cancelledStartGame = false;
|
|
||||||
private InputStateHandler stateHandler;
|
|
||||||
private boolean errorsOccurred = false;
|
|
||||||
|
|
||||||
public static class Options {
|
|
||||||
SpaceSafeURI downloadURI = null;
|
|
||||||
String manifestFile = "packwiz.json"; // TODO: make configurable
|
|
||||||
String packFolder = ".";
|
|
||||||
Side side = Side.CLIENT;
|
|
||||||
|
|
||||||
public enum Side {
|
|
||||||
@SerializedName("client")
|
|
||||||
CLIENT("client"),
|
|
||||||
@SerializedName("server")
|
|
||||||
SERVER("server"),
|
|
||||||
@SerializedName("both")
|
|
||||||
BOTH("both", new Side[] { CLIENT, SERVER });
|
|
||||||
|
|
||||||
private final String sideName;
|
|
||||||
private final Side[] depSides;
|
|
||||||
|
|
||||||
Side(String sideName) {
|
|
||||||
this.sideName = sideName.toLowerCase();
|
|
||||||
this.depSides = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
Side(String sideName, Side[] depSides) {
|
|
||||||
this.sideName = sideName.toLowerCase();
|
|
||||||
this.depSides = depSides;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.sideName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasSide(Side tSide) {
|
|
||||||
if (this.equals(tSide)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (this.depSides != null) {
|
|
||||||
for (Side depSide : this.depSides) {
|
|
||||||
if (depSide.equals(tSide)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Side from(String name) {
|
|
||||||
String lowerName = name.toLowerCase();
|
|
||||||
for (Side side : Side.values()) {
|
|
||||||
if (side.sideName.equals(lowerName)) {
|
|
||||||
return side;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateManager(Options opts, IUserInterface ui, InputStateHandler inputStateHandler) {
|
|
||||||
this.opts = opts;
|
|
||||||
this.ui = ui;
|
|
||||||
this.stateHandler = inputStateHandler;
|
|
||||||
this.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void start() {
|
|
||||||
this.checkOptions();
|
|
||||||
|
|
||||||
ui.submitProgress(new InstallProgress("Loading manifest file..."));
|
|
||||||
Gson gson = new GsonBuilder().registerTypeAdapter(Hash.class, new Hash.TypeHandler()).create();
|
|
||||||
ManifestFile manifest;
|
|
||||||
try {
|
|
||||||
manifest = gson.fromJson(new FileReader(Paths.get(opts.packFolder, opts.manifestFile).toString()),
|
|
||||||
ManifestFile.class);
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
manifest = new ManifestFile();
|
|
||||||
} catch (JsonSyntaxException | JsonIOException e) {
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
showCancellationDialog();
|
|
||||||
handleCancellation();
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.submitProgress(new InstallProgress("Loading pack file..."));
|
|
||||||
GeneralHashingSource packFileSource;
|
|
||||||
try {
|
|
||||||
Source src = HandlerManager.getFileSource(opts.downloadURI);
|
|
||||||
packFileSource = HashUtils.getHasher("sha256").getHashingSource(src);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: still launch the game if updating doesn't work?
|
|
||||||
// TODO: ask user if they want to launch the game, exit(1) if they don't
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
PackFile pf;
|
|
||||||
try {
|
|
||||||
pf = new Toml().read(Okio.buffer(packFileSource).inputStream()).to(PackFile.class);
|
|
||||||
} catch (IllegalStateException e) {
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
showCancellationDialog();
|
|
||||||
handleCancellation();
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.submitProgress(new InstallProgress("Checking local files..."));
|
|
||||||
|
|
||||||
// Invalidation checking must be done here, as it must happen before pack/index hashes are checked
|
|
||||||
List<SpaceSafeURI> invalidatedUris = new ArrayList<>();
|
|
||||||
if (manifest.cachedFiles != null) {
|
|
||||||
for (Map.Entry<SpaceSafeURI, ManifestFile.File> entry : manifest.cachedFiles.entrySet()) {
|
|
||||||
// ignore onlyOtherSide files
|
|
||||||
if (entry.getValue().onlyOtherSide) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
boolean invalid = false;
|
|
||||||
// if isn't optional, or is optional but optionValue == true
|
|
||||||
if (!entry.getValue().isOptional || entry.getValue().optionValue) {
|
|
||||||
if (entry.getValue().cachedLocation != null) {
|
|
||||||
if (!Paths.get(opts.packFolder, entry.getValue().cachedLocation).toFile().exists()) {
|
|
||||||
invalid = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// if cachedLocation == null, should probably be installed!!
|
|
||||||
invalid = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (invalid) {
|
|
||||||
SpaceSafeURI fileUri = entry.getKey();
|
|
||||||
System.out.println("File " + fileUri.toString() + " invalidated, marked for redownloading");
|
|
||||||
invalidatedUris.add(fileUri);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (manifest.packFileHash != null && packFileSource.hashIsEqual(manifest.packFileHash) && invalidatedUris.isEmpty()) {
|
|
||||||
System.out.println("Modpack is already up to date!");
|
|
||||||
// todo: --force?
|
|
||||||
if (!stateHandler.getOptionsButton()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Modpack name: " + pf.name);
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
showCancellationDialog();
|
|
||||||
handleCancellation();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// This is badly written, I'll probably heavily refactor it at some point
|
|
||||||
processIndex(HandlerManager.getNewLoc(opts.downloadURI, pf.index.file),
|
|
||||||
HashUtils.getHash(pf.index.hashFormat, pf.index.hash), pf.index.hashFormat, manifest, invalidatedUris);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
ui.handleExceptionAndExit(e1);
|
|
||||||
}
|
|
||||||
|
|
||||||
handleCancellation();
|
|
||||||
|
|
||||||
// TODO: update MMC params, java args etc
|
|
||||||
|
|
||||||
// If there were errors, don't write the manifest/index hashes, to ensure they are rechecked later
|
|
||||||
if (errorsOccurred) {
|
|
||||||
manifest.indexFileHash = null;
|
|
||||||
manifest.packFileHash = null;
|
|
||||||
} else {
|
|
||||||
manifest.packFileHash = packFileSource.getHash();
|
|
||||||
}
|
|
||||||
|
|
||||||
manifest.cachedSide = opts.side;
|
|
||||||
try (Writer writer = new FileWriter(Paths.get(opts.packFolder, opts.manifestFile).toString())) {
|
|
||||||
gson.toJson(manifest, writer);
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO: add message?
|
|
||||||
ui.handleException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkOptions() {
|
|
||||||
// TODO: implement
|
|
||||||
}
|
|
||||||
|
|
||||||
private void processIndex(SpaceSafeURI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest, List<SpaceSafeURI> invalidatedUris) {
|
|
||||||
if (manifest.indexFileHash != null && manifest.indexFileHash.equals(indexHash) && invalidatedUris.isEmpty()) {
|
|
||||||
System.out.println("Modpack files are already up to date!");
|
|
||||||
if (!stateHandler.getOptionsButton()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
manifest.indexFileHash = indexHash;
|
|
||||||
|
|
||||||
GeneralHashingSource indexFileSource;
|
|
||||||
try {
|
|
||||||
Source src = HandlerManager.getFileSource(indexUri);
|
|
||||||
indexFileSource = HashUtils.getHasher(hashFormat).getHashingSource(src);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: still launch the game if updating doesn't work?
|
|
||||||
// TODO: ask user if they want to launch the game, exit(1) if they don't
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
IndexFile indexFile;
|
|
||||||
try {
|
|
||||||
indexFile = new Toml().read(Okio.buffer(indexFileSource).inputStream()).to(IndexFile.class);
|
|
||||||
} catch (IllegalStateException e) {
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!indexFileSource.hashIsEqual(indexHash)) {
|
|
||||||
// TODO: throw exception
|
|
||||||
System.out.println("I was meant to put an error message here but I'll do that later");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
showCancellationDialog();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (manifest.cachedFiles == null) {
|
|
||||||
manifest.cachedFiles = new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.submitProgress(new InstallProgress("Checking local files..."));
|
|
||||||
Iterator<Map.Entry<SpaceSafeURI, ManifestFile.File>> it = manifest.cachedFiles.entrySet().iterator();
|
|
||||||
while (it.hasNext()) {
|
|
||||||
Map.Entry<SpaceSafeURI, ManifestFile.File> entry = it.next();
|
|
||||||
if (entry.getValue().cachedLocation != null) {
|
|
||||||
boolean alreadyDeleted = false;
|
|
||||||
// Delete if option value has been set to false
|
|
||||||
if (entry.getValue().isOptional && !entry.getValue().optionValue) {
|
|
||||||
try {
|
|
||||||
Files.deleteIfExists(Paths.get(opts.packFolder, entry.getValue().cachedLocation));
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO: should this be shown to the user in some way?
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
// Set to null, as it doesn't exist anymore
|
|
||||||
entry.getValue().cachedLocation = null;
|
|
||||||
alreadyDeleted = true;
|
|
||||||
}
|
|
||||||
if (indexFile.files.stream().noneMatch(f -> f.file.equals(entry.getKey()))) {
|
|
||||||
// File has been removed from the index
|
|
||||||
if (!alreadyDeleted) {
|
|
||||||
try {
|
|
||||||
Files.deleteIfExists(Paths.get(opts.packFolder, entry.getValue().cachedLocation));
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO: should this be shown to the user in some way?
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
showCancellationDialog();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ui.submitProgress(new InstallProgress("Comparing new files..."));
|
|
||||||
|
|
||||||
// TODO: progress bar?
|
|
||||||
if (indexFile.files == null || indexFile.files.size() == 0) {
|
|
||||||
System.out.println("Warning: Index is empty!");
|
|
||||||
indexFile.files = new ArrayList<>();
|
|
||||||
}
|
|
||||||
List<DownloadTask> tasks = DownloadTask.createTasksFromIndex(indexFile, indexFile.hashFormat, opts.side);
|
|
||||||
// If the side changes, invalidate EVERYTHING just in case
|
|
||||||
// Might not be needed, but done just to be safe
|
|
||||||
boolean invalidateAll = !opts.side.equals(manifest.cachedSide);
|
|
||||||
if (invalidateAll) {
|
|
||||||
System.out.println("Side changed, invalidating all mods");
|
|
||||||
}
|
|
||||||
tasks.forEach(f -> {
|
|
||||||
// TODO: should linkedfile be checked as well? should this be done in the download section?
|
|
||||||
if (invalidateAll) {
|
|
||||||
f.invalidate();
|
|
||||||
} else if (invalidatedUris.contains(f.metadata.file)) {
|
|
||||||
f.invalidate();
|
|
||||||
}
|
|
||||||
ManifestFile.File file = manifest.cachedFiles.get(f.metadata.file);
|
|
||||||
if (file != null) {
|
|
||||||
// Ensure the file can be reverted later if necessary - the DownloadTask modifies the file so if it fails we need the old version back
|
|
||||||
file.backup();
|
|
||||||
}
|
|
||||||
// If it is null, the DownloadTask will make a new empty cachedFile
|
|
||||||
f.updateFromCache(file);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
showCancellationDialog();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let's hope downloadMetadata is a pure function!!!
|
|
||||||
tasks.parallelStream().forEach(f -> f.downloadMetadata(indexFile, indexUri));
|
|
||||||
|
|
||||||
List<IExceptionDetails> failedTasks = tasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList());
|
|
||||||
if (!failedTasks.isEmpty()) {
|
|
||||||
errorsOccurred = true;
|
|
||||||
IExceptionDetails.ExceptionListResult exceptionListResult;
|
|
||||||
try {
|
|
||||||
exceptionListResult = ui.showExceptions(failedTasks, tasks.size(), true).get();
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
// Interrupted means cancelled???
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (exceptionListResult) {
|
|
||||||
case CONTINUE:
|
|
||||||
break;
|
|
||||||
case CANCEL:
|
|
||||||
cancelled = true;
|
|
||||||
return;
|
|
||||||
case IGNORE:
|
|
||||||
cancelledStartGame = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
showCancellationDialog();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<DownloadTask> nonFailedFirstTasks = tasks.stream().filter(t -> t.getException() == null).collect(Collectors.toList());
|
|
||||||
List<DownloadTask> optionTasks = nonFailedFirstTasks.stream().filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).collect(Collectors.toList());
|
|
||||||
// If options changed, present all options again
|
|
||||||
if (stateHandler.getOptionsButton() || optionTasks.stream().anyMatch(DownloadTask::isNewOptional)) {
|
|
||||||
// new ArrayList is requires so it's an IOptionDetails rather than a DownloadTask list
|
|
||||||
Future<Boolean> cancelledResult = ui.showOptions(new ArrayList<>(optionTasks));
|
|
||||||
try {
|
|
||||||
if (cancelledResult.get()) {
|
|
||||||
cancelled = true;
|
|
||||||
// TODO: Should the UI be closed somehow??
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
// Interrupted means cancelled???
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ui.disableOptionsButton();
|
|
||||||
|
|
||||||
// TODO: different thread pool type?
|
|
||||||
ExecutorService threadPool = Executors.newFixedThreadPool(10);
|
|
||||||
CompletionService<DownloadTask> completionService = new ExecutorCompletionService<>(threadPool);
|
|
||||||
|
|
||||||
tasks.forEach(t -> completionService.submit(() -> {
|
|
||||||
t.download(opts.packFolder, indexUri);
|
|
||||||
return t;
|
|
||||||
}));
|
|
||||||
|
|
||||||
for (int i = 0; i < tasks.size(); i++) {
|
|
||||||
DownloadTask task;
|
|
||||||
try {
|
|
||||||
task = completionService.take().get();
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
ui.handleException(e);
|
|
||||||
task = null;
|
|
||||||
}
|
|
||||||
// Update manifest - If there were no errors cachedFile has already been modified in place (good old pass by reference)
|
|
||||||
if (task != null) {
|
|
||||||
if (task.getException() != null) {
|
|
||||||
ManifestFile.File file = task.cachedFile.getRevert();
|
|
||||||
if (file != null) {
|
|
||||||
manifest.cachedFiles.putIfAbsent(task.metadata.file, file);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// idiot, if it wasn't there in the first place it won't magically appear there
|
|
||||||
manifest.cachedFiles.putIfAbsent(task.metadata.file, task.cachedFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String progress;
|
|
||||||
if (task != null) {
|
|
||||||
if (task.getException() != null) {
|
|
||||||
progress = "Failed to download " + task.metadata.getName() + ": " + task.getException().getMessage();
|
|
||||||
task.getException().printStackTrace();
|
|
||||||
} else {
|
|
||||||
// TODO: should this be revised for tasks that didn't actually download it?
|
|
||||||
progress = "Downloaded " + task.metadata.getName();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
progress = "Failed to download, unknown reason";
|
|
||||||
}
|
|
||||||
ui.submitProgress(new InstallProgress(progress, i + 1, tasks.size()));
|
|
||||||
|
|
||||||
if (stateHandler.getCancelButton()) {
|
|
||||||
// Stop all tasks, don't launch the game (it's in an invalid state!)
|
|
||||||
threadPool.shutdown();
|
|
||||||
cancelled = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shut down the thread pool when the update is done
|
|
||||||
threadPool.shutdown();
|
|
||||||
|
|
||||||
List<IExceptionDetails> failedTasks2ElectricBoogaloo = nonFailedFirstTasks.stream().filter(t -> t.getException() != null).collect(Collectors.toList());
|
|
||||||
if (!failedTasks2ElectricBoogaloo.isEmpty()) {
|
|
||||||
errorsOccurred = true;
|
|
||||||
IExceptionDetails.ExceptionListResult exceptionListResult;
|
|
||||||
try {
|
|
||||||
exceptionListResult = ui.showExceptions(failedTasks2ElectricBoogaloo, tasks.size(), false).get();
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
// Interrupted means cancelled???
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (exceptionListResult) {
|
|
||||||
case CONTINUE:
|
|
||||||
break;
|
|
||||||
case CANCEL:
|
|
||||||
cancelled = true;
|
|
||||||
return;
|
|
||||||
case IGNORE:
|
|
||||||
cancelledStartGame = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showCancellationDialog() {
|
|
||||||
IExceptionDetails.ExceptionListResult exceptionListResult;
|
|
||||||
try {
|
|
||||||
exceptionListResult = ui.showCancellationDialog().get();
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
// Interrupted means cancelled???
|
|
||||||
ui.handleExceptionAndExit(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (exceptionListResult) {
|
|
||||||
case CONTINUE:
|
|
||||||
throw new RuntimeException("Continuation not allowed here!");
|
|
||||||
case CANCEL:
|
|
||||||
cancelled = true;
|
|
||||||
return;
|
|
||||||
case IGNORE:
|
|
||||||
cancelledStartGame = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleCancellation() {
|
|
||||||
if (cancelled) {
|
|
||||||
System.out.println("Update cancelled by user!");
|
|
||||||
System.exit(1);
|
|
||||||
} else if (cancelledStartGame) {
|
|
||||||
System.out.println("Update cancelled by user! Continuing to start game...");
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,29 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata;
|
|
||||||
|
|
||||||
import com.google.gson.TypeAdapter;
|
|
||||||
import com.google.gson.stream.JsonReader;
|
|
||||||
import com.google.gson.stream.JsonToken;
|
|
||||||
import com.google.gson.stream.JsonWriter;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class EfficientBooleanAdapter extends TypeAdapter<Boolean> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(JsonWriter out, Boolean value) throws IOException {
|
|
||||||
if (value == null || !value) {
|
|
||||||
out.nullValue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
out.value(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean read(JsonReader in) throws IOException {
|
|
||||||
if (in.peek() == JsonToken.NULL) {
|
|
||||||
in.nextNull();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return in.nextBoolean();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,105 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata;
|
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
|
||||||
import com.moandjiezana.toml.Toml;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.GeneralHashingSource;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.Hash;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.HashUtils;
|
|
||||||
import link.infra.packwiz.installer.request.HandlerManager;
|
|
||||||
import okio.Okio;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class IndexFile {
|
|
||||||
@SerializedName("hash-format")
|
|
||||||
public String hashFormat;
|
|
||||||
public List<File> files;
|
|
||||||
|
|
||||||
public static class File {
|
|
||||||
public SpaceSafeURI file;
|
|
||||||
@SerializedName("hash-format")
|
|
||||||
public String hashFormat;
|
|
||||||
public String hash;
|
|
||||||
public SpaceSafeURI alias;
|
|
||||||
public boolean metafile;
|
|
||||||
public boolean preserve;
|
|
||||||
|
|
||||||
public transient ModFile linkedFile;
|
|
||||||
public transient SpaceSafeURI linkedFileURI;
|
|
||||||
|
|
||||||
public void downloadMeta(IndexFile parentIndexFile, SpaceSafeURI indexUri) throws Exception {
|
|
||||||
if (!metafile) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (hashFormat == null || hashFormat.length() == 0) {
|
|
||||||
hashFormat = parentIndexFile.hashFormat;
|
|
||||||
}
|
|
||||||
Hash fileHash = HashUtils.getHash(hashFormat, hash);
|
|
||||||
linkedFileURI = HandlerManager.getNewLoc(indexUri, file);
|
|
||||||
Source src = HandlerManager.getFileSource(linkedFileURI);
|
|
||||||
GeneralHashingSource fileStream = HashUtils.getHasher(hashFormat).getHashingSource(src);
|
|
||||||
|
|
||||||
linkedFile = new Toml().read(Okio.buffer(fileStream).inputStream()).to(ModFile.class);
|
|
||||||
if (!fileStream.hashIsEqual(fileHash)) {
|
|
||||||
throw new Exception("Invalid mod file hash");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Source getSource(SpaceSafeURI indexUri) throws Exception {
|
|
||||||
if (metafile) {
|
|
||||||
if (linkedFile == null) {
|
|
||||||
throw new Exception("Linked file doesn't exist!");
|
|
||||||
}
|
|
||||||
return linkedFile.getSource(linkedFileURI);
|
|
||||||
} else {
|
|
||||||
SpaceSafeURI newLoc = HandlerManager.getNewLoc(indexUri, file);
|
|
||||||
if (newLoc == null) {
|
|
||||||
throw new Exception("Index file URI is invalid");
|
|
||||||
}
|
|
||||||
return HandlerManager.getFileSource(newLoc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Hash getHash() throws Exception {
|
|
||||||
if (hash == null) {
|
|
||||||
// TODO: should these be more specific exceptions (e.g. IndexFileException?!)
|
|
||||||
throw new Exception("Index file doesn't have a hash");
|
|
||||||
}
|
|
||||||
if (hashFormat == null) {
|
|
||||||
throw new Exception("Index file doesn't have a hash format");
|
|
||||||
}
|
|
||||||
return HashUtils.getHash(hashFormat, hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
if (metafile) {
|
|
||||||
if (linkedFile != null) {
|
|
||||||
if (linkedFile.name != null) {
|
|
||||||
return linkedFile.name;
|
|
||||||
} else if (linkedFile.filename != null) {
|
|
||||||
return linkedFile.filename;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (file != null) {
|
|
||||||
return Paths.get(file.getPath()).getFileName().toString();
|
|
||||||
}
|
|
||||||
// TODO: throw some kind of exception?
|
|
||||||
return "Invalid file";
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpaceSafeURI getDestURI() {
|
|
||||||
if (alias != null) {
|
|
||||||
return alias;
|
|
||||||
}
|
|
||||||
if (metafile && linkedFile != null) {
|
|
||||||
// TODO: URIs are bad
|
|
||||||
return file.resolve(linkedFile.filename);
|
|
||||||
} else {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,45 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata;
|
|
||||||
|
|
||||||
import com.google.gson.annotations.JsonAdapter;
|
|
||||||
import link.infra.packwiz.installer.UpdateManager;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.Hash;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ManifestFile {
|
|
||||||
public Hash packFileHash = null;
|
|
||||||
public Hash indexFileHash = null;
|
|
||||||
public Map<SpaceSafeURI, File> cachedFiles;
|
|
||||||
// If the side changes, EVERYTHING invalidates. FUN!!!
|
|
||||||
public UpdateManager.Options.Side cachedSide = UpdateManager.Options.Side.CLIENT;
|
|
||||||
|
|
||||||
public static class File {
|
|
||||||
private transient File revert;
|
|
||||||
|
|
||||||
public Hash hash = null;
|
|
||||||
public Hash linkedFileHash = null;
|
|
||||||
public String cachedLocation = null;
|
|
||||||
|
|
||||||
@JsonAdapter(EfficientBooleanAdapter.class)
|
|
||||||
public boolean isOptional = false;
|
|
||||||
public boolean optionValue = true;
|
|
||||||
|
|
||||||
@JsonAdapter(EfficientBooleanAdapter.class)
|
|
||||||
public boolean onlyOtherSide = false;
|
|
||||||
|
|
||||||
// When an error occurs, the state needs to be reverted. To do this, I have a crude revert system.
|
|
||||||
public void backup() {
|
|
||||||
revert = new File();
|
|
||||||
revert.hash = hash;
|
|
||||||
revert.linkedFileHash = linkedFileHash;
|
|
||||||
revert.cachedLocation = cachedLocation;
|
|
||||||
revert.isOptional = isOptional;
|
|
||||||
revert.optionValue = optionValue;
|
|
||||||
revert.onlyOtherSide = onlyOtherSide;
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getRevert() {
|
|
||||||
return revert;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,70 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata;
|
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
|
||||||
import link.infra.packwiz.installer.UpdateManager.Options.Side;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.Hash;
|
|
||||||
import link.infra.packwiz.installer.metadata.hash.HashUtils;
|
|
||||||
import link.infra.packwiz.installer.request.HandlerManager;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ModFile {
|
|
||||||
public String name;
|
|
||||||
public String filename;
|
|
||||||
public Side side;
|
|
||||||
|
|
||||||
public Download download;
|
|
||||||
public static class Download {
|
|
||||||
public SpaceSafeURI url;
|
|
||||||
@SerializedName("hash-format")
|
|
||||||
public String hashFormat;
|
|
||||||
public String hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Object> update;
|
|
||||||
|
|
||||||
public Option option;
|
|
||||||
public static class Option {
|
|
||||||
public boolean optional;
|
|
||||||
public String description;
|
|
||||||
@SerializedName("default")
|
|
||||||
public boolean defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Source getSource(SpaceSafeURI baseLoc) throws Exception {
|
|
||||||
if (download == null) {
|
|
||||||
throw new Exception("Metadata file doesn't have download");
|
|
||||||
}
|
|
||||||
if (download.url == null) {
|
|
||||||
throw new Exception("Metadata file doesn't have a download URI");
|
|
||||||
}
|
|
||||||
SpaceSafeURI newLoc = HandlerManager.getNewLoc(baseLoc, download.url);
|
|
||||||
if (newLoc == null) {
|
|
||||||
throw new Exception("Metadata file URI is invalid");
|
|
||||||
}
|
|
||||||
|
|
||||||
return HandlerManager.getFileSource(newLoc);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Hash getHash() throws Exception {
|
|
||||||
if (download == null) {
|
|
||||||
throw new Exception("Metadata file doesn't have download");
|
|
||||||
}
|
|
||||||
if (download.hash == null) {
|
|
||||||
throw new Exception("Metadata file doesn't have a hash");
|
|
||||||
}
|
|
||||||
if (download.hashFormat == null) {
|
|
||||||
throw new Exception("Metadata file doesn't have a hash format");
|
|
||||||
}
|
|
||||||
return HashUtils.getHash(download.hashFormat, download.hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isOptional() {
|
|
||||||
if (option != null) {
|
|
||||||
return option.optional;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,21 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata;
|
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class PackFile {
|
|
||||||
public String name;
|
|
||||||
|
|
||||||
public IndexFileLoc index;
|
|
||||||
public static class IndexFileLoc {
|
|
||||||
public SpaceSafeURI file;
|
|
||||||
@SerializedName("hash-format")
|
|
||||||
public String hashFormat;
|
|
||||||
public String hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, String> versions;
|
|
||||||
public Map<String, Object> client;
|
|
||||||
public Map<String, Object> server;
|
|
||||||
}
|
|
@@ -1,88 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata;
|
|
||||||
|
|
||||||
import com.google.gson.annotations.JsonAdapter;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
// The world's worst URI wrapper
|
|
||||||
@JsonAdapter(SpaceSafeURIParser.class)
|
|
||||||
public class SpaceSafeURI implements Comparable<SpaceSafeURI>, Serializable {
|
|
||||||
private final URI u;
|
|
||||||
|
|
||||||
public SpaceSafeURI(String str) throws URISyntaxException {
|
|
||||||
u = new URI(str.replace(" ", "%20"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpaceSafeURI(URI uri) {
|
|
||||||
this.u = uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpaceSafeURI(String scheme, String authority, String path, String query, String fragment) throws URISyntaxException {
|
|
||||||
// TODO: do all components need to be replaced?
|
|
||||||
scheme = scheme.replace(" ", "%20");
|
|
||||||
authority = authority.replace(" ", "%20");
|
|
||||||
path = path.replace(" ", "%20");
|
|
||||||
query = query.replace(" ", "%20");
|
|
||||||
fragment = fragment.replace(" ", "%20");
|
|
||||||
u = new URI(scheme, authority, path, query, fragment);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPath() {
|
|
||||||
return u.getPath().replace("%20", " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return u.toString().replace("%20", " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
|
||||||
public SpaceSafeURI resolve(String path) {
|
|
||||||
return new SpaceSafeURI(u.resolve(path.replace(" ", "%20")));
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpaceSafeURI resolve(SpaceSafeURI loc) {
|
|
||||||
return new SpaceSafeURI(u.resolve(loc.u));
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpaceSafeURI relativize(SpaceSafeURI loc) {
|
|
||||||
return new SpaceSafeURI(u.relativize(loc.u));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (obj instanceof SpaceSafeURI) {
|
|
||||||
return u.equals(((SpaceSafeURI) obj).u);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return u.hashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(SpaceSafeURI uri) {
|
|
||||||
return u.compareTo(uri.u);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getScheme() {
|
|
||||||
return u.getScheme();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAuthority() {
|
|
||||||
return u.getAuthority();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getHost() {
|
|
||||||
return u.getHost();
|
|
||||||
}
|
|
||||||
|
|
||||||
public URL toURL() throws MalformedURLException {
|
|
||||||
return u.toURL();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,29 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata;
|
|
||||||
|
|
||||||
import com.google.gson.JsonDeserializationContext;
|
|
||||||
import com.google.gson.JsonDeserializer;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonParseException;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class encodes spaces before parsing the URI, so the URI can actually be
|
|
||||||
* parsed.
|
|
||||||
*/
|
|
||||||
class SpaceSafeURIParser implements JsonDeserializer<SpaceSafeURI> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SpaceSafeURI deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
|
||||||
throws JsonParseException {
|
|
||||||
try {
|
|
||||||
return new SpaceSafeURI(json.getAsString());
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
throw new JsonParseException("Failed to parse URI", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: replace this with a better solution?
|
|
||||||
|
|
||||||
}
|
|
@@ -1,18 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata.hash;
|
|
||||||
|
|
||||||
import okio.ForwardingSource;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
public abstract class GeneralHashingSource extends ForwardingSource {
|
|
||||||
|
|
||||||
public GeneralHashingSource(Source delegate) {
|
|
||||||
super(delegate);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract Hash getHash();
|
|
||||||
|
|
||||||
public boolean hashIsEqual(Object compareTo) {
|
|
||||||
return compareTo.equals(getHash());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,48 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata.hash;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
import com.google.gson.JsonDeserializationContext;
|
|
||||||
import com.google.gson.JsonDeserializer;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonParseException;
|
|
||||||
import com.google.gson.JsonPrimitive;
|
|
||||||
import com.google.gson.JsonSerializationContext;
|
|
||||||
import com.google.gson.JsonSerializer;
|
|
||||||
|
|
||||||
public abstract class Hash {
|
|
||||||
protected abstract String getStringValue();
|
|
||||||
|
|
||||||
protected abstract String getType();
|
|
||||||
|
|
||||||
public static class TypeHandler implements JsonDeserializer<Hash>, JsonSerializer<Hash> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonElement serialize(Hash src, Type typeOfSrc, JsonSerializationContext context) {
|
|
||||||
JsonObject out = new JsonObject();
|
|
||||||
out.add("type", new JsonPrimitive(src.getType()));
|
|
||||||
out.add("value", new JsonPrimitive(src.getStringValue()));
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Hash deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
|
||||||
throws JsonParseException {
|
|
||||||
JsonObject obj = json.getAsJsonObject();
|
|
||||||
String type, value;
|
|
||||||
try {
|
|
||||||
type = obj.get("type").getAsString();
|
|
||||||
value = obj.get("value").getAsString();
|
|
||||||
} catch (NullPointerException e) {
|
|
||||||
throw new JsonParseException("Invalid hash JSON data");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return HashUtils.getHash(type, value);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JsonParseException("Failed to create hash object", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,29 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata.hash;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class HashUtils {
|
|
||||||
private static final Map<String, IHasher> hashTypeConversion = new HashMap<String, IHasher>();
|
|
||||||
static {
|
|
||||||
hashTypeConversion.put("sha256", new HashingSourceHasher("sha256"));
|
|
||||||
hashTypeConversion.put("murmur2", new Murmur2Hasher());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IHasher getHasher(String type) throws Exception {
|
|
||||||
IHasher hasher = hashTypeConversion.get(type);
|
|
||||||
if (hasher == null) {
|
|
||||||
throw new Exception("Hash type not supported: " + type);
|
|
||||||
}
|
|
||||||
return hasher;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Hash getHash(String type, String value) throws Exception {
|
|
||||||
if (hashTypeConversion.containsKey(type)) {
|
|
||||||
return hashTypeConversion.get(type).getHash(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception("Hash type not supported: " + type);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,89 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata.hash;
|
|
||||||
|
|
||||||
import okio.HashingSource;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
public class HashingSourceHasher implements IHasher {
|
|
||||||
private String type;
|
|
||||||
|
|
||||||
HashingSourceHasher(String type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
// i love naming things
|
|
||||||
private class HashingSourceGeneralHashingSource extends GeneralHashingSource {
|
|
||||||
HashingSource delegateHashing;
|
|
||||||
HashingSourceHash value;
|
|
||||||
|
|
||||||
HashingSourceGeneralHashingSource(HashingSource delegate) {
|
|
||||||
super(delegate);
|
|
||||||
delegateHashing = delegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Hash getHash() {
|
|
||||||
if (value == null) {
|
|
||||||
value = new HashingSourceHash(delegateHashing.hash().hex());
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// this some funky inner class stuff
|
|
||||||
// each of these classes is specific to the instance of the HasherHashingSource
|
|
||||||
// therefore HashingSourceHashes from different parent instances will be not instanceof each other
|
|
||||||
private class HashingSourceHash extends Hash {
|
|
||||||
String value;
|
|
||||||
private HashingSourceHash(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (!(obj instanceof HashingSourceHash)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
HashingSourceHash objHash = (HashingSourceHash) obj;
|
|
||||||
if (value != null) {
|
|
||||||
return value.equalsIgnoreCase(objHash.value);
|
|
||||||
} else {
|
|
||||||
return objHash.value == null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return type + ": " + value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getStringValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public GeneralHashingSource getHashingSource(Source delegate) {
|
|
||||||
switch (type) {
|
|
||||||
case "md5":
|
|
||||||
return new HashingSourceGeneralHashingSource(HashingSource.md5(delegate));
|
|
||||||
case "sha256":
|
|
||||||
return new HashingSourceGeneralHashingSource(HashingSource.sha256(delegate));
|
|
||||||
case "sha512":
|
|
||||||
return new HashingSourceGeneralHashingSource(HashingSource.sha512(delegate));
|
|
||||||
}
|
|
||||||
throw new RuntimeException("Invalid hash type provided");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Hash getHash(String value) {
|
|
||||||
return new HashingSourceHash(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,8 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata.hash;
|
|
||||||
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
public interface IHasher {
|
|
||||||
public GeneralHashingSource getHashingSource(Source delegate);
|
|
||||||
public Hash getHash(String value);
|
|
||||||
}
|
|
@@ -1,103 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.metadata.hash;
|
|
||||||
|
|
||||||
import okio.Buffer;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Murmur2Hasher implements IHasher {
|
|
||||||
private class Murmur2GeneralHashingSource extends GeneralHashingSource {
|
|
||||||
Murmur2Hash value;
|
|
||||||
Buffer internalBuffer = new Buffer();
|
|
||||||
Buffer tempBuffer = new Buffer();
|
|
||||||
Source delegate;
|
|
||||||
|
|
||||||
public Murmur2GeneralHashingSource(Source delegate) {
|
|
||||||
super(delegate);
|
|
||||||
this.delegate = delegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long read(Buffer sink, long byteCount) throws IOException {
|
|
||||||
long out = delegate.read(tempBuffer, byteCount);
|
|
||||||
if (out > -1) {
|
|
||||||
sink.write(tempBuffer.clone(), out);
|
|
||||||
internalBuffer.write(tempBuffer, out);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Hash getHash() {
|
|
||||||
if (value == null) {
|
|
||||||
byte[] data = computeNormalizedArray(internalBuffer.readByteArray());
|
|
||||||
value = new Murmur2Hash(Murmur2Lib.hash32(data, data.length, 1));
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Credit to https://github.com/modmuss50/CAV2/blob/master/murmur.go
|
|
||||||
private byte[] computeNormalizedArray(byte[] input) {
|
|
||||||
byte[] output = new byte[input.length];
|
|
||||||
int num = 0;
|
|
||||||
for (byte b : input) {
|
|
||||||
if (!(b == 9 || b == 10 || b == 13 || b == 32)) {
|
|
||||||
output[num] = b;
|
|
||||||
num++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
byte[] outputTrimmed = new byte[num];
|
|
||||||
System.arraycopy(output, 0, outputTrimmed, 0, num);
|
|
||||||
return outputTrimmed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Murmur2Hash extends Hash {
|
|
||||||
int value;
|
|
||||||
private Murmur2Hash(String value) {
|
|
||||||
// Parsing as long then casting to int converts values gt int max value but lt uint max value
|
|
||||||
// into negatives. I presume this is how the murmur2 code handles this.
|
|
||||||
this.value = (int)Long.parseLong(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Murmur2Hash(int value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (!(obj instanceof Murmur2Hash)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Murmur2Hash objHash = (Murmur2Hash) obj;
|
|
||||||
return value == objHash.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "murmur2: " + value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getStringValue() {
|
|
||||||
return Integer.toString(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getType() {
|
|
||||||
return "murmur2";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public GeneralHashingSource getHashingSource(Source delegate) {
|
|
||||||
return new Murmur2GeneralHashingSource(delegate);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Hash getHash(String value) {
|
|
||||||
return new Murmur2Hash(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -74,13 +74,13 @@ public class Murmur2Lib {
|
|||||||
int left = length - len_m;
|
int left = length - len_m;
|
||||||
if (left != 0) {
|
if (left != 0) {
|
||||||
if (left >= 3) {
|
if (left >= 3) {
|
||||||
h ^= (int) data[length - 3] << 16;
|
h ^= (int) data[length - (left - 2)] << 16;
|
||||||
}
|
}
|
||||||
if (left >= 2) {
|
if (left >= 2) {
|
||||||
h ^= (int) data[length - 2] << 8;
|
h ^= (int) data[length - (left - 1)] << 8;
|
||||||
}
|
}
|
||||||
if (left >= 1) {
|
if (left >= 1) {
|
||||||
h ^= (int) data[length - 1];
|
h ^= data[length - left];
|
||||||
}
|
}
|
||||||
|
|
||||||
h *= M_32;
|
h *= M_32;
|
||||||
@@ -152,7 +152,7 @@ public class Murmur2Lib {
|
|||||||
case 2:
|
case 2:
|
||||||
h ^= (long) (data[tailStart + 1] & 0xff) << 8;
|
h ^= (long) (data[tailStart + 1] & 0xff) << 8;
|
||||||
case 1:
|
case 1:
|
||||||
h ^= (long) (data[tailStart] & 0xff);
|
h ^= data[tailStart] & 0xff;
|
||||||
h *= M_64;
|
h *= M_64;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,59 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.request;
|
|
||||||
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
|
||||||
import link.infra.packwiz.installer.request.handlers.RequestHandlerGithub;
|
|
||||||
import link.infra.packwiz.installer.request.handlers.RequestHandlerHTTP;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public abstract class HandlerManager {
|
|
||||||
|
|
||||||
private static List<IRequestHandler> handlers = new ArrayList<>();
|
|
||||||
|
|
||||||
static {
|
|
||||||
handlers.add(new RequestHandlerGithub());
|
|
||||||
handlers.add(new RequestHandlerHTTP());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SpaceSafeURI getNewLoc(SpaceSafeURI base, SpaceSafeURI loc) {
|
|
||||||
if (loc == null) return null;
|
|
||||||
if (base != null) {
|
|
||||||
loc = base.resolve(loc);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (IRequestHandler handler : handlers) {
|
|
||||||
if (handler.matchesHandler(loc)) {
|
|
||||||
return handler.getNewLoc(loc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return loc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: What if files are read multiple times??
|
|
||||||
// Zip handler discards once read, requesting multiple times on other handlers would cause multiple downloads
|
|
||||||
// Caching system? Copy from already downloaded files?
|
|
||||||
|
|
||||||
public static Source getFileSource(SpaceSafeURI loc) throws Exception {
|
|
||||||
for (IRequestHandler handler : handlers) {
|
|
||||||
if (handler.matchesHandler(loc)) {
|
|
||||||
Source src = handler.getFileSource(loc);
|
|
||||||
if (src == null) {
|
|
||||||
throw new Exception("Couldn't find URI: " + loc.toString());
|
|
||||||
} else {
|
|
||||||
return src;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO: specialised exception classes??
|
|
||||||
throw new Exception("No handler available for URI: " + loc.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// github toml resolution
|
|
||||||
// e.g. https://github.com/comp500/Demagnetize -> demagnetize.toml
|
|
||||||
// https://github.com/comp500/Demagnetize/blob/master/demagnetize.toml
|
|
||||||
|
|
||||||
// To handle "progress", just count tasks, rather than individual progress
|
|
||||||
// It'll look bad, especially for zip-based things, but it should work fine
|
|
||||||
}
|
|
@@ -1,90 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.request.handlers;
|
|
||||||
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class RequestHandlerGithub extends RequestHandlerZip {
|
|
||||||
|
|
||||||
public RequestHandlerGithub() {
|
|
||||||
super(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SpaceSafeURI getNewLoc(SpaceSafeURI loc) {
|
|
||||||
return loc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: is caching really needed, if HTTPURLConnection follows redirects correctly?
|
|
||||||
private Map<String, SpaceSafeURI> zipUriMap = new HashMap<>();
|
|
||||||
private final ReentrantReadWriteLock zipUriLock = new ReentrantReadWriteLock();
|
|
||||||
private static Pattern repoMatcherPattern = Pattern.compile("/([\\w.-]+/[\\w.-]+).*");
|
|
||||||
|
|
||||||
private String getRepoName(SpaceSafeURI loc) {
|
|
||||||
Matcher matcher = repoMatcherPattern.matcher(loc.getPath());
|
|
||||||
if (matcher.matches()) {
|
|
||||||
return matcher.group(1);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected SpaceSafeURI getZipUri(SpaceSafeURI loc) throws Exception {
|
|
||||||
String repoName = getRepoName(loc);
|
|
||||||
String branchName = getBranch(loc);
|
|
||||||
zipUriLock.readLock().lock();
|
|
||||||
SpaceSafeURI zipUri = zipUriMap.get(repoName + "/" + branchName);
|
|
||||||
zipUriLock.readLock().unlock();
|
|
||||||
if (zipUri != null) {
|
|
||||||
return zipUri;
|
|
||||||
}
|
|
||||||
|
|
||||||
zipUri = new SpaceSafeURI("https://api.github.com/repos/" + repoName + "/zipball/" + branchName);
|
|
||||||
|
|
||||||
zipUriLock.writeLock().lock();
|
|
||||||
// If another thread sets the value concurrently, use the value of the
|
|
||||||
// thread that first acquired the lock.
|
|
||||||
SpaceSafeURI zipUriInserted = zipUriMap.putIfAbsent(repoName + "/" + branchName, zipUri);
|
|
||||||
if (zipUriInserted != null) {
|
|
||||||
zipUri = zipUriInserted;
|
|
||||||
}
|
|
||||||
zipUriLock.writeLock().unlock();
|
|
||||||
return zipUri;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Pattern branchMatcherPattern = Pattern.compile("/[\\w.-]+/[\\w.-]+/blob/([\\w.-]+).*");
|
|
||||||
|
|
||||||
private String getBranch(SpaceSafeURI loc) {
|
|
||||||
Matcher matcher = branchMatcherPattern.matcher(loc.getPath());
|
|
||||||
if (matcher.matches()) {
|
|
||||||
return matcher.group(1);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected SpaceSafeURI getLocationInZip(SpaceSafeURI loc) throws Exception {
|
|
||||||
String path = "/" + getRepoName(loc) + "/blob/" + getBranch(loc);
|
|
||||||
return new SpaceSafeURI(loc.getScheme(), loc.getAuthority(), path, null, null).relativize(loc);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean matchesHandler(SpaceSafeURI loc) {
|
|
||||||
String scheme = loc.getScheme();
|
|
||||||
if (!("http".equals(scheme) || "https".equals(scheme))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!"github.com".equals(loc.getHost())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// TODO: sanity checks, support for more github urls
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,29 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.request.handlers;
|
|
||||||
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
|
||||||
import link.infra.packwiz.installer.request.IRequestHandler;
|
|
||||||
import okio.Okio;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.net.URLConnection;
|
|
||||||
|
|
||||||
public class RequestHandlerHTTP implements IRequestHandler {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean matchesHandler(SpaceSafeURI loc) {
|
|
||||||
String scheme = loc.getScheme();
|
|
||||||
return "http".equals(scheme) || "https".equals(scheme);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Source getFileSource(SpaceSafeURI loc) throws Exception {
|
|
||||||
URLConnection conn = loc.toURL().openConnection();
|
|
||||||
// TODO: when do we send specific headers??? should there be a way to signal this?
|
|
||||||
// github *sometimes* requires it, sometimes not!
|
|
||||||
//conn.addRequestProperty("Accept", "application/octet-stream");
|
|
||||||
// 30 second read timeout
|
|
||||||
conn.setReadTimeout(30 * 1000);
|
|
||||||
return Okio.source(conn.getInputStream());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,169 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.request.handlers;
|
|
||||||
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
|
||||||
import okio.Buffer;
|
|
||||||
import okio.BufferedSource;
|
|
||||||
import okio.Okio;
|
|
||||||
import okio.Source;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
|
|
||||||
public abstract class RequestHandlerZip extends RequestHandlerHTTP {
|
|
||||||
|
|
||||||
private final boolean modeHasFolder;
|
|
||||||
|
|
||||||
public RequestHandlerZip(boolean modeHasFolder) {
|
|
||||||
this.modeHasFolder = modeHasFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String removeFolder(String name) {
|
|
||||||
if (modeHasFolder) {
|
|
||||||
return name.substring(name.indexOf("/")+1);
|
|
||||||
} else {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ZipReader {
|
|
||||||
|
|
||||||
private final ZipInputStream zis;
|
|
||||||
private final Map<SpaceSafeURI, Buffer> readFiles = new HashMap<>();
|
|
||||||
// Write lock implies access to ZipInputStream - only 1 thread must read at a time!
|
|
||||||
final ReentrantLock filesLock = new ReentrantLock();
|
|
||||||
private ZipEntry entry;
|
|
||||||
|
|
||||||
private final BufferedSource zipSource;
|
|
||||||
|
|
||||||
ZipReader(Source zip) {
|
|
||||||
zis = new ZipInputStream(Okio.buffer(zip).inputStream());
|
|
||||||
zipSource = Okio.buffer(Okio.source(zis));
|
|
||||||
}
|
|
||||||
|
|
||||||
// File lock must be obtained before calling this function
|
|
||||||
private Buffer readCurrFile() throws IOException {
|
|
||||||
Buffer fileBuffer = new Buffer();
|
|
||||||
zipSource.readFully(fileBuffer, entry.getSize());
|
|
||||||
return fileBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// File lock must be obtained before calling this function
|
|
||||||
private Buffer findFile(SpaceSafeURI loc) throws IOException, URISyntaxException {
|
|
||||||
while (true) {
|
|
||||||
entry = zis.getNextEntry();
|
|
||||||
if (entry == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Buffer data = readCurrFile();
|
|
||||||
SpaceSafeURI fileLoc = new SpaceSafeURI(removeFolder(entry.getName()));
|
|
||||||
if (loc.equals(fileLoc)) {
|
|
||||||
return data;
|
|
||||||
} else {
|
|
||||||
readFiles.put(fileLoc, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Source getFileSource(SpaceSafeURI loc) throws Exception {
|
|
||||||
filesLock.lock();
|
|
||||||
// Assume files are only read once, allow GC by removing
|
|
||||||
Buffer file = readFiles.remove(loc);
|
|
||||||
if (file != null) {
|
|
||||||
filesLock.unlock();
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
file = findFile(loc);
|
|
||||||
filesLock.unlock();
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
SpaceSafeURI findInZip(Predicate<SpaceSafeURI> matches) throws Exception {
|
|
||||||
filesLock.lock();
|
|
||||||
for (SpaceSafeURI file : readFiles.keySet()) {
|
|
||||||
if (matches.test(file)) {
|
|
||||||
filesLock.unlock();
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
entry = zis.getNextEntry();
|
|
||||||
if (entry == null) {
|
|
||||||
filesLock.unlock();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Buffer data = readCurrFile();
|
|
||||||
SpaceSafeURI fileLoc = new SpaceSafeURI(removeFolder(entry.getName()));
|
|
||||||
readFiles.put(fileLoc, data);
|
|
||||||
if (matches.test(fileLoc)) {
|
|
||||||
filesLock.unlock();
|
|
||||||
return fileLoc;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Map<SpaceSafeURI, ZipReader> cache = new HashMap<>();
|
|
||||||
private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock();
|
|
||||||
|
|
||||||
protected abstract SpaceSafeURI getZipUri(SpaceSafeURI loc) throws Exception;
|
|
||||||
|
|
||||||
protected abstract SpaceSafeURI getLocationInZip(SpaceSafeURI loc) throws Exception;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract boolean matchesHandler(SpaceSafeURI loc);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Source getFileSource(SpaceSafeURI loc) throws Exception {
|
|
||||||
SpaceSafeURI zipUri = getZipUri(loc);
|
|
||||||
cacheLock.readLock().lock();
|
|
||||||
ZipReader zr = cache.get(zipUri);
|
|
||||||
cacheLock.readLock().unlock();
|
|
||||||
if (zr == null) {
|
|
||||||
cacheLock.writeLock().lock();
|
|
||||||
// Recheck, because unlocking read lock allows another thread to modify it
|
|
||||||
zr = cache.get(zipUri);
|
|
||||||
if (zr == null) {
|
|
||||||
Source src = super.getFileSource(zipUri);
|
|
||||||
if (src == null) {
|
|
||||||
cacheLock.writeLock().unlock();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
zr = new ZipReader(src);
|
|
||||||
cache.put(zipUri, zr);
|
|
||||||
}
|
|
||||||
cacheLock.writeLock().unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
return zr.getFileSource(getLocationInZip(loc));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected SpaceSafeURI findInZip(SpaceSafeURI loc, Predicate<SpaceSafeURI> matches) throws Exception {
|
|
||||||
SpaceSafeURI zipUri = getZipUri(loc);
|
|
||||||
cacheLock.readLock().lock();
|
|
||||||
ZipReader zr = cache.get(zipUri);
|
|
||||||
cacheLock.readLock().unlock();
|
|
||||||
if (zr == null) {
|
|
||||||
cacheLock.writeLock().lock();
|
|
||||||
// Recheck, because unlocking read lock allows another thread to modify it
|
|
||||||
zr = cache.get(zipUri);
|
|
||||||
if (zr == null) {
|
|
||||||
zr = new ZipReader(super.getFileSource(zipUri));
|
|
||||||
cache.put(zipUri, zr);
|
|
||||||
}
|
|
||||||
cacheLock.writeLock().unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
return zr.findInZip(matches);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,55 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
public class CLIHandler implements IUserInterface {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleException(Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void show(InputStateHandler h) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void submitProgress(InstallProgress progress) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
if (progress.hasProgress) {
|
|
||||||
sb.append('(');
|
|
||||||
sb.append(progress.progress);
|
|
||||||
sb.append('/');
|
|
||||||
sb.append(progress.progressTotal);
|
|
||||||
sb.append(") ");
|
|
||||||
}
|
|
||||||
sb.append(progress.message);
|
|
||||||
System.out.println(sb.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executeManager(Runnable task) {
|
|
||||||
task.run();
|
|
||||||
System.out.println("Finished successfully!");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Future<Boolean> showOptions(List<IOptionDetails> options) {
|
|
||||||
for (IOptionDetails opt : options) {
|
|
||||||
opt.setOptionValue(true);
|
|
||||||
System.out.println("Warning: accepting option " + opt.getName() + " as option choosing is not implemented in the CLI");
|
|
||||||
}
|
|
||||||
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
|
||||||
future.complete(false); // Can't be cancelled!
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal, boolean allowsIgnore) {
|
|
||||||
CompletableFuture<IExceptionDetails.ExceptionListResult> future = new CompletableFuture<>();
|
|
||||||
future.complete(IExceptionDetails.ExceptionListResult.CANCEL);
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,183 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
import link.infra.packwiz.installer.ui.IExceptionDetails.ExceptionListResult;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.WindowAdapter;
|
|
||||||
import java.awt.event.WindowEvent;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
class ExceptionListWindow extends JDialog {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private final JTextArea lblExceptionStacktrace;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the dialog.
|
|
||||||
*/
|
|
||||||
ExceptionListWindow(List<IExceptionDetails> eList, CompletableFuture<ExceptionListResult> future, int numTotal, boolean allowsIgnore, JFrame parentWindow) {
|
|
||||||
super(parentWindow, "Failed file downloads", true);
|
|
||||||
|
|
||||||
setBounds(100, 100, 540, 340);
|
|
||||||
setLocationRelativeTo(parentWindow);
|
|
||||||
getContentPane().setLayout(new BorderLayout());
|
|
||||||
{
|
|
||||||
JPanel errorPanel = new JPanel();
|
|
||||||
getContentPane().add(errorPanel, BorderLayout.NORTH);
|
|
||||||
{
|
|
||||||
JLabel lblWarning = new JLabel("One or more errors were encountered while installing the modpack!");
|
|
||||||
lblWarning.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
|
|
||||||
errorPanel.add(lblWarning);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
JPanel contentPanel = new JPanel();
|
|
||||||
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
||||||
getContentPane().add(contentPanel, BorderLayout.CENTER);
|
|
||||||
contentPanel.setLayout(new BorderLayout(0, 0));
|
|
||||||
{
|
|
||||||
JSplitPane splitPane = new JSplitPane();
|
|
||||||
splitPane.setResizeWeight(0.3);
|
|
||||||
contentPanel.add(splitPane);
|
|
||||||
{
|
|
||||||
lblExceptionStacktrace = new JTextArea("Select a file");
|
|
||||||
lblExceptionStacktrace.setBackground(UIManager.getColor("List.background"));
|
|
||||||
lblExceptionStacktrace.setOpaque(true);
|
|
||||||
lblExceptionStacktrace.setWrapStyleWord(true);
|
|
||||||
lblExceptionStacktrace.setLineWrap(true);
|
|
||||||
lblExceptionStacktrace.setEditable(false);
|
|
||||||
lblExceptionStacktrace.setFocusable(true);
|
|
||||||
lblExceptionStacktrace.setFont(UIManager.getFont("Label.font"));
|
|
||||||
lblExceptionStacktrace.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
||||||
JScrollPane scrollPane = new JScrollPane(lblExceptionStacktrace);
|
|
||||||
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
|
|
||||||
splitPane.setRightComponent(scrollPane);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
JList<String> list = new JList<>();
|
|
||||||
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
||||||
list.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
||||||
ExceptionListModel listModel = new ExceptionListModel(eList);
|
|
||||||
list.setModel(listModel);
|
|
||||||
list.addListSelectionListener(e -> {
|
|
||||||
int i = list.getSelectedIndex();
|
|
||||||
if (i > -1) {
|
|
||||||
StringWriter sw = new StringWriter();
|
|
||||||
listModel.getExceptionAt(i).printStackTrace(new PrintWriter(sw));
|
|
||||||
lblExceptionStacktrace.setText(sw.toString());
|
|
||||||
// Scroll to the top
|
|
||||||
lblExceptionStacktrace.setCaretPosition(0);
|
|
||||||
} else {
|
|
||||||
lblExceptionStacktrace.setText("Select a file");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
JScrollPane scrollPane = new JScrollPane(list);
|
|
||||||
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
|
|
||||||
splitPane.setLeftComponent(scrollPane);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
JPanel buttonPane = new JPanel();
|
|
||||||
getContentPane().add(buttonPane, BorderLayout.SOUTH);
|
|
||||||
buttonPane.setLayout(new BorderLayout(0, 0));
|
|
||||||
{
|
|
||||||
JPanel rightButtons = new JPanel();
|
|
||||||
buttonPane.add(rightButtons, BorderLayout.EAST);
|
|
||||||
{
|
|
||||||
JButton btnContinue = new JButton("Continue");
|
|
||||||
btnContinue.setToolTipText("Attempt to continue installing, excluding the failed downloads");
|
|
||||||
btnContinue.addActionListener(e -> {
|
|
||||||
future.complete(ExceptionListResult.CONTINUE);
|
|
||||||
ExceptionListWindow.this.dispose();
|
|
||||||
});
|
|
||||||
rightButtons.add(btnContinue);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
JButton btnCancelLaunch = new JButton("Cancel launch");
|
|
||||||
btnCancelLaunch.setToolTipText("Stop launching the game");
|
|
||||||
btnCancelLaunch.addActionListener(e -> {
|
|
||||||
future.complete(ExceptionListResult.CANCEL);
|
|
||||||
ExceptionListWindow.this.dispose();
|
|
||||||
});
|
|
||||||
rightButtons.add(btnCancelLaunch);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
JButton btnIgnoreUpdate = new JButton("Ignore update");
|
|
||||||
btnIgnoreUpdate.setEnabled(allowsIgnore);
|
|
||||||
btnIgnoreUpdate.setToolTipText("Start the game without attempting to update");
|
|
||||||
btnIgnoreUpdate.addActionListener(e -> {
|
|
||||||
future.complete(ExceptionListResult.IGNORE);
|
|
||||||
ExceptionListWindow.this.dispose();
|
|
||||||
});
|
|
||||||
rightButtons.add(btnIgnoreUpdate);
|
|
||||||
{
|
|
||||||
JLabel lblErrored = new JLabel(eList.size() + "/" + numTotal + " errored");
|
|
||||||
lblErrored.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
buttonPane.add(lblErrored, BorderLayout.CENTER);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
JPanel leftButtons = new JPanel();
|
|
||||||
buttonPane.add(leftButtons, BorderLayout.WEST);
|
|
||||||
{
|
|
||||||
JButton btnReportIssue = new JButton("Report issue");
|
|
||||||
boolean supported = Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE);
|
|
||||||
btnReportIssue.setEnabled(supported);
|
|
||||||
if (supported) {
|
|
||||||
btnReportIssue.addActionListener(e -> {
|
|
||||||
try {
|
|
||||||
Desktop.getDesktop().browse(new URI("https://github.com/comp500/packwiz-installer/issues/new"));
|
|
||||||
} catch (IOException | URISyntaxException e1) {
|
|
||||||
// lol the button just won't work i guess
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
leftButtons.add(btnReportIssue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addWindowListener(new WindowAdapter() {
|
|
||||||
@Override
|
|
||||||
public void windowClosing(WindowEvent e) {
|
|
||||||
future.complete(ExceptionListResult.CANCEL);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void windowClosed(WindowEvent e) {
|
|
||||||
// Just in case closing didn't get triggered - if something else called dispose() the
|
|
||||||
// future will have already completed
|
|
||||||
future.complete(ExceptionListResult.CANCEL);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ExceptionListModel extends AbstractListModel<String> {
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private final List<IExceptionDetails> details;
|
|
||||||
|
|
||||||
ExceptionListModel(List<IExceptionDetails> details) {
|
|
||||||
this.details = details;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize() {
|
|
||||||
return details.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getElementAt(int index) {
|
|
||||||
return details.get(index).getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
Exception getExceptionAt(int index) {
|
|
||||||
return details.get(index).getException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,10 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
public interface IExceptionDetails {
|
|
||||||
Exception getException();
|
|
||||||
String getName();
|
|
||||||
|
|
||||||
enum ExceptionListResult {
|
|
||||||
CONTINUE, CANCEL, IGNORE
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,8 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
public interface IOptionDetails {
|
|
||||||
String getName();
|
|
||||||
boolean getOptionValue();
|
|
||||||
String getOptionDescription();
|
|
||||||
void setOptionValue(boolean value);
|
|
||||||
}
|
|
@@ -1,38 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
public interface IUserInterface {
|
|
||||||
|
|
||||||
void show(InputStateHandler handler);
|
|
||||||
|
|
||||||
void handleException(Exception e);
|
|
||||||
|
|
||||||
default void handleExceptionAndExit(Exception e) {
|
|
||||||
handleException(e);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
default void setTitle(String title) {}
|
|
||||||
|
|
||||||
void submitProgress(InstallProgress progress);
|
|
||||||
|
|
||||||
void executeManager(Runnable task);
|
|
||||||
|
|
||||||
// Return true if the installation was cancelled!
|
|
||||||
Future<Boolean> showOptions(List<IOptionDetails> option);
|
|
||||||
|
|
||||||
Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal, boolean allowsIgnore);
|
|
||||||
|
|
||||||
default void disableOptionsButton() {}
|
|
||||||
|
|
||||||
// Should not return CONTINUE
|
|
||||||
default Future<IExceptionDetails.ExceptionListResult> showCancellationDialog() {
|
|
||||||
CompletableFuture<IExceptionDetails.ExceptionListResult> future = new CompletableFuture<>();
|
|
||||||
future.complete(IExceptionDetails.ExceptionListResult.CANCEL);
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,22 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
public class InputStateHandler {
|
|
||||||
private boolean optionsButtonPressed = false;
|
|
||||||
private boolean cancelButtonPressed = false;
|
|
||||||
|
|
||||||
synchronized void pressCancelButton() {
|
|
||||||
this.cancelButtonPressed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized void pressOptionsButton() {
|
|
||||||
this.optionsButtonPressed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized boolean getCancelButton() {
|
|
||||||
return cancelButtonPressed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized boolean getOptionsButton() {
|
|
||||||
return optionsButtonPressed;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,22 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
public class InstallProgress {
|
|
||||||
public final String message;
|
|
||||||
public final boolean hasProgress;
|
|
||||||
public final int progress;
|
|
||||||
public final int progressTotal;
|
|
||||||
|
|
||||||
public InstallProgress(String message) {
|
|
||||||
this.message = message;
|
|
||||||
hasProgress = false;
|
|
||||||
progress = 0;
|
|
||||||
progressTotal = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public InstallProgress(String message, int progress, int progressTotal) {
|
|
||||||
this.message = message;
|
|
||||||
hasProgress = true;
|
|
||||||
this.progress = progress;
|
|
||||||
this.progressTotal = progressTotal;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,228 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
public class InstallWindow implements IUserInterface {
|
|
||||||
|
|
||||||
private JFrame frmPackwizlauncher;
|
|
||||||
private JLabel lblProgresslabel;
|
|
||||||
private JProgressBar progressBar;
|
|
||||||
private InputStateHandler inputStateHandler;
|
|
||||||
|
|
||||||
private String title = "Updating modpack...";
|
|
||||||
private SwingWorkerButWithPublicPublish<Void, InstallProgress> worker;
|
|
||||||
private AtomicBoolean aboutToCrash = new AtomicBoolean();
|
|
||||||
private JButton btnOptions;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void show(InputStateHandler handler) {
|
|
||||||
this.inputStateHandler = handler;
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
try {
|
|
||||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
||||||
initialize();
|
|
||||||
frmPackwizlauncher.setVisible(true);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the contents of the frame.
|
|
||||||
* @wbp.parser.entryPoint
|
|
||||||
*/
|
|
||||||
private void initialize() {
|
|
||||||
frmPackwizlauncher = new JFrame();
|
|
||||||
frmPackwizlauncher.setTitle(title);
|
|
||||||
frmPackwizlauncher.setBounds(100, 100, 493, 95);
|
|
||||||
frmPackwizlauncher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
frmPackwizlauncher.setLocationRelativeTo(null);
|
|
||||||
|
|
||||||
JPanel panel = new JPanel();
|
|
||||||
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
|
||||||
frmPackwizlauncher.getContentPane().add(panel, BorderLayout.CENTER);
|
|
||||||
panel.setLayout(new BorderLayout(0, 0));
|
|
||||||
|
|
||||||
progressBar = new JProgressBar();
|
|
||||||
progressBar.setIndeterminate(true);
|
|
||||||
panel.add(progressBar, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
lblProgresslabel = new JLabel("Loading...");
|
|
||||||
panel.add(lblProgresslabel, BorderLayout.SOUTH);
|
|
||||||
|
|
||||||
JPanel panel_1 = new JPanel();
|
|
||||||
panel_1.setBorder(new EmptyBorder(0, 5, 0, 5));
|
|
||||||
frmPackwizlauncher.getContentPane().add(panel_1, BorderLayout.EAST);
|
|
||||||
GridBagLayout gbl_panel_1 = new GridBagLayout();
|
|
||||||
panel_1.setLayout(gbl_panel_1);
|
|
||||||
|
|
||||||
btnOptions = new JButton("Optional mods...");
|
|
||||||
btnOptions.addActionListener(e -> {
|
|
||||||
btnOptions.setText("Loading...");
|
|
||||||
btnOptions.setEnabled(false);
|
|
||||||
inputStateHandler.pressOptionsButton();
|
|
||||||
});
|
|
||||||
btnOptions.setAlignmentX(Component.CENTER_ALIGNMENT);
|
|
||||||
GridBagConstraints gbc_btnOptions = new GridBagConstraints();
|
|
||||||
gbc_btnOptions.gridx = 0;
|
|
||||||
gbc_btnOptions.gridy = 0;
|
|
||||||
panel_1.add(btnOptions, gbc_btnOptions);
|
|
||||||
|
|
||||||
JButton btnCancel = new JButton("Cancel");
|
|
||||||
btnCancel.addActionListener(e -> {
|
|
||||||
btnCancel.setEnabled(false);
|
|
||||||
inputStateHandler.pressCancelButton();
|
|
||||||
});
|
|
||||||
btnCancel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
|
||||||
GridBagConstraints gbc_btnCancel = new GridBagConstraints();
|
|
||||||
gbc_btnCancel.gridx = 0;
|
|
||||||
gbc_btnCancel.gridy = 1;
|
|
||||||
panel_1.add(btnCancel, gbc_btnCancel);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleException(Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
JOptionPane.showMessageDialog(null, "An error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleExceptionAndExit(Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
// Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet
|
|
||||||
aboutToCrash.set(true);
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
|
|
||||||
System.exit(1);
|
|
||||||
});
|
|
||||||
// Pause forever, so it blocks while we wait for System.exit to take effect
|
|
||||||
try {
|
|
||||||
Thread.currentThread().join();
|
|
||||||
} catch (InterruptedException ex) {
|
|
||||||
// no u
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
if (frmPackwizlauncher != null) {
|
|
||||||
EventQueue.invokeLater(() -> frmPackwizlauncher.setTitle(title));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void submitProgress(InstallProgress progress) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
if (progress.hasProgress) {
|
|
||||||
sb.append('(');
|
|
||||||
sb.append(progress.progress);
|
|
||||||
sb.append('/');
|
|
||||||
sb.append(progress.progressTotal);
|
|
||||||
sb.append(") ");
|
|
||||||
}
|
|
||||||
sb.append(progress.message);
|
|
||||||
// TODO: better logging library?
|
|
||||||
System.out.println(sb.toString());
|
|
||||||
if (worker != null) {
|
|
||||||
worker.publishPublic(progress);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executeManager(Runnable task) {
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
worker = new SwingWorkerButWithPublicPublish<Void, InstallProgress>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground() {
|
|
||||||
task.run();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void process(List<InstallProgress> chunks) {
|
|
||||||
// Only process last chunk
|
|
||||||
if (chunks.size() > 0) {
|
|
||||||
InstallProgress prog = chunks.get(chunks.size() - 1);
|
|
||||||
if (prog.hasProgress) {
|
|
||||||
progressBar.setIndeterminate(false);
|
|
||||||
progressBar.setValue(prog.progress);
|
|
||||||
progressBar.setMaximum(prog.progressTotal);
|
|
||||||
} else {
|
|
||||||
progressBar.setIndeterminate(true);
|
|
||||||
progressBar.setValue(0);
|
|
||||||
}
|
|
||||||
lblProgresslabel.setText(prog.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void done() {
|
|
||||||
if (aboutToCrash.get()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// TODO: a better way to do this?
|
|
||||||
frmPackwizlauncher.dispose();
|
|
||||||
System.out.println("Finished successfully!");
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
worker.execute();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Future<Boolean> showOptions(List<IOptionDetails> opts) {
|
|
||||||
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
OptionsSelectWindow dialog = new OptionsSelectWindow(opts, future, frmPackwizlauncher);
|
|
||||||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
|
||||||
dialog.setVisible(true);
|
|
||||||
});
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Future<IExceptionDetails.ExceptionListResult> showExceptions(List<IExceptionDetails> opts, int numTotal, boolean allowsIgnore) {
|
|
||||||
CompletableFuture<IExceptionDetails.ExceptionListResult> future = new CompletableFuture<>();
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
ExceptionListWindow dialog = new ExceptionListWindow(opts, future, numTotal, allowsIgnore, frmPackwizlauncher);
|
|
||||||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
|
||||||
dialog.setVisible(true);
|
|
||||||
});
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void disableOptionsButton() {
|
|
||||||
if (btnOptions != null) {
|
|
||||||
btnOptions.setText("Optional mods...");
|
|
||||||
btnOptions.setEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Future<IExceptionDetails.ExceptionListResult> showCancellationDialog() {
|
|
||||||
CompletableFuture<IExceptionDetails.ExceptionListResult> future = new CompletableFuture<>();
|
|
||||||
EventQueue.invokeLater(() -> {
|
|
||||||
Object[] buttons = {"Quit", "Ignore"};
|
|
||||||
int result = JOptionPane.showOptionDialog(frmPackwizlauncher,
|
|
||||||
"The installation was cancelled. Would you like to quit the game, or ignore the update and start the game?",
|
|
||||||
"Cancelled installation",
|
|
||||||
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]);
|
|
||||||
future.complete(result == 0 ? IExceptionDetails.ExceptionListResult.CANCEL : IExceptionDetails.ExceptionListResult.IGNORE);
|
|
||||||
});
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,33 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
// Serves as a proxy for IOptionDetails, so that setOptionValue isn't called until OK is clicked
|
|
||||||
class OptionTempHandler implements IOptionDetails {
|
|
||||||
private final IOptionDetails opt;
|
|
||||||
private boolean tempValue;
|
|
||||||
|
|
||||||
OptionTempHandler(IOptionDetails opt) {
|
|
||||||
this.opt = opt;
|
|
||||||
tempValue = opt.getOptionValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return opt.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getOptionDescription() {
|
|
||||||
return opt.getOptionDescription();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getOptionValue() {
|
|
||||||
return tempValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOptionValue(boolean value) {
|
|
||||||
tempValue = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void finalise() {
|
|
||||||
opt.setOptionValue(tempValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,205 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
import javax.swing.event.ListSelectionEvent;
|
|
||||||
import javax.swing.event.ListSelectionListener;
|
|
||||||
import javax.swing.event.TableModelListener;
|
|
||||||
import javax.swing.table.TableModel;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.awt.event.WindowAdapter;
|
|
||||||
import java.awt.event.WindowEvent;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
public class OptionsSelectWindow extends JDialog implements ActionListener {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private final JTextArea lblOptionDescription;
|
|
||||||
private final OptionTableModel tableModel;
|
|
||||||
private final CompletableFuture<Boolean> future;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the dialog.
|
|
||||||
*/
|
|
||||||
OptionsSelectWindow(List<IOptionDetails> optList, CompletableFuture<Boolean> future, JFrame parentWindow) {
|
|
||||||
super(parentWindow, "Select optional mods...", true);
|
|
||||||
|
|
||||||
tableModel = new OptionTableModel(optList);
|
|
||||||
this.future = future;
|
|
||||||
|
|
||||||
setBounds(100, 100, 450, 300);
|
|
||||||
setLocationRelativeTo(parentWindow);
|
|
||||||
getContentPane().setLayout(new BorderLayout());
|
|
||||||
JPanel contentPanel = new JPanel();
|
|
||||||
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
||||||
getContentPane().add(contentPanel, BorderLayout.CENTER);
|
|
||||||
contentPanel.setLayout(new BorderLayout(0, 0));
|
|
||||||
{
|
|
||||||
JSplitPane splitPane = new JSplitPane();
|
|
||||||
splitPane.setResizeWeight(0.5);
|
|
||||||
contentPanel.add(splitPane);
|
|
||||||
{
|
|
||||||
JTable table = new JTable();
|
|
||||||
table.setShowVerticalLines(false);
|
|
||||||
table.setShowHorizontalLines(false);
|
|
||||||
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
||||||
table.setShowGrid(false);
|
|
||||||
table.setModel(tableModel);
|
|
||||||
table.getColumnModel().getColumn(0).setResizable(false);
|
|
||||||
table.getColumnModel().getColumn(0).setPreferredWidth(15);
|
|
||||||
table.getColumnModel().getColumn(0).setMaxWidth(15);
|
|
||||||
table.getColumnModel().getColumn(1).setResizable(false);
|
|
||||||
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
|
||||||
@Override
|
|
||||||
public void valueChanged(ListSelectionEvent e) {
|
|
||||||
int i = table.getSelectedRow();
|
|
||||||
if (i > -1) {
|
|
||||||
lblOptionDescription.setText(tableModel.getDescription(i));
|
|
||||||
} else {
|
|
||||||
lblOptionDescription.setText("Select an option...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
table.setTableHeader(null);
|
|
||||||
JScrollPane scrollPane = new JScrollPane(table);
|
|
||||||
scrollPane.getViewport().setBackground(UIManager.getColor("List.background"));
|
|
||||||
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
|
||||||
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
|
|
||||||
splitPane.setLeftComponent(scrollPane);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
lblOptionDescription = new JTextArea("Select an option...");
|
|
||||||
lblOptionDescription.setBackground(UIManager.getColor("List.background"));
|
|
||||||
lblOptionDescription.setOpaque(true);
|
|
||||||
lblOptionDescription.setWrapStyleWord(true);
|
|
||||||
lblOptionDescription.setLineWrap(true);
|
|
||||||
lblOptionDescription.setEditable(false);
|
|
||||||
lblOptionDescription.setFocusable(false);
|
|
||||||
lblOptionDescription.setFont(UIManager.getFont("Label.font"));
|
|
||||||
lblOptionDescription.setBorder(new EmptyBorder(10, 10, 10, 10));
|
|
||||||
JScrollPane scrollPane = new JScrollPane(lblOptionDescription);
|
|
||||||
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
|
|
||||||
splitPane.setRightComponent(scrollPane);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
JPanel buttonPane = new JPanel();
|
|
||||||
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
|
||||||
getContentPane().add(buttonPane, BorderLayout.SOUTH);
|
|
||||||
{
|
|
||||||
JButton okButton = new JButton("OK");
|
|
||||||
okButton.setActionCommand("OK");
|
|
||||||
okButton.addActionListener(this);
|
|
||||||
buttonPane.add(okButton);
|
|
||||||
getRootPane().setDefaultButton(okButton);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
JButton cancelButton = new JButton("Cancel");
|
|
||||||
cancelButton.setActionCommand("Cancel");
|
|
||||||
cancelButton.addActionListener(this);
|
|
||||||
buttonPane.add(cancelButton);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addWindowListener(new WindowAdapter() {
|
|
||||||
@Override
|
|
||||||
public void windowClosing(WindowEvent e) {
|
|
||||||
future.complete(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void windowClosed(WindowEvent e) {
|
|
||||||
// Just in case closing didn't get triggered - if something else called dispose() the
|
|
||||||
// future will have already completed
|
|
||||||
future.complete(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class OptionTableModel implements TableModel {
|
|
||||||
private List<OptionTempHandler> opts = new ArrayList<>();
|
|
||||||
|
|
||||||
OptionTableModel(List<IOptionDetails> givenOpts) {
|
|
||||||
for (IOptionDetails opt : givenOpts) {
|
|
||||||
opts.add(new OptionTempHandler(opt));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getRowCount() {
|
|
||||||
return opts.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getColumnCount() {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
private final String[] columnNames = {"Enabled", "Mod name"};
|
|
||||||
private final Class<?>[] columnTypes = {Boolean.class, String.class};
|
|
||||||
private final boolean[] columnEditables = {true, false};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getColumnName(int columnIndex) {
|
|
||||||
return columnNames[columnIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<?> getColumnClass(int columnIndex) {
|
|
||||||
return columnTypes[columnIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isCellEditable(int rowIndex, int columnIndex) {
|
|
||||||
return columnEditables[columnIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getValueAt(int rowIndex, int columnIndex) {
|
|
||||||
OptionTempHandler opt = opts.get(rowIndex);
|
|
||||||
return columnIndex == 0 ? opt.getOptionValue() : opt.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
|
||||||
if (columnIndex == 0) {
|
|
||||||
OptionTempHandler opt = opts.get(rowIndex);
|
|
||||||
opt.setOptionValue((boolean) aValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Noop, the table model doesn't change!
|
|
||||||
@Override
|
|
||||||
public void addTableModelListener(TableModelListener l) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeTableModelListener(TableModelListener l) {}
|
|
||||||
|
|
||||||
String getDescription(int rowIndex) {
|
|
||||||
return opts.get(rowIndex).getOptionDescription();
|
|
||||||
}
|
|
||||||
|
|
||||||
void finalise() {
|
|
||||||
for (OptionTempHandler opt : opts) {
|
|
||||||
opt.finalise();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (e.getActionCommand().equals("OK")) {
|
|
||||||
tableModel.finalise();
|
|
||||||
future.complete(false);
|
|
||||||
dispose();
|
|
||||||
} else if (e.getActionCommand().equals("Cancel")) {
|
|
||||||
future.complete(true);
|
|
||||||
dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,13 +0,0 @@
|
|||||||
package link.infra.packwiz.installer.ui;
|
|
||||||
|
|
||||||
import javax.swing.SwingWorker;
|
|
||||||
|
|
||||||
// Q: AAA WHAT HAVE YOU DONE THIS IS DISGUSTING
|
|
||||||
// A: it just makes things easier, so i can easily have one interface for CLI/GUI
|
|
||||||
// if someone has a better way to do this please PR it
|
|
||||||
public abstract class SwingWorkerButWithPublicPublish<T,V> extends SwingWorker<T,V> {
|
|
||||||
@SafeVarargs
|
|
||||||
public final void publishPublic(V... chunks) {
|
|
||||||
publish(chunks);
|
|
||||||
}
|
|
||||||
}
|
|
5
src/main/kotlin/link/infra/packwiz/installer/DevMain.kt
Normal file
5
src/main/kotlin/link/infra/packwiz/installer/DevMain.kt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package link.infra.packwiz.installer
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
Main(args)
|
||||||
|
}
|
303
src/main/kotlin/link/infra/packwiz/installer/DownloadTask.kt
Normal file
303
src/main/kotlin/link/infra/packwiz/installer/DownloadTask.kt
Normal file
@@ -0,0 +1,303 @@
|
|||||||
|
package link.infra.packwiz.installer
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.IndexFile
|
||||||
|
import link.infra.packwiz.installer.metadata.ManifestFile
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils.getHash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils.getHasher
|
||||||
|
import link.infra.packwiz.installer.target.Side
|
||||||
|
import link.infra.packwiz.installer.ui.data.ExceptionDetails
|
||||||
|
import link.infra.packwiz.installer.ui.data.IOptionDetails
|
||||||
|
import link.infra.packwiz.installer.util.Log
|
||||||
|
import okio.*
|
||||||
|
import okio.Path.Companion.toOkioPath
|
||||||
|
import java.io.IOException
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Paths
|
||||||
|
import java.nio.file.StandardCopyOption
|
||||||
|
import java.util.*
|
||||||
|
import kotlin.io.use
|
||||||
|
|
||||||
|
internal class DownloadTask private constructor(val metadata: IndexFile.File, defaultFormat: String, private val downloadSide: Side) : IOptionDetails {
|
||||||
|
var cachedFile: ManifestFile.File? = null
|
||||||
|
|
||||||
|
private var err: Exception? = null
|
||||||
|
val exceptionDetails get() = err?.let { e -> ExceptionDetails(name, e) }
|
||||||
|
|
||||||
|
fun failed() = err != null
|
||||||
|
|
||||||
|
var alreadyUpToDate = false
|
||||||
|
private var metadataRequired = true
|
||||||
|
private var invalidated = false
|
||||||
|
// If file is new or isOptional changed to true, the option needs to be presented again
|
||||||
|
private var newOptional = true
|
||||||
|
|
||||||
|
val isOptional get() = metadata.linkedFile?.isOptional ?: false
|
||||||
|
|
||||||
|
fun isNewOptional() = isOptional && newOptional
|
||||||
|
|
||||||
|
fun correctSide() = metadata.linkedFile?.side?.hasSide(downloadSide) ?: true
|
||||||
|
|
||||||
|
override val name get() = metadata.name
|
||||||
|
|
||||||
|
// Ensure that an update is done if it changes from false to true, or from true to false
|
||||||
|
override var optionValue: Boolean
|
||||||
|
get() = cachedFile?.optionValue ?: true
|
||||||
|
set(value) {
|
||||||
|
if (value && !optionValue) { // Ensure that an update is done if it changes from false to true, or from true to false
|
||||||
|
alreadyUpToDate = false
|
||||||
|
}
|
||||||
|
cachedFile?.optionValue = value
|
||||||
|
}
|
||||||
|
|
||||||
|
override val optionDescription get() = metadata.linkedFile?.option?.description ?: ""
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (metadata.hashFormat?.isEmpty() != false) {
|
||||||
|
metadata.hashFormat = defaultFormat
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun invalidate() {
|
||||||
|
invalidated = true
|
||||||
|
alreadyUpToDate = false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateFromCache(cachedFile: ManifestFile.File?) {
|
||||||
|
if (err != null) return
|
||||||
|
|
||||||
|
if (cachedFile == null) {
|
||||||
|
this.cachedFile = ManifestFile.File()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.cachedFile = cachedFile
|
||||||
|
if (!invalidated) {
|
||||||
|
val currHash = try {
|
||||||
|
getHash(metadata.hashFormat!!, metadata.hash!!)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
err = e
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (currHash == cachedFile.hash) { // Already up to date
|
||||||
|
alreadyUpToDate = true
|
||||||
|
metadataRequired = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cachedFile.isOptional) {
|
||||||
|
// Because option selection dialog might set this task to true/false, metadata is always needed to download
|
||||||
|
// the file, and to show the description and name
|
||||||
|
metadataRequired = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun downloadMetadata(parentIndexFile: IndexFile, indexUri: SpaceSafeURI) {
|
||||||
|
if (err != null) return
|
||||||
|
|
||||||
|
if (metadataRequired) {
|
||||||
|
try {
|
||||||
|
// Retrieve the linked metadata file
|
||||||
|
metadata.downloadMeta(parentIndexFile, indexUri)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
err = e
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cachedFile?.let { cachedFile ->
|
||||||
|
val linkedFile = metadata.linkedFile
|
||||||
|
if (linkedFile != null) {
|
||||||
|
linkedFile.option?.let { opt ->
|
||||||
|
if (opt.optional) {
|
||||||
|
if (cachedFile.isOptional) {
|
||||||
|
// isOptional didn't change
|
||||||
|
newOptional = false
|
||||||
|
} else {
|
||||||
|
// isOptional false -> true, set option to it's default value
|
||||||
|
// TODO: preserve previous option value, somehow??
|
||||||
|
cachedFile.optionValue = opt.defaultValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cachedFile.isOptional = isOptional
|
||||||
|
cachedFile.onlyOtherSide = !correctSide()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the file in the destination location is already valid
|
||||||
|
* Must be done after metadata retrieval
|
||||||
|
*/
|
||||||
|
fun validateExistingFile(packFolder: String) {
|
||||||
|
if (!alreadyUpToDate) {
|
||||||
|
try {
|
||||||
|
// TODO: only do this for files that didn't exist before or have been modified since last full update?
|
||||||
|
val destPath = Paths.get(packFolder, metadata.destURI.toString())
|
||||||
|
FileSystem.SYSTEM.source(destPath.toOkioPath()).buffer().use { src ->
|
||||||
|
val hash: Hash
|
||||||
|
val fileHashFormat: String
|
||||||
|
val linkedFile = metadata.linkedFile
|
||||||
|
|
||||||
|
if (linkedFile != null) {
|
||||||
|
hash = linkedFile.hash
|
||||||
|
fileHashFormat = linkedFile.download!!.hashFormat!!
|
||||||
|
} else {
|
||||||
|
hash = metadata.getHashObj()
|
||||||
|
fileHashFormat = metadata.hashFormat!!
|
||||||
|
}
|
||||||
|
|
||||||
|
val fileSource = getHasher(fileHashFormat).getHashingSource(src)
|
||||||
|
fileSource.buffer().readAll(blackholeSink())
|
||||||
|
if (fileSource.hashIsEqual(hash)) {
|
||||||
|
alreadyUpToDate = true
|
||||||
|
|
||||||
|
// Update the manifest file
|
||||||
|
cachedFile = (cachedFile ?: ManifestFile.File()).also {
|
||||||
|
try {
|
||||||
|
it.hash = metadata.getHashObj()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
err = e
|
||||||
|
return
|
||||||
|
}
|
||||||
|
it.isOptional = isOptional
|
||||||
|
it.cachedLocation = metadata.destURI.toString()
|
||||||
|
metadata.linkedFile?.let { linked ->
|
||||||
|
try {
|
||||||
|
it.linkedFileHash = linked.hash
|
||||||
|
} catch (e: Exception) {
|
||||||
|
err = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
// Ignore exceptions; if the file doesn't exist we'll be downloading it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun download(packFolder: String, indexUri: SpaceSafeURI) {
|
||||||
|
if (err != null) return
|
||||||
|
|
||||||
|
// Ensure wrong-side or optional false files are removed
|
||||||
|
cachedFile?.let {
|
||||||
|
if (!it.optionValue || !correctSide()) {
|
||||||
|
if (it.cachedLocation == null) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(Paths.get(packFolder, it.cachedLocation))
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.warn("Failed to delete file before downloading", e)
|
||||||
|
}
|
||||||
|
it.cachedLocation = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (alreadyUpToDate) return
|
||||||
|
|
||||||
|
val destPath = Paths.get(packFolder, metadata.destURI.toString())
|
||||||
|
|
||||||
|
// Don't update files marked with preserve if they already exist on disk
|
||||||
|
if (metadata.preserve) {
|
||||||
|
if (destPath.toFile().exists()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: if already exists and has correct hash, ignore?
|
||||||
|
// TODO: add .disabled support?
|
||||||
|
|
||||||
|
try {
|
||||||
|
val hash: Hash
|
||||||
|
val fileHashFormat: String
|
||||||
|
val linkedFile = metadata.linkedFile
|
||||||
|
|
||||||
|
if (linkedFile != null) {
|
||||||
|
hash = linkedFile.hash
|
||||||
|
fileHashFormat = linkedFile.download!!.hashFormat!!
|
||||||
|
} else {
|
||||||
|
hash = metadata.getHashObj()
|
||||||
|
fileHashFormat = metadata.hashFormat!!
|
||||||
|
}
|
||||||
|
|
||||||
|
val src = metadata.getSource(indexUri)
|
||||||
|
val fileSource = getHasher(fileHashFormat).getHashingSource(src)
|
||||||
|
val data = Buffer()
|
||||||
|
|
||||||
|
// Read all the data into a buffer (very inefficient for large files! but allows rollback if hash check fails)
|
||||||
|
// TODO: should we instead rename the existing file, then stream straight to the file and rollback from the renamed file?
|
||||||
|
fileSource.buffer().use {
|
||||||
|
it.readAll(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileSource.hashIsEqual(hash)) {
|
||||||
|
// isDirectory follows symlinks, but createDirectories doesn't
|
||||||
|
try {
|
||||||
|
Files.createDirectories(destPath.parent)
|
||||||
|
} catch (e: java.nio.file.FileAlreadyExistsException) {
|
||||||
|
if (!Files.isDirectory(destPath.parent)) {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Files.copy(data.inputStream(), destPath, StandardCopyOption.REPLACE_EXISTING)
|
||||||
|
data.clear()
|
||||||
|
} else {
|
||||||
|
// TODO: move println to something visible in the error window
|
||||||
|
println("Invalid hash for " + metadata.destURI.toString())
|
||||||
|
println("Calculated: " + fileSource.hash)
|
||||||
|
println("Expected: $hash")
|
||||||
|
// Attempt to get the SHA256 hash
|
||||||
|
val sha256 = HashingSink.sha256(blackholeSink())
|
||||||
|
data.readAll(sha256)
|
||||||
|
println("SHA256 hash value: " + sha256.hash)
|
||||||
|
err = Exception("Hash invalid!")
|
||||||
|
data.clear()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cachedFile?.cachedLocation?.let {
|
||||||
|
if (destPath != Paths.get(packFolder, it)) {
|
||||||
|
// Delete old file if location changes
|
||||||
|
try {
|
||||||
|
Files.delete(Paths.get(packFolder, cachedFile!!.cachedLocation))
|
||||||
|
} catch (e: IOException) {
|
||||||
|
// Continue, as it was probably already deleted?
|
||||||
|
// TODO: log it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
err = e
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the manifest file
|
||||||
|
cachedFile = (cachedFile ?: ManifestFile.File()).also {
|
||||||
|
try {
|
||||||
|
it.hash = metadata.getHashObj()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
err = e
|
||||||
|
return
|
||||||
|
}
|
||||||
|
it.isOptional = isOptional
|
||||||
|
it.cachedLocation = metadata.destURI.toString()
|
||||||
|
metadata.linkedFile?.let { linked ->
|
||||||
|
try {
|
||||||
|
it.linkedFileHash = linked.hash
|
||||||
|
} catch (e: Exception) {
|
||||||
|
err = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
fun createTasksFromIndex(index: IndexFile, defaultFormat: String, downloadSide: Side): List<DownloadTask> {
|
||||||
|
val tasks = ArrayList<DownloadTask>()
|
||||||
|
for (file in Objects.requireNonNull(index.files)) {
|
||||||
|
tasks.add(DownloadTask(file, defaultFormat, downloadSide))
|
||||||
|
}
|
||||||
|
return tasks
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
132
src/main/kotlin/link/infra/packwiz/installer/Main.kt
Normal file
132
src/main/kotlin/link/infra/packwiz/installer/Main.kt
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
@file:JvmName("Main")
|
||||||
|
|
||||||
|
package link.infra.packwiz.installer
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import link.infra.packwiz.installer.target.Side
|
||||||
|
import link.infra.packwiz.installer.ui.cli.CLIHandler
|
||||||
|
import link.infra.packwiz.installer.ui.gui.GUIHandler
|
||||||
|
import link.infra.packwiz.installer.util.Log
|
||||||
|
import org.apache.commons.cli.DefaultParser
|
||||||
|
import org.apache.commons.cli.Options
|
||||||
|
import org.apache.commons.cli.ParseException
|
||||||
|
import java.awt.EventQueue
|
||||||
|
import java.awt.GraphicsEnvironment
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
import javax.swing.JOptionPane
|
||||||
|
import javax.swing.UIManager
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
|
class Main(args: Array<String>) {
|
||||||
|
// Don't attempt to start a GUI if we are headless
|
||||||
|
private var guiEnabled = !GraphicsEnvironment.isHeadless()
|
||||||
|
|
||||||
|
private fun startup(args: Array<String>) {
|
||||||
|
val options = Options()
|
||||||
|
addNonBootstrapOptions(options)
|
||||||
|
addBootstrapOptions(options)
|
||||||
|
|
||||||
|
val parser = DefaultParser()
|
||||||
|
val cmd = try {
|
||||||
|
parser.parse(options, args)
|
||||||
|
} catch (e: ParseException) {
|
||||||
|
Log.fatal("Failed to parse command line arguments", e)
|
||||||
|
if (guiEnabled) {
|
||||||
|
EventQueue.invokeAndWait {
|
||||||
|
try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
// Ignore the exceptions, just continue using the ugly L&F
|
||||||
|
}
|
||||||
|
JOptionPane.showMessageDialog(null, "Failed to parse command line arguments: $e",
|
||||||
|
"packwiz-installer", JOptionPane.ERROR_MESSAGE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (guiEnabled && cmd.hasOption("no-gui")) {
|
||||||
|
guiEnabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
val ui = if (guiEnabled) GUIHandler() else CLIHandler()
|
||||||
|
|
||||||
|
val unparsedArgs = cmd.args
|
||||||
|
if (unparsedArgs.size > 1) {
|
||||||
|
ui.showErrorAndExit("Too many arguments specified!")
|
||||||
|
} else if (unparsedArgs.isEmpty()) {
|
||||||
|
ui.showErrorAndExit("pack.toml URI to install from must be specified!")
|
||||||
|
}
|
||||||
|
|
||||||
|
val title = cmd.getOptionValue("title")
|
||||||
|
if (title != null) {
|
||||||
|
ui.title = title
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.show()
|
||||||
|
|
||||||
|
val uOptions = try {
|
||||||
|
UpdateManager.Options.construct(
|
||||||
|
downloadURI = SpaceSafeURI(unparsedArgs[0]),
|
||||||
|
side = cmd.getOptionValue("side")?.let((Side)::from),
|
||||||
|
packFolder = cmd.getOptionValue("pack-folder"),
|
||||||
|
manifestFile = cmd.getOptionValue("meta-file")
|
||||||
|
)
|
||||||
|
} catch (e: URISyntaxException) {
|
||||||
|
ui.showErrorAndExit("Failed to read pack.toml URI", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start update process!
|
||||||
|
try {
|
||||||
|
UpdateManager(uOptions, ui)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
ui.showErrorAndExit("Update process failed", e)
|
||||||
|
}
|
||||||
|
println("Finished successfully!")
|
||||||
|
ui.dispose()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// Called by packwiz-installer-bootstrap to set up the help command
|
||||||
|
@JvmStatic
|
||||||
|
fun addNonBootstrapOptions(options: Options) {
|
||||||
|
options.addOption("s", "side", true, "Side to install mods from (client/server, defaults to client)")
|
||||||
|
options.addOption(null, "title", true, "Title of the installer window")
|
||||||
|
options.addOption(null, "pack-folder", true, "Folder to install the pack to (defaults to the JAR directory)")
|
||||||
|
options.addOption(null, "meta-file", true, "JSON file to store pack metadata, relative to the pack folder (defaults to packwiz.json)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: link these somehow so they're only defined once?
|
||||||
|
@JvmStatic
|
||||||
|
private fun addBootstrapOptions(options: Options) {
|
||||||
|
options.addOption(null, "bootstrap-update-url", true, "Github API URL for checking for updates")
|
||||||
|
options.addOption(null, "bootstrap-update-token", true, "Github API Access Token, for private repositories")
|
||||||
|
options.addOption(null, "bootstrap-no-update", false, "Don't update packwiz-installer")
|
||||||
|
options.addOption(null, "bootstrap-main-jar", true, "Location of the packwiz-installer JAR file")
|
||||||
|
options.addOption("g", "no-gui", false, "Don't display a GUI to show update progress")
|
||||||
|
options.addOption("h", "help", false, "Display this message") // Implemented in packwiz-installer-bootstrap!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actual main() is in RequiresBootstrap!
|
||||||
|
init {
|
||||||
|
// Big overarching try/catch just in case everything breaks
|
||||||
|
try {
|
||||||
|
startup(args)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.fatal("Error from main", e)
|
||||||
|
if (guiEnabled) {
|
||||||
|
EventQueue.invokeLater {
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
"A fatal error occurred: \n$e",
|
||||||
|
"packwiz-installer", JOptionPane.ERROR_MESSAGE)
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
// In case the EventQueue is broken, exit after 1 minute
|
||||||
|
Thread.sleep(60 * 1000.toLong())
|
||||||
|
}
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
446
src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt
Normal file
446
src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt
Normal file
@@ -0,0 +1,446 @@
|
|||||||
|
package link.infra.packwiz.installer
|
||||||
|
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import com.google.gson.JsonIOException
|
||||||
|
import com.google.gson.JsonSyntaxException
|
||||||
|
import com.moandjiezana.toml.Toml
|
||||||
|
import link.infra.packwiz.installer.DownloadTask.Companion.createTasksFromIndex
|
||||||
|
import link.infra.packwiz.installer.metadata.IndexFile
|
||||||
|
import link.infra.packwiz.installer.metadata.ManifestFile
|
||||||
|
import link.infra.packwiz.installer.metadata.PackFile
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import link.infra.packwiz.installer.metadata.curseforge.resolveCfMetadata
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils.getHash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils.getHasher
|
||||||
|
import link.infra.packwiz.installer.request.HandlerManager.getFileSource
|
||||||
|
import link.infra.packwiz.installer.request.HandlerManager.getNewLoc
|
||||||
|
import link.infra.packwiz.installer.target.Side
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface.CancellationResult
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult
|
||||||
|
import link.infra.packwiz.installer.ui.data.InstallProgress
|
||||||
|
import link.infra.packwiz.installer.util.Log
|
||||||
|
import link.infra.packwiz.installer.util.ifletOrErr
|
||||||
|
import okio.buffer
|
||||||
|
import java.io.*
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Paths
|
||||||
|
import java.util.concurrent.CompletionService
|
||||||
|
import java.util.concurrent.ExecutionException
|
||||||
|
import java.util.concurrent.ExecutorCompletionService
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
|
class UpdateManager internal constructor(private val opts: Options, val ui: IUserInterface) {
|
||||||
|
private var cancelled = false
|
||||||
|
private var cancelledStartGame = false
|
||||||
|
private var errorsOccurred = false
|
||||||
|
|
||||||
|
init {
|
||||||
|
start()
|
||||||
|
}
|
||||||
|
|
||||||
|
data class Options(
|
||||||
|
val downloadURI: SpaceSafeURI,
|
||||||
|
val manifestFile: String,
|
||||||
|
val packFolder: String,
|
||||||
|
val side: Side
|
||||||
|
) {
|
||||||
|
// Horrible workaround for default params not working cleanly with nullable values
|
||||||
|
companion object {
|
||||||
|
fun construct(downloadURI: SpaceSafeURI, manifestFile: String?, packFolder: String?, side: Side?) =
|
||||||
|
Options(downloadURI, manifestFile ?: "packwiz.json", packFolder ?: ".", side ?: Side.CLIENT)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun start() {
|
||||||
|
checkOptions()
|
||||||
|
|
||||||
|
ui.submitProgress(InstallProgress("Loading manifest file..."))
|
||||||
|
val gson = GsonBuilder().registerTypeAdapter(Hash::class.java, Hash.TypeHandler()).create()
|
||||||
|
val manifest = try {
|
||||||
|
gson.fromJson(FileReader(Paths.get(opts.packFolder, opts.manifestFile).toString()),
|
||||||
|
ManifestFile::class.java)
|
||||||
|
} catch (e: FileNotFoundException) {
|
||||||
|
ui.firstInstall = true
|
||||||
|
ManifestFile()
|
||||||
|
} catch (e: JsonSyntaxException) {
|
||||||
|
ui.showErrorAndExit("Invalid local manifest file, try deleting ${opts.manifestFile}", e)
|
||||||
|
} catch (e: JsonIOException) {
|
||||||
|
ui.showErrorAndExit("Failed to read local manifest file, try deleting ${opts.manifestFile}", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
handleCancellation()
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.submitProgress(InstallProgress("Loading pack file..."))
|
||||||
|
val packFileSource = try {
|
||||||
|
val src = getFileSource(opts.downloadURI)
|
||||||
|
getHasher("sha256").getHashingSource(src)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
ui.showErrorAndExit("Failed to download pack.toml", e)
|
||||||
|
}
|
||||||
|
val pf = packFileSource.buffer().use {
|
||||||
|
try {
|
||||||
|
Toml().read(InputStreamReader(it.inputStream(), "UTF-8")).to(PackFile::class.java)
|
||||||
|
} catch (e: IllegalStateException) {
|
||||||
|
ui.showErrorAndExit("Failed to parse pack.toml", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
handleCancellation()
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.submitProgress(InstallProgress("Checking local files..."))
|
||||||
|
|
||||||
|
// Invalidation checking must be done here, as it must happen before pack/index hashes are checked
|
||||||
|
val invalidatedUris: MutableList<SpaceSafeURI> = ArrayList()
|
||||||
|
for ((fileUri, file) in manifest.cachedFiles) {
|
||||||
|
// ignore onlyOtherSide files
|
||||||
|
if (file.onlyOtherSide) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var invalid = false
|
||||||
|
// if isn't optional, or is optional but optionValue == true
|
||||||
|
if (!file.isOptional || file.optionValue) {
|
||||||
|
if (file.cachedLocation != null) {
|
||||||
|
if (!Paths.get(opts.packFolder, file.cachedLocation).toFile().exists()) {
|
||||||
|
invalid = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// if cachedLocation == null, should probably be installed!!
|
||||||
|
invalid = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (invalid) {
|
||||||
|
Log.info("File $fileUri invalidated, marked for redownloading")
|
||||||
|
invalidatedUris.add(fileUri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (manifest.packFileHash?.let { packFileSource.hashIsEqual(it) } == true && invalidatedUris.isEmpty()) {
|
||||||
|
// todo: --force?
|
||||||
|
ui.submitProgress(InstallProgress("Modpack is already up to date!", 1, 1))
|
||||||
|
ui.awaitOptionalButton(false)
|
||||||
|
if (!ui.optionsButtonPressed) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.info("Modpack name: ${pf.name}")
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
handleCancellation()
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// TODO: switch to OkHttp for better redirect handling
|
||||||
|
ui.ifletOrErr(pf.index, "No index file found, or the pack file is empty; note that Java doesn't automatically follow redirects from HTTP to HTTPS (and may cause this error)") { index ->
|
||||||
|
ui.ifletOrErr(index.hashFormat, index.hash, "Pack has no hash or hashFormat for index") { hashFormat, hash ->
|
||||||
|
ui.ifletOrErr(getNewLoc(opts.downloadURI, index.file), "Pack has invalid index file: " + index.file) { newLoc ->
|
||||||
|
processIndex(
|
||||||
|
newLoc,
|
||||||
|
getHash(hashFormat, hash),
|
||||||
|
hashFormat,
|
||||||
|
manifest,
|
||||||
|
invalidatedUris
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e1: Exception) {
|
||||||
|
ui.showErrorAndExit("Failed to process index file", e1)
|
||||||
|
}
|
||||||
|
|
||||||
|
handleCancellation()
|
||||||
|
|
||||||
|
// TODO: update MMC params, java args etc
|
||||||
|
|
||||||
|
// If there were errors, don't write the manifest/index hashes, to ensure they are rechecked later
|
||||||
|
if (errorsOccurred) {
|
||||||
|
manifest.indexFileHash = null
|
||||||
|
manifest.packFileHash = null
|
||||||
|
} else {
|
||||||
|
manifest.packFileHash = packFileSource.hash
|
||||||
|
}
|
||||||
|
|
||||||
|
manifest.cachedSide = opts.side
|
||||||
|
try {
|
||||||
|
FileWriter(Paths.get(opts.packFolder, opts.manifestFile).toString()).use { writer -> gson.toJson(manifest, writer) }
|
||||||
|
} catch (e: IOException) {
|
||||||
|
ui.showErrorAndExit("Failed to save local manifest file", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkOptions() {
|
||||||
|
// TODO: implement
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun processIndex(indexUri: SpaceSafeURI, indexHash: Hash, hashFormat: String, manifest: ManifestFile, invalidatedUris: List<SpaceSafeURI>) {
|
||||||
|
if (manifest.indexFileHash == indexHash && invalidatedUris.isEmpty()) {
|
||||||
|
ui.submitProgress(InstallProgress("Modpack files are already up to date!", 1, 1))
|
||||||
|
ui.awaitOptionalButton(false)
|
||||||
|
if (!ui.optionsButtonPressed) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
manifest.indexFileHash = indexHash
|
||||||
|
|
||||||
|
val indexFileSource = try {
|
||||||
|
val src = getFileSource(indexUri)
|
||||||
|
getHasher(hashFormat).getHashingSource(src)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
ui.showErrorAndExit("Failed to download index file", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
val indexFile = try {
|
||||||
|
Toml().read(InputStreamReader(indexFileSource.buffer().inputStream(), "UTF-8")).to(IndexFile::class.java)
|
||||||
|
} catch (e: IllegalStateException) {
|
||||||
|
ui.showErrorAndExit("Failed to parse index file", e)
|
||||||
|
}
|
||||||
|
if (!indexFileSource.hashIsEqual(indexHash)) {
|
||||||
|
ui.showErrorAndExit("Your index file hash is invalid! The pack developer should packwiz refresh on the pack again")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.submitProgress(InstallProgress("Checking local files..."))
|
||||||
|
// TODO: use kotlin filtering/FP rather than an iterator?
|
||||||
|
val it: MutableIterator<Map.Entry<SpaceSafeURI, ManifestFile.File>> = manifest.cachedFiles.entries.iterator()
|
||||||
|
while (it.hasNext()) {
|
||||||
|
val (uri, file) = it.next()
|
||||||
|
if (file.cachedLocation != null) {
|
||||||
|
var alreadyDeleted = false
|
||||||
|
// Delete if option value has been set to false
|
||||||
|
if (file.isOptional && !file.optionValue) {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(Paths.get(opts.packFolder, file.cachedLocation))
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.warn("Failed to delete optional disabled file", e)
|
||||||
|
}
|
||||||
|
// Set to null, as it doesn't exist anymore
|
||||||
|
file.cachedLocation = null
|
||||||
|
alreadyDeleted = true
|
||||||
|
}
|
||||||
|
if (indexFile.files.none { it.file == uri }) { // File has been removed from the index
|
||||||
|
if (!alreadyDeleted) {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(Paths.get(opts.packFolder, file.cachedLocation))
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.warn("Failed to delete file removed from index", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
it.remove()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ui.submitProgress(InstallProgress("Comparing new files..."))
|
||||||
|
|
||||||
|
// TODO: progress bar?
|
||||||
|
if (indexFile.files.isEmpty()) {
|
||||||
|
Log.warn("Index is empty!")
|
||||||
|
}
|
||||||
|
val tasks = createTasksFromIndex(indexFile, indexFile.hashFormat, opts.side)
|
||||||
|
// If the side changes, invalidate EVERYTHING just in case
|
||||||
|
// Might not be needed, but done just to be safe
|
||||||
|
val invalidateAll = opts.side != manifest.cachedSide
|
||||||
|
if (invalidateAll) {
|
||||||
|
Log.info("Side changed, invalidating all mods")
|
||||||
|
}
|
||||||
|
tasks.forEach{ f ->
|
||||||
|
// TODO: should linkedfile be checked as well? should this be done in the download section?
|
||||||
|
if (invalidateAll) {
|
||||||
|
f.invalidate()
|
||||||
|
} else if (invalidatedUris.contains(f.metadata.file)) {
|
||||||
|
f.invalidate()
|
||||||
|
}
|
||||||
|
val file = manifest.cachedFiles[f.metadata.file]
|
||||||
|
// Ensure the file can be reverted later if necessary - the DownloadTask modifies the file so if it fails we need the old version back
|
||||||
|
file?.backup()
|
||||||
|
// If it is null, the DownloadTask will make a new empty cachedFile
|
||||||
|
f.updateFromCache(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's hope downloadMetadata is a pure function!!!
|
||||||
|
tasks.parallelStream().forEach { f -> f.downloadMetadata(indexFile, indexUri) }
|
||||||
|
|
||||||
|
val failedTaskDetails = tasks.asSequence().map(DownloadTask::exceptionDetails).filterNotNull().toList()
|
||||||
|
if (failedTaskDetails.isNotEmpty()) {
|
||||||
|
errorsOccurred = true
|
||||||
|
when (ui.showExceptions(failedTaskDetails, tasks.size, true)) {
|
||||||
|
ExceptionListResult.CONTINUE -> {}
|
||||||
|
ExceptionListResult.CANCEL -> {
|
||||||
|
cancelled = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ExceptionListResult.IGNORE -> {
|
||||||
|
cancelledStartGame = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: task failed function?
|
||||||
|
val nonFailedFirstTasks = tasks.filter { t -> !t.failed() }.toList()
|
||||||
|
val optionTasks = nonFailedFirstTasks.filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).toList()
|
||||||
|
val optionsChanged = optionTasks.any(DownloadTask::isNewOptional)
|
||||||
|
if (optionTasks.isNotEmpty() && !optionsChanged) {
|
||||||
|
if (!ui.optionsButtonPressed) {
|
||||||
|
// TODO: this is so ugly
|
||||||
|
ui.submitProgress(InstallProgress("Reconfigure optional mods?", 0,1))
|
||||||
|
ui.awaitOptionalButton(true)
|
||||||
|
if (ui.cancelButtonPressed) {
|
||||||
|
showCancellationDialog()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If options changed, present all options again
|
||||||
|
if (ui.optionsButtonPressed || optionsChanged) {
|
||||||
|
// new ArrayList is required so it's an IOptionDetails rather than a DownloadTask list
|
||||||
|
if (ui.showOptions(ArrayList(optionTasks))) {
|
||||||
|
cancelled = true
|
||||||
|
handleCancellation()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: keep this enabled? then apply changes after download process?
|
||||||
|
ui.disableOptionsButton(optionTasks.isNotEmpty())
|
||||||
|
|
||||||
|
ui.submitProgress(InstallProgress("Validating existing files..."))
|
||||||
|
|
||||||
|
// Validate existing files
|
||||||
|
for (downloadTask in nonFailedFirstTasks.filter(DownloadTask::correctSide)) {
|
||||||
|
downloadTask.validateExistingFile(opts.packFolder)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve CurseForge metadata
|
||||||
|
val cfFiles = nonFailedFirstTasks.asSequence().filter { !it.alreadyUpToDate }
|
||||||
|
.filter(DownloadTask::correctSide)
|
||||||
|
.map { it.metadata }
|
||||||
|
.filter { it.linkedFile != null }
|
||||||
|
.filter { it.linkedFile?.download?.mode == "metadata:curseforge" }.toList()
|
||||||
|
if (cfFiles.isNotEmpty()) {
|
||||||
|
ui.submitProgress(InstallProgress("Resolving CurseForge metadata..."))
|
||||||
|
val resolveFailures = resolveCfMetadata(cfFiles)
|
||||||
|
if (resolveFailures.isNotEmpty()) {
|
||||||
|
errorsOccurred = true
|
||||||
|
when (ui.showExceptions(resolveFailures, cfFiles.size, true)) {
|
||||||
|
ExceptionListResult.CONTINUE -> {}
|
||||||
|
ExceptionListResult.CANCEL -> {
|
||||||
|
cancelled = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ExceptionListResult.IGNORE -> {
|
||||||
|
cancelledStartGame = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: different thread pool type?
|
||||||
|
val threadPool = Executors.newFixedThreadPool(10)
|
||||||
|
val completionService: CompletionService<DownloadTask> = ExecutorCompletionService(threadPool)
|
||||||
|
tasks.forEach { t ->
|
||||||
|
completionService.submit {
|
||||||
|
t.download(opts.packFolder, indexUri)
|
||||||
|
t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i in tasks.indices) {
|
||||||
|
val task: DownloadTask = try {
|
||||||
|
completionService.take().get()
|
||||||
|
} catch (e: InterruptedException) {
|
||||||
|
ui.showErrorAndExit("Interrupted when consuming download tasks", e)
|
||||||
|
} catch (e: ExecutionException) {
|
||||||
|
ui.showErrorAndExit("Failed to execute download task", e)
|
||||||
|
}
|
||||||
|
// Update manifest - If there were no errors cachedFile has already been modified in place (good old pass by reference)
|
||||||
|
task.cachedFile?.let { file ->
|
||||||
|
if (task.failed()) {
|
||||||
|
val oldFile = file.revert
|
||||||
|
if (oldFile != null) {
|
||||||
|
task.metadata.file?.let { uri -> manifest.cachedFiles.putIfAbsent(uri, oldFile) }
|
||||||
|
} else { null }
|
||||||
|
} else {
|
||||||
|
task.metadata.file?.let { uri -> manifest.cachedFiles.putIfAbsent(uri, file) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val exDetails = task.exceptionDetails
|
||||||
|
val progress = if (exDetails != null) {
|
||||||
|
"Failed to download ${exDetails.name}: ${exDetails.exception.message}"
|
||||||
|
} else {
|
||||||
|
"Downloaded ${task.name}"
|
||||||
|
}
|
||||||
|
ui.submitProgress(InstallProgress(progress, i + 1, tasks.size))
|
||||||
|
|
||||||
|
if (ui.cancelButtonPressed) { // Stop all tasks, don't launch the game (it's in an invalid state!)
|
||||||
|
threadPool.shutdown()
|
||||||
|
cancelled = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shut down the thread pool when the update is done
|
||||||
|
threadPool.shutdown()
|
||||||
|
|
||||||
|
val failedTasks2ElectricBoogaloo = nonFailedFirstTasks.asSequence().map(DownloadTask::exceptionDetails).filterNotNull().toList()
|
||||||
|
if (failedTasks2ElectricBoogaloo.isNotEmpty()) {
|
||||||
|
errorsOccurred = true
|
||||||
|
when (ui.showExceptions(failedTasks2ElectricBoogaloo, tasks.size, false)) {
|
||||||
|
ExceptionListResult.CONTINUE -> {}
|
||||||
|
ExceptionListResult.CANCEL -> cancelled = true
|
||||||
|
ExceptionListResult.IGNORE -> cancelledStartGame = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showCancellationDialog() {
|
||||||
|
when (ui.showCancellationDialog()) {
|
||||||
|
CancellationResult.QUIT -> cancelled = true
|
||||||
|
CancellationResult.CONTINUE -> cancelledStartGame = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: move to UI?
|
||||||
|
private fun handleCancellation() {
|
||||||
|
if (cancelled) {
|
||||||
|
println("Update cancelled by user!")
|
||||||
|
exitProcess(1)
|
||||||
|
} else if (cancelledStartGame) {
|
||||||
|
println("Update cancelled by user! Continuing to start game...")
|
||||||
|
exitProcess(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,27 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata
|
||||||
|
|
||||||
|
import com.google.gson.TypeAdapter
|
||||||
|
import com.google.gson.stream.JsonReader
|
||||||
|
import com.google.gson.stream.JsonToken
|
||||||
|
import com.google.gson.stream.JsonWriter
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class EfficientBooleanAdapter : TypeAdapter<Boolean?>() {
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun write(out: JsonWriter, value: Boolean?) {
|
||||||
|
if (value == null || !value) {
|
||||||
|
out.nullValue()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out.value(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun read(reader: JsonReader): Boolean? {
|
||||||
|
if (reader.peek() == JsonToken.NULL) {
|
||||||
|
reader.nextNull()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return reader.nextBoolean()
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,100 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
import com.moandjiezana.toml.Toml
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils.getHash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils.getHasher
|
||||||
|
import link.infra.packwiz.installer.request.HandlerManager.getFileSource
|
||||||
|
import link.infra.packwiz.installer.request.HandlerManager.getNewLoc
|
||||||
|
import okio.Source
|
||||||
|
import okio.buffer
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
class IndexFile {
|
||||||
|
@SerializedName("hash-format")
|
||||||
|
var hashFormat: String = "sha-256"
|
||||||
|
var files: MutableList<File> = ArrayList()
|
||||||
|
|
||||||
|
class File {
|
||||||
|
var file: SpaceSafeURI? = null
|
||||||
|
@SerializedName("hash-format")
|
||||||
|
var hashFormat: String? = null
|
||||||
|
var hash: String? = null
|
||||||
|
var alias: SpaceSafeURI? = null
|
||||||
|
var metafile = false
|
||||||
|
var preserve = false
|
||||||
|
|
||||||
|
@Transient
|
||||||
|
var linkedFile: ModFile? = null
|
||||||
|
@Transient
|
||||||
|
var linkedFileURI: SpaceSafeURI? = null
|
||||||
|
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun downloadMeta(parentIndexFile: IndexFile, indexUri: SpaceSafeURI?) {
|
||||||
|
if (!metafile) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (hashFormat?.length ?: 0 == 0) {
|
||||||
|
hashFormat = parentIndexFile.hashFormat
|
||||||
|
}
|
||||||
|
// TODO: throw a proper exception instead of allowing NPE?
|
||||||
|
val fileHash = getHash(hashFormat!!, hash!!)
|
||||||
|
linkedFileURI = getNewLoc(indexUri, file)
|
||||||
|
val src = getFileSource(linkedFileURI!!)
|
||||||
|
val fileStream = getHasher(hashFormat!!).getHashingSource(src)
|
||||||
|
linkedFile = Toml().read(InputStreamReader(fileStream.buffer().inputStream(), "UTF-8")).to(ModFile::class.java)
|
||||||
|
if (!fileStream.hashIsEqual(fileHash)) {
|
||||||
|
throw Exception("Invalid mod file hash")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun getSource(indexUri: SpaceSafeURI?): Source {
|
||||||
|
return if (metafile) {
|
||||||
|
if (linkedFile == null) {
|
||||||
|
throw Exception("Linked file doesn't exist!")
|
||||||
|
}
|
||||||
|
linkedFile!!.getSource(linkedFileURI)
|
||||||
|
} else {
|
||||||
|
val newLoc = getNewLoc(indexUri, file) ?: throw Exception("Index file URI is invalid")
|
||||||
|
getFileSource(newLoc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun getHashObj(): Hash {
|
||||||
|
if (hash == null) { // TODO: should these be more specific exceptions (e.g. IndexFileException?!)
|
||||||
|
throw Exception("Index file doesn't have a hash")
|
||||||
|
}
|
||||||
|
if (hashFormat == null) {
|
||||||
|
throw Exception("Index file doesn't have a hash format")
|
||||||
|
}
|
||||||
|
return getHash(hashFormat!!, hash!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: throw some kind of exception?
|
||||||
|
val name: String
|
||||||
|
get() {
|
||||||
|
if (metafile) {
|
||||||
|
return linkedFile?.name ?: linkedFile?.filename ?:
|
||||||
|
file?.run { Paths.get(path ?: return "Invalid file").fileName.toString() } ?: "Invalid file"
|
||||||
|
}
|
||||||
|
return file?.run { Paths.get(path ?: return "Invalid file").fileName.toString() } ?: "Invalid file"
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: URIs are bad
|
||||||
|
val destURI: SpaceSafeURI?
|
||||||
|
get() {
|
||||||
|
if (alias != null) {
|
||||||
|
return alias
|
||||||
|
}
|
||||||
|
return if (metafile && linkedFile != null) {
|
||||||
|
linkedFile?.filename?.let { file?.resolve(it) }
|
||||||
|
} else {
|
||||||
|
file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,44 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata
|
||||||
|
|
||||||
|
import com.google.gson.annotations.JsonAdapter
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import link.infra.packwiz.installer.target.Side
|
||||||
|
|
||||||
|
class ManifestFile {
|
||||||
|
var packFileHash: Hash? = null
|
||||||
|
var indexFileHash: Hash? = null
|
||||||
|
var cachedFiles: MutableMap<SpaceSafeURI, File> = HashMap()
|
||||||
|
// If the side changes, EVERYTHING invalidates. FUN!!!
|
||||||
|
var cachedSide = Side.CLIENT
|
||||||
|
|
||||||
|
// TODO: switch to Kotlin-friendly JSON/TOML libs?
|
||||||
|
class File {
|
||||||
|
@Transient
|
||||||
|
var revert: File? = null
|
||||||
|
private set
|
||||||
|
|
||||||
|
var hash: Hash? = null
|
||||||
|
var linkedFileHash: Hash? = null
|
||||||
|
var cachedLocation: String? = null
|
||||||
|
|
||||||
|
@JsonAdapter(EfficientBooleanAdapter::class)
|
||||||
|
var isOptional = false
|
||||||
|
var optionValue = true
|
||||||
|
|
||||||
|
@JsonAdapter(EfficientBooleanAdapter::class)
|
||||||
|
var onlyOtherSide = false
|
||||||
|
|
||||||
|
// When an error occurs, the state needs to be reverted. To do this, I have a crude revert system.
|
||||||
|
fun backup() {
|
||||||
|
revert = File().also {
|
||||||
|
it.hash = hash
|
||||||
|
it.linkedFileHash = linkedFileHash
|
||||||
|
it.cachedLocation = cachedLocation
|
||||||
|
it.isOptional = isOptional
|
||||||
|
it.optionValue = optionValue
|
||||||
|
it.onlyOtherSide = onlyOtherSide
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,74 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata
|
||||||
|
|
||||||
|
import com.google.gson.annotations.JsonAdapter
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
import link.infra.packwiz.installer.metadata.curseforge.UpdateData
|
||||||
|
import link.infra.packwiz.installer.metadata.curseforge.UpdateDeserializer
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils.getHash
|
||||||
|
import link.infra.packwiz.installer.request.HandlerManager.getFileSource
|
||||||
|
import link.infra.packwiz.installer.request.HandlerManager.getNewLoc
|
||||||
|
import link.infra.packwiz.installer.target.Side
|
||||||
|
import okio.Source
|
||||||
|
|
||||||
|
class ModFile {
|
||||||
|
var name: String? = null
|
||||||
|
var filename: String? = null
|
||||||
|
var side: Side? = null
|
||||||
|
var download: Download? = null
|
||||||
|
|
||||||
|
class Download {
|
||||||
|
var url: SpaceSafeURI? = null
|
||||||
|
@SerializedName("hash-format")
|
||||||
|
var hashFormat: String? = null
|
||||||
|
var hash: String? = null
|
||||||
|
var mode: String? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonAdapter(UpdateDeserializer::class)
|
||||||
|
var update: Map<String, UpdateData>? = null
|
||||||
|
var option: Option? = null
|
||||||
|
|
||||||
|
@Transient
|
||||||
|
val resolvedUpdateData = mutableMapOf<String, SpaceSafeURI>()
|
||||||
|
|
||||||
|
class Option {
|
||||||
|
var optional = false
|
||||||
|
var description: String? = null
|
||||||
|
@SerializedName("default")
|
||||||
|
var defaultValue = false
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun getSource(baseLoc: SpaceSafeURI?): Source {
|
||||||
|
download?.let {
|
||||||
|
if (it.mode == null || it.mode == "" || it.mode == "url") {
|
||||||
|
if (it.url == null) {
|
||||||
|
throw Exception("Metadata file doesn't have a download URI")
|
||||||
|
}
|
||||||
|
val newLoc = getNewLoc(baseLoc, it.url) ?: throw Exception("Metadata file URI is invalid")
|
||||||
|
return getFileSource(newLoc)
|
||||||
|
} else if (it.mode == "metadata:curseforge") {
|
||||||
|
if (!resolvedUpdateData.contains("curseforge")) {
|
||||||
|
throw Exception("Metadata file specifies CurseForge mode, but is missing metadata")
|
||||||
|
}
|
||||||
|
return getFileSource(resolvedUpdateData["curseforge"]!!)
|
||||||
|
} else {
|
||||||
|
throw Exception("Unsupported download mode " + it.mode)
|
||||||
|
}
|
||||||
|
} ?: throw Exception("Metadata file doesn't have download")
|
||||||
|
}
|
||||||
|
|
||||||
|
@get:Throws(Exception::class)
|
||||||
|
val hash: Hash
|
||||||
|
get() {
|
||||||
|
download?.let {
|
||||||
|
return getHash(
|
||||||
|
it.hashFormat ?: throw Exception("Metadata file doesn't have a hash format"),
|
||||||
|
it.hash ?: throw Exception("Metadata file doesn't have a hash")
|
||||||
|
)
|
||||||
|
} ?: throw Exception("Metadata file doesn't have download")
|
||||||
|
}
|
||||||
|
|
||||||
|
val isOptional: Boolean get() = option?.optional ?: false
|
||||||
|
}
|
@@ -0,0 +1,17 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
class PackFile {
|
||||||
|
var name: String? = null
|
||||||
|
var index: IndexFileLoc? = null
|
||||||
|
|
||||||
|
class IndexFileLoc {
|
||||||
|
var file: SpaceSafeURI? = null
|
||||||
|
@SerializedName("hash-format")
|
||||||
|
var hashFormat: String? = null
|
||||||
|
var hash: String? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
var versions: Map<String, String>? = null
|
||||||
|
}
|
@@ -0,0 +1,61 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata
|
||||||
|
|
||||||
|
import com.google.gson.annotations.JsonAdapter
|
||||||
|
import java.io.Serializable
|
||||||
|
import java.net.MalformedURLException
|
||||||
|
import java.net.URI
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
// The world's worst URI wrapper
|
||||||
|
@JsonAdapter(SpaceSafeURIParser::class)
|
||||||
|
class SpaceSafeURI : Comparable<SpaceSafeURI>, Serializable {
|
||||||
|
private val u: URI
|
||||||
|
|
||||||
|
@Throws(URISyntaxException::class)
|
||||||
|
constructor(str: String) {
|
||||||
|
u = URI(str.replace(" ", "%20"))
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(uri: URI) {
|
||||||
|
u = uri
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(URISyntaxException::class)
|
||||||
|
constructor(scheme: String?, authority: String?, path: String?, query: String?, fragment: String?) { // TODO: do all components need to be replaced?
|
||||||
|
u = URI(
|
||||||
|
scheme?.replace(" ", "%20"),
|
||||||
|
authority?.replace(" ", "%20"),
|
||||||
|
path?.replace(" ", "%20"),
|
||||||
|
query?.replace(" ", "%20"),
|
||||||
|
fragment?.replace(" ", "%20")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val path: String? get() = u.path?.replace("%20", " ")
|
||||||
|
|
||||||
|
override fun toString(): String = u.toString().replace("%20", " ")
|
||||||
|
|
||||||
|
fun resolve(path: String): SpaceSafeURI = SpaceSafeURI(u.resolve(path.replace(" ", "%20")))
|
||||||
|
|
||||||
|
fun resolve(loc: SpaceSafeURI): SpaceSafeURI = SpaceSafeURI(u.resolve(loc.u))
|
||||||
|
|
||||||
|
fun relativize(loc: SpaceSafeURI): SpaceSafeURI = SpaceSafeURI(u.relativize(loc.u))
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
return if (other is SpaceSafeURI) {
|
||||||
|
u == other.u
|
||||||
|
} else false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode() = u.hashCode()
|
||||||
|
|
||||||
|
override fun compareTo(other: SpaceSafeURI): Int = u.compareTo(other.u)
|
||||||
|
|
||||||
|
val scheme: String? get() = u.scheme
|
||||||
|
val authority: String? get() = u.authority
|
||||||
|
val host: String? get() = u.host
|
||||||
|
|
||||||
|
@Throws(MalformedURLException::class)
|
||||||
|
fun toURL(): URL = u.toURL()
|
||||||
|
}
|
@@ -0,0 +1,25 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata
|
||||||
|
|
||||||
|
import com.google.gson.JsonDeserializationContext
|
||||||
|
import com.google.gson.JsonDeserializer
|
||||||
|
import com.google.gson.JsonElement
|
||||||
|
import com.google.gson.JsonParseException
|
||||||
|
import java.lang.reflect.Type
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class encodes spaces before parsing the URI, so the URI can actually be
|
||||||
|
* parsed.
|
||||||
|
*/
|
||||||
|
internal class SpaceSafeURIParser : JsonDeserializer<SpaceSafeURI> {
|
||||||
|
@Throws(JsonParseException::class)
|
||||||
|
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): SpaceSafeURI {
|
||||||
|
return try {
|
||||||
|
SpaceSafeURI(json.asString)
|
||||||
|
} catch (e: URISyntaxException) {
|
||||||
|
throw JsonParseException("Failed to parse URI", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: replace this with a better solution?
|
||||||
|
}
|
@@ -0,0 +1,132 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.curseforge
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.JsonIOException
|
||||||
|
import com.google.gson.JsonSyntaxException
|
||||||
|
import link.infra.packwiz.installer.metadata.IndexFile
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import link.infra.packwiz.installer.target.ClientHolder
|
||||||
|
import link.infra.packwiz.installer.ui.data.ExceptionDetails
|
||||||
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
|
import okhttp3.internal.closeQuietly
|
||||||
|
import okio.ByteString.Companion.decodeBase64
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
|
private class GetFilesRequest(val fileIds: List<Int>)
|
||||||
|
private class GetModsRequest(val modIds: List<Int>)
|
||||||
|
|
||||||
|
private class GetFilesResponse {
|
||||||
|
class CfFile {
|
||||||
|
var id = 0
|
||||||
|
var modId = 0
|
||||||
|
var downloadUrl: SpaceSafeURI? = null
|
||||||
|
}
|
||||||
|
val data = mutableListOf<CfFile>()
|
||||||
|
}
|
||||||
|
|
||||||
|
private class GetModsResponse {
|
||||||
|
class CfMod {
|
||||||
|
var id = 0
|
||||||
|
var name = ""
|
||||||
|
var links: CfLinks? = null
|
||||||
|
}
|
||||||
|
class CfLinks {
|
||||||
|
var websiteUrl = ""
|
||||||
|
}
|
||||||
|
val data = mutableListOf<CfMod>()
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val APIServer = "api.curseforge.com"
|
||||||
|
// If you fork/derive from packwiz, I request that you obtain your own API key.
|
||||||
|
private val APIKey = "JDJhJDEwJHNBWVhqblU1N0EzSmpzcmJYM3JVdk92UWk2NHBLS3BnQ2VpbGc1TUM1UGNKL0RYTmlGWWxh".decodeBase64()!!
|
||||||
|
.string(StandardCharsets.UTF_8)
|
||||||
|
|
||||||
|
private val clientHolder = ClientHolder()
|
||||||
|
|
||||||
|
// TODO: switch to PackwizPath stuff and OkHttp in old code
|
||||||
|
|
||||||
|
@Throws(JsonSyntaxException::class, JsonIOException::class)
|
||||||
|
fun resolveCfMetadata(mods: List<IndexFile.File>): List<ExceptionDetails> {
|
||||||
|
val failures = mutableListOf<ExceptionDetails>()
|
||||||
|
val fileIdMap = mutableMapOf<Int, IndexFile.File>()
|
||||||
|
|
||||||
|
for (mod in mods) {
|
||||||
|
if (mod.linkedFile!!.update == null) {
|
||||||
|
failures.add(ExceptionDetails(mod.linkedFile!!.name ?: mod.linkedFile!!.filename!!, Exception("Failed to resolve CurseForge metadata: no update section")))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (!mod.linkedFile!!.update!!.contains("curseforge")) {
|
||||||
|
failures.add(ExceptionDetails(mod.linkedFile!!.name ?: mod.linkedFile!!.filename!!, Exception("Failed to resolve CurseForge metadata: no CurseForge update section")))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fileIdMap[(mod.linkedFile!!.update!!["curseforge"] as CurseForgeUpdateData).fileId] = mod
|
||||||
|
}
|
||||||
|
|
||||||
|
val reqData = GetFilesRequest(fileIdMap.keys.toList())
|
||||||
|
val req = Request.Builder()
|
||||||
|
.url("https://${APIServer}/v1/mods/files")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.header("User-Agent", "packwiz-installer")
|
||||||
|
.header("X-API-Key", APIKey)
|
||||||
|
.post(Gson().toJson(reqData, GetFilesRequest::class.java).toRequestBody("application/json".toMediaType()))
|
||||||
|
.build()
|
||||||
|
val res = clientHolder.okHttpClient.newCall(req).execute()
|
||||||
|
if (!res.isSuccessful || res.body == null) {
|
||||||
|
res.closeQuietly()
|
||||||
|
failures.add(ExceptionDetails("Other", Exception("Failed to resolve CurseForge metadata for file data: error code ${res.code}")))
|
||||||
|
return failures
|
||||||
|
}
|
||||||
|
|
||||||
|
val resData = Gson().fromJson(res.body!!.charStream(), GetFilesResponse::class.java)
|
||||||
|
res.closeQuietly()
|
||||||
|
|
||||||
|
val manualDownloadMods = mutableMapOf<Int, Pair<IndexFile.File, Int>>()
|
||||||
|
for (file in resData.data) {
|
||||||
|
if (!fileIdMap.contains(file.id)) {
|
||||||
|
failures.add(ExceptionDetails(file.id.toString(),
|
||||||
|
Exception("Failed to find file from result: ID ${file.id}, Project ID ${file.modId}")))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (file.downloadUrl == null) {
|
||||||
|
manualDownloadMods[file.modId] = Pair(fileIdMap[file.id]!!, file.id)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fileIdMap[file.id]!!.linkedFile!!.resolvedUpdateData["curseforge"] = file.downloadUrl!!
|
||||||
|
}
|
||||||
|
|
||||||
|
if (manualDownloadMods.isNotEmpty()) {
|
||||||
|
val reqModsData = GetModsRequest(manualDownloadMods.keys.toList())
|
||||||
|
val reqMods = Request.Builder()
|
||||||
|
.url("https://${APIServer}/v1/mods")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.header("User-Agent", "packwiz-installer")
|
||||||
|
.header("X-API-Key", APIKey)
|
||||||
|
.post(Gson().toJson(reqModsData, GetModsRequest::class.java).toRequestBody("application/json".toMediaType()))
|
||||||
|
.build()
|
||||||
|
val resMods = clientHolder.okHttpClient.newCall(reqMods).execute()
|
||||||
|
if (!resMods.isSuccessful || resMods.body == null) {
|
||||||
|
resMods.closeQuietly()
|
||||||
|
failures.add(ExceptionDetails("Other", Exception("Failed to resolve CurseForge metadata for mod data: error code ${resMods.code}")))
|
||||||
|
return failures
|
||||||
|
}
|
||||||
|
|
||||||
|
val resModsData = Gson().fromJson(resMods.body!!.charStream(), GetModsResponse::class.java)
|
||||||
|
resMods.closeQuietly()
|
||||||
|
|
||||||
|
for (mod in resModsData.data) {
|
||||||
|
if (!manualDownloadMods.contains(mod.id)) {
|
||||||
|
failures.add(ExceptionDetails(mod.name,
|
||||||
|
Exception("Failed to find project from result: ID ${mod.id}")))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val modFile = manualDownloadMods[mod.id]!!
|
||||||
|
failures.add(ExceptionDetails(mod.name, Exception("This mod is excluded from the CurseForge API and must be downloaded manually.\n" +
|
||||||
|
"Please go to ${mod.links?.websiteUrl}/files/${modFile.second} and save this file to ${modFile.first.destURI}")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return failures
|
||||||
|
}
|
@@ -0,0 +1,10 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.curseforge
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
class CurseForgeUpdateData: UpdateData {
|
||||||
|
@SerializedName("file-id")
|
||||||
|
var fileId = 0
|
||||||
|
@SerializedName("project-id")
|
||||||
|
var projectId = 0
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.curseforge
|
||||||
|
|
||||||
|
interface UpdateData
|
@@ -0,0 +1,22 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.curseforge
|
||||||
|
|
||||||
|
import com.google.gson.JsonDeserializationContext
|
||||||
|
import com.google.gson.JsonDeserializer
|
||||||
|
import com.google.gson.JsonElement
|
||||||
|
import java.lang.reflect.Type
|
||||||
|
|
||||||
|
class UpdateDeserializer: JsonDeserializer<Map<String, UpdateData>> {
|
||||||
|
override fun deserialize(
|
||||||
|
json: JsonElement?,
|
||||||
|
typeOfT: Type?,
|
||||||
|
context: JsonDeserializationContext?
|
||||||
|
): Map<String, UpdateData> {
|
||||||
|
val out = mutableMapOf<String, UpdateData>()
|
||||||
|
for ((k, v) in json!!.asJsonObject.entrySet()) {
|
||||||
|
if (k == "curseforge") {
|
||||||
|
out[k] = context!!.deserialize(v, CurseForgeUpdateData::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,10 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.hash
|
||||||
|
|
||||||
|
import okio.ForwardingSource
|
||||||
|
import okio.Source
|
||||||
|
|
||||||
|
abstract class GeneralHashingSource(delegate: Source) : ForwardingSource(delegate) {
|
||||||
|
abstract val hash: Hash
|
||||||
|
|
||||||
|
fun hashIsEqual(compareTo: Any) = compareTo == hash
|
||||||
|
}
|
@@ -0,0 +1,34 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.hash
|
||||||
|
|
||||||
|
import com.google.gson.*
|
||||||
|
import java.lang.reflect.Type
|
||||||
|
|
||||||
|
abstract class Hash {
|
||||||
|
protected abstract val stringValue: String
|
||||||
|
protected abstract val type: String
|
||||||
|
|
||||||
|
class TypeHandler : JsonDeserializer<Hash>, JsonSerializer<Hash> {
|
||||||
|
override fun serialize(src: Hash, typeOfSrc: Type, context: JsonSerializationContext): JsonElement = JsonObject().apply {
|
||||||
|
add("type", JsonPrimitive(src.type))
|
||||||
|
add("value", JsonPrimitive(src.stringValue))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(JsonParseException::class)
|
||||||
|
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Hash {
|
||||||
|
val obj = json.asJsonObject
|
||||||
|
val type: String
|
||||||
|
val value: String
|
||||||
|
try {
|
||||||
|
type = obj["type"].asString
|
||||||
|
value = obj["value"].asString
|
||||||
|
} catch (e: NullPointerException) {
|
||||||
|
throw JsonParseException("Invalid hash JSON data")
|
||||||
|
}
|
||||||
|
return try {
|
||||||
|
HashUtils.getHash(type, value)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw JsonParseException("Failed to create hash object", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,22 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.hash
|
||||||
|
|
||||||
|
object HashUtils {
|
||||||
|
private val hashTypeConversion: Map<String, IHasher> = mapOf(
|
||||||
|
"sha256" to HashingSourceHasher("sha256"),
|
||||||
|
"sha512" to HashingSourceHasher("sha512"),
|
||||||
|
"murmur2" to Murmur2Hasher(),
|
||||||
|
"sha1" to HashingSourceHasher("sha1")
|
||||||
|
)
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun getHasher(type: String): IHasher {
|
||||||
|
return hashTypeConversion[type] ?: throw Exception("Hash type not supported: $type")
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun getHash(type: String, value: String): Hash {
|
||||||
|
return hashTypeConversion[type]?.getHash(value) ?: throw Exception("Hash type not supported: $type")
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,46 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.hash
|
||||||
|
|
||||||
|
import okio.HashingSource
|
||||||
|
import okio.Source
|
||||||
|
|
||||||
|
class HashingSourceHasher internal constructor(private val type: String) : IHasher {
|
||||||
|
// i love naming things
|
||||||
|
private inner class HashingSourceGeneralHashingSource(val delegateHashing: HashingSource) : GeneralHashingSource(delegateHashing) {
|
||||||
|
override val hash: Hash by lazy(LazyThreadSafetyMode.NONE) {
|
||||||
|
HashingSourceHash(delegateHashing.hash.hex())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this some funky inner class stuff
|
||||||
|
// each of these classes is specific to the instance of the HasherHashingSource
|
||||||
|
// therefore HashingSourceHashes from different parent instances will be not instanceof each other
|
||||||
|
private inner class HashingSourceHash(val value: String) : Hash() {
|
||||||
|
override val stringValue get() = value
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (other !is HashingSourceHash) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return stringValue.equals(other.stringValue, ignoreCase = true)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String = "$type: $stringValue"
|
||||||
|
override fun hashCode(): Int = value.hashCode()
|
||||||
|
|
||||||
|
override val type: String get() = this@HashingSourceHasher.type
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getHashingSource(delegate: Source): GeneralHashingSource {
|
||||||
|
when (type) {
|
||||||
|
"md5" -> return HashingSourceGeneralHashingSource(HashingSource.md5(delegate))
|
||||||
|
"sha256" -> return HashingSourceGeneralHashingSource(HashingSource.sha256(delegate))
|
||||||
|
"sha512" -> return HashingSourceGeneralHashingSource(HashingSource.sha512(delegate))
|
||||||
|
"sha1" -> return HashingSourceGeneralHashingSource(HashingSource.sha1(delegate))
|
||||||
|
}
|
||||||
|
throw RuntimeException("Invalid hash type provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getHash(value: String): Hash {
|
||||||
|
return HashingSourceHash(value)
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,8 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.hash
|
||||||
|
|
||||||
|
import okio.Source
|
||||||
|
|
||||||
|
interface IHasher {
|
||||||
|
fun getHashingSource(delegate: Source): GeneralHashingSource
|
||||||
|
fun getHash(value: String): Hash
|
||||||
|
}
|
@@ -0,0 +1,91 @@
|
|||||||
|
package link.infra.packwiz.installer.metadata.hash
|
||||||
|
|
||||||
|
import okio.Buffer
|
||||||
|
import okio.Source
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class Murmur2Hasher : IHasher {
|
||||||
|
private inner class Murmur2GeneralHashingSource(delegate: Source) : GeneralHashingSource(delegate) {
|
||||||
|
val internalBuffer = Buffer()
|
||||||
|
val tempBuffer = Buffer()
|
||||||
|
|
||||||
|
override val hash: Hash by lazy(LazyThreadSafetyMode.NONE) {
|
||||||
|
val data = internalBuffer.readByteArray()
|
||||||
|
Murmur2Hash(Murmur2Lib.hash32(data, data.size, 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun read(sink: Buffer, byteCount: Long): Long {
|
||||||
|
val out = delegate.read(tempBuffer, byteCount)
|
||||||
|
if (out > -1) {
|
||||||
|
sink.write(tempBuffer.clone(), out)
|
||||||
|
computeNormalizedBufferFaster(tempBuffer, internalBuffer)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// Credit to https://github.com/modmuss50/CAV2/blob/master/murmur.go
|
||||||
|
// private fun computeNormalizedArray(input: ByteArray): ByteArray {
|
||||||
|
// val output = ByteArray(input.size)
|
||||||
|
// var index = 0
|
||||||
|
// for (b in input) {
|
||||||
|
// when (b) {
|
||||||
|
// 9.toByte(), 10.toByte(), 13.toByte(), 32.toByte() -> {}
|
||||||
|
// else -> {
|
||||||
|
// output[index] = b
|
||||||
|
// index++
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// val outputTrimmed = ByteArray(index)
|
||||||
|
// System.arraycopy(output, 0, outputTrimmed, 0, index)
|
||||||
|
// return outputTrimmed
|
||||||
|
// }
|
||||||
|
|
||||||
|
private fun computeNormalizedBufferFaster(input: Buffer, output: Buffer) {
|
||||||
|
var index = 0
|
||||||
|
val arr = input.readByteArray()
|
||||||
|
for (b in arr) {
|
||||||
|
when (b) {
|
||||||
|
9.toByte(), 10.toByte(), 13.toByte(), 32.toByte() -> {}
|
||||||
|
else -> {
|
||||||
|
arr[index] = b
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output.write(arr, 0, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Murmur2Hash : Hash {
|
||||||
|
val value: Int
|
||||||
|
|
||||||
|
constructor(value: String) {
|
||||||
|
// Parsing as long then casting to int converts values gt int max value but lt uint max value
|
||||||
|
// into negatives. I presume this is how the murmur2 code handles this.
|
||||||
|
this.value = value.toLong().toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(value: Int) {
|
||||||
|
this.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
override val stringValue get() = value.toString()
|
||||||
|
override val type get() = "murmur2"
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (other !is Murmur2Hash) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return value == other.value
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String = "murmur2: $value"
|
||||||
|
override fun hashCode(): Int = value
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getHashingSource(delegate: Source): GeneralHashingSource = Murmur2GeneralHashingSource(delegate)
|
||||||
|
override fun getHash(value: String): Hash = Murmur2Hash(value)
|
||||||
|
}
|
@@ -0,0 +1,52 @@
|
|||||||
|
package link.infra.packwiz.installer.request
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import link.infra.packwiz.installer.request.handlers.RequestHandlerFile
|
||||||
|
import link.infra.packwiz.installer.request.handlers.RequestHandlerGithub
|
||||||
|
import link.infra.packwiz.installer.request.handlers.RequestHandlerHTTP
|
||||||
|
import okio.Source
|
||||||
|
|
||||||
|
object HandlerManager {
|
||||||
|
|
||||||
|
private val handlers: List<IRequestHandler> = listOf(
|
||||||
|
RequestHandlerGithub(),
|
||||||
|
RequestHandlerHTTP(),
|
||||||
|
RequestHandlerFile()
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: get rid of nullable stuff here
|
||||||
|
@JvmStatic
|
||||||
|
fun getNewLoc(base: SpaceSafeURI?, loc: SpaceSafeURI?): SpaceSafeURI? {
|
||||||
|
if (loc == null) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val dest = base?.run { resolve(loc) } ?: loc
|
||||||
|
for (handler in handlers) with (handler) {
|
||||||
|
if (matchesHandler(dest)) {
|
||||||
|
return getNewLoc(dest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dest
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: What if files are read multiple times??
|
||||||
|
// Zip handler discards once read, requesting multiple times on other handlers would cause multiple downloads
|
||||||
|
// Caching system? Copy from already downloaded files?
|
||||||
|
|
||||||
|
// TODO: change to use something more idiomatic than exceptions?
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun getFileSource(loc: SpaceSafeURI): Source {
|
||||||
|
for (handler in handlers) {
|
||||||
|
if (handler.matchesHandler(loc)) {
|
||||||
|
return handler.getFileSource(loc) ?: throw Exception("Couldn't find URI: $loc")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw Exception("No handler available for URI: $loc")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: github toml resolution?
|
||||||
|
// e.g. https://github.com/comp500/Demagnetize -> demagnetize.toml
|
||||||
|
// https://github.com/comp500/Demagnetize/blob/master/demagnetize.toml
|
||||||
|
}
|
@@ -1,19 +1,18 @@
|
|||||||
package link.infra.packwiz.installer.request;
|
package link.infra.packwiz.installer.request
|
||||||
|
|
||||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
import okio.Source;
|
import okio.Source
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IRequestHandler handles requests for locations specified in modpack metadata.
|
* IRequestHandler handles requests for locations specified in modpack metadata.
|
||||||
*/
|
*/
|
||||||
public interface IRequestHandler {
|
interface IRequestHandler {
|
||||||
|
fun matchesHandler(loc: SpaceSafeURI): Boolean
|
||||||
boolean matchesHandler(SpaceSafeURI loc);
|
|
||||||
|
fun getNewLoc(loc: SpaceSafeURI): SpaceSafeURI {
|
||||||
default SpaceSafeURI getNewLoc(SpaceSafeURI loc) {
|
return loc
|
||||||
return loc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the Source for a location. Must be threadsafe.
|
* Gets the Source for a location. Must be threadsafe.
|
||||||
* It is assumed that each location is read only once for the duration of an IRequestHandler.
|
* It is assumed that each location is read only once for the duration of an IRequestHandler.
|
||||||
@@ -21,6 +20,5 @@ public interface IRequestHandler {
|
|||||||
* @return The Source containing the data of the file
|
* @return The Source containing the data of the file
|
||||||
* @throws Exception Exception if it failed to download a file!!!
|
* @throws Exception Exception if it failed to download a file!!!
|
||||||
*/
|
*/
|
||||||
Source getFileSource(SpaceSafeURI loc) throws Exception;
|
fun getFileSource(loc: SpaceSafeURI): Source?
|
||||||
|
}
|
||||||
}
|
|
@@ -0,0 +1,70 @@
|
|||||||
|
package link.infra.packwiz.installer.request
|
||||||
|
|
||||||
|
import okhttp3.Response
|
||||||
|
import okio.IOException
|
||||||
|
|
||||||
|
sealed class RequestException: Exception {
|
||||||
|
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||||
|
constructor(message: String) : super(message)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal errors that should not be shown to the user when the code is correct
|
||||||
|
*/
|
||||||
|
sealed class Internal: RequestException {
|
||||||
|
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||||
|
constructor(message: String) : super(message)
|
||||||
|
|
||||||
|
class UnsinkableBase: Internal("Base associated with this path is not a SinkableBase")
|
||||||
|
|
||||||
|
sealed class HTTP: Internal {
|
||||||
|
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||||
|
constructor(message: String) : super(message)
|
||||||
|
|
||||||
|
class NoResponseBody: HTTP("HTTP response in onResponse must have a response body")
|
||||||
|
class RequestFailed(cause: IOException): HTTP("HTTP request failed; may have been cancelled", cause)
|
||||||
|
class IllegalState(cause: IllegalStateException): HTTP("Internal fatal HTTP request error", cause)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Errors indicating that the request is malformed
|
||||||
|
*/
|
||||||
|
sealed class Validation: RequestException {
|
||||||
|
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||||
|
constructor(message: String) : super(message)
|
||||||
|
|
||||||
|
// TODO: move out of RequestException?
|
||||||
|
class PathContainsNUL(path: String): Validation("Invalid path; contains NUL bytes: ${path.replace("\u0000", "")}")
|
||||||
|
class PathContainsVolumeLetter(path: String): Validation("Invalid path; contains volume letter: $path")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Errors relating to the response from the server
|
||||||
|
*/
|
||||||
|
sealed class Response: RequestException {
|
||||||
|
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||||
|
constructor(message: String) : super(message)
|
||||||
|
|
||||||
|
// TODO: fancier way of displaying this?
|
||||||
|
sealed class HTTP: Response {
|
||||||
|
val response: okhttp3.Response
|
||||||
|
|
||||||
|
constructor(response: okhttp3.Response, message: String, cause: Throwable) : super(message, cause) {
|
||||||
|
this.response = response
|
||||||
|
}
|
||||||
|
constructor(response: okhttp3.Response, message: String) : super(message) {
|
||||||
|
this.response = response
|
||||||
|
}
|
||||||
|
|
||||||
|
class ErrorCode(res: okhttp3.Response): HTTP(res, "Non-successful error code from HTTP request: ${res.code}")
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class File: RequestException {
|
||||||
|
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||||
|
constructor(message: String) : super(message)
|
||||||
|
|
||||||
|
class FileNotFound(file: String): File("File path not found: $file")
|
||||||
|
class Other(cause: Throwable): File("Failed to read file", cause)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,18 @@
|
|||||||
|
package link.infra.packwiz.installer.request.handlers
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import link.infra.packwiz.installer.request.IRequestHandler
|
||||||
|
import okio.Source
|
||||||
|
import okio.source
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
open class RequestHandlerFile : IRequestHandler {
|
||||||
|
override fun matchesHandler(loc: SpaceSafeURI): Boolean {
|
||||||
|
return "file" == loc.scheme
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getFileSource(loc: SpaceSafeURI): Source? {
|
||||||
|
val path = Paths.get(loc.toURL().toURI())
|
||||||
|
return path.source()
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,70 @@
|
|||||||
|
package link.infra.packwiz.installer.request.handlers
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
import kotlin.concurrent.read
|
||||||
|
import kotlin.concurrent.write
|
||||||
|
|
||||||
|
class RequestHandlerGithub : RequestHandlerZip(true) {
|
||||||
|
override fun getNewLoc(loc: SpaceSafeURI): SpaceSafeURI {
|
||||||
|
return loc
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val repoMatcherPattern = Pattern.compile("/([\\w.-]+/[\\w.-]+).*")
|
||||||
|
private val branchMatcherPattern = Pattern.compile("/[\\w.-]+/[\\w.-]+/blob/([\\w.-]+).*")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: is caching really needed, if HTTPURLConnection follows redirects correctly?
|
||||||
|
private val zipUriMap: MutableMap<String, SpaceSafeURI> = HashMap()
|
||||||
|
private val zipUriLock = ReentrantReadWriteLock()
|
||||||
|
private fun getRepoName(loc: SpaceSafeURI): String? {
|
||||||
|
val matcher = repoMatcherPattern.matcher(loc.path ?: return null)
|
||||||
|
return if (matcher.matches()) {
|
||||||
|
matcher.group(1)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getZipUri(loc: SpaceSafeURI): SpaceSafeURI {
|
||||||
|
val repoName = getRepoName(loc)
|
||||||
|
val branchName = getBranch(loc)
|
||||||
|
|
||||||
|
zipUriLock.read {
|
||||||
|
zipUriMap["$repoName/$branchName"]
|
||||||
|
}?.let { return it }
|
||||||
|
|
||||||
|
var zipUri = SpaceSafeURI("https://api.github.com/repos/$repoName/zipball/$branchName")
|
||||||
|
zipUriLock.write {
|
||||||
|
// If another thread sets the value concurrently, use the existing value from the
|
||||||
|
// thread that first acquired the lock.
|
||||||
|
zipUri = zipUriMap.putIfAbsent("$repoName/$branchName", zipUri) ?: zipUri
|
||||||
|
}
|
||||||
|
return zipUri
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getBranch(loc: SpaceSafeURI): String? {
|
||||||
|
val matcher = branchMatcherPattern.matcher(loc.path ?: return null)
|
||||||
|
return if (matcher.matches()) {
|
||||||
|
matcher.group(1)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getLocationInZip(loc: SpaceSafeURI): SpaceSafeURI {
|
||||||
|
val path = "/" + getRepoName(loc) + "/blob/" + getBranch(loc)
|
||||||
|
return SpaceSafeURI(loc.scheme, loc.authority, path, null, null).relativize(loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun matchesHandler(loc: SpaceSafeURI): Boolean {
|
||||||
|
val scheme = loc.scheme
|
||||||
|
if (!("http" == scheme || "https" == scheme)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// TODO: more match testing?
|
||||||
|
return "github.com" == loc.host && branchMatcherPattern.matcher(loc.path ?: return false).matches()
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,29 @@
|
|||||||
|
package link.infra.packwiz.installer.request.handlers
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import link.infra.packwiz.installer.request.IRequestHandler
|
||||||
|
import okio.Source
|
||||||
|
import okio.source
|
||||||
|
import java.net.HttpURLConnection
|
||||||
|
|
||||||
|
open class RequestHandlerHTTP : IRequestHandler {
|
||||||
|
override fun matchesHandler(loc: SpaceSafeURI): Boolean {
|
||||||
|
val scheme = loc.scheme
|
||||||
|
return "http" == scheme || "https" == scheme
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getFileSource(loc: SpaceSafeURI): Source? {
|
||||||
|
val conn = loc.toURL().openConnection() as HttpURLConnection
|
||||||
|
// TODO: when do we send specific headers??? should there be a way to signal this?
|
||||||
|
conn.addRequestProperty("Accept", "application/octet-stream")
|
||||||
|
// TODO: include version?
|
||||||
|
conn.addRequestProperty("User-Agent", "packwiz-installer")
|
||||||
|
|
||||||
|
conn.apply {
|
||||||
|
// 30 second read timeout
|
||||||
|
readTimeout = 30 * 1000
|
||||||
|
requestMethod = "GET"
|
||||||
|
}
|
||||||
|
return conn.inputStream.source()
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,123 @@
|
|||||||
|
package link.infra.packwiz.installer.request.handlers
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||||
|
import okio.Buffer
|
||||||
|
import okio.Source
|
||||||
|
import okio.buffer
|
||||||
|
import okio.source
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.locks.ReentrantLock
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||||
|
import java.util.function.Predicate
|
||||||
|
import java.util.zip.ZipEntry
|
||||||
|
import java.util.zip.ZipInputStream
|
||||||
|
import kotlin.concurrent.read
|
||||||
|
import kotlin.concurrent.withLock
|
||||||
|
import kotlin.concurrent.write
|
||||||
|
|
||||||
|
abstract class RequestHandlerZip(private val modeHasFolder: Boolean) : RequestHandlerHTTP() {
|
||||||
|
private fun removeFolder(name: String): String {
|
||||||
|
return if (modeHasFolder) {
|
||||||
|
// TODO: replace with proper path checks once switched to Path??
|
||||||
|
name.substring(name.indexOf("/") + 1)
|
||||||
|
} else {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private inner class ZipReader(zip: Source) {
|
||||||
|
private val zis = ZipInputStream(zip.buffer().inputStream())
|
||||||
|
private val readFiles: MutableMap<SpaceSafeURI, Buffer> = HashMap()
|
||||||
|
// Write lock implies access to ZipInputStream - only 1 thread must read at a time!
|
||||||
|
val filesLock = ReentrantLock()
|
||||||
|
private var entry: ZipEntry? = null
|
||||||
|
|
||||||
|
private val zipSource = zis.source().buffer()
|
||||||
|
|
||||||
|
// File lock must be obtained before calling this function
|
||||||
|
private fun readCurrFile(): Buffer {
|
||||||
|
val fileBuffer = Buffer()
|
||||||
|
zipSource.readFully(fileBuffer, entry!!.size)
|
||||||
|
return fileBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
// File lock must be obtained before calling this function
|
||||||
|
private fun findFile(loc: SpaceSafeURI): Buffer? {
|
||||||
|
while (true) {
|
||||||
|
entry = zis.nextEntry
|
||||||
|
entry?.also {
|
||||||
|
val data = readCurrFile()
|
||||||
|
val fileLoc = SpaceSafeURI(removeFolder(it.name))
|
||||||
|
if (loc == fileLoc) {
|
||||||
|
return data
|
||||||
|
} else {
|
||||||
|
readFiles[fileLoc] = data
|
||||||
|
}
|
||||||
|
} ?: return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getFileSource(loc: SpaceSafeURI): Source? {
|
||||||
|
filesLock.withLock {
|
||||||
|
// Assume files are only read once, allow GC by removing
|
||||||
|
readFiles.remove(loc)?.also { return it }
|
||||||
|
return findFile(loc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findInZip(matches: Predicate<SpaceSafeURI>): SpaceSafeURI? {
|
||||||
|
filesLock.withLock {
|
||||||
|
readFiles.keys.find { matches.test(it) }?.let { return it }
|
||||||
|
|
||||||
|
do {
|
||||||
|
val entry = zis.nextEntry?.also {
|
||||||
|
val data = readCurrFile()
|
||||||
|
val fileLoc = SpaceSafeURI(removeFolder(it.name))
|
||||||
|
readFiles[fileLoc] = data
|
||||||
|
if (matches.test(fileLoc)) {
|
||||||
|
return fileLoc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (entry != null)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val cache: MutableMap<SpaceSafeURI, ZipReader> = HashMap()
|
||||||
|
private val cacheLock = ReentrantReadWriteLock()
|
||||||
|
|
||||||
|
protected abstract fun getZipUri(loc: SpaceSafeURI): SpaceSafeURI
|
||||||
|
protected abstract fun getLocationInZip(loc: SpaceSafeURI): SpaceSafeURI
|
||||||
|
abstract override fun matchesHandler(loc: SpaceSafeURI): Boolean
|
||||||
|
|
||||||
|
override fun getFileSource(loc: SpaceSafeURI): Source? {
|
||||||
|
val zipUri = getZipUri(loc)
|
||||||
|
var zr = cacheLock.read { cache[zipUri] }
|
||||||
|
if (zr == null) {
|
||||||
|
cacheLock.write {
|
||||||
|
// Recheck, because unlocking read lock allows another thread to modify it
|
||||||
|
zr = cache[zipUri]
|
||||||
|
|
||||||
|
if (zr == null) {
|
||||||
|
val src = super.getFileSource(zipUri) ?: return null
|
||||||
|
zr = ZipReader(src).also { cache[zipUri] = it }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return zr?.getFileSource(getLocationInZip(loc))
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun findInZip(loc: SpaceSafeURI, matches: Predicate<SpaceSafeURI>): SpaceSafeURI? {
|
||||||
|
val zipUri = getZipUri(loc)
|
||||||
|
return (cacheLock.read { cache[zipUri] } ?: cacheLock.write {
|
||||||
|
// Recheck, because unlocking read lock allows another thread to modify it
|
||||||
|
cache[zipUri] ?: run {
|
||||||
|
// Create the ZipReader if it doesn't exist, return null if getFileSource returns null
|
||||||
|
super.getFileSource(zipUri)?.let { ZipReader(it) }
|
||||||
|
?.also { cache[zipUri] = it }
|
||||||
|
}
|
||||||
|
})?.findInZip(matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,23 @@
|
|||||||
|
package link.infra.packwiz.installer.target
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
data class CachedTarget(
|
||||||
|
/**
|
||||||
|
* @see Target.name
|
||||||
|
*/
|
||||||
|
val name: String,
|
||||||
|
/**
|
||||||
|
* The location where the target was last downloaded to.
|
||||||
|
* This is used for removing old files when the destination path changes.
|
||||||
|
* This shouldn't be set to the .disabled path (that is manually appended and checked)
|
||||||
|
*/
|
||||||
|
val cachedLocation: Path,
|
||||||
|
val enabled: Boolean,
|
||||||
|
val hash: Hash,
|
||||||
|
/**
|
||||||
|
* For detecting when a target transitions non-optional -> optional and showing the option selection screen
|
||||||
|
*/
|
||||||
|
val isOptional: Boolean
|
||||||
|
)
|
@@ -0,0 +1,95 @@
|
|||||||
|
package link.infra.packwiz.installer.target
|
||||||
|
|
||||||
|
import java.io.IOException
|
||||||
|
import java.nio.file.*
|
||||||
|
import java.nio.file.attribute.BasicFileAttributes
|
||||||
|
import kotlin.io.path.relativeTo
|
||||||
|
|
||||||
|
data class CachedTargetStatus(val target: CachedTarget, var isValid: Boolean, var markDisabled: Boolean)
|
||||||
|
|
||||||
|
fun validate(targets: List<CachedTarget>, baseDir: Path) = runCatching {
|
||||||
|
val results = targets.map {
|
||||||
|
CachedTargetStatus(it, isValid = false, markDisabled = false)
|
||||||
|
}
|
||||||
|
val tree = buildTree(results, baseDir)
|
||||||
|
|
||||||
|
// Efficient file exists checking using directory listing, several orders of magnitude faster than Files.exists calls
|
||||||
|
Files.walkFileTree(baseDir, setOf(FileVisitOption.FOLLOW_LINKS), Int.MAX_VALUE, object : FileVisitor<Path> {
|
||||||
|
var currentNode: PathNode<CachedTargetStatus> = tree
|
||||||
|
|
||||||
|
override fun preVisitDirectory(dir: Path?, attrs: BasicFileAttributes?): FileVisitResult {
|
||||||
|
if (dir == null) {
|
||||||
|
return FileVisitResult.SKIP_SUBTREE
|
||||||
|
}
|
||||||
|
val subdirNode = currentNode.subdirs[dir.getName(dir.nameCount - 1)]
|
||||||
|
return if (subdirNode != null) {
|
||||||
|
currentNode = subdirNode
|
||||||
|
FileVisitResult.CONTINUE
|
||||||
|
} else {
|
||||||
|
FileVisitResult.SKIP_SUBTREE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitFile(file: Path?, attrs: BasicFileAttributes?): FileVisitResult {
|
||||||
|
if (file == null) {
|
||||||
|
return FileVisitResult.CONTINUE
|
||||||
|
}
|
||||||
|
// TODO: these are relative paths to baseDir
|
||||||
|
// TODO: strip the .disabled for lookup
|
||||||
|
val target = currentNode.files[file.getName(file.nameCount - 1)]
|
||||||
|
if (target != null) {
|
||||||
|
val disabledFile = file.endsWith(".disabled")
|
||||||
|
// If a .disabled file and the actual file both exist, mark as invalid if the target is disabled
|
||||||
|
if ((disabledFile )) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return FileVisitResult.CONTINUE
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun visitFileFailed(file: Path?, exc: IOException?): FileVisitResult {
|
||||||
|
if (exc != null) {
|
||||||
|
throw exc
|
||||||
|
}
|
||||||
|
throw IOException("visitFileFailed called with no exception")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun postVisitDirectory(dir: Path?, exc: IOException?): FileVisitResult {
|
||||||
|
if (exc != null) {
|
||||||
|
throw exc
|
||||||
|
} else {
|
||||||
|
val parent = currentNode.parent
|
||||||
|
if (parent != null) {
|
||||||
|
currentNode = parent
|
||||||
|
} else {
|
||||||
|
throw IOException("Invalid visitor tree structure")
|
||||||
|
}
|
||||||
|
return FileVisitResult.CONTINUE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
|
fun buildTree(targets: List<CachedTargetStatus>, baseDir: Path): PathNode<CachedTargetStatus> {
|
||||||
|
val root = PathNode<CachedTargetStatus>()
|
||||||
|
for (target in targets) {
|
||||||
|
val relPath = target.target.cachedLocation.relativeTo(baseDir)
|
||||||
|
var node = root
|
||||||
|
// Traverse all the directory components, except for the last one
|
||||||
|
for (i in 0 until (relPath.nameCount - 1)) {
|
||||||
|
node = node.createSubdir(relPath.getName(i))
|
||||||
|
}
|
||||||
|
node.files[relPath.getName(relPath.nameCount - 1)] = target
|
||||||
|
}
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
data class PathNode<T>(val subdirs: MutableMap<Path, PathNode<T>>, val files: MutableMap<Path, T>, val parent: PathNode<T>?) {
|
||||||
|
constructor() : this(mutableMapOf(), mutableMapOf(), null)
|
||||||
|
|
||||||
|
fun createSubdir(nextComponent: Path) = subdirs.getOrPut(nextComponent, { PathNode(mutableMapOf(), mutableMapOf(), this) })
|
||||||
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
package link.infra.packwiz.installer.target
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okio.FileSystem
|
||||||
|
|
||||||
|
class ClientHolder {
|
||||||
|
// TODO: timeouts?
|
||||||
|
// TODO: a button to increase timeouts temporarily when retrying?
|
||||||
|
val okHttpClient by lazy { OkHttpClient.Builder().build() }
|
||||||
|
|
||||||
|
val fileSystem = FileSystem.SYSTEM
|
||||||
|
}
|
@@ -0,0 +1,13 @@
|
|||||||
|
package link.infra.packwiz.installer.target
|
||||||
|
|
||||||
|
enum class OverwriteMode {
|
||||||
|
/**
|
||||||
|
* Overwrite the destination with the source file, if the source file has changed.
|
||||||
|
*/
|
||||||
|
IF_SRC_CHANGED,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Never overwrite the destination; if it exists, it should not be written to.
|
||||||
|
*/
|
||||||
|
NEVER
|
||||||
|
}
|
54
src/main/kotlin/link/infra/packwiz/installer/target/Side.kt
Normal file
54
src/main/kotlin/link/infra/packwiz/installer/target/Side.kt
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package link.infra.packwiz.installer.target
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
enum class Side {
|
||||||
|
@SerializedName("client")
|
||||||
|
CLIENT("client"),
|
||||||
|
@SerializedName("server")
|
||||||
|
SERVER("server"),
|
||||||
|
@SerializedName("both")
|
||||||
|
@Suppress("unused")
|
||||||
|
BOTH("both", arrayOf(CLIENT, SERVER));
|
||||||
|
|
||||||
|
private val sideName: String
|
||||||
|
private val depSides: Array<Side>?
|
||||||
|
|
||||||
|
constructor(sideName: String) {
|
||||||
|
this.sideName = sideName.toLowerCase()
|
||||||
|
depSides = null
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(sideName: String, depSides: Array<Side>) {
|
||||||
|
this.sideName = sideName.toLowerCase()
|
||||||
|
this.depSides = depSides
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString() = sideName
|
||||||
|
|
||||||
|
fun hasSide(tSide: Side): Boolean {
|
||||||
|
if (this == tSide) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (depSides != null) {
|
||||||
|
for (depSide in depSides) {
|
||||||
|
if (depSide == tSide) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(name: String): Side? {
|
||||||
|
val lowerName = name.toLowerCase()
|
||||||
|
for (side in values()) {
|
||||||
|
if (side.sideName == lowerName) {
|
||||||
|
return side
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,45 @@
|
|||||||
|
package link.infra.packwiz.installer.target
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.target.path.PackwizPath
|
||||||
|
|
||||||
|
// TODO: rename to avoid conflicting with @Target
|
||||||
|
interface Target {
|
||||||
|
val src: PackwizPath
|
||||||
|
val dest: PackwizPath
|
||||||
|
val validityToken: ValidityToken
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token interface for types used to compare Target identity. Implementations should use equals to indicate that
|
||||||
|
* these tokens represent the same file; used to preserve optional target choices between file renames.
|
||||||
|
*/
|
||||||
|
interface IdentityToken
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation of IdentityToken that assumes files are not renamed; so optional choices do not need to
|
||||||
|
* be preserved across renames.
|
||||||
|
*/
|
||||||
|
@JvmInline
|
||||||
|
value class PathIdentityToken(val path: String): IdentityToken
|
||||||
|
|
||||||
|
val ident: IdentityToken
|
||||||
|
get() = PathIdentityToken(dest.path)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A user-friendly name; defaults to the destination path of the file.
|
||||||
|
*/
|
||||||
|
val name: String
|
||||||
|
get() = dest.path
|
||||||
|
val side: Side
|
||||||
|
get() = Side.BOTH
|
||||||
|
val overwriteMode: OverwriteMode
|
||||||
|
get() = OverwriteMode.IF_SRC_CHANGED
|
||||||
|
|
||||||
|
interface Optional: Target {
|
||||||
|
val optional: Boolean
|
||||||
|
val optionalDefaultValue: Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OptionalDescribed: Optional {
|
||||||
|
val optionalDescription: String
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,16 @@
|
|||||||
|
package link.infra.packwiz.installer.target
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A token used to determine if the source or destination file is changed, and ensure that the destination file is valid.
|
||||||
|
*/
|
||||||
|
interface ValidityToken {
|
||||||
|
// TODO: functions to allow validating this from file metadata, from file, or during the download process
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation of ValidityToken based on a single hash.
|
||||||
|
*/
|
||||||
|
@JvmInline
|
||||||
|
value class HashValidityToken(val hash: Hash): ValidityToken
|
||||||
|
}
|
@@ -0,0 +1,29 @@
|
|||||||
|
package link.infra.packwiz.installer.target.path
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.request.RequestException
|
||||||
|
import link.infra.packwiz.installer.target.ClientHolder
|
||||||
|
import okio.*
|
||||||
|
|
||||||
|
data class FilePathBase(private val path: Path): PackwizPath.Base, PackwizPath.SinkableBase {
|
||||||
|
override fun source(path: String, clientHolder: ClientHolder): BufferedSource {
|
||||||
|
val resolved = this.path.resolve(path, true)
|
||||||
|
try {
|
||||||
|
return clientHolder.fileSystem.source(resolved).buffer()
|
||||||
|
} catch (e: FileNotFoundException) {
|
||||||
|
throw RequestException.Response.File.FileNotFound(resolved.toString())
|
||||||
|
} catch (e: IOException) {
|
||||||
|
throw RequestException.Response.File.Other(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun sink(path: String, clientHolder: ClientHolder): BufferedSink {
|
||||||
|
val resolved = this.path.resolve(path, true)
|
||||||
|
try {
|
||||||
|
return clientHolder.fileSystem.sink(resolved).buffer()
|
||||||
|
} catch (e: FileNotFoundException) {
|
||||||
|
throw RequestException.Response.File.FileNotFound(resolved.toString())
|
||||||
|
} catch (e: IOException) {
|
||||||
|
throw RequestException.Response.File.Other(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,41 @@
|
|||||||
|
package link.infra.packwiz.installer.target.path
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.request.RequestException
|
||||||
|
import link.infra.packwiz.installer.target.ClientHolder
|
||||||
|
import okhttp3.HttpUrl
|
||||||
|
import okhttp3.Request
|
||||||
|
import okio.BufferedSource
|
||||||
|
import okio.IOException
|
||||||
|
|
||||||
|
data class HttpUrlBase(private val url: HttpUrl): PackwizPath.Base {
|
||||||
|
private fun resolve(path: String) = url.newBuilder().addPathSegments(path).build()
|
||||||
|
|
||||||
|
override fun source(path: String, clientHolder: ClientHolder): BufferedSource {
|
||||||
|
val req = Request.Builder()
|
||||||
|
.url(resolve(path))
|
||||||
|
.header("Accept", "application/octet-stream")
|
||||||
|
.header("User-Agent", "packwiz-installer")
|
||||||
|
.get()
|
||||||
|
.build()
|
||||||
|
try {
|
||||||
|
val res = clientHolder.okHttpClient.newCall(req).execute()
|
||||||
|
// Can't use .use since it would close the response body before returning it to the caller
|
||||||
|
try {
|
||||||
|
if (!res.isSuccessful) {
|
||||||
|
throw RequestException.Response.HTTP.ErrorCode(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
val body = res.body ?: throw RequestException.Internal.HTTP.NoResponseBody()
|
||||||
|
return body.source()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// If an exception is thrown, close the response and rethrow
|
||||||
|
res.close()
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
throw RequestException.Internal.HTTP.RequestFailed(e)
|
||||||
|
} catch (e: IllegalStateException) {
|
||||||
|
throw RequestException.Internal.HTTP.IllegalState(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,124 @@
|
|||||||
|
package link.infra.packwiz.installer.target.path
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.request.RequestException
|
||||||
|
import link.infra.packwiz.installer.target.ClientHolder
|
||||||
|
import okio.BufferedSink
|
||||||
|
import okio.BufferedSource
|
||||||
|
|
||||||
|
class PackwizPath(path: String, base: Base) {
|
||||||
|
val path: String
|
||||||
|
val base: Base
|
||||||
|
|
||||||
|
init {
|
||||||
|
this.base = base
|
||||||
|
|
||||||
|
// Check for NUL bytes
|
||||||
|
if (path.contains('\u0000')) { throw RequestException.Validation.PathContainsNUL(path) }
|
||||||
|
// Normalise separator, to prevent differences between Unix/Windows
|
||||||
|
val pathNorm = path.replace('\\', '/')
|
||||||
|
// Split, create new lists for output
|
||||||
|
val split = pathNorm.split('/')
|
||||||
|
val canonicalised = mutableListOf<String>()
|
||||||
|
|
||||||
|
// Backward pass: collapse ".." components, remove "." components and empty components (except an empty component at the end; indicating a folder)
|
||||||
|
var parentComponentCount = 0
|
||||||
|
var first = true
|
||||||
|
for (component in split.asReversed()) {
|
||||||
|
if (first) {
|
||||||
|
first = false
|
||||||
|
if (component == "") {
|
||||||
|
canonicalised += component
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// URL-encoded . is normalised
|
||||||
|
val componentNorm = component.replace("%2e", ".")
|
||||||
|
if (componentNorm == "." || componentNorm == "") {
|
||||||
|
// Do nothing
|
||||||
|
} else if (componentNorm == "..") {
|
||||||
|
parentComponentCount++
|
||||||
|
} else if (parentComponentCount > 0) {
|
||||||
|
parentComponentCount--
|
||||||
|
} else {
|
||||||
|
canonicalised += componentNorm
|
||||||
|
// Don't allow volume letters (allows traversal to the root on Windows)
|
||||||
|
if (componentNorm[0] in 'a'..'z' || componentNorm[0] in 'A'..'Z') {
|
||||||
|
if (componentNorm[1] == ':') {
|
||||||
|
throw RequestException.Validation.PathContainsVolumeLetter(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join path
|
||||||
|
this.path = canonicalised.asReversed().joinToString("/")
|
||||||
|
}
|
||||||
|
|
||||||
|
val folder: Boolean get() = path.endsWith("/")
|
||||||
|
|
||||||
|
fun resolve(path: String): PackwizPath {
|
||||||
|
return if (path.startsWith('/') || path.startsWith('\\')) {
|
||||||
|
// Absolute (but still relative to base of pack)
|
||||||
|
PackwizPath(path, base)
|
||||||
|
} else if (folder) {
|
||||||
|
// File in folder; append
|
||||||
|
PackwizPath(this.path + path, base)
|
||||||
|
} else {
|
||||||
|
// File in parent folder; append with parent component
|
||||||
|
PackwizPath(this.path + "/../" + path, base)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtain a BufferedSource for this path
|
||||||
|
* @throws RequestException When resolving the file failed
|
||||||
|
*/
|
||||||
|
fun source(clientHolder: ClientHolder): BufferedSource = base.source(path, clientHolder)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtain a BufferedSink for this path
|
||||||
|
* @throws RequestException.Internal.UnsinkableBase When the base of this path does not have a sink
|
||||||
|
* @throws RequestException When resolving the file failed
|
||||||
|
*/
|
||||||
|
fun sink(clientHolder: ClientHolder): BufferedSink =
|
||||||
|
if (base is SinkableBase) { base.sink(path, clientHolder) } else { throw RequestException.Internal.UnsinkableBase() }
|
||||||
|
|
||||||
|
interface Base {
|
||||||
|
/**
|
||||||
|
* Resolve the given (canonical) path against the base, and get a BufferedSource for this file.
|
||||||
|
* @throws RequestException
|
||||||
|
*/
|
||||||
|
fun source(path: String, clientHolder: ClientHolder): BufferedSource
|
||||||
|
|
||||||
|
operator fun div(path: String) = PackwizPath(path, this)
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SinkableBase: Base {
|
||||||
|
/**
|
||||||
|
* Resolve the given (canonical) path against the base, and get a BufferedSink for this file.
|
||||||
|
* @throws RequestException
|
||||||
|
*/
|
||||||
|
fun sink(path: String, clientHolder: ClientHolder): BufferedSink
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (javaClass != other?.javaClass) return false
|
||||||
|
|
||||||
|
other as PackwizPath
|
||||||
|
|
||||||
|
if (path != other.path) return false
|
||||||
|
if (base != other.base) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = path.hashCode()
|
||||||
|
result = 31 * result + base.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "base=$base; $path"
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
package link.infra.packwiz.installer.task
|
||||||
|
|
||||||
|
data class CacheKey<T>(val key: String, val version: Int)
|
@@ -0,0 +1,22 @@
|
|||||||
|
package link.infra.packwiz.installer.task
|
||||||
|
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
class CacheManager {
|
||||||
|
class CacheValue<T> {
|
||||||
|
operator fun getValue(thisVal: Any?, property: KProperty<*>): T {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun setValue(thisVal: Any?, property: KProperty<*>, value: T) {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun <T> get(cacheKey: CacheKey<T>): CacheValue<T> {
|
||||||
|
return CacheValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
20
src/main/kotlin/link/infra/packwiz/installer/task/Task.kt
Normal file
20
src/main/kotlin/link/infra/packwiz/installer/task/Task.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package link.infra.packwiz.installer.task
|
||||||
|
|
||||||
|
import kotlin.reflect.KMutableProperty0
|
||||||
|
|
||||||
|
// TODO: task processing on 1 background thread; actual resolving of values calls out to a thread group
|
||||||
|
// TODO: progress bar is updated from each of these tasks
|
||||||
|
// TODO: have everything be lazy so there's no need to determine task ordering upfront? a bit like rust async - task results must be queried to occur
|
||||||
|
|
||||||
|
abstract class Task<T>(protected val ctx: TaskContext): TaskInput<T> {
|
||||||
|
// TODO: lazy wrapper for fallible results
|
||||||
|
// TODO: multithreaded fanout subclass/helper
|
||||||
|
|
||||||
|
protected fun <T> wasUpdated(value: KMutableProperty0<T>, newValue: T): Boolean {
|
||||||
|
if (value.get() == newValue) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
value.set(newValue)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,6 @@
|
|||||||
|
package link.infra.packwiz.installer.task
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An object for storing results where result and upToDate are calculated simultaneously
|
||||||
|
*/
|
||||||
|
data class TaskCombinedResult<T>(val result: T, val upToDate: Boolean)
|
@@ -0,0 +1,12 @@
|
|||||||
|
package link.infra.packwiz.installer.task
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.target.ClientHolder
|
||||||
|
|
||||||
|
class TaskContext {
|
||||||
|
// TODO: thread pools, protocol roots
|
||||||
|
// TODO: cache management
|
||||||
|
|
||||||
|
val cache = CacheManager()
|
||||||
|
|
||||||
|
val clients = ClientHolder()
|
||||||
|
}
|
@@ -0,0 +1,29 @@
|
|||||||
|
package link.infra.packwiz.installer.task
|
||||||
|
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
interface TaskInput<T> {
|
||||||
|
/**
|
||||||
|
* The value of this task input. May be lazily evaluated; must be threadsafe.
|
||||||
|
*/
|
||||||
|
val value: T
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True if the effective value of this input has changed since the task was last run.
|
||||||
|
* Doesn't require evaluation of the input value; should use cached data if possible.
|
||||||
|
* May be lazily evaluated; must be threadsafe.
|
||||||
|
*/
|
||||||
|
val upToDate: Boolean
|
||||||
|
|
||||||
|
operator fun getValue(thisVal: Any?, property: KProperty<*>): T = value
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun <T> raw(value: T): TaskInput<T> {
|
||||||
|
return object: TaskInput<T> {
|
||||||
|
override val value = value
|
||||||
|
override val upToDate: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,6 @@
|
|||||||
|
package link.infra.packwiz.installer.task.formats.packwizv1
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import link.infra.packwiz.installer.target.path.PackwizPath
|
||||||
|
|
||||||
|
data class PackwizV1PackFile(val name: String, val indexPath: PackwizPath, val indexHash: Hash)
|
@@ -0,0 +1,48 @@
|
|||||||
|
package link.infra.packwiz.installer.task.formats.packwizv1
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
import com.moandjiezana.toml.Toml
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.Hash
|
||||||
|
import link.infra.packwiz.installer.metadata.hash.HashUtils
|
||||||
|
import link.infra.packwiz.installer.target.path.PackwizPath
|
||||||
|
import link.infra.packwiz.installer.task.CacheKey
|
||||||
|
import link.infra.packwiz.installer.task.Task
|
||||||
|
import link.infra.packwiz.installer.task.TaskCombinedResult
|
||||||
|
import link.infra.packwiz.installer.task.TaskContext
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
|
||||||
|
class PackwizV1PackTomlTask(ctx: TaskContext, val path: PackwizPath): Task<PackwizV1PackFile>(ctx) {
|
||||||
|
// TODO: make hierarchically defined by caller? - then changing the pack format type doesn't leave junk in the cache
|
||||||
|
private var cache by ctx.cache[CacheKey<Hash>("packwiz.v1.packtoml.hash", 1)]
|
||||||
|
|
||||||
|
private class PackFile {
|
||||||
|
var name: String? = null
|
||||||
|
var index: IndexFileLoc? = null
|
||||||
|
|
||||||
|
class IndexFileLoc {
|
||||||
|
var file: String? = null
|
||||||
|
@SerializedName("hash-format")
|
||||||
|
var hashFormat: String? = null
|
||||||
|
var hash: String? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
var versions: Map<String, String>? = null
|
||||||
|
}
|
||||||
|
|
||||||
|
private val internalResult by lazy {
|
||||||
|
// TODO: query, parse JSON
|
||||||
|
val packFile = Toml().read(InputStreamReader(path.source(ctx.clients).inputStream(), "UTF-8")).to(PackFile::class.java)
|
||||||
|
|
||||||
|
val resolved = PackwizV1PackFile(packFile.name ?: throw RuntimeException("Name required"), // TODO: better exception handling
|
||||||
|
path.resolve(packFile.index?.file ?: throw RuntimeException("File required")),
|
||||||
|
HashUtils.getHash(packFile.index?.hashFormat ?: throw RuntimeException("Hash format required"),
|
||||||
|
packFile.index?.hash ?: throw RuntimeException("Hash required"))
|
||||||
|
)
|
||||||
|
val hash = HashUtils.getHash("sha256", "whatever was just read")
|
||||||
|
|
||||||
|
TaskCombinedResult(resolved, wasUpdated(::cache, hash))
|
||||||
|
}
|
||||||
|
|
||||||
|
override val value by internalResult::result
|
||||||
|
override val upToDate by internalResult::upToDate
|
||||||
|
}
|
@@ -0,0 +1,40 @@
|
|||||||
|
package link.infra.packwiz.installer.ui
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.data.ExceptionDetails
|
||||||
|
import link.infra.packwiz.installer.ui.data.IOptionDetails
|
||||||
|
import link.infra.packwiz.installer.ui.data.InstallProgress
|
||||||
|
|
||||||
|
interface IUserInterface {
|
||||||
|
fun show()
|
||||||
|
fun dispose()
|
||||||
|
|
||||||
|
fun showErrorAndExit(message: String): Nothing {
|
||||||
|
showErrorAndExit(message, null)
|
||||||
|
}
|
||||||
|
fun showErrorAndExit(message: String, e: Exception?): Nothing
|
||||||
|
|
||||||
|
var title: String
|
||||||
|
fun submitProgress(progress: InstallProgress)
|
||||||
|
// Return true if the installation was cancelled!
|
||||||
|
fun showOptions(options: List<IOptionDetails>): Boolean
|
||||||
|
|
||||||
|
fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult
|
||||||
|
fun disableOptionsButton(hasOptions: Boolean) {}
|
||||||
|
|
||||||
|
fun showCancellationDialog(): CancellationResult = CancellationResult.QUIT
|
||||||
|
|
||||||
|
fun awaitOptionalButton(showCancel: Boolean)
|
||||||
|
|
||||||
|
enum class ExceptionListResult {
|
||||||
|
CONTINUE, CANCEL, IGNORE
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class CancellationResult {
|
||||||
|
QUIT, CONTINUE
|
||||||
|
}
|
||||||
|
|
||||||
|
var optionsButtonPressed: Boolean
|
||||||
|
var cancelButtonPressed: Boolean
|
||||||
|
|
||||||
|
var firstInstall: Boolean
|
||||||
|
}
|
@@ -0,0 +1,66 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.cli
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult
|
||||||
|
import link.infra.packwiz.installer.ui.data.ExceptionDetails
|
||||||
|
import link.infra.packwiz.installer.ui.data.IOptionDetails
|
||||||
|
import link.infra.packwiz.installer.ui.data.InstallProgress
|
||||||
|
import link.infra.packwiz.installer.util.Log
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
|
class CLIHandler : IUserInterface {
|
||||||
|
@Volatile
|
||||||
|
override var optionsButtonPressed = false
|
||||||
|
@Volatile
|
||||||
|
override var cancelButtonPressed = false
|
||||||
|
@Volatile
|
||||||
|
override var firstInstall = false
|
||||||
|
|
||||||
|
override var title: String = ""
|
||||||
|
|
||||||
|
override fun showErrorAndExit(message: String, e: Exception?): Nothing {
|
||||||
|
if (e != null) {
|
||||||
|
Log.fatal(message, e)
|
||||||
|
} else {
|
||||||
|
Log.fatal(message)
|
||||||
|
}
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun show() {}
|
||||||
|
override fun dispose() {}
|
||||||
|
override fun submitProgress(progress: InstallProgress) {
|
||||||
|
val sb = StringBuilder()
|
||||||
|
if (progress.hasProgress) {
|
||||||
|
sb.append('(')
|
||||||
|
sb.append(progress.progress)
|
||||||
|
sb.append('/')
|
||||||
|
sb.append(progress.progressTotal)
|
||||||
|
sb.append(") ")
|
||||||
|
}
|
||||||
|
sb.append(progress.message)
|
||||||
|
println(sb.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun showOptions(options: List<IOptionDetails>): Boolean {
|
||||||
|
for (opt in options) {
|
||||||
|
opt.optionValue = true
|
||||||
|
// TODO: implement option choice in the CLI?
|
||||||
|
Log.warn("Accepting option ${opt.name} as option choosing is not implemented in the CLI")
|
||||||
|
}
|
||||||
|
return false // Can't be cancelled!
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult {
|
||||||
|
println("Failed to download modpack, the following errors were encountered:")
|
||||||
|
for (ex in exceptions) {
|
||||||
|
print(ex.name + ": ")
|
||||||
|
ex.exception.printStackTrace()
|
||||||
|
}
|
||||||
|
return ExceptionListResult.CANCEL
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun awaitOptionalButton(showCancel: Boolean) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,6 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.data
|
||||||
|
|
||||||
|
data class ExceptionDetails(
|
||||||
|
val name: String,
|
||||||
|
val exception: Exception
|
||||||
|
)
|
@@ -0,0 +1,7 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.data
|
||||||
|
|
||||||
|
interface IOptionDetails {
|
||||||
|
val name: String
|
||||||
|
var optionValue: Boolean
|
||||||
|
val optionDescription: String
|
||||||
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.data
|
||||||
|
|
||||||
|
data class InstallProgress(
|
||||||
|
val message: String,
|
||||||
|
val hasProgress: Boolean = false,
|
||||||
|
val progress: Int = 0,
|
||||||
|
val progressTotal: Int = 0
|
||||||
|
) {
|
||||||
|
constructor(message: String, progress: Int, progressTotal: Int) : this(message, true, progress, progressTotal)
|
||||||
|
|
||||||
|
constructor(message: String) : this(message, false)
|
||||||
|
}
|
@@ -0,0 +1,153 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.gui
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface
|
||||||
|
import link.infra.packwiz.installer.ui.data.ExceptionDetails
|
||||||
|
import java.awt.BorderLayout
|
||||||
|
import java.awt.Desktop
|
||||||
|
import java.awt.event.WindowAdapter
|
||||||
|
import java.awt.event.WindowEvent
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.PrintWriter
|
||||||
|
import java.io.StringWriter
|
||||||
|
import java.net.URI
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
import java.util.concurrent.CompletableFuture
|
||||||
|
import javax.swing.*
|
||||||
|
import javax.swing.border.EmptyBorder
|
||||||
|
|
||||||
|
class ExceptionListWindow(eList: List<ExceptionDetails>, future: CompletableFuture<IUserInterface.ExceptionListResult>, numTotal: Int, allowsIgnore: Boolean, parentWindow: JFrame?) : JDialog(parentWindow, "Failed file downloads", true) {
|
||||||
|
private val lblExceptionStacktrace: JTextArea
|
||||||
|
|
||||||
|
private class ExceptionListModel(private val details: List<ExceptionDetails>) : AbstractListModel<String>() {
|
||||||
|
override fun getSize() = details.size
|
||||||
|
override fun getElementAt(index: Int) = details[index].name
|
||||||
|
fun getExceptionAt(index: Int) = details[index].exception
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the dialog.
|
||||||
|
*/
|
||||||
|
init {
|
||||||
|
setBounds(100, 100, 540, 340)
|
||||||
|
setLocationRelativeTo(parentWindow)
|
||||||
|
|
||||||
|
contentPane.apply {
|
||||||
|
layout = BorderLayout()
|
||||||
|
|
||||||
|
// Error panel
|
||||||
|
add(JPanel().apply {
|
||||||
|
add(JLabel("One or more errors were encountered while installing the modpack!").apply {
|
||||||
|
icon = UIManager.getIcon("OptionPane.warningIcon")
|
||||||
|
})
|
||||||
|
}, BorderLayout.NORTH)
|
||||||
|
|
||||||
|
// Content panel
|
||||||
|
add(JPanel().apply {
|
||||||
|
border = EmptyBorder(5, 5, 5, 5)
|
||||||
|
layout = BorderLayout(0, 0)
|
||||||
|
|
||||||
|
add(JSplitPane().apply {
|
||||||
|
resizeWeight = 0.3
|
||||||
|
|
||||||
|
lblExceptionStacktrace = JTextArea("Select a file")
|
||||||
|
lblExceptionStacktrace.background = UIManager.getColor("List.background")
|
||||||
|
lblExceptionStacktrace.isOpaque = true
|
||||||
|
lblExceptionStacktrace.wrapStyleWord = true
|
||||||
|
lblExceptionStacktrace.lineWrap = true
|
||||||
|
lblExceptionStacktrace.isEditable = false
|
||||||
|
lblExceptionStacktrace.isFocusable = true
|
||||||
|
lblExceptionStacktrace.font = UIManager.getFont("Label.font")
|
||||||
|
lblExceptionStacktrace.border = EmptyBorder(5, 5, 5, 5)
|
||||||
|
|
||||||
|
rightComponent = JScrollPane(lblExceptionStacktrace)
|
||||||
|
|
||||||
|
leftComponent = JScrollPane(JList<String>().apply {
|
||||||
|
selectionMode = ListSelectionModel.SINGLE_SELECTION
|
||||||
|
border = EmptyBorder(5, 5, 5, 5)
|
||||||
|
val listModel = ExceptionListModel(eList)
|
||||||
|
model = listModel
|
||||||
|
addListSelectionListener {
|
||||||
|
val i = selectedIndex
|
||||||
|
if (i > -1) {
|
||||||
|
val sw = StringWriter()
|
||||||
|
listModel.getExceptionAt(i).printStackTrace(PrintWriter(sw))
|
||||||
|
lblExceptionStacktrace.text = sw.toString()
|
||||||
|
// Scroll to the top
|
||||||
|
lblExceptionStacktrace.caretPosition = 0
|
||||||
|
} else {
|
||||||
|
lblExceptionStacktrace.text = "Select a file"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}, BorderLayout.CENTER)
|
||||||
|
|
||||||
|
// Button pane
|
||||||
|
add(JPanel().apply {
|
||||||
|
layout = BorderLayout(0, 0)
|
||||||
|
|
||||||
|
// Right buttons
|
||||||
|
add(JPanel().apply {
|
||||||
|
add(JButton("Continue").apply {
|
||||||
|
toolTipText = "Attempt to continue installing, excluding the failed downloads"
|
||||||
|
addActionListener {
|
||||||
|
future.complete(IUserInterface.ExceptionListResult.CONTINUE)
|
||||||
|
this@ExceptionListWindow.dispose()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
add(JButton("Cancel launch").apply {
|
||||||
|
toolTipText = "Stop launching the game"
|
||||||
|
addActionListener {
|
||||||
|
future.complete(IUserInterface.ExceptionListResult.CANCEL)
|
||||||
|
this@ExceptionListWindow.dispose()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
add(JButton("Ignore update").apply {
|
||||||
|
toolTipText = "Start the game without attempting to update"
|
||||||
|
isEnabled = allowsIgnore
|
||||||
|
addActionListener {
|
||||||
|
future.complete(IUserInterface.ExceptionListResult.IGNORE)
|
||||||
|
this@ExceptionListWindow.dispose()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}, BorderLayout.EAST)
|
||||||
|
|
||||||
|
// Errored label
|
||||||
|
add(JLabel(eList.size.toString() + "/" + numTotal + " errored").apply {
|
||||||
|
horizontalAlignment = SwingConstants.CENTER
|
||||||
|
}, BorderLayout.CENTER)
|
||||||
|
|
||||||
|
// Left buttons
|
||||||
|
add(JPanel().apply {
|
||||||
|
add(JButton("Report issue").apply {
|
||||||
|
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
|
||||||
|
addActionListener {
|
||||||
|
try {
|
||||||
|
Desktop.getDesktop().browse(URI("https://github.com/packwiz/packwiz-installer/issues/new"))
|
||||||
|
} catch (e: IOException) {
|
||||||
|
// lol the button just won't work i guess
|
||||||
|
} catch (e: URISyntaxException) {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
isEnabled = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}, BorderLayout.WEST)
|
||||||
|
}, BorderLayout.SOUTH)
|
||||||
|
}
|
||||||
|
|
||||||
|
addWindowListener(object : WindowAdapter() {
|
||||||
|
override fun windowClosing(e: WindowEvent) {
|
||||||
|
future.complete(IUserInterface.ExceptionListResult.CANCEL)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun windowClosed(e: WindowEvent) {
|
||||||
|
// Just in case closing didn't get triggered - if something else called dispose() the
|
||||||
|
// future will have already completed
|
||||||
|
future.complete(IUserInterface.ExceptionListResult.CANCEL)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,179 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.gui
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult
|
||||||
|
import link.infra.packwiz.installer.ui.data.ExceptionDetails
|
||||||
|
import link.infra.packwiz.installer.ui.data.IOptionDetails
|
||||||
|
import link.infra.packwiz.installer.ui.data.InstallProgress
|
||||||
|
import link.infra.packwiz.installer.util.Log
|
||||||
|
import java.awt.EventQueue
|
||||||
|
import java.util.concurrent.CompletableFuture
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
import javax.swing.JDialog
|
||||||
|
import javax.swing.JOptionPane
|
||||||
|
import javax.swing.UIManager
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
|
class GUIHandler : IUserInterface {
|
||||||
|
private lateinit var frmPackwizlauncher: InstallWindow
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
override var optionsButtonPressed = false
|
||||||
|
set(value) {
|
||||||
|
optionalSelectedLatch.countDown()
|
||||||
|
field = value
|
||||||
|
}
|
||||||
|
@Volatile
|
||||||
|
override var cancelButtonPressed = false
|
||||||
|
set(value) {
|
||||||
|
optionalSelectedLatch.countDown()
|
||||||
|
field = value
|
||||||
|
}
|
||||||
|
var okButtonPressed = false
|
||||||
|
set(value) {
|
||||||
|
optionalSelectedLatch.countDown()
|
||||||
|
field = value
|
||||||
|
}
|
||||||
|
@Volatile
|
||||||
|
override var firstInstall = false
|
||||||
|
|
||||||
|
override var title = "packwiz-installer"
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
EventQueue.invokeLater { frmPackwizlauncher.title = value }
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
EventQueue.invokeAndWait {
|
||||||
|
try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.warn("Failed to set look and feel", e)
|
||||||
|
}
|
||||||
|
frmPackwizlauncher = InstallWindow(this).apply {
|
||||||
|
title = this@GUIHandler.title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val visibleCountdownLatch = CountDownLatch(1)
|
||||||
|
private val optionalSelectedLatch = CountDownLatch(1)
|
||||||
|
|
||||||
|
override fun show() = EventQueue.invokeLater {
|
||||||
|
frmPackwizlauncher.isVisible = true
|
||||||
|
visibleCountdownLatch.countDown()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun dispose() = EventQueue.invokeAndWait {
|
||||||
|
frmPackwizlauncher.dispose()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun showErrorAndExit(message: String, e: Exception?): Nothing {
|
||||||
|
val buttons = arrayOf("Quit", if (firstInstall) "Continue without installing" else "Continue without updating")
|
||||||
|
if (e != null) {
|
||||||
|
Log.fatal(message, e)
|
||||||
|
EventQueue.invokeAndWait {
|
||||||
|
val result = JOptionPane.showOptionDialog(frmPackwizlauncher,
|
||||||
|
"$message: $e",
|
||||||
|
title,
|
||||||
|
JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, buttons, buttons[0])
|
||||||
|
if (result == 1) {
|
||||||
|
Log.info("User selected to continue without installing/updating, exiting with code 0...")
|
||||||
|
exitProcess(0)
|
||||||
|
} else {
|
||||||
|
Log.info("User selected to quit, exiting with code 1...")
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.fatal(message)
|
||||||
|
EventQueue.invokeAndWait {
|
||||||
|
val result = JOptionPane.showOptionDialog(frmPackwizlauncher,
|
||||||
|
message,
|
||||||
|
title,
|
||||||
|
JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, buttons, buttons[0])
|
||||||
|
if (result == 1) {
|
||||||
|
Log.info("User selected to continue without installing/updating, exiting with code 0...")
|
||||||
|
exitProcess(0)
|
||||||
|
} else {
|
||||||
|
Log.info("User selected to quit, exiting with code 1...")
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun submitProgress(progress: InstallProgress) {
|
||||||
|
val sb = StringBuilder()
|
||||||
|
if (progress.hasProgress) {
|
||||||
|
sb.append('(')
|
||||||
|
sb.append(progress.progress)
|
||||||
|
sb.append('/')
|
||||||
|
sb.append(progress.progressTotal)
|
||||||
|
sb.append(") ")
|
||||||
|
}
|
||||||
|
sb.append(progress.message)
|
||||||
|
Log.info(sb.toString())
|
||||||
|
EventQueue.invokeLater {
|
||||||
|
frmPackwizlauncher.displayProgress(progress)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun showOptions(options: List<IOptionDetails>): Boolean {
|
||||||
|
val future = CompletableFuture<Boolean>()
|
||||||
|
EventQueue.invokeAndWait {
|
||||||
|
if (options.isEmpty()) {
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
"This modpack has no optional mods!",
|
||||||
|
"Optional mods", JOptionPane.INFORMATION_MESSAGE)
|
||||||
|
future.complete(false)
|
||||||
|
} else {
|
||||||
|
OptionsSelectWindow(options, future, frmPackwizlauncher).apply {
|
||||||
|
defaultCloseOperation = JDialog.DISPOSE_ON_CLOSE
|
||||||
|
isVisible = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return future.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult {
|
||||||
|
val future = CompletableFuture<ExceptionListResult>()
|
||||||
|
EventQueue.invokeLater {
|
||||||
|
ExceptionListWindow(exceptions, future, numTotal, allowsIgnore, frmPackwizlauncher).apply {
|
||||||
|
defaultCloseOperation = JDialog.DISPOSE_ON_CLOSE
|
||||||
|
isVisible = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return future.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun disableOptionsButton(hasOptions: Boolean) = EventQueue.invokeLater {
|
||||||
|
frmPackwizlauncher.disableOptionsButton(hasOptions)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun showCancellationDialog(): IUserInterface.CancellationResult {
|
||||||
|
val future = CompletableFuture<IUserInterface.CancellationResult>()
|
||||||
|
EventQueue.invokeLater {
|
||||||
|
val buttons = arrayOf("Quit", "Ignore")
|
||||||
|
val result = JOptionPane.showOptionDialog(frmPackwizlauncher,
|
||||||
|
"The installation was cancelled. Would you like to quit the game, or ignore the update and start the game?",
|
||||||
|
"Cancelled installation",
|
||||||
|
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0])
|
||||||
|
future.complete(if (result == 0) IUserInterface.CancellationResult.QUIT else IUserInterface.CancellationResult.CONTINUE)
|
||||||
|
}
|
||||||
|
return future.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun awaitOptionalButton(showCancel: Boolean) {
|
||||||
|
EventQueue.invokeAndWait {
|
||||||
|
frmPackwizlauncher.showOk(!showCancel)
|
||||||
|
}
|
||||||
|
visibleCountdownLatch.await()
|
||||||
|
optionalSelectedLatch.await()
|
||||||
|
EventQueue.invokeLater {
|
||||||
|
frmPackwizlauncher.hideOk()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,124 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.gui
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.data.InstallProgress
|
||||||
|
import java.awt.BorderLayout
|
||||||
|
import java.awt.Component
|
||||||
|
import java.awt.GridBagConstraints
|
||||||
|
import java.awt.GridBagLayout
|
||||||
|
import javax.swing.*
|
||||||
|
import javax.swing.border.EmptyBorder
|
||||||
|
|
||||||
|
class InstallWindow(private val handler: GUIHandler) : JFrame() {
|
||||||
|
private var lblProgresslabel: JLabel
|
||||||
|
private var progressBar: JProgressBar
|
||||||
|
private var btnOptions: JButton
|
||||||
|
private val btnCancel: JButton
|
||||||
|
private val btnOk: JButton
|
||||||
|
private val buttonsPanel: JPanel
|
||||||
|
|
||||||
|
init {
|
||||||
|
setBounds(100, 100, 493, 95)
|
||||||
|
// Works better with tiling window managers - there isn't any reason to change window size currently anyway
|
||||||
|
isResizable = false
|
||||||
|
defaultCloseOperation = EXIT_ON_CLOSE
|
||||||
|
setLocationRelativeTo(null)
|
||||||
|
|
||||||
|
// Progress bar and loading text
|
||||||
|
add(JPanel().apply {
|
||||||
|
border = EmptyBorder(10, 10, 10, 10)
|
||||||
|
layout = BorderLayout(0, 0)
|
||||||
|
|
||||||
|
progressBar = JProgressBar().apply {
|
||||||
|
isIndeterminate = true
|
||||||
|
}
|
||||||
|
add(progressBar, BorderLayout.CENTER)
|
||||||
|
|
||||||
|
lblProgresslabel = JLabel("Loading...")
|
||||||
|
add(lblProgresslabel, BorderLayout.SOUTH)
|
||||||
|
}, BorderLayout.CENTER)
|
||||||
|
|
||||||
|
// Buttons
|
||||||
|
buttonsPanel = JPanel().apply {
|
||||||
|
border = EmptyBorder(0, 5, 0, 5)
|
||||||
|
layout = GridBagLayout()
|
||||||
|
|
||||||
|
btnOptions = JButton("Optional mods...").apply {
|
||||||
|
alignmentX = Component.CENTER_ALIGNMENT
|
||||||
|
|
||||||
|
addActionListener {
|
||||||
|
text = "Loading..."
|
||||||
|
isEnabled = false
|
||||||
|
handler.optionsButtonPressed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add(btnOptions, GridBagConstraints().apply {
|
||||||
|
gridx = 1
|
||||||
|
gridy = 0
|
||||||
|
})
|
||||||
|
|
||||||
|
btnCancel = JButton("Cancel").apply {
|
||||||
|
addActionListener {
|
||||||
|
isEnabled = false
|
||||||
|
handler.cancelButtonPressed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add(btnCancel, GridBagConstraints().apply {
|
||||||
|
gridx = 1
|
||||||
|
gridy = 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
btnOk = JButton("Continue").apply {
|
||||||
|
addActionListener {
|
||||||
|
handler.okButtonPressed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add(buttonsPanel, BorderLayout.EAST)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun displayProgress(progress: InstallProgress) {
|
||||||
|
if (progress.hasProgress) {
|
||||||
|
progressBar.isIndeterminate = false
|
||||||
|
progressBar.value = progress.progress
|
||||||
|
progressBar.maximum = progress.progressTotal
|
||||||
|
} else {
|
||||||
|
progressBar.isIndeterminate = true
|
||||||
|
progressBar.value = 0
|
||||||
|
}
|
||||||
|
lblProgresslabel.text = progress.message
|
||||||
|
}
|
||||||
|
|
||||||
|
fun disableOptionsButton(hasOptions: Boolean) {
|
||||||
|
btnOptions.apply {
|
||||||
|
text = if (hasOptions) { "Optional mods..." } else { "No optional mods" }
|
||||||
|
isEnabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun showOk(hideCancel: Boolean) {
|
||||||
|
if (hideCancel) {
|
||||||
|
buttonsPanel.add(btnOk, GridBagConstraints().apply {
|
||||||
|
gridx = 1
|
||||||
|
gridy = 1
|
||||||
|
})
|
||||||
|
buttonsPanel.remove(btnCancel)
|
||||||
|
} else {
|
||||||
|
buttonsPanel.add(btnOk, GridBagConstraints().apply {
|
||||||
|
gridx = 0
|
||||||
|
gridy = 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
buttonsPanel.revalidate()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun hideOk() {
|
||||||
|
buttonsPanel.remove(btnOk)
|
||||||
|
if (!buttonsPanel.components.contains(btnCancel)) {
|
||||||
|
buttonsPanel.add(btnCancel, GridBagConstraints().apply {
|
||||||
|
gridx = 1
|
||||||
|
gridy = 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
buttonsPanel.revalidate()
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.gui
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.data.IOptionDetails
|
||||||
|
|
||||||
|
// Serves as a proxy for IOptionDetails, so that setOptionValue isn't called until OK is clicked
|
||||||
|
internal class OptionTempHandler(private val opt: IOptionDetails) : IOptionDetails {
|
||||||
|
override var optionValue = opt.optionValue
|
||||||
|
|
||||||
|
override val name get() = opt.name
|
||||||
|
override val optionDescription get() = opt.optionDescription
|
||||||
|
|
||||||
|
fun finalise() {
|
||||||
|
opt.optionValue = optionValue
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,167 @@
|
|||||||
|
package link.infra.packwiz.installer.ui.gui
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.data.IOptionDetails
|
||||||
|
import java.awt.BorderLayout
|
||||||
|
import java.awt.FlowLayout
|
||||||
|
import java.awt.event.ActionEvent
|
||||||
|
import java.awt.event.ActionListener
|
||||||
|
import java.awt.event.WindowAdapter
|
||||||
|
import java.awt.event.WindowEvent
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.CompletableFuture
|
||||||
|
import javax.swing.*
|
||||||
|
import javax.swing.border.EmptyBorder
|
||||||
|
import javax.swing.event.TableModelListener
|
||||||
|
import javax.swing.table.TableModel
|
||||||
|
|
||||||
|
class OptionsSelectWindow internal constructor(optList: List<IOptionDetails>, future: CompletableFuture<Boolean>, parentWindow: JFrame?) : JDialog(parentWindow, "Select optional mods...", true), ActionListener {
|
||||||
|
private val lblOptionDescription: JTextArea
|
||||||
|
private val tableModel: OptionTableModel
|
||||||
|
private val future: CompletableFuture<Boolean>
|
||||||
|
|
||||||
|
private class OptionTableModel(givenOpts: List<IOptionDetails>) : TableModel {
|
||||||
|
private val opts: List<OptionTempHandler>
|
||||||
|
|
||||||
|
init {
|
||||||
|
val mutOpts = ArrayList<OptionTempHandler>()
|
||||||
|
for (opt in givenOpts) {
|
||||||
|
mutOpts.add(OptionTempHandler(opt))
|
||||||
|
}
|
||||||
|
opts = mutOpts
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getRowCount() = opts.size
|
||||||
|
override fun getColumnCount() = 2
|
||||||
|
|
||||||
|
private val columnNames = arrayOf("Enabled", "Mod name")
|
||||||
|
private val columnTypes = arrayOf(Boolean::class.javaObjectType, String::class.java)
|
||||||
|
private val columnEditables = booleanArrayOf(true, false)
|
||||||
|
|
||||||
|
override fun getColumnName(columnIndex: Int) = columnNames[columnIndex]
|
||||||
|
override fun getColumnClass(columnIndex: Int) = columnTypes[columnIndex]
|
||||||
|
override fun isCellEditable(rowIndex: Int, columnIndex: Int) = columnEditables[columnIndex]
|
||||||
|
|
||||||
|
override fun getValueAt(rowIndex: Int, columnIndex: Int): Any {
|
||||||
|
val opt = opts[rowIndex]
|
||||||
|
return if (columnIndex == 0) opt.optionValue else opt.name
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setValueAt(aValue: Any, rowIndex: Int, columnIndex: Int) {
|
||||||
|
if (columnIndex == 0) {
|
||||||
|
val opt = opts[rowIndex]
|
||||||
|
opt.optionValue = aValue as Boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Noop, the table model doesn't change!
|
||||||
|
override fun addTableModelListener(l: TableModelListener) {}
|
||||||
|
override fun removeTableModelListener(l: TableModelListener) {}
|
||||||
|
|
||||||
|
fun getDescription(rowIndex: Int) = opts[rowIndex].optionDescription
|
||||||
|
|
||||||
|
fun finalise() {
|
||||||
|
for (opt in opts) {
|
||||||
|
opt.finalise()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun actionPerformed(e: ActionEvent) {
|
||||||
|
if (e.actionCommand == "OK") {
|
||||||
|
tableModel.finalise()
|
||||||
|
future.complete(false)
|
||||||
|
dispose()
|
||||||
|
} else if (e.actionCommand == "Cancel") {
|
||||||
|
future.complete(true)
|
||||||
|
dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the dialog.
|
||||||
|
*/
|
||||||
|
init {
|
||||||
|
tableModel = OptionTableModel(optList)
|
||||||
|
this.future = future
|
||||||
|
|
||||||
|
setBounds(100, 100, 450, 300)
|
||||||
|
setLocationRelativeTo(parentWindow)
|
||||||
|
|
||||||
|
contentPane.apply {
|
||||||
|
layout = BorderLayout()
|
||||||
|
add(JPanel().apply {
|
||||||
|
border = EmptyBorder(5, 5, 5, 5)
|
||||||
|
layout = BorderLayout(0, 0)
|
||||||
|
|
||||||
|
add(JSplitPane().apply {
|
||||||
|
resizeWeight = 0.5
|
||||||
|
|
||||||
|
lblOptionDescription = JTextArea("Select an option...").apply {
|
||||||
|
background = UIManager.getColor("List.background")
|
||||||
|
isOpaque = true
|
||||||
|
wrapStyleWord = true
|
||||||
|
lineWrap = true
|
||||||
|
isEditable = false
|
||||||
|
isFocusable = false
|
||||||
|
font = UIManager.getFont("Label.font")
|
||||||
|
border = EmptyBorder(10, 10, 10, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
leftComponent = JScrollPane(JTable().apply {
|
||||||
|
showVerticalLines = false
|
||||||
|
showHorizontalLines = false
|
||||||
|
setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
|
||||||
|
setShowGrid(false)
|
||||||
|
model = tableModel
|
||||||
|
columnModel.getColumn(0).resizable = false
|
||||||
|
columnModel.getColumn(0).preferredWidth = 15
|
||||||
|
columnModel.getColumn(0).maxWidth = 15
|
||||||
|
columnModel.getColumn(1).resizable = false
|
||||||
|
selectionModel.addListSelectionListener {
|
||||||
|
val i = selectedRow
|
||||||
|
if (i > -1) {
|
||||||
|
lblOptionDescription.text = tableModel.getDescription(i)
|
||||||
|
} else {
|
||||||
|
lblOptionDescription.text = "Select an option..."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tableHeader = null
|
||||||
|
}).apply {
|
||||||
|
viewport.background = UIManager.getColor("List.background")
|
||||||
|
horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
|
||||||
|
}
|
||||||
|
|
||||||
|
rightComponent = JScrollPane(lblOptionDescription)
|
||||||
|
})
|
||||||
|
|
||||||
|
add(JPanel().apply {
|
||||||
|
layout = FlowLayout(FlowLayout.RIGHT)
|
||||||
|
|
||||||
|
add(JButton("OK").apply {
|
||||||
|
actionCommand = "OK"
|
||||||
|
addActionListener(this@OptionsSelectWindow)
|
||||||
|
|
||||||
|
this@OptionsSelectWindow.rootPane.defaultButton = this
|
||||||
|
})
|
||||||
|
|
||||||
|
add(JButton("Cancel").apply {
|
||||||
|
actionCommand = "Cancel"
|
||||||
|
addActionListener(this@OptionsSelectWindow)
|
||||||
|
})
|
||||||
|
}, BorderLayout.SOUTH)
|
||||||
|
}, BorderLayout.CENTER)
|
||||||
|
}
|
||||||
|
|
||||||
|
addWindowListener(object : WindowAdapter() {
|
||||||
|
override fun windowClosing(e: WindowEvent) {
|
||||||
|
future.complete(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun windowClosed(e: WindowEvent) {
|
||||||
|
// Just in case closing didn't get triggered - if something else called dispose() the
|
||||||
|
// future will have already completed
|
||||||
|
future.complete(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
38
src/main/kotlin/link/infra/packwiz/installer/util/Exts.kt
Normal file
38
src/main/kotlin/link/infra/packwiz/installer/util/Exts.kt
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package link.infra.packwiz.installer.util
|
||||||
|
|
||||||
|
import link.infra.packwiz.installer.ui.IUserInterface
|
||||||
|
|
||||||
|
inline fun <T> iflet(value: T?, whenNotNull: (T) -> Unit) {
|
||||||
|
if (value != null) {
|
||||||
|
whenNotNull(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T, U> IUserInterface.ifletOrErr(value: T?, message: String, whenNotNull: (T) -> U): U =
|
||||||
|
if (value != null) {
|
||||||
|
whenNotNull(value)
|
||||||
|
} else {
|
||||||
|
this.showErrorAndExit(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T, U, V> IUserInterface.ifletOrErr(value: T?, value2: U?, message: String, whenNotNull: (T, U) -> V): V =
|
||||||
|
if (value != null && value2 != null) {
|
||||||
|
whenNotNull(value, value2)
|
||||||
|
} else {
|
||||||
|
this.showErrorAndExit(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> ifletOrWarn(value: T?, message: String, whenNotNull: (T) -> Unit) {
|
||||||
|
if (value != null) {
|
||||||
|
whenNotNull(value)
|
||||||
|
} else {
|
||||||
|
Log.warn(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T, U> iflet(value: T?, whenNotNull: (T) -> U, whenNull: () -> U): U =
|
||||||
|
if (value != null) {
|
||||||
|
whenNotNull(value)
|
||||||
|
} else {
|
||||||
|
whenNull()
|
||||||
|
}
|
16
src/main/kotlin/link/infra/packwiz/installer/util/Log.kt
Normal file
16
src/main/kotlin/link/infra/packwiz/installer/util/Log.kt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package link.infra.packwiz.installer.util
|
||||||
|
|
||||||
|
object Log {
|
||||||
|
fun info(message: String) = println(message)
|
||||||
|
|
||||||
|
fun warn(message: String) = println("[Warning] $message")
|
||||||
|
fun warn(message: String, exception: Exception) = println("[Warning] $message: $exception")
|
||||||
|
|
||||||
|
fun fatal(message: String) {
|
||||||
|
println("[FATAL] $message")
|
||||||
|
}
|
||||||
|
fun fatal(message: String, exception: Exception) {
|
||||||
|
println("[FATAL] $message: ")
|
||||||
|
exception.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user