Skip to content
This repository was archived by the owner on Jan 24, 2026. It is now read-only.

Commit 8564337

Browse files
committed
The Matrix is real, confirmed.
1 parent ffb2339 commit 8564337

File tree

1 file changed

+48
-58
lines changed
  • core/src/main/kotlin/dev/ultreon/launcher

1 file changed

+48
-58
lines changed

core/src/main/kotlin/dev/ultreon/launcher/Main.kt

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ import com.badlogic.gdx.utils.JsonReader
1414
import com.badlogic.gdx.utils.JsonValue
1515
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
1616
import java.io.File
17-
import java.io.RandomAccessFile
1817
import java.net.URL
1918
import java.nio.file.Files
2019
import java.nio.file.Path
2120
import java.nio.file.Paths
22-
import java.nio.file.StandardCopyOption
2321
import java.nio.file.StandardOpenOption
2422
import java.nio.file.attribute.PosixFilePermissions
2523
import java.util.zip.GZIPInputStream
@@ -28,7 +26,7 @@ import kotlin.concurrent.thread
2826
import kotlin.io.path.absolute
2927
import kotlin.io.path.notExists
3028
import kotlin.io.path.readText
31-
import kotlin.streams.asSequence
29+
import kotlin.io.path.relativeTo
3230

3331

