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

Commit 851f5f0

Browse files
committed
Overwrite files to delete and delete them fully on restart
Windows doesn't allow you to delete files while there open in a program. So I can't delete the jars on Windows directly. So write "MODMANAGER" into the jar to make it invalid. After that when minecraft restarts it will be deleted by ModManager Signed-off-by: DeathsGun <deathsgun@protonmail.com>
1 parent 036f0c1 commit 851f5f0

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class ModManager : ClientModInitializer {
6868
provider[modrinth.getName().lowercase()] = modrinth
6969
updateProvider[modrinth.getName().lowercase()] = modrinth
7070
GlobalScope.launch {
71+
update.fullyDeleteMods()
7172
update.checkUpdates()
7273
icons.cleanupCache()
7374
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import java.net.http.HttpRequest
4747
import java.net.http.HttpResponse
4848
import java.nio.file.Files
4949
import java.nio.file.Path
50+
import java.nio.file.StandardOpenOption
5051
import java.security.MessageDigest
5152
import java.time.Duration
5253
import java.util.zip.ZipFile
@@ -388,8 +389,8 @@ class UpdateManager {
388389
try {
389390
Files.delete(this)
390391
} catch (e: Exception) {
391-
logger.info("Error while deleting {} trying on exit again", this.absolutePathString())
392-
this.toFile().deleteOnExit()
392+
logger.info("Error while deleting {} trying on restart again", this.absolutePathString())
393+
Files.writeString(this, "MODMANAGER", StandardOpenOption.WRITE)
393394
}
394395
}
395396

@@ -420,4 +421,22 @@ class UpdateManager {
420421
return URI("dummy", url.replace("\t", ""), null).rawSchemeSpecificPart
421422
}
422423

424+
fun fullyDeleteMods() {
425+
val jars =
426+
FileUtils.listFiles(FabricLoader.getInstance().gameDir.resolve("mods").toFile(), arrayOf("jar"), true)
427+
for (jar in jars) {
428+
val content = try {
429+
Files.readString(jar.toPath())
430+
} catch (e: Exception) {
431+
logger.info("Failed to read file ignoring it: {}", e.message)
432+
""
433+
}
434+
if (content != "MODMANGER") {
435+
continue
436+
}
437+
logger.info("Deleting {}", jar.absolutePath)
438+
jar.delete()
439+
}
440+
}
441+
423442
}

0 commit comments

Comments
 (0)