-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1] Implemented OpenSR installer. Also:
- Settings are now correctly loaded before the UI starts loading. - On first run, user will be asked if they want to install OpenSR. - Added more detailed progress tracking to Git/FS operations during mod installation.
- Loading branch information
Showing
20 changed files
with
393 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/io/github/openstarruler/launchpad/adapter/FileProgressHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.openstarruler.launchpad.adapter | ||
|
||
class FileProgressHandler(private val count: Int, private val handler: TextHandler?) { | ||
var current = 0 | ||
|
||
fun handle() { | ||
current++ | ||
handler?.handle("Copying file $current/$count") | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/io/github/openstarruler/launchpad/adapter/FormattingTextHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.github.openstarruler.launchpad.adapter | ||
|
||
class FormattingTextHandler(private val pattern: String, private val handler: TextHandler?): TextHandler { | ||
override fun handle(text: String) { | ||
handler?.handle(String.format(pattern, text)) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/github/openstarruler/launchpad/adapter/GitHub.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.openstarruler.launchpad.adapter | ||
|
||
import com.google.gson.Gson | ||
import io.github.openstarruler.launchpad.model.Release | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
|
||
object GitHub { | ||
val GH_API = "https://api.github.com" | ||
|
||
fun getReleasesUrl(owner: String = "OpenSRProject", repo: String): String { | ||
return "$GH_API/repos/$owner/$repo/releases" | ||
} | ||
|
||
fun getReleases(owner: String = "OpenSRProject", repo: String): List<Release> { | ||
val okHttpClient = OkHttpClient.Builder() | ||
.build() | ||
val request = Request.Builder() | ||
.url(getReleasesUrl(owner, repo)) | ||
.build() | ||
okHttpClient.newCall(request).execute().body?.charStream().let { | ||
return try { Gson().fromJson(it!!) } | ||
catch(e: Exception) { listOf() } | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/io/github/openstarruler/launchpad/adapter/GitProgressHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.openstarruler.launchpad.adapter | ||
|
||
import org.eclipse.jgit.lib.ProgressMonitor | ||
|
||
class GitProgressHandler(val task: String, val handler: TextHandler?): ProgressMonitor { | ||
var taskTitle = "" | ||
var tasksDone = 0 | ||
var taskCount = 0 | ||
|
||
override fun start(totalTasks: Int) = Unit | ||
|
||
override fun beginTask(title: String?, totalWork: Int) { | ||
taskTitle = title ?: "" | ||
tasksDone = 0 | ||
taskCount = totalWork | ||
handler?.handle("$task $taskTitle $tasksDone/$taskCount") | ||
} | ||
|
||
override fun update(completed: Int) { | ||
tasksDone++ | ||
handler?.handle("$task $taskTitle $tasksDone/$taskCount") | ||
} | ||
|
||
override fun endTask() = Unit | ||
|
||
override fun isCancelled(): Boolean = false | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/io/github/openstarruler/launchpad/adapter/Gson.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.openstarruler.launchpad.adapter | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonIOException | ||
import com.google.gson.JsonSyntaxException | ||
import com.google.gson.reflect.TypeToken | ||
import java.io.Reader | ||
|
||
@Throws(JsonIOException::class, JsonSyntaxException::class) | ||
inline fun <reified T> Gson.fromJson(json: Reader): T = fromJson(json, object: TypeToken<T>() {}.type) | ||
|
||
@Throws(JsonIOException::class, JsonSyntaxException::class) | ||
inline fun <reified T> Gson.fromJson(json: String): T = fromJson(json, object: TypeToken<T>() {}.type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/java/io/github/openstarruler/launchpad/adapter/OpenSRManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package io.github.openstarruler.launchpad.adapter | ||
|
||
import io.github.openstarruler.launchpad.model.Release | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.api.ResetCommand.ResetType | ||
import org.eclipse.jgit.transport.URIish | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.util.concurrent.locks.ReentrantLock | ||
import java.util.zip.ZipFile | ||
import kotlin.concurrent.withLock | ||
|
||
object OpenSRManager { | ||
val openSRVersions: List<Release> = GitHub.getReleases(repo = "OpenStarRuler") | ||
|
||
fun installOpenSR( | ||
version: Release, | ||
warningHandler: TextHandler?, | ||
errorHandler: TextHandler?, | ||
progressHandler: TextHandler? | ||
) { | ||
progressHandler?.handle("Detecting OS version...") | ||
val type = if (Utils.IS_WINDOWS) "windows" else "linux" | ||
progressHandler?.handle("Searching for binaries...") | ||
val zipAsset = version.assets.find { it.name.contains(type) } | ||
if(zipAsset == null) { | ||
errorHandler?.handle("Failed to find platform-appropriate binary package for selected version!\n\nPlease try installing a different version of OpenSR, and report this issue to the OpenSR team.") | ||
return | ||
} | ||
|
||
progressHandler?.handle("Downloading binary package...") | ||
val okHttpClient = OkHttpClient.Builder() | ||
.build() | ||
val request = Request.Builder() | ||
.url(zipAsset.downloadUrl) | ||
.build() | ||
okHttpClient.newCall(request).execute().body?.byteStream().use { zipBuffer -> | ||
if(zipBuffer == null) { | ||
errorHandler?.handle("Failed to download platform-appropriate binary package for selected version!\n\nPlease try installing a different version of OpenSR, and report this issue to the OpenSR team.") | ||
return | ||
} | ||
progressHandler?.handle("Saving binary package...") | ||
FileOutputStream("opensr-binaries.zip", false).use { zipBuffer.copyTo(it) } | ||
} | ||
|
||
progressHandler?.handle("Connecting to OpenSR data repository...") | ||
val dataRepo = try { | ||
Git.open(File(Settings.instance.gamePath)) | ||
} catch (e: IOException) { | ||
Git.init().setDirectory(File(Settings.instance.gamePath)).call() | ||
} | ||
dataRepo.use { | ||
val remotes = dataRepo.remoteList().call() | ||
val origin = remotes.find { it.name == "origin" } | ||
if (origin == null) | ||
dataRepo.remoteAdd() | ||
.setName("origin") | ||
.setUri(URIish("https://github.com/OpenSRProject/OpenStarRuler-Data.git")) | ||
.call() | ||
else if (origin.urIs.find { it.toString() == "https://github.com/OpenSRProject/OpenStarRuler-Data.git" } == null) | ||
dataRepo.remoteSetUrl() | ||
.setRemoteName("origin") | ||
.setRemoteUri(URIish("https://github.com/OpenSRProject/OpenStarRuler-Data.git")) | ||
.call() | ||
|
||
if (version.tagName == "nightly") { | ||
dataRepo.fetch() | ||
.setProgressMonitor(GitProgressHandler("Downloading latest data...", progressHandler)) | ||
.call() | ||
|
||
dataRepo.reset() | ||
.setProgressMonitor(GitProgressHandler("Installing latest data...", progressHandler)) | ||
.setMode(ResetType.HARD) | ||
.setRef("refs/remotes/origin/master") | ||
.call() | ||
} else try { | ||
dataRepo.fetch() | ||
.setProgressMonitor(GitProgressHandler("Downloading appropriate data...", progressHandler)) | ||
.setRefSpecs("refs/tags/${version.tagName}:refs/tags/${version.tagName}") | ||
.call() | ||
|
||
dataRepo.reset() | ||
.setProgressMonitor(GitProgressHandler("Installing appropriate data...", progressHandler)) | ||
.setMode(ResetType.HARD) | ||
.setRef("refs/tags/${version.tagName}") | ||
.call() | ||
} catch (e: Exception) { | ||
warningHandler?.handle("Failed to get data tag corresponding to this release! Falling back to master branch.\n\nPlease report this issue to the OpenSR team. It should still be safe to play this version of OpenSR, but there is a slight possibility that something will go wrong.") | ||
dataRepo.fetch() | ||
.setProgressMonitor(GitProgressHandler("Downloading latest data...", progressHandler)) | ||
.call() | ||
dataRepo.reset() | ||
.setProgressMonitor(GitProgressHandler("Installing latest data...", progressHandler)) | ||
.setMode(ResetType.HARD) | ||
.setRef("refs/remotes/origin/master") | ||
.call() | ||
} | ||
} | ||
|
||
progressHandler?.handle("Extracting binaries...") | ||
ZipFile("opensr-binaries.zip").use { zip -> | ||
val total = zip.size() | ||
var extracted = 0 | ||
val lock = ReentrantLock() | ||
zip.stream().parallel().forEach { entry -> | ||
if(!entry.isDirectory) { | ||
File(Settings.instance.gamePath, entry.name).also { file -> | ||
file.parentFile.mkdirs() | ||
file.createNewFile() | ||
file.outputStream().use { | ||
zip.getInputStream(entry).copyTo(it) | ||
} | ||
lock.withLock { | ||
progressHandler?.handle("Extracting binaries... ${++extracted}/$total files extracted") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/io/github/openstarruler/launchpad/adapter/TextHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.openstarruler.launchpad.adapter | ||
|
||
fun interface TextHandler { | ||
fun handle(text: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/io/github/openstarruler/launchpad/model/Asset.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.openstarruler.launchpad.model | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class Asset( | ||
@SerializedName("browser_download_url") val downloadUrl: String, | ||
val name: String | ||
) |
Oops, something went wrong.