1+ val moduleName by extra(" dev.faststats.hytale" )
2+
3+ val libsDir: Directory = layout.projectDirectory.dir(" libs" )
4+ val hytaleServerJar: RegularFile = libsDir.file(" HytaleServer.jar" )
5+ val credentialsFile: RegularFile = layout.projectDirectory.file(" .hytale-downloader-credentials.json" )
6+ val downloadDir: Provider <Directory > = layout.buildDirectory.dir(" download" )
7+ val hytaleZip: Provider <RegularFile > = downloadDir.map { it.file(" hytale.zip" ) }
8+
9+ dependencies {
10+ api(project(" :core" ))
11+ compileOnly(files(hytaleServerJar))
12+ }
13+
14+ tasks.register(" download-server" ) {
15+ group = " hytale"
16+
17+ doLast {
18+ if (hytaleServerJar.asFile.exists()) {
19+ println (" HytaleServer.jar already exists, skipping download" )
20+ return @doLast
21+ }
22+
23+ val downloaderZip: Provider <RegularFile > = downloadDir.map { it.file(" hytale-downloader.zip" ) }
24+
25+ libsDir.asFile.mkdirs()
26+ downloadDir.get().asFile.mkdirs()
27+
28+ val os = org.gradle.internal.os.OperatingSystem .current()
29+ val downloaderExecutable = when {
30+ os.isLinux -> downloadDir.map { it.file(" hytale-downloader-linux-amd64" ) }
31+ os.isWindows -> downloadDir.map { it.file(" hytale-downloader-windows-amd64.exe" ) }
32+ else -> throw GradleException (" Unsupported operating system: ${os.name} " )
33+ }
34+
35+ if (! downloaderExecutable.get().asFile.exists()) {
36+ if (! downloaderZip.get().asFile.exists()) ant.invokeMethod(
37+ " get" , mapOf (
38+ " src" to " https://downloader.hytale.com/hytale-downloader.zip" ,
39+ " dest" to downloaderZip.get().asFile.absolutePath
40+ )
41+ ) else {
42+ println (" hytale-downloader.zip already exists, skipping download" )
43+ }
44+
45+ copy {
46+ from(zipTree(downloaderZip))
47+ include(downloaderExecutable.get().asFile.name)
48+ into(downloadDir)
49+ }
50+ } else {
51+ println (" Hytale downloader binary already exists, skipping download and extraction" )
52+ }
53+
54+ if (downloaderZip.get().asFile.delete()) {
55+ println (" Deleted hytale-downloader.zip after extracting binaries" )
56+ }
57+
58+ downloaderExecutable.get().asFile.setExecutable(true )
59+
60+ if (! hytaleZip.get().asFile.exists()) {
61+ val credentials = System .getenv(" HYTALE_DOWNLOADER_CREDENTIALS" )
62+ if (! credentials.isNullOrBlank()) {
63+ if (! credentialsFile.asFile.exists()) {
64+ credentialsFile.asFile.writeText(credentials)
65+ println (" Hytale downloader credentials written from environment variable to ${credentialsFile.asFile.absolutePath} " )
66+ } else {
67+ println (" Using existing credentials file at ${credentialsFile.asFile.absolutePath} " )
68+ }
69+ }
70+
71+ val processBuilder = ProcessBuilder (
72+ downloaderExecutable.get().asFile.absolutePath,
73+ " -download-path" ,
74+ " hytale" ,
75+ " -credentials-path" ,
76+ credentialsFile.asFile.absolutePath
77+ )
78+ processBuilder.directory(downloadDir.get().asFile)
79+ processBuilder.redirectErrorStream(true )
80+ val process = processBuilder.start()
81+
82+ process.inputStream.bufferedReader().use { reader ->
83+ reader.lines().forEach { line ->
84+ println (line)
85+ }
86+ }
87+
88+ val exitCode = process.waitFor()
89+ if (exitCode != 0 ) {
90+ throw GradleException (" Hytale downloader failed with exit code: $exitCode " )
91+ }
92+ } else {
93+ println (" hytale.zip already exists, skipping download" )
94+ }
95+
96+ if (hytaleZip.get().asFile.exists()) {
97+ val serverDir = downloadDir.map { it.dir(" Server" ) }
98+ copy {
99+ from(zipTree(hytaleZip))
100+ include(" Server/HytaleServer.jar" )
101+ into(downloadDir)
102+ }
103+
104+ val extractedJar = serverDir.map { it.file(" HytaleServer.jar" ) }
105+ if (extractedJar.get().asFile.exists()) {
106+ extractedJar.get().asFile.copyTo(hytaleServerJar.asFile, overwrite = true )
107+ serverDir.get().asFile.deleteRecursively()
108+ } else {
109+ throw GradleException (" HytaleServer.jar was not found in Server/ subdirectory" )
110+ }
111+
112+ if (! hytaleServerJar.asFile.exists()) {
113+ throw GradleException (" HytaleServer.jar was not found in hytale.zip" )
114+ }
115+
116+ hytaleZip.get().asFile.delete()
117+ println (" Deleted hytale.zip after extracting HytaleServer.jar" )
118+ } else {
119+ throw GradleException (
120+ " hytale.zip not found at ${hytaleZip.get().asFile.absolutePath} . " +
121+ " The downloader may not have completed successfully."
122+ )
123+ }
124+ }
125+ }
126+
127+ tasks.register(" update-server" ) {
128+ group = " hytale"
129+ hytaleServerJar.asFile.delete()
130+ hytaleZip.get().asFile.delete()
131+ dependsOn(tasks.named(" download-server" ))
132+ }
133+
134+ tasks.compileJava {
135+ dependsOn(tasks.named(" download-server" ))
136+ }
0 commit comments