|
| 1 | +/* |
| 2 | + * Copyright 2025 Lambda |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.lambda.util |
| 19 | + |
| 20 | +import com.github.kittinunf.fuel.core.FuelError |
| 21 | +import com.github.kittinunf.fuel.httpDownload |
| 22 | +import com.github.kittinunf.fuel.httpGet |
| 23 | +import com.github.kittinunf.result.getOrNull |
| 24 | +import com.lambda.Lambda.mc |
| 25 | +import com.lambda.util.StringUtils.sanitizeForFilename |
| 26 | +import java.io.File |
| 27 | +import java.net.InetSocketAddress |
| 28 | + |
| 29 | +object FileUtils { |
| 30 | + /** |
| 31 | + * Returns a sequence of all the files in a tree that matches the [predicate] |
| 32 | + */ |
| 33 | + fun File.listRecursive(predicate: (File) -> Boolean): Sequence<File> = walk().filter(predicate) |
| 34 | + |
| 35 | + /** |
| 36 | + * Ensures the current file exists by creating it if it does not. |
| 37 | + * |
| 38 | + * If the file already exists, it will not be recreated. The necessary |
| 39 | + * parent directories will be created if they do not exist. |
| 40 | + */ |
| 41 | + fun File.createIfNotExists(): File = also { parentFile.mkdirs(); createNewFile() } |
| 42 | + |
| 43 | + /** |
| 44 | + * Retrieves or creates a directory based on the current network connection and world dimension. |
| 45 | + * |
| 46 | + * The directory is determined by the host name of the current network connection (or "singleplayer" if offline) |
| 47 | + * and the dimension key of the current world. These values are sanitized for use as filenames and combined |
| 48 | + * to form a path under the current file. If the directory does not exist, it will be created. |
| 49 | + * |
| 50 | + * @receiver The base directory where the location-bound directory will be created. |
| 51 | + * @return A `File` object representing the location-bound directory. |
| 52 | + * |
| 53 | + * The path is structured as: |
| 54 | + * - `[base directory]/[host name]/[dimension key]` |
| 55 | + * |
| 56 | + * Example: |
| 57 | + * If playing on a server with hostname "example.com" and in the "overworld" dimension, the path would be: |
| 58 | + * - `[base directory]/example.com/overworld` |
| 59 | + */ |
| 60 | + fun File.locationBoundDirectory(): File { |
| 61 | + val hostName = (mc.networkHandler?.connection?.address as? InetSocketAddress)?.hostName ?: "singleplayer" |
| 62 | + val path = resolve( |
| 63 | + hostName.sanitizeForFilename() |
| 64 | + ).resolve( |
| 65 | + mc.world?.dimensionKey?.value?.path?.sanitizeForFilename() ?: "unknown" // TODO: Change with utils when merged to master |
| 66 | + ) |
| 67 | + path.mkdirs() |
| 68 | + return path |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Executes the [block] if the receiver file exists |
| 73 | + */ |
| 74 | + inline fun File.ifExists(block: (File) -> Unit): File { |
| 75 | + if (exists()) block(this) |
| 76 | + return this |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Executes the [block] if the receiver file does not exist. |
| 81 | + */ |
| 82 | + inline fun File.ifNotExists(block: (File) -> Unit): File { |
| 83 | + if (!exists()) block(this) |
| 84 | + return this |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Downloads the given file url if the file is not present |
| 89 | + * |
| 90 | + * This function does not guarantee that the given file will be created |
| 91 | + */ |
| 92 | + fun File.downloadIfNotPresent(url: String, success: (ByteArray) -> Unit = {}, failure: (FuelError) -> Unit = {}): File = |
| 93 | + ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } } |
| 94 | + |
| 95 | + /** |
| 96 | + * Downloads the given file url if the file is not present |
| 97 | + * |
| 98 | + * This function does not guarantee that the given file will be created |
| 99 | + */ |
| 100 | + fun String.downloadIfNotPresent(file: File, success: (ByteArray) -> Unit = {}, failure: (FuelError) -> Unit = {}): File = |
| 101 | + file.ifNotExists { httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } } |
| 102 | + |
| 103 | + /** |
| 104 | + * Downloads the given file url if the file is not present |
| 105 | + * |
| 106 | + * This function does not guarantee that the given file will be created |
| 107 | + */ |
| 108 | + fun File.downloadIfNotPresent(): (String) -> Unit { |
| 109 | + return { url -> ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, _ -> } } } } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets the given url if the file is not present |
| 113 | + * |
| 114 | + * This function does not guarantee that the given file will be created |
| 115 | + */ |
| 116 | + fun File.getIfNotPresent(): (String) -> Unit { return { url -> ifNotExists { url.httpGet().responseString().third.getOrNull()?.let { writeText(it) } } } } |
| 117 | +} |
0 commit comments