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

Commit

Permalink
Delete mods on next start using preLaunch hook
Browse files Browse the repository at this point in the history
Closes #91

Signed-off-by: DeathsGun <deathsgun@protonmail.com>
  • Loading branch information
DeathsGun committed Nov 2, 2021
1 parent 473371a commit 81d30d0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/main/kotlin/xyz/deathsgun/modmanager/ModManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class ModManager : ClientModInitializer {
provider[modrinth.getName().lowercase()] = modrinth
updateProvider[modrinth.getName().lowercase()] = modrinth
GlobalScope.launch {
update.fullyDeleteMods()
update.checkUpdates()
icons.cleanupCache()
}
Expand Down
57 changes: 57 additions & 0 deletions src/main/kotlin/xyz/deathsgun/modmanager/PreLaunchHook.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 DeathsGun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package xyz.deathsgun.modmanager

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import net.fabricmc.loader.api.FabricLoader
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint
import org.apache.logging.log4j.LogManager
import java.nio.file.Files
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class PreLaunchHook : PreLaunchEntrypoint {

private val logger = LogManager.getLogger("ModManager")

override fun onPreLaunch() {
val filesToDelete = try {
loadFiles()
} catch (e: Exception) {
ArrayList()
}
for (file in filesToDelete) {
logger.info("Deleting {}", file)
val path = Path(file)
Files.delete(path)
}
}

@OptIn(ExperimentalSerializationApi::class)
private fun loadFiles(): ArrayList<String> {
val configFile = FabricLoader.getInstance().configDir.resolve(".modmanager.delete.json")
if (Files.notExists(configFile)) {
return ArrayList()
}
val data = Files.readString(configFile, Charsets.UTF_8)
configFile.deleteIfExists()
return Json.decodeFromString(data)
}

}
38 changes: 19 additions & 19 deletions src/main/kotlin/xyz/deathsgun/modmanager/update/UpdateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.fabricmc.loader.api.FabricLoader
import net.fabricmc.loader.api.metadata.ModMetadata
Expand All @@ -47,7 +48,6 @@ import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption
import java.security.MessageDigest
import java.time.Duration
import java.util.zip.ZipFile
Expand All @@ -59,8 +59,13 @@ class UpdateManager {
private val logger = LogManager.getLogger("UpdateCheck")
private val blockedIds = arrayOf("java", "minecraft", "fabricloader")
private val http: HttpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build()
private val deletableMods = ArrayList<String>()
val updates = ArrayList<Update>()

init {
Runtime.getRuntime().addShutdownHook(Thread(this::saveDeletableFiles))
}

//region Update Checking

suspend fun checkUpdates() = coroutineScope {
Expand Down Expand Up @@ -372,6 +377,18 @@ class UpdateManager {
return ids
}

@OptIn(ExperimentalSerializationApi::class)
private fun saveDeletableFiles() {
if (deletableMods.isEmpty()) {
return
}
logger.info("Deleting {} mods on the next start.", deletableMods.size)
val configFile = FabricLoader.getInstance().configDir.resolve(".modmanager.delete.json")
val data = json.encodeToString(deletableMods)
Files.writeString(configFile, data, Charsets.UTF_8)
}


private fun getCheckableMods(): List<ModMetadata> {
return FabricLoader.getInstance().allMods.map { it.metadata }.filter {
!it.id.startsWith("fabric-") &&
Expand All @@ -394,7 +411,7 @@ class UpdateManager {
Files.delete(this)
} catch (e: Exception) {
logger.info("Error while deleting {} trying on restart again", this.absolutePathString())
Files.writeString(this, "MODMANAGER", StandardOpenOption.WRITE)
deletableMods.add(this.absolutePathString())
}
}

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

fun fullyDeleteMods() {
val jars =
FileUtils.listFiles(FabricLoader.getInstance().gameDir.resolve("mods").toFile(), arrayOf("jar"), true)
for (jar in jars) {
val content = try {
Files.readString(jar.toPath())
} catch (e: Exception) {
""
}
if (content != "MODMANGER") {
continue
}
logger.info("Deleting {}", jar.absolutePath)
jar.delete()
}
}

}
8 changes: 7 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
"value": "xyz.deathsgun.modmanager.ModManager"
}
],
"preLaunch": [
{
"adapter": "kotlin",
"value": "xyz.deathsgun.modmanager.PreLaunchHook"
}
],
"modmenu": [
"xyz.deathsgun.modmanager.ModMenuEntrypoint"
]
]
},
"custom": {
"modmanager": {
Expand Down

0 comments on commit 81d30d0

Please sign in to comment.