-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
375 lines (334 loc) · 13.3 KB
/
build.gradle
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import arc.util.OS
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.nio.file.StandardOpenOption
buildscript {
repositories {//more than you need
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
maven { url 'https://jitpack.io' }
google()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("com.github.Anuken.Arc:arc-core:$arcHash")//why not
}
}
apply plugin: 'java'
apply plugin: 'java-library'
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
//github CI don't sort folder by default
static File getLatestFolder(File fe) {
ArrayList<File> f = new ArrayList<>()
if (!fe.exists()) throw new FileNotFoundException(fe.getAbsolutePath())
File[] files = fe.listFiles()
f.addAll(Arrays.asList(files))
f.sort(new Comparator<File>() {
@Override
int compare(File o1, File o2) {
return o1.getAbsolutePath() <=> o2.getAbsolutePath()
}
})
return f.get(f.size() - 1)
}
allprojects {
apply plugin: 'java'
apply plugin: 'java-library'
group = 'org.example'
version = modsNversion
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
maven { url 'https://jitpack.io' }
}
ext {
try {
System.properties.load(new FileReader("gradle.properties"))
System.properties.load(new FileReader("local.properties"))
} catch (ignored) {
}
//automatically detect latest shit and skipping it if doesn't exists
sdkRoot = System.getenv("ANDROID_HOME") ?: System.getenv("ANDROID_SDK_ROOT")
//or just replace it by direct path if you don't need github CI
if (sdkRoot == null)
sdkRoot = new File("SDK").getAbsolutePath()
sdkD8 = sdkRoot + "nope"
sdkJar = sdkRoot + "nope"//nopped in case folder didn't exist
//redudant ?
if (new File(sdkRoot).exists()) {
try {
//somehow it end .bat
String extension = OS.isWindows ? ".bat" : ""
sdkD8 = new File(getLatestFolder(new File(sdkRoot, 'build-tools/')), "d8" + extension).getAbsolutePath()
sdkJar = new File(getLatestFolder(new File(sdkRoot, "platforms/")), "android.jar").getAbsolutePath()
} catch (ignored) {
//FileNotFoundException
}
}
if (!new File(sdkJar).exists())//redundant ?
sdkJar = new File(sdkRoot, "nope").getAbsolutePath()
if (!new File(sdkD8).exists())
sdkD8 = new File(sdkRoot, "nope").getAbsolutePath()
mindustryExecutable = getMindustryFile()
mindustryServerExecutable = getMindustryServerFile()
//can't assign type
androidJarFile = new File(sdkJar)
//imagine writing json
modhjson = ""
modhjson += 'name: "' + modsNname + '"\n'
modhjson += 'displayName: "' + modsNdisplayName + '"\n'
modhjson += 'description: "' + modsNdescription + '"\n'
modhjson += 'author: "' + modsNauthor + '"\n'
modhjson += 'main: "' + modsNmain + '"\n'
modhjson += 'minGameVersion: ' + modsNminMindustryVersion + '\n'
modhjson += "version: " + modsNversion + "\n"
modhjson += 'hidden: ' + modsNsupportVanillaServer + '\n'
if (project.hasProperty("githubRepo"))//supplied by workflow runner (gradle.yml) -PgithubRepo="$GITHUB_REPOSITORY"
modhjson += 'repo: \"' + githubRepo + '\" \n'
else
modhjson += 'repo: "' + modsNrepo + '"\n'
}
//force arc version
configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.github.Anuken.Arc') {
details.useVersion "$mindustryHash"
}
}
}
//From Root to every project
dependencies {
def be = System.getProperty("modsNBE") != null && !System.getProperty("modsNBE").isEmpty()
if (be) downloadMindustry(getMindustryURL(), mindustryExecutable)
if (be && mindustryExecutable.exists()) {
compileOnly(files(mindustryExecutable))
//testCompileOnly(files(mindustry))
testImplementation(files(mindustryExecutable))
}
compileOnly("com.github.Anuken.Arc:arc-core:$arcHash") {
because("provided")
}
compileOnly("com.github.Anuken.Arc:backend-sdl:$arcHash") {
because("provided")
}
compileOnly("com.github.Anuken.MindustryJitpack:core:$mindustryHash") {
because("provided")
}
}
}
ext {
//rewrite mod.hjson everytime gradle rollin
try {
File f = new File(projectDir, "mod.hjson")
f.delete()
Files.write(f.toPath(), modhjson.toString().getBytes(), StandardOpenOption.WRITE, StandardOpenOption.CREATE)
} catch (ignored) {
}
}
//Root Module only
dependencies {
if (!OS.isWindows) annotationProcessor "com.github.Anuken:jabel:$jabelVersion"//this thing crash in Windows
if (androidJarFile.exists())
compileOnly(files(androidJarFile))
}
//not every module use java 8
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.compilerArgs += ["-Xlint:deprecation", "-Xlint:unchecked"]
options.compilerArgs.addAll(['--release', '8'])//android like java 8
}
//make .class jar with executable main and MANIFEST.MF
//your typical jar
task base(type: Jar) {
archiveFileName = modsNname + ".jar"//Example-Mods,jar
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)//stop including duplicated copy of library, D8 really mad
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
//Runtime library only, compile library like arc won't get included
}
with jar//what is this
manifest {
attributes 'Main-Class': modsNJavaMain//what
}
from(rootDir) {
include "mod.hjson"//assuming the rootdir is actually current folder
include "icon.png"
}
}
//source code documentation instead of decompiled .class, if you use jitpack that is
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'//deprecated
from sourceSets.main.allSource
}
//not jar task
//just handle making dexed jar after making typical jar
task baseJar {
dependsOn 'base'//typical jar
//collect dependencies needed for desugaring
if (new File(sdkD8).exists()) {
def files = (configurations.compileClasspath.asList() + configurations.runtimeClasspath.asList() + [new File(sdkJar)])
//all dependency go
def dependencies = files.collect { "--classpath $it.path" }.join(" ")//argument builder
def cmd = "${sdkD8} $dependencies --min-api ${minAndroidApi} --output Dex.jar ${modsNname}.jar"
//should output Dex.jar from Example-Mods.jar
def dir = new File(projectDir, "build/libs/")
dir.mkdirs()//cuz sike, file not found exception
doLast {
if (new File(sdkD8).exists() && new File(sdkJar).exists()) {
println(cmd)//comment this to disable spam
Runtime.getRuntime().exec(cmd, new String[0], dir).waitForProcessOutput(System.out, System.err)
}
}
} else {
println "No sdkD8 detected on ANDROID_HOME, skipping desugaring"//y not
}
}
//here we combine typical jar and dex jar to make
//typical-dex.jar
//if android sdk exist
task makeJar(type: Jar) {
dependsOn baseJar//dex jar
if (new File(sdkD8).exists()) {//some people don't have android SDK
archiveFileName = modsNname + "-Dexed.jar"
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
if (new File(sdkD8).exists()) {
from() {
zipTree(new File(projectDir, "build/libs/Dex.jar"))//include Dex.jar
}
}
} else {
println "No sdkD8 detected on ANDROID_HOME, skipping making Combined Dex-Class Jar"
}
from() {
zipTree(new File(projectDir, "build/libs/${modsNname}.jar"))//include YourModsName.jar or typical.jar
}
}
//TODO add more dependsOn
//if you have multiple version of mods
task deploy(type: Jar) {
dependsOn makeJar
}
//h
//add progress bar
static void downloadMindustry(URL url, File download) {
download.getParentFile().mkdirs()
if (download.exists()) return
System.out.println("Downloading: " + url.toString())
HttpURLConnection con = (HttpURLConnection) url.openConnection()
BufferedInputStream input = new BufferedInputStream(con.getInputStream())
download.getAbsoluteFile().getParentFile().mkdirs()
RandomAccessFile output = new RandomAccessFile(download, "rw")
int bytesRead
byte[] buffer = new byte[4096]
long nextLog = System.currentTimeMillis() + 4000
long readed = 0
while ((bytesRead = input.read(buffer)) != -1) {
readed += bytesRead
output.write(buffer, 0, bytesRead)
if (System.currentTimeMillis() > nextLog) {
System.out.println(readed / 1_000_000)
nextLog = System.currentTimeMillis() + 4000
}
}
output.close()
}
//don't crash
static void copyToMindustry(File f) {
File target = new File(OS.getAppDataDirectoryString("Mindustry"))
target = new File(target, "mods")
target = new File(target, f.getName())
target.getParentFile().mkdirs()
System.out.println("Copying: " + f.getAbsolutePath() + " to " + target.getAbsolutePath())
Files.copy(f.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
static URL getMindustryServerURL() {
if (System.getProperty("modsNBE", null) != null) {
String h = System.getProperty("modsNBE")
return new URL("https://github.com/Anuken/MindustryBuilds/releases/download/" + h + "/Mindustry-BE-Server-" + h + ".jar")
}
return new URL("https://github.com/Anuken/Mindustry/releases/download/" + System.getProperty("mindustryHash") + "/server-release.jar")
}
static File getMindustryServerFile() {
URL u = getMindustryServerURL()
File mindustry = new File(new File(new File(OS.getAppDataDirectoryString("Mindustry")), "build/cache/"), u.getFile())
return mindustry.getAbsoluteFile()
}
static URL getMindustryURL() {
if (System.getProperty("modsNBE", null) != null) {
String h = System.getProperty("modsNBE")
return new URL("https://github.com/Anuken/MindustryBuilds/releases/download/" + h + "/Mindustry-BE-Desktop-" + h + ".jar")
}
return new URL("https://github.com/Anuken/Mindustry/releases/download/" + System.getProperty("mindustryHash") + "/Mindustry.jar")
}
static File getMindustryFile() {
URL u = getMindustryURL()
File mindustry = new File(new File(new File(OS.getAppDataDirectoryString("Mindustry")), "build/cache/"), u.getFile())
return mindustry.getAbsoluteFile()
}
//how lazy to download this
task download() {
doLast {
downloadMindustry(getMindustryServerURL(), getMindustryServerFile())
downloadMindustry(getMindustryURL(), getMindustryFile())
}
}
//don't want open file manager
task copyMods() {
dependsOn 'base'//you don't need dex jar for pc
doLast {
//client side
File srcJar = new File(new File(projectDir.toString()), "build/libs/" + modsNname + ".jar")
copyToMindustry(srcJar)
//server side
File target = new File(projectDir.toString(), "config/mods/" + modsNname + ".jar")
target.getParentFile().mkdirs()
System.out.println("Copying: " + srcJar.getAbsolutePath() + " to " + target.getAbsolutePath())
Files.copy(srcJar.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
}
//java -jar Mindustry.jar
//cooler java -jar Mindustry.jar
task runClient(type: JavaExec) {
dependsOn download
dependsOn copyMods//make class jar then copy it, why the hell you need to dex it
File mindustry = getMindustryFile()
println(mindustry.getAbsolutePath())
classpath = files(mindustry.getAbsolutePath())
//classpath += sourceSets.main.runtimeClasspath//will append classpath to java
main = "mindustry.desktop.DesktopLauncher"
}
task runServer(type: JavaExec) {
dependsOn download
dependsOn copyMods//make class jar then copy it, why the hell you need to dex it
File mindustry = getMindustryServerFile()
println(mindustry.getAbsolutePath())
classpath = files(mindustry.getAbsolutePath())
//classpath += sourceSets.main.runtimeClasspath//will append classpath to java
args = ["host", "Ancient_Caldera", "sandbox"]
main = "mindustry.server.ServerLauncher"
}
task runCleanup {
doLast {
//delete copyMods
//server side
File server = new File(projectDir.toString(), "config/mods/" + modsNname + ".jar")
server.delete()
//client side
File srcJar = new File(new File(projectDir.toString()), "build/libs/" + modsNname + ".jar")
//srcJar.delete()
File client = new File(OS.getAppDataDirectoryString("Mindustry"))
client = new File(client, "mods")
client = new File(client, srcJar.getName())
client.delete()
println("Cleanup done")
}
}
runClient.finalizedBy runCleanup
runServer.finalizedBy runCleanup