11package com.lambda.util
22
33import com.lambda.Lambda.mc
4- import com.lambda.util.FolderRegister.config
5- import com.lambda.util.FolderRegister.lambda
6- import com.lambda.util.FolderRegister.minecraft
7- import com.lambda.util.FolderRegister.mods
8- import com.lambda.util.FolderRegister.packetLogs
9- import com.lambda.util.FolderRegister.replay
104import com.lambda.util.StringUtils.sanitizeForFilename
11- import org.apache.commons.codec.digest.DigestUtils
125import java.io.File
13- import java.io.InputStream
146import java.net.InetSocketAddress
15- import java.security.MessageDigest
167
178/* *
189 * The [FolderRegister] object is responsible for managing the directory structure of the application.
@@ -33,12 +24,36 @@ object FolderRegister {
3324 val replay: File = File (lambda, " replay" )
3425 val cache: File = File (lambda, " cache" )
3526
36- fun File.createIfNotExists () {
37- createFileIfNotExists(this .name, this .parentFile)
38- }
27+ /* *
28+ * Ensures the current file exists by creating it if it does not.
29+ *
30+ * If the file already exists, it will not be recreated. The necessary
31+ * parent directories will be created if they do not exist.
32+ */
33+ fun File.createIfNotExists (): File = also { parentFile.mkdirs(); createNewFile() }
3934
40- fun File.listRecursive (predicate : (File ) -> Boolean = { true }) = walk().filter(predicate)
35+ /* *
36+ * Returns a sequence of all the files in a tree that matches the [predicate]
37+ */
38+ fun File.listRecursive (predicate : (File ) -> Boolean ) = walk().filter(predicate)
4139
40+ /* *
41+ * Retrieves or creates a directory based on the current network connection and world dimension.
42+ *
43+ * The directory is determined by the host name of the current network connection (or "singleplayer" if offline)
44+ * and the dimension key of the current world. These values are sanitized for use as filenames and combined
45+ * to form a path under the current file. If the directory does not exist, it will be created.
46+ *
47+ * @receiver The base directory where the location-bound directory will be created.
48+ * @return A `File` object representing the location-bound directory.
49+ *
50+ * The path is structured as:
51+ * - `[base directory]/[host name]/[dimension key]`
52+ *
53+ * Example:
54+ * If playing on a server with hostname "example.com" and in the "overworld" dimension, the path would be:
55+ * - `[base directory]/example.com/overworld`
56+ */
4257 fun File.locationBoundDirectory (): File {
4358 val hostName = (mc.networkHandler?.connection?.address as ? InetSocketAddress )?.hostName ? : " singleplayer"
4459 val path = resolve(
@@ -49,72 +64,4 @@ object FolderRegister {
4964 path.createIfNotExists()
5065 return path
5166 }
52-
53- /* *
54- * Returns a file with the given name in the specified directory, creating it if it does not exist.
55- * If the directory is not specified, it will try to parse it from the name.
56- * Otherwise, it will default to the Lambda directory.
57- *
58- * @param name The name of the file.
59- * @param directory The directory in which the file is located. Default is the Lambda directory.
60- * @param hash Whether to hash the name of the file.
61- * @return A pair containing the file and a boolean indicating whether the file was created.
62- */
63- fun createFileIfNotExists (name : String , directory : File ? = null, hash : Boolean = false): Pair <File , Boolean > {
64- var parsedDir: File = directory ? : lambda
65-
66- if (directory == null ) {
67- parsedDir = name.substringAfterLast(' /' ).substringBeforeLast(' .' )
68- .let { if (it.isEmpty()) lambda else File (it) }
69- }
70-
71- val compiledName =
72- if (hash) DigestUtils .sha256Hex(name)
73- else name
74-
75- val file = File (parsedDir, compiledName)
76- val created = ! file.exists()
77-
78- if (created) {
79- file.parentFile.mkdirs()
80- file.createNewFile()
81- }
82-
83- return file to created
84- }
85-
86-
87- /* *
88- * Returns a file with the given name in the specified directory, creating it if it does not exist.
89- * If the directory is not specified, it will try to parse it from the name.
90- * Otherwise, it will default to the Lambda directory.
91- *
92- * @param name The name of the file.
93- * @param directory The directory in which the file is located. Default is the Lambda directory.
94- * @param compute A lambda function to compute the file contents if it was created.
95- */
96- @JvmName(" getFileOrComputeByteArray" )
97- inline fun getFileOrCompute (name : String , directory : File ? = null, compute : () -> ByteArray ): File {
98- val (file, wasCreated) = createFileIfNotExists(name, directory)
99- if (wasCreated) file.outputStream().use { it.write(compute()) }
100-
101- return file
102- }
103-
104- /* *
105- * Returns a file with the given name in the specified directory, creating it if it does not exist.
106- * If the directory is not specified, it will try to parse it from the name.
107- * Otherwise, it will default to the Lambda directory.
108- *
109- * @param name The name of the file.
110- * @param directory The directory in which the file is located. Default is the Lambda directory.
111- * @param compute A lambda function to compute the file contents if it was created.
112- */
113- @JvmName(" getFileOrComputeInputStream" )
114- inline fun getFileOrCompute (name : String , directory : File ? = null, compute : () -> InputStream ): File {
115- val (file, wasCreated) = createFileIfNotExists(name, directory)
116- if (wasCreated) file.outputStream().use { compute().copyTo(it) }
117-
118- return file
119- }
12067}
0 commit comments