Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 81d30d0

Browse files
committed
Delete mods on next start using preLaunch hook
Closes #91 Signed-off-by: DeathsGun <deathsgun@protonmail.com>
1 parent 473371a commit 81d30d0

File tree

4 files changed

+83
-21
lines changed

4 files changed

+83
-21
lines changed

src/main/kotlin/xyz/deathsgun/modmanager/ModManager.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class ModManager : ClientModInitializer {
6868
provider[modrinth.getName().lowercase()] = modrinth
6969
updateProvider[modrinth.getName().lowercase()] = modrinth
7070
GlobalScope.launch {
71-
update.fullyDeleteMods()
7271
update.checkUpdates()
7372
icons.cleanupCache()
7473
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2021 DeathsGun
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package xyz.deathsgun.modmanager
18+
19+
import kotlinx.serialization.ExperimentalSerializationApi
20+
import kotlinx.serialization.decodeFromString
21+
import kotlinx.serialization.json.Json
22+
import net.fabricmc.loader.api.FabricLoader
23+
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint
24+
import org.apache.logging.log4j.LogManager
25+
import java.nio.file.Files
26+
import kotlin.io.path.Path
27+
import kotlin.io.path.deleteIfExists
28+
29+
class PreLaunchHook : PreLaunchEntrypoint {
30+
31+
private val logger = LogManager.getLogger("ModManager")
32+
33+
override fun onPreLaunch() {
34+
val filesToDelete = try {
35+
loadFiles()
36+
} catch (e: Exception) {
37+
ArrayList()
38+
}
39+
for (file in filesToDelete) {
40+
logger.info("Deleting {}", file)
41+
val path = Path(file)
42+
Files.delete(path)
43+
}
44+
}
45+
46+
@OptIn(ExperimentalSerializationApi::class)
47+
private fun loadFiles(): ArrayList<String> {
48+
val configFile = FabricLoader.getInstance().configDir.resolve(".modmanager.delete.json")
49+
if (Files.notExists(configFile)) {
50+
return ArrayList()
51+
}
52+
val data = Files.readString(configFile, Charsets.UTF_8)
53+
configFile.deleteIfExists()
54+
return Json.decodeFromString(data)
55+
}
56+
57+
}

src/main/kotlin/xyz/deathsgun/modmanager/update/UpdateManager.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import kotlinx.coroutines.coroutineScope
2121
import kotlinx.coroutines.launch
2222
import kotlinx.serialization.ExperimentalSerializationApi
2323
import kotlinx.serialization.decodeFromString
24+
import kotlinx.serialization.encodeToString
2425
import kotlinx.serialization.json.Json
2526
import net.fabricmc.loader.api.FabricLoader
2627
import net.fabricmc.loader.api.metadata.ModMetadata
@@ -47,7 +48,6 @@ import java.net.http.HttpRequest
4748
import java.net.http.HttpResponse
4849
import java.nio.file.Files
4950
import java.nio.file.Path
50-
import java.nio.file.StandardOpenOption
5151
import java.security.MessageDigest
5252
import java.time.Duration
5353
import java.util.zip.ZipFile
@@ -59,8 +59,13 @@ class UpdateManager {
5959
private val logger = LogManager.getLogger("UpdateCheck")
6060
private val blockedIds = arrayOf("java", "minecraft", "fabricloader")
6161
private val http: HttpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build()
62+
private val deletableMods = ArrayList<String>()
6263
val updates = ArrayList<Update>()
6364

65+
init {
66+
Runtime.getRuntime().addShutdownHook(Thread(this::saveDeletableFiles))
67+
}
68+
6469
//region Update Checking
6570

6671
suspend fun checkUpdates() = coroutineScope {
@@ -372,6 +377,18 @@ class UpdateManager {
372377
return ids
373378
}
374379

380+
@OptIn(ExperimentalSerializationApi::class)
381+
private fun saveDeletableFiles() {
382+
if (deletableMods.isEmpty()) {
383+
return
384+
}
385+
logger.info("Deleting {} mods on the next start.", deletableMods.size)
386+
val configFile = FabricLoader.getInstance().configDir.resolve(".modmanager.delete.json")
387+
val data = json.encodeToString(deletableMods)
388+
Files.writeString(configFile, data, Charsets.UTF_8)
389+
}
390+
391+
375392
private fun getCheckableMods(): List<ModMetadata> {
376393
return FabricLoader.getInstance().allMods.map { it.metadata }.filter {
377394
!it.id.startsWith("fabric-") &&
@@ -394,7 +411,7 @@ class UpdateManager {
394411
Files.delete(this)
395412
} catch (e: Exception) {
396413
logger.info("Error while deleting {} trying on restart again", this.absolutePathString())
397-
Files.writeString(this, "MODMANAGER", StandardOpenOption.WRITE)
414+
deletableMods.add(this.absolutePathString())
398415
}
399416
}
400417

@@ -425,21 +442,4 @@ class UpdateManager {
425442
return URI("dummy", url.replace("\t", ""), null).rawSchemeSpecificPart
426443
}
427444

428-
fun fullyDeleteMods() {
429-
val jars =
430-
FileUtils.listFiles(FabricLoader.getInstance().gameDir.resolve("mods").toFile(), arrayOf("jar"), true)
431-
for (jar in jars) {
432-
val content = try {
433-
Files.readString(jar.toPath())
434-
} catch (e: Exception) {
435-
""
436-
}
437-
if (content != "MODMANGER") {
438-
continue
439-
}
440-
logger.info("Deleting {}", jar.absolutePath)
441-
jar.delete()
442-
}
443-
}
444-
445445
}

src/main/resources/fabric.mod.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@
2222
"value": "xyz.deathsgun.modmanager.ModManager"
2323
}
2424
],
25+
"preLaunch": [
26+
{
27+
"adapter": "kotlin",
28+
"value": "xyz.deathsgun.modmanager.PreLaunchHook"
29+
}
30+
],
2531
"modmenu": [
2632
"xyz.deathsgun.modmanager.ModMenuEntrypoint"
27-
]
33+
]
2834
},
2935
"custom": {
3036
"modmanager": {

0 commit comments

Comments
 (0)