3432
abstract class Widget(var x: Float = 0f, var y: Float = 0f, var width: Float = 20f, var height: Float = 20f) {
@@ -149,19 +147,19 @@ private fun launchGame(version: GameVersion, button: Button): Process {
149147
ProcessBuilder("cmd", "/c", "gradlew.bat --no-daemon lwjgl3:run").run {
150148
environment()["PATH"] = "$JAVA_HOME\\bin:${System.getenv("PATH")}"
151149
environment()["JAVA_HOME"] = JAVA_HOME
152-
directory(File("versions/${version.id}/"))
150+
directory(File("versions/${version.id}/").absoluteFile)
153151
}.inheritIO().start()
154152
} else if (System.getProperty("os.name").startsWith("Linux")) {
155153
ProcessBuilder("bash", "-c", "chmod +x gradlew && ./gradlew --no-daemon lwjgl3:run").run {
156154
environment()["PATH"] = "$JAVA_HOME/bin:${System.getenv("PATH")}"
157155
environment()["JAVA_HOME"] = JAVA_HOME
158-
directory(File("versions/${version.id}/"))
156+
directory(File("versions/${version.id}/").absoluteFile)
159157
}.inheritIO().start()
160158
} else if (System.getProperty("os.name").startsWith("Mac")) {
161159
ProcessBuilder("bash", "-c", "chmod +x gradlew && ./gradlew --no-daemon lwjgl3:run").run {
162160
environment()["PATH"] = "$JAVA_HOME/bin:${System.getenv("PATH")}"
163161
environment()["JAVA_HOME"] = JAVA_HOME
164-
directory(File("versions/${version.id}/"))
162+
directory(File("versions/${version.id}/").absoluteFile)
165163
}.inheritIO().start()
166164
} else {
167165
throw UnsupportedOperationException()
@@ -176,7 +174,7 @@ private fun launchGame(version: GameVersion, button: Button): Process {
176174
).run {
177175
environment()["PATH"] = "${File(JAVA_HOME).absolutePath}\\bin:${System.getenv("PATH")}"
178176
environment()["JAVA_HOME"] = File(JAVA_HOME).absolutePath
179-
directory(File("versions/${version.id}/"))
177+
directory(File("versions/${version.id}/").absoluteFile)
180178
}.inheritIO().start()
181179
} else if (System.getProperty("os.name").startsWith("Linux")) {
182180
ProcessBuilder(
@@ -187,7 +185,7 @@ private fun launchGame(version: GameVersion, button: Button): Process {
187185
).run {
188186
environment()["PATH"] = "${File(JAVA_HOME).absolutePath}/bin:${System.getenv("PATH")}"
189187
environment()["JAVA_HOME"] = File(JAVA_HOME).absolutePath
190-
directory(File("versions/${version.id}/"))
188+
directory(File("versions/${version.id}/").absoluteFile)
191189
}.inheritIO().start()
192190
} else if (System.getProperty("os.name").startsWith("Mac")) {
193191
ProcessBuilder(
@@ -199,7 +197,7 @@ private fun launchGame(version: GameVersion, button: Button): Process {
199197
).run {
200198
environment()["PATH"] = "${File(JAVA_HOME).absolutePath}/bin:${System.getenv("PATH")}"
201199
environment()["JAVA_HOME"] = File(JAVA_HOME).absolutePath
202-
directory(File("versions/${version.id}/"))
200+
directory(File("versions/${version.id}/").absoluteFile)
203201
}.inheritIO().start()
204202
} else {
205203
throw UnsupportedOperationException()
@@ -228,7 +226,8 @@ fun download(
228226
Files.createDirectories(Paths.get("temp").absolute())
229227
}
230228

231-
val file = RandomAccessFile(File("temp/$name").absolutePath, "rw")
229+
val absolute = Paths.get("temp/$name").absolute()
230+
val file = Files.newOutputStream(absolute)
232231

233232
connection.inputStream.use { inputStream ->
234233
val buffer = ByteArray(1024)
@@ -240,14 +239,15 @@ fun download(
240239
}
241240
}
242241

242+
file.flush()
243243
file.close()
244244

245245
// Wait for the file to be found (some random issue on macOS cause the file not to be found immediately)
246-
while (Paths.get("temp/$name").absolute().notExists()) {
246+
while (absolute.notExists()) {
247247
Thread.sleep(100)
248248
}
249249

250-
onComplete?.invoke(Paths.get("temp/$name").absolute().also {
250+
onComplete?.invoke(absolute.also {
251251
println("Downloaded $url -> $it")
252252
})
253253
}
@@ -429,32 +429,32 @@ fun versionsFromGitHub(): List<GameVersion> {
429429
}
430430

431431
fun unpackGame(version: GameVersion): Int {
432-
val unpacked = unpackZip("temp/${version.id}", "temp/${version.id}-extract")
432+
val unpacked = unpackZip(Paths.get("temp/${version.id}").absolute(), Paths.get("temp/${version.id}-extract").absolute())
433433
if (unpacked != 0) {
434434
return 1
435435
}
436436

437437
if (version.id in arrayOf("0.0.0-indev", "0.0.1-indev") || version is ChannelVersion) {
438-
if (move(Files.list(Paths.get("temp/${version.id}-extract")).findFirst().orElseThrow().toString(), "versions/${version.id}") != 0) {
438+
if (move(Files.list(Paths.get("temp/${version.id}-extract").absolute()).findFirst().orElseThrow(), Paths.get("versions/${version.id}").absolute()) != 0) {
439439
return 1
440440
}
441441
} else {
442-
if (move("temp/${version.id}-extract", "versions/${version.id}") != 0) {
442+
if (move(Paths.get("temp/${version.id}-extract").absolute(), Paths.get("versions/${version.id}").absolute()) != 0) {
443443
return 1
444444
}
445445
}
446446

447447
return 0
448448
}
449449

450-
fun unpack(path: String, dest: String): Int {
451-
if (!Gdx.files.local(path).exists()) {
450+
fun unpack(path: Path, dest: Path): Int {
451+
if (Files.notExists(path)) {
452452
println("Failed to find $path")
453453
return -1
454454
}
455455

456-
if (!Gdx.files.local(dest).exists()) {
457-
Gdx.files.local(dest).mkdirs()
456+
if (Files.notExists(dest)) {
457+
Files.createDirectories(dest)
458458
println("Created $dest")
459459
} else {
460460
println("Found $dest")
@@ -470,19 +470,19 @@ fun unpack(path: String, dest: String): Int {
470470
}
471471
}
472472

473-
fun move(path: String, dest: String): Int {
474-
if (!Gdx.files.local(path).exists()) {
473+
fun move(path: Path, dest: Path): Int {
474+
if (Files.notExists(path)) {
475475
println("Failed to find $path")
476476
return 1
477477
}
478478

479-
if (Gdx.files.local(dest).exists()) {
479+
if (Files.exists(dest)) {
480480
println("Failed to move $path to $dest, $dest already exists")
481481
return 1
482482
}
483483

484484
try {
485-
Files.move(Paths.get(path), Paths.get(dest))
485+
Files.move(path, dest)
486486
} catch (e: Exception) {
487487
e.printStackTrace()
488488
return 1
@@ -491,35 +491,22 @@ fun move(path: String, dest: String): Int {
491491
return 0
492492
}
493493

494-
fun rename(path: String, newName: String): Int {
495-
if (!Gdx.files.local(path).exists()) {
496-
println("Failed to find $path")
497-
return 1
498-
}
499-
500-
try {
501-
Files.move(Paths.get(path), Paths.get(newName), StandardCopyOption.ATOMIC_MOVE)
502-
} catch (e: Exception) {
503-
e.printStackTrace()
504-
return 1
505-
}
506-
507-
return 0
508-
}
509-
510-
fun unpackZip(path: String, dest: String, subFolder: String = ""): Int {
511-
if (!Gdx.files.local(path).exists()) {
494+
fun unpackZip(path: Path, dest: Path, subFolder: String = ""): Int {
495+
if (Files.notExists(path)) {
512496
println("Failed to find $path")
513497
return -1
514498
}
515499

516-
if (!Gdx.files.local(dest).exists()) {
517-
Gdx.files.local(dest).mkdirs()
500+
if (Files.notExists(dest)) {
501+
Files.createDirectories(dest)
502+
println("Created $dest")
503+
} else {
504+
println("Found $dest")
518505
}
519506

520507
println("Extracting (zip) $path!/ -> $dest")
521508

522-
ZipInputStream(Gdx.files.local(path).read()).use { zipStream ->
509+
ZipInputStream(Files.newInputStream(path)).use { zipStream ->
523510
try {
524511
var entry = zipStream.nextEntry
525512
while (entry != null) {
@@ -528,14 +515,15 @@ fun unpackZip(path: String, dest: String, subFolder: String = ""): Int {
528515
continue
529516
}
530517

531-
val file = Gdx.files.local("$dest/${entry.name.substringAfter(subFolder)}")
532-
println("Extracting $path!/${entry.name} -> ${file.path()}")
518+
val file = dest.resolve(entry.name.substringAfter(subFolder))
519+
println("Extracting $path!/${entry.name} -> $file")
533520
if (entry.isDirectory) {
534-
file.mkdirs()
521+
Files.createDirectories(file)
535522
} else {
536-
if (!file.parent().exists())
537-
file.parent().mkdirs()
538-
file.write(false).use { output ->
523+
if (Files.notExists(file.parent))
524+
Files.createDirectories(file.parent)
525+
526+
Files.newOutputStream(file, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING).use { output ->
539527
zipStream.copyTo(output)
540528
}
541529
}
@@ -550,20 +538,23 @@ fun unpackZip(path: String, dest: String, subFolder: String = ""): Int {
550538
return 0
551539
}
552540

553-
fun unpackTarGZ(path: String, dest: String): Int {
554-
if (!Gdx.files.local(path).exists()) {
541+
fun unpackTarGZ(path: Path, dest: Path): Int {
542+
if (Files.notExists(path)) {
555543
println("Failed to find $path")
556544
return -1
557545
}
558546

559-
if (!Gdx.files.local(dest).exists()) {
560-
Gdx.files.local(dest).mkdirs()
547+
if (Files.notExists(dest)) {
548+
Files.createDirectories(dest)
549+
println("Created $dest")
550+
} else {
551+
println("Found $dest")
561552
}
562553

563554
println("Extracting (tar.gz) $path!/ -> $dest")
564555

565556
try {
566-
GZIPInputStream(Gdx.files.local(path).read()).use { gzipStream ->
557+
GZIPInputStream(Files.newInputStream(path)).use { gzipStream ->
567558
println("Extracting (tar) $path!/!/ -> $dest")
568559

569560
try {
@@ -590,7 +581,7 @@ fun unpackTarGZ(path: String, dest: String): Int {
590581
return 0
591582
}
592583

593-
private fun unpackTar(gzipStream: GZIPInputStream, dest: String, path: String): Int {
584+
private fun unpackTar(gzipStream: GZIPInputStream, dest: Path, path: Path): Int {
594585
TarArchiveInputStream(gzipStream).use { tarStream ->
595586
try {
596587
var entry = tarStream.nextEntry
@@ -669,7 +660,6 @@ object Main : ApplicationAdapter() {
669660
text = "Extracting Game"
670661
unpackGame(version)
671662

672-
File("temp").deleteRecursively()
673663
text = "Launching ${version.name}"
674664
launchGame(version, this)
675665
}
@@ -739,7 +729,7 @@ object Main : ApplicationAdapter() {
739729
playButton.text = "Downloading JDK (${(it * 100).toInt()}%)"
740730
}) {
741731
playButton.text = "Extracting JDK"
742-
if (unpack(it.toString(), Paths.get("jdk").toString()) != 0) {
732+
if (unpack(it, Paths.get("jdk")) != 0) {
743733
playButton.enabled = false
744734
playButton.text = "Failed to unpack JDK"
745735

0 commit comments

Comments
 (0)