aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-12-30 21:48:56 +0100
committerLinnea Gräf <nea@nea.moe>2024-12-30 21:48:56 +0100
commit1c0090ec301b87edebc29453287e585f88de2c4f (patch)
tree588b3e3269da774f924308c7b51503bf5a8b3ff1
parent9122f9c85e96d988eeebe760af19edc50222f909 (diff)
downloadLocalTransactionLedger-1c0090ec301b87edebc29453287e585f88de2c4f.tar.gz
LocalTransactionLedger-1c0090ec301b87edebc29453287e585f88de2c4f.tar.bz2
LocalTransactionLedger-1c0090ec301b87edebc29453287e585f88de2c4f.zip
feat: Add auto generated ItemIds class
-rw-r--r--build.gradle.kts158
-rw-r--r--ledger-rules.pro3
-rw-r--r--src/main/kotlin/moe/nea/ledger/ItemId.kt7
-rw-r--r--src/main/kotlin/moe/nea/ledger/modules/GambleDetection.kt3
4 files changed, 159 insertions, 12 deletions
diff --git a/build.gradle.kts b/build.gradle.kts
index dad4b3e..269f000 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,6 +1,20 @@
import com.github.gmazzo.buildconfig.BuildConfigExtension
+import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
+import com.google.gson.Gson
import org.apache.commons.lang3.SystemUtils
+import proguard.gradle.ProGuardTask
import java.io.ByteArrayOutputStream
+import java.net.URI
+import java.util.zip.ZipInputStream
+
+buildscript {
+ repositories {
+ mavenCentral()
+ }
+ dependencies {
+ classpath("com.guardsquare:proguard-gradle:7.6.1")
+ }
+}
plugins {
idea
@@ -108,6 +122,11 @@ dependencies {
// Tasks:
+// Delete default shadow configuration
+tasks.shadowJar {
+ doFirst { error("Incorrect shadow JAR built!") }
+}
+
tasks.test {
useJUnitPlatform()
}
@@ -116,6 +135,110 @@ tasks.withType(JavaCompile::class) {
options.encoding = "UTF-8"
}
+abstract class GenerateItemIds : DefaultTask() {
+ @get: OutputDirectory
+ abstract val outputDirectory: DirectoryProperty
+
+ @get: InputDirectory
+ abstract val repoFiles: DirectoryProperty
+
+ @get: Input
+ abstract val repoHash: Property<String>
+
+ @get: Input
+ abstract val packageName: Property<String>
+
+ @get:Internal
+ val outputFile get() = outputDirectory.asFile.get().resolve(packageName.get().replace(".", "/") + "/ItemIds.java")
+
+ @TaskAction
+ fun generateItemIds() {
+ val nonIdName = "[^A-Z0-9_]".toRegex()
+
+ data class Item(val id: String) {
+ val javaName get() = id.replace(nonIdName, { "__" + it.value.single().code })
+ }
+
+ val items = mutableListOf<Item>()
+ for (listFile in repoFiles.asFile.get().resolve("items").listFiles() ?: emptyArray()) {
+ listFile ?: continue
+ if (listFile.extension != "json") {
+ error("Unknown file $listFile")
+ }
+ items.add(Item(listFile.nameWithoutExtension))
+ }
+ items.sortedBy { it.id }
+ outputFile.parentFile.mkdirs()
+ val writer = outputFile.writer().buffered()
+ writer.appendLine("// @generated from " + repoHash.get())
+ writer.appendLine("package " + packageName.get() + ";")
+ writer.appendLine()
+ writer.appendLine("import moe.nea.ledger.ItemId;")
+ writer.appendLine()
+ writer.appendLine("/**")
+ writer.appendLine(" * Automatically generated {@link ItemId} list.")
+ writer.appendLine(" */")
+ writer.appendLine("public class ItemIds {")
+ val gson = Gson()
+ for (item in items) {
+ writer.appendLine("\t/**")
+ writer.appendLine("\t * @see <a href=${gson.toJson("https://github.com/NotEnoughUpdates/NotEnoughUpdates-REPO/blob/${repoHash.get()}/items/${item.id}.json")}>JSON definition</a>")
+ writer.appendLine("\t */")
+ writer.appendLine("\tpublic static final ItemId ${item.javaName} =" +
+ " ItemId.forName(${gson.toJson(item.id)});")
+ }
+ writer.appendLine("}")
+ writer.close()
+ }
+}
+
+abstract class RepoDownload : DefaultTask() {
+ @get:Input
+ abstract val hash: Property<String>
+
+ @get:OutputDirectory
+ abstract val outputDirectory: DirectoryProperty
+
+ init {
+ outputDirectory.convention(project.layout.buildDirectory.dir("extracted-test-repo"))
+ }
+
+ @TaskAction
+ fun performDownload() {
+ val outputDir = outputDirectory.asFile.get().absoluteFile
+ outputDir.mkdirs()
+ URI("https://github.com/notEnoughUpdates/notEnoughUpdates-rEPO/archive/${hash.get()}.zip").toURL().openStream()
+ .let(::ZipInputStream)
+ .use { zipInput ->
+ while (true) {
+ val entry = zipInput.nextEntry ?: break
+ val destination = outputDir.resolve(
+ entry.name.substringAfter('/')).absoluteFile
+ require(outputDir in generateSequence(destination) { it.parentFile })
+ if (entry.isDirectory) continue
+ destination.parentFile.mkdirs()
+ destination.outputStream().use { output ->
+ zipInput.copyTo(output)
+ }
+ }
+ }
+ }
+}
+
+val downloadRepo by tasks.register("downloadRepo", RepoDownload::class) {
+ hash.set("725ddb8")
+}
+
+val generateItemIds by tasks.register("generateItemIds", GenerateItemIds::class) {
+ repoHash.set(downloadRepo.hash)
+ packageName.set("moe.nea.ledger.gen")
+ outputDirectory.set(layout.buildDirectory.dir("generated/sources/itemIds"))
+ repoFiles.set(downloadRepo.outputDirectory)
+}
+sourceSets.main {
+ java.srcDir(generateItemIds)
+}
+
tasks.withType(Jar::class) {
archiveBaseName.set(modid)
manifest.attributes.run {
@@ -142,10 +265,34 @@ tasks.processResources {
}
+val proguardOutJar = project.layout.buildDirectory.file("badjars/stripped.jar")
+val proguard = tasks.register("proguard", ProGuardTask::class) {
+ dependsOn(tasks.jar)
+ injars(tasks.jar.map { it.archiveFile })
+ outjars(proguardOutJar)
+ configuration(file("ledger-rules.pro"))
+ verbose()
+ val libJava = javaToolchains.launcherFor(java.toolchain)
+ .get()
+ .metadata.installationPath.file("jre/lib/rt.jar")
+ println(libJava)
+ libraryjars(libJava)
+ libraryjars(configurations.compileClasspath)
+}
+
+val shadowJar2 = tasks.register("shadowJar2", ShadowJar::class) {
+ destinationDirectory.set(layout.buildDirectory.dir("badjars"))
+ archiveClassifier.set("all-dev")
+ from(proguardOutJar)
+ dependsOn(proguard)
+ configurations = listOf(shadowImpl)
+ relocate("moe.nea.libautoupdate", "moe.nea.ledger.deps.libautoupdate")
+ mergeServiceFiles()
+}
val remapJar by tasks.named<net.fabricmc.loom.task.RemapJarTask>("remapJar") {
archiveClassifier.set("")
- from(tasks.shadowJar)
- input.set(tasks.shadowJar.get().archiveFile)
+ from(shadowJar2)
+ input.set(shadowJar2.get().archiveFile)
}
tasks.jar {
@@ -153,13 +300,6 @@ tasks.jar {
destinationDirectory.set(layout.buildDirectory.dir("badjars"))
}
-tasks.shadowJar {
- destinationDirectory.set(layout.buildDirectory.dir("badjars"))
- archiveClassifier.set("all-dev")
- configurations = listOf(shadowImpl)
- relocate("moe.nea.libautoupdate", "moe.nea.ledger.deps.libautoupdate")
- mergeServiceFiles()
-}
tasks.assemble.get().dependsOn(tasks.remapJar)
diff --git a/ledger-rules.pro b/ledger-rules.pro
new file mode 100644
index 0000000..2d8459e
--- /dev/null
+++ b/ledger-rules.pro
@@ -0,0 +1,3 @@
+-keep class !moe.nea.ledger.gen.** {*;}
+-dontobfuscate
+-assumenosideeffects class moe.nea.ledger.ItemId { *; } \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea/ledger/ItemId.kt b/src/main/kotlin/moe/nea/ledger/ItemId.kt
index 6be23fb..f4786cd 100644
--- a/src/main/kotlin/moe/nea/ledger/ItemId.kt
+++ b/src/main/kotlin/moe/nea/ledger/ItemId.kt
@@ -1,7 +1,6 @@
package moe.nea.ledger
-@JvmInline
-value class ItemId(
+data class ItemId(
val string: String
) {
fun singleItem(): Pair<ItemId, Double> {
@@ -14,11 +13,15 @@ value class ItemId(
companion object {
+
+ @JvmStatic
+ fun forName(string: String) = ItemId(string)
fun skill(skill: String) = ItemId("SKYBLOCK_SKILL_$skill")
val GARDEN = skill("GARDEN")
val FARMING = skill("FARMING")
+
val ARCHFIEND_DYE = ItemId("DYE_ARCHFIEND")
val ARCHFIEND_HIGH_CLASS = ItemId("HIGH_CLASS_ARCHFIEND_DICE")
val ARCHFIEND_LOW_CLASS = ItemId("ARCHFIEND_DICE")
diff --git a/src/main/kotlin/moe/nea/ledger/modules/GambleDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/GambleDetection.kt
index 6a339d7..0ef43a2 100644
--- a/src/main/kotlin/moe/nea/ledger/modules/GambleDetection.kt
+++ b/src/main/kotlin/moe/nea/ledger/modules/GambleDetection.kt
@@ -6,6 +6,7 @@ import moe.nea.ledger.LedgerEntry
import moe.nea.ledger.LedgerLogger
import moe.nea.ledger.TransactionType
import moe.nea.ledger.events.ChatReceived
+import moe.nea.ledger.gen.ItemIds
import moe.nea.ledger.useMatcher
import moe.nea.ledger.utils.di.Inject
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
@@ -33,7 +34,7 @@ class GambleDetection {
listOf(
ItemChange.lose(item, 1),
ItemChange.loseCoins(rollCost),
- ItemChange.gain(ItemId.ARCHFIEND_DYE, 1),
+ ItemChange.gain(ItemIds.DYE_ARCHFIEND, 1),
)
))
} else if (face == "6") {