Skip to content

Commit

Permalink
Start API split and refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Aug 3, 2024
1 parent 6445c90 commit 1214a70
Show file tree
Hide file tree
Showing 15 changed files with 359 additions and 256 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@

## 3.0.0

- Updated to Minecraft 1.21 ([#227](https://github.com/LambdAurora/LambDynamicLights/pull/227))
- Updated to Minecraft 1.21 ([#227](https://github.com/LambdAurora/LambDynamicLights/pull/227)).
- Updated configuration library.
- Configuration corruption should now be fixed.
- Updated Mexican Spanish translations ([#214](https://github.com/LambdAurora/LambDynamicLights/pull/214)).
- Updated Italian translations ([#232](https://github.com/LambdAurora/LambDynamicLights/pull/232)).
- Updated Polish translations ([#235](https://github.com/LambdAurora/LambDynamicLights/pull/235)).
- Switched license to [Lambda License](https://github.com/LambdAurora/LambDynamicLights/blob/bbefb8860bca2e797f8a2ba8a59d1120b6e1c7b4/LICENSE).

[SpruceUI]: https://github.com/LambdAurora/SpruceUI "SpruceUI page"
Expand Down
7 changes: 7 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import lambdynamiclights.Constants

plugins {
id("lambdynamiclights")
}

base.archivesName.set(Constants.NAME + "-api")
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright © 2020 LambdAurora <[email protected]>
*
* This file is part of LambDynamicLights.
*
* Licensed under the Lambda License. For more information,
* see the LICENSE file.
*/

package dev.lambdaurora.lambdynlights.api;

/**
* Represents the entrypoint for LambDynamicLights API.
*
* @author LambdAurora
* @version 3.0.0
* @since 1.3.2
*/
public interface DynamicLightsInitializer {
/**
* Called when LambDynamicLights is initialized to register custom dynamic light handlers and item light sources.
*/
void onInitializeDynamicLights();
}
233 changes: 14 additions & 219 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,102 +1,23 @@
import com.modrinth.minotaur.dependencies.ModDependency
import lambdynamiclights.Constants
import lambdynamiclights.Utils
import net.darkhax.curseforgegradle.TaskPublishCurseForge
import net.fabricmc.loom.api.mappings.layered.MappingContext
import net.fabricmc.loom.api.mappings.layered.MappingLayer
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace
import net.fabricmc.loom.api.mappings.layered.spec.MappingsSpec
import net.fabricmc.loom.configuration.providers.mappings.intermediary.IntermediaryMappingLayer
import net.fabricmc.loom.configuration.providers.mappings.utils.DstNameFilterMappingVisitor
import net.fabricmc.loom.util.download.DownloadException
import net.fabricmc.mappingio.MappingVisitor
import net.fabricmc.mappingio.adapter.MappingDstNsReorder
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch
import net.fabricmc.mappingio.format.proguard.ProGuardFileReader
import net.fabricmc.mappingio.tree.MemoryMappingTree
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.util.regex.Pattern

plugins {
id("fabric-loom").version("1.7.+")
id("dev.yumi.gradle.licenser").version("1.1.+")
`java-library`
id("lambdynamiclights")
`maven-publish`
id("com.github.johnrengelman.shadow").version("8.1.1")
id("com.modrinth.minotaur").version("2.+")
id("net.darkhax.curseforgegradle").version("1.1.+")
}

group = project.property("maven_group") as String
base.archivesName.set(project.property("archives_base_name") as String)
val minecraftVersion = libs.versions.minecraft.get()
val modVersion = project.property("mod_version") as String
version = "${modVersion}+${minecraftVersion}"

// This field defines the Java version your mod target.
val targetJavaVersion = 21
base.archivesName.set(Constants.NAME)

if (!(System.getenv("CURSEFORGE_TOKEN") != null || System.getenv("MODRINTH_TOKEN") != null || System.getenv("LDL_MAVEN") != null)) {
version = (version as String) + "-local"
}
logger.lifecycle("Preparing version ${version}...")

fun isMCVersionNonRelease(): Boolean {
return minecraftVersion.matches(Regex("^\\d\\dw\\d\\d[a-z]$"))
|| minecraftVersion.matches(Regex("\\d+\\.\\d+-(pre|rc)(\\d+)"))
}

fun getMCVersionString(): String {
if (isMCVersionNonRelease()) {
return minecraftVersion
}
val version = minecraftVersion.split("\\.".toRegex())
return version[0] + "." + version[1]
}

fun getVersionType(): String {
return if (isMCVersionNonRelease() || "-alpha." in modVersion) {
"alpha"
} else if ("-beta." in modVersion) {
"beta"
} else {
"release"
}
}

fun parseReadme(): String {
val excludeRegex = "(?m)<!-- modrinth_exclude\\.start -->(.|\n)*?<!-- modrinth_exclude\\.end -->"
val linkRegex = "!\\[([A-z_ ]+)]\\((images\\/[A-z.\\/_]+)\\)"

var readme = file("README.md").readText()
readme = readme.replace(excludeRegex.toRegex(), "")
readme = readme.replace(linkRegex.toRegex(), "![\$1](https://raw.githubusercontent.com/LambdAurora/LambDynamicLights/1.19/\$2)")
return readme
}

fun fetchChangelog(): String? {
val changelogText = file("CHANGELOG.md").readText()
val regexVersion = modVersion.replace("\\.".toRegex(), "\\.").replace("\\+".toRegex(), "\\+")
val changelogRegex = "###? ${regexVersion}\\n\\n(( *- .+\\n)+)".toRegex()
val matcher = changelogRegex.find(changelogText)

if (matcher != null) {
var changelogContent = matcher.groupValues[1]

val changelogLines = changelogText.split("\n")
val linkRefRegex = "^\\[([A-z\\d _\\-/+.]+)]: ".toRegex()
for (i in changelogLines.size - 1 downTo 0) {
val line = changelogLines[i]
if (line matches linkRefRegex)
changelogContent += "\n" + line
else break
}
return changelogContent
} else {
return null;
}
}

configurations["api"].extendsFrom(configurations["shadow"])

repositories {
Expand Down Expand Up @@ -133,113 +54,10 @@ repositories {

loom {
accessWidenerPath = file("src/main/resources/lambdynlights.accesswidener")
runtimeOnlyLog4j = true
}

// Based off Loom, this is required as the releases at the time of writing this buildscript have
// a flaw with the mapping layering preventing the usage of the usual MojangMappingLayer.
@Suppress("UnstableApiUsage")
internal data class MojangMappingLayer(
val clientMappings: Path, val serverMappings: Path, val nameSyntheticMembers: Boolean,
val intermediaryMappings: MemoryMappingTree, val logger: Logger
) : MappingLayer {
@Throws(IOException::class)
override fun visit(mappingVisitor: MappingVisitor) {
val mojmap = MemoryMappingTree()

// Filter out field names matching the pattern
val nameFilter = DstNameFilterMappingVisitor(mojmap, SYNTHETIC_NAME_PATTERN)

// Make official the source namespace
val nsSwitch = MappingSourceNsSwitch(if (nameSyntheticMembers) mojmap else nameFilter, MappingsNamespace.OFFICIAL.toString())

Files.newBufferedReader(clientMappings).use { clientBufferedReader ->
Files.newBufferedReader(serverMappings).use { serverBufferedReader ->
ProGuardFileReader.read(
clientBufferedReader,
MappingsNamespace.NAMED.toString(),
MappingsNamespace.OFFICIAL.toString(),
nsSwitch
)
ProGuardFileReader.read(
serverBufferedReader,
MappingsNamespace.NAMED.toString(),
MappingsNamespace.OFFICIAL.toString(),
nsSwitch
)
}
}

intermediaryMappings.accept(MappingDstNsReorder(mojmap, MappingsNamespace.INTERMEDIARY.toString()))

val switch = MappingSourceNsSwitch(MappingDstNsReorder(mappingVisitor, MappingsNamespace.NAMED.toString()), MappingsNamespace.INTERMEDIARY.toString(), true)
mojmap.accept(switch)
}

override fun getSourceNamespace(): MappingsNamespace {
return MappingsNamespace.INTERMEDIARY
}

override fun dependsOn(): List<Class<out MappingLayer?>> {
return listOf(IntermediaryMappingLayer::class.java)
}

companion object {
private val SYNTHETIC_NAME_PATTERN: Pattern = Pattern.compile("^(access|this|val\\\$this|lambda\\$.*)\\$[0-9]+$")
}
}

@Suppress("UnstableApiUsage")
internal data class MojangMappingsSpec(val nameSyntheticMembers: Boolean) : MappingsSpec<MojangMappingLayer?> {
override fun createLayer(context: MappingContext): MojangMappingLayer {
val versionInfo = context.minecraftProvider().versionInfo
val clientDownload = versionInfo.download(MANIFEST_CLIENT_MAPPINGS)
val serverDownload = versionInfo.download(MANIFEST_SERVER_MAPPINGS)

if (clientDownload == null) {
throw RuntimeException("Failed to find official mojang mappings for " + context.minecraftVersion())
}

val clientMappings = context.workingDirectory("mojang").resolve("client.txt")
val serverMappings = context.workingDirectory("mojang").resolve("server.txt")

try {
context.download(clientDownload.url())
.sha1(clientDownload.sha1())
.downloadPath(clientMappings)

context.download(serverDownload.url())
.sha1(serverDownload.sha1())
.downloadPath(serverMappings)
} catch (e: DownloadException) {
throw UncheckedIOException("Failed to download mappings", e)
}

return MojangMappingLayer(
clientMappings,
serverMappings,
nameSyntheticMembers,
context.intermediaryTree().get(),
context.logger
)
}

companion object {
// Keys in dependency manifest
private const val MANIFEST_CLIENT_MAPPINGS = "client_mappings"
private const val MANIFEST_SERVER_MAPPINGS = "server_mappings"
}
}

dependencies {
minecraft(libs.minecraft)
@Suppress("UnstableApiUsage")
mappings(loom.layered {
addLayer(MojangMappingsSpec(false))
parchment("org.parchmentmc.data:parchment-$minecraftVersion:${libs.versions.mappings.parchment.get()}@zip")
mappings("dev.lambdaurora:yalmm:$minecraftVersion+build.${libs.versions.mappings.yalmm.get()}")
})
modImplementation(libs.fabric.loader)
shadow(project(":api"))

modImplementation(libs.fabric.api)

Expand All @@ -258,19 +76,6 @@ dependencies {
shadow(libs.nightconfig.toml)
}

java {
sourceCompatibility = JavaVersion.toVersion(targetJavaVersion)
targetCompatibility = JavaVersion.toVersion(targetJavaVersion)

withSourcesJar()
}

tasks.withType<JavaCompile>().configureEach {
options.encoding = "UTF-8"

options.release.set(targetJavaVersion)
}

tasks.processResources {
inputs.property("version", project.version)

Expand All @@ -279,16 +84,6 @@ tasks.processResources {
}
}

tasks.jar {
from("LICENSE") {
rename { "${it}_${base.archivesName.get()}" }
}
}

license {
rule(file("HEADER"))
}

tasks.shadowJar {
dependsOn(tasks.jar)
configurations = listOf(project.configurations["shadow"])
Expand All @@ -304,20 +99,20 @@ tasks.remapJar {

modrinth {
projectId = project.property("modrinth_id") as String
versionName = "LambDynamicLights $modVersion (${minecraftVersion})"
versionName = "LambDynamicLights ${Constants.VERSION} (${Constants.mcVersion()})"
uploadFile.set(tasks.remapJar.get())
loaders.set(listOf("fabric", "quilt"))
gameVersions.set(listOf(minecraftVersion))
versionType.set(if (isMCVersionNonRelease()) "beta" else "release")
syncBodyFrom.set(parseReadme())
gameVersions.set(listOf(Constants.mcVersion()))
versionType.set(Constants.getVersionType())
syncBodyFrom.set(Utils.parseReadme(project))
dependencies.set(
listOf(
ModDependency("P7dR8mSH", "required")
)
)

// Changelog fetching
val changelogContent = fetchChangelog()
val changelogContent = Utils.fetchChangelog(project)

if (changelogContent != null) {
changelog.set(changelogContent)
Expand All @@ -344,7 +139,7 @@ tasks.register("curseforge", TaskPublishCurseForge::class) {
}

// Changelog fetching
var changelogContent = fetchChangelog()
var changelogContent = Utils.fetchChangelog(project)

if (changelogContent != null) {
changelogContent = "Changelog:\n\n${changelogContent}"
Expand All @@ -354,13 +149,13 @@ tasks.register("curseforge", TaskPublishCurseForge::class) {
}

val mainFile = upload(project.property("curseforge_id"), tasks.remapJar.get())
mainFile.releaseType = getVersionType()
mainFile.addGameVersion(minecraftVersion)
mainFile.releaseType = Constants.getVersionType()
mainFile.addGameVersion(Constants.mcVersion())
mainFile.addModLoader("Fabric", "Quilt")
mainFile.addJavaVersion("Java 21", "Java 22")
mainFile.addEnvironment("Client")

mainFile.displayName = "LambDynamicLights $modVersion ($minecraftVersion)"
mainFile.displayName = "LambDynamicLights ${Constants.VERSION} (${Constants.mcVersion()})"
mainFile.addRequirement("fabric-api")
mainFile.addOptional("modmenu")
mainFile.addIncompatibility("optifabric")
Expand Down
37 changes: 37 additions & 0 deletions build_logic/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`java-gradle-plugin`
`kotlin-dsl`
}

val javaVersion = 21

repositories {
gradlePluginPortal()
maven {
name = "Fabric"
url = uri("https://maven.fabricmc.net/")
}
}

dependencies {
implementation(libs.gradle.licenser)
implementation(libs.gradle.loom)
implementation(libs.mappingio)

// A bit of a hack you definitely should not worry about.
// https://github.com/gradle/gradle/issues/15383#issuecomment-779893192
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

java {
sourceCompatibility = JavaVersion.toVersion(javaVersion)
targetCompatibility = JavaVersion.toVersion(javaVersion)
}

kotlin {
compilerOptions {
jvmTarget = JvmTarget.fromTarget(javaVersion.toString())
}
}
Loading

0 comments on commit 1214a70

Please sign in to comment.