build.gradle 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. buildscript {
  2. repositories {
  3. mavenCentral()
  4. }
  5. dependencies {
  6. classpath("commons-io:commons-io:2.6")
  7. }
  8. }
  9. import java.nio.file.FileVisitResult
  10. import java.nio.file.Files
  11. import java.nio.file.Path
  12. import java.nio.file.SimpleFileVisitor
  13. import java.nio.file.attribute.BasicFileAttributes
  14. import java.util.stream.Stream
  15. import java.util.zip.ZipEntry
  16. import java.util.zip.ZipInputStream
  17. import java.util.zip.ZipOutputStream
  18. plugins {
  19. id "architectury-plugin" version "1.3.39"
  20. id "forgified-fabric-loom" version "0.5.16" apply false
  21. }
  22. architect {
  23. minecraft = minecraft_version
  24. }
  25. subprojects {
  26. apply plugin: "forgified-fabric-loom"
  27. loom {
  28. silentMojangMappingsLicense()
  29. }
  30. }
  31. allprojects {
  32. apply plugin: "java"
  33. apply plugin: "architectury-plugin"
  34. group "me.shedaniel"
  35. archivesBaseName = rootProject.name
  36. version = rootProject.mod_version
  37. tasks.withType(JavaCompile) {
  38. options.encoding = "UTF-8"
  39. }
  40. }
  41. task buildMerged {
  42. allprojects {
  43. dependsOn it.tasks.getByName("build")
  44. }
  45. doLast {
  46. def folder = file(".gradle/.mergemods")
  47. folder.mkdirs()
  48. def fabricJar = file("fabric/build/libs/${rootProject.name}-${rootProject.mod_version}-fabric.jar")
  49. def forgeJar = file("forge/build/libs/${rootProject.name}-${rootProject.mod_version}-forge.jar")
  50. def fabricFolder = new File(folder, ".tempFabric")
  51. def forgeFolder = new File(folder, ".tempForge")
  52. def mergeFolder = new File(folder, ".tempMerge")
  53. def policyMap = new HashMap<String, String>()
  54. file("merging.policy").eachLine {
  55. if (it.isBlank() || it.startsWith("#")) return
  56. def env = it.substring(0, it.indexOf(' '))
  57. if (env == "FABRIC")
  58. policyMap.put(it.substring(env.length() + 1), "Fabric")
  59. else if (env == "FORGE")
  60. policyMap.put(it.substring(env.length() + 1), "Forge")
  61. else throw new IllegalStateException("Illegal env $env at $it")
  62. }
  63. forgeFolder.deleteDir()
  64. fabricFolder.deleteDir()
  65. mergeFolder.deleteDir()
  66. unzip(fabricJar, fabricFolder)
  67. unzip(forgeJar, forgeFolder)
  68. mergeFolder.mkdirs()
  69. Stream.of(forgeFolder, fabricFolder).each { useFolder ->
  70. try {
  71. Files.walkFileTree(useFolder.toPath(), new SimpleFileVisitor<Path>() {
  72. @Override
  73. FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  74. try {
  75. File ogFile = file.toFile()
  76. File outFile = new File(mergeFolder, ogFile.getAbsolutePath().replace(useFolder.getAbsolutePath(), ""))
  77. outFile.getParentFile().mkdirs()
  78. if (outFile.exists()) {
  79. def env = useFolder.getName().substring(5)
  80. def fileName = outFile.getAbsolutePath().replace(mergeFolder.getAbsolutePath(), "")
  81. if (!ogFile.isFile() || !outFile.isFile() || !Arrays.equals(ogFile.readBytes(), outFile.readBytes())) {
  82. def policyEnv = policyMap.get(fileName)
  83. if (policyEnv == null) {
  84. throw new IllegalStateException("Unhandled duplicate file: $fileName")
  85. }
  86. println "Chose env ${policyEnv.toUpperCase(Locale.ROOT)} for duplicate file: $fileName"
  87. if (policyEnv != env)
  88. return FileVisitResult.CONTINUE
  89. }
  90. }
  91. if (!ogFile.isDirectory()) {
  92. org.apache.commons.io.FileUtils.copyFile(ogFile, outFile)
  93. } else {
  94. org.apache.commons.io.FileUtils.copyDirectory(ogFile, outFile)
  95. }
  96. } catch (IOException e) {
  97. e.printStackTrace()
  98. System.exit(0)
  99. }
  100. return FileVisitResult.CONTINUE
  101. }
  102. })
  103. } catch (IOException e) {
  104. e.printStackTrace()
  105. System.exit(0)
  106. }
  107. }
  108. File finalMerge = file("build/libs/${rootProject.name}-${rootProject.mod_version}.jar")
  109. finalMerge.parentFile.mkdirs()
  110. finalMerge.delete()
  111. compress(mergeFolder.toPath(), finalMerge)
  112. folder.deleteDir()
  113. }
  114. }
  115. rootProject.subprojects.forEach {
  116. buildMerged.mustRunAfter it.tasks.getByName("build")
  117. }
  118. static def compress(Path sourceDir, File zipFile) {
  119. try {
  120. final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFile))
  121. Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
  122. @Override
  123. FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
  124. try {
  125. Path targetFile = sourceDir.relativize(file)
  126. outputStream.putNextEntry(new ZipEntry(targetFile.toString()))
  127. byte[] bytes = Files.readAllBytes(file)
  128. outputStream.write(bytes, 0, bytes.length)
  129. outputStream.closeEntry()
  130. } catch (IOException e) {
  131. e.printStackTrace()
  132. }
  133. return FileVisitResult.CONTINUE
  134. }
  135. })
  136. outputStream.close()
  137. } catch (IOException e) {
  138. e.printStackTrace()
  139. }
  140. }
  141. static def unzip(File zipFile, File destDir) {
  142. if (!destDir.exists())
  143. destDir.mkdirs()
  144. FileInputStream fis
  145. byte[] buffer = new byte[1024]
  146. try {
  147. fis = new FileInputStream(zipFile)
  148. ZipInputStream zis = new ZipInputStream(fis)
  149. ZipEntry zipEntry = zis.getNextEntry()
  150. while (zipEntry != null) {
  151. if (!zipEntry.isDirectory()) {
  152. File newFile = new File(destDir, zipEntry.getName())
  153. new File(newFile.getParent()).mkdirs()
  154. FileOutputStream fos = new FileOutputStream(newFile)
  155. int len
  156. while ((len = zis.read(buffer)) > 0) {
  157. fos.write(buffer, 0, len)
  158. }
  159. fos.close()
  160. }
  161. zis.closeEntry()
  162. zipEntry = zis.getNextEntry()
  163. }
  164. zis.closeEntry()
  165. zis.close()
  166. fis.close()
  167. } catch (IOException e) {
  168. e.printStackTrace()
  169. }
  170. }