aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/gui/config/storage/LegacyImporter.kt
blob: c1f8b90cce821504d3830ea607f6b91ca1d11439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package moe.nea.firmament.gui.config.storage

import java.nio.file.Path
import kotlin.io.path.copyTo
import kotlin.io.path.createDirectories
import kotlin.io.path.createParentDirectories
import kotlin.io.path.exists
import kotlin.io.path.forEachDirectoryEntry
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.moveTo
import kotlin.io.path.name
import kotlin.io.path.nameWithoutExtension
import kotlin.io.path.writeText
import moe.nea.firmament.gui.config.storage.FirmamentConfigLoader.configFolder
import moe.nea.firmament.gui.config.storage.FirmamentConfigLoader.configVersionFile
import moe.nea.firmament.gui.config.storage.FirmamentConfigLoader.storageFolder

object LegacyImporter {
	val legacyConfigVersion = 995
	val backupPath = configFolder.resolveSibling("firmament-legacy-config-${System.currentTimeMillis()}")

	fun copyIf(from: Path, to: Path) {
		if (from.exists()) {
			to.createParentDirectories()
			from.copyTo(to)
		}
	}

	val legacyStorage = listOf(
		"inventory-buttons",
		"macros",
	)

	fun importFromLegacy() {
		if (!configFolder.exists()) return
		configFolder.moveTo(backupPath)
		configFolder.createDirectories()

		legacyStorage.forEach {
			copyIf(
				backupPath.resolve("$it.json"),
				storageFolder.resolve("$it.json")
			)
		}

		backupPath.listDirectoryEntries("*.json")
			.filter { it.nameWithoutExtension !in legacyStorage }
			.forEach { path ->
				val name = path.name
				path.copyTo(configFolder.resolve(name))
			}

		backupPath.resolve("profiles")
			.takeIf { it.exists() }
			?.forEachDirectoryEntry { category ->
				category.forEachDirectoryEntry { profile ->
					copyIf(
						profile,
						FirmamentConfigLoader.profilePath
							.resolve(profile.nameWithoutExtension)
							.resolve(category.name + ".json")
					)
				}
			}

		configVersionFile.writeText("$legacyConfigVersion LEGACY")
	}
}