|
| 1 | +package com.mairwunnx.projectessentials.backup |
| 2 | + |
| 3 | +import com.mairwunnx.projectessentials.backup.configuration.BackupConfigurationController |
| 4 | +import kotlinx.coroutines.* |
| 5 | +import net.minecraftforge.api.distmarker.Dist |
| 6 | +import net.minecraftforge.fml.DistExecutor |
| 7 | +import org.zeroturnaround.zip.ZipUtil |
| 8 | +import java.io.File |
| 9 | +import java.text.SimpleDateFormat |
| 10 | +import java.util.* |
| 11 | + |
| 12 | +object BackupController { |
| 13 | + private lateinit var job: Job |
| 14 | + var isFirstLaunch = true |
| 15 | + |
| 16 | + fun launchBackupLoop() { |
| 17 | + job = GlobalScope.launch { |
| 18 | + while (isActive) { |
| 19 | + backupLoop() |
| 20 | + } |
| 21 | + } |
| 22 | + job.start() |
| 23 | + } |
| 24 | + |
| 25 | + fun abortBackupLoop() = job.cancel() |
| 26 | + |
| 27 | + private suspend fun backupLoop() { |
| 28 | + val configuration = BackupConfigurationController.get() |
| 29 | + |
| 30 | + if (configuration.backupEnabled) { |
| 31 | + if (configuration.firstLaunchDelay) { |
| 32 | + if (isFirstLaunch) { |
| 33 | + delay(configuration.backupCreationDelay) |
| 34 | + isFirstLaunch = false |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + val backupDirectory = File(configuration.backupDirectoryPath) |
| 39 | + backupDirectory.mkdirs() |
| 40 | + |
| 41 | + val fileList = backupDirectory.listFiles() ?: emptyArray() |
| 42 | + removeExtraFiles(fileList) |
| 43 | + |
| 44 | + if (fileList.count() >= configuration.maxBackupFiles) { |
| 45 | + if (configuration.rollingBackupFilesEnabled) { |
| 46 | + doRollingFiles(fileList) |
| 47 | + } else { |
| 48 | + fileList.forEach { |
| 49 | + if (it.isFile && !it.isDirectory) { |
| 50 | + it.delete() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + DistExecutor.runWhenOn(Dist.CLIENT) { |
| 57 | + Runnable { |
| 58 | + makeBackup("saves${File.separator}${EntryPoint.serverInstance.worldName}") |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + DistExecutor.runWhenOn(Dist.DEDICATED_SERVER) { |
| 63 | + Runnable { |
| 64 | + makeBackup(EntryPoint.serverInstance.worldName) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + delay(configuration.backupCreationDelay) |
| 69 | + } else { |
| 70 | + abortBackupLoop() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private fun removeExtraFiles(files: Array<File>) { |
| 75 | + if (!BackupConfigurationController.get().removeExtraFiles) return |
| 76 | + |
| 77 | + files.forEach { |
| 78 | + if (it.isDirectory || it.extension != "zip") { |
| 79 | + it.delete() |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private fun doRollingFiles(fileList: Array<File>) { |
| 85 | + val lastModifiedDates = mutableListOf<Long>() |
| 86 | + fileList.forEach { |
| 87 | + if (it.isFile && !it.isDirectory) { |
| 88 | + lastModifiedDates.add(it.lastModified()) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + val lastModified = lastModifiedDates.min() |
| 93 | + if (lastModified != null) { |
| 94 | + fileList.forEach { |
| 95 | + if (it.isFile && !it.isDirectory) { |
| 96 | + if (it.lastModified() == lastModified) { |
| 97 | + it.delete() |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private fun makeBackup(path: String) { |
| 105 | + val configuration = BackupConfigurationController.get() |
| 106 | + |
| 107 | + ZipUtil.pack( |
| 108 | + File(path), |
| 109 | + File(buildFilePathName(path)), |
| 110 | + configuration.backupCompressionLevel |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + private fun buildFilePathName(path: String): String { |
| 115 | + val configuration = BackupConfigurationController.get() |
| 116 | + val dateFormat = SimpleDateFormat(configuration.backupDateFormat) |
| 117 | + val extension = ".zip" |
| 118 | + |
| 119 | + val backupDirectory = configuration.backupDirectoryPath |
| 120 | + val currentDateTime = dateFormat.format(Date()) |
| 121 | + |
| 122 | + return if (path.contains(File.separator)) { |
| 123 | + val worldName = path.split(File.separator)[1] |
| 124 | + backupDirectory + File.separator + worldName + currentDateTime + extension |
| 125 | + } else { |
| 126 | + backupDirectory + File.separator + path + currentDateTime + extension |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments