plugins {
    id "idea"
    id "signing"
    id "java"
    id "com.gradleup.shadow" version "8.3.6"
    id "dev.architectury.architectury-pack200"
    id "gg.essential.loom"
}

version = "4.1.5"
group = "kr.syeyoung.dungeonsguide"
archivesBaseName = "dungeonsguide-loader"


java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(8))
}


def urlFile = { url, name ->
    File file = new File("$buildDir/download/${name}")
    file.parentFile.mkdirs()
    if (!file.exists()) {
        new URL(url).withInputStream { downloadStream ->
            file.withOutputStream { fileOut ->
                fileOut << downloadStream
            }
        }
    }
    return file.absolutePath
}

def platform = net.fabricmc.loom.util.CurrentPlatform.INSTANCE

loom {
    // I use osx btw. I need that sweet performance from using arm64 native libs!!!
    if (platform.getArchitecture().isArm()) {
        loom.customMinecraftManifest = "https://gist.githubusercontent.com/cyoung06/76a8208bd4575c17bdb138a0771c0886/raw/beb9ef6cc3c26abf5014d810133d21bedbffa9bb/1.8.9.json"
    }
    runs {
        client {
            ideConfigGenerated = true
            name = "Launch Loader Only"
            runDir = '../runtime'
            property('devauth.enabled','true')
            property("fml.coreMods.load", "kr.syeyoung.dungeonsguide.launcher.coremod.DGTweaker")
            client()
//            vmArgs.remove("-XstartOnFirstThread")
        }
    }
    forge {
        pack200Provider.set(new dev.architectury.pack200.java.Pack200Adapter())
    }
}

//
//sourceSets.main {
//    output.setResourcesDir(file("$buildDir/classes/java/main"))
//}


repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
    // dev auth
    maven {url "https://pkgs.dev.azure.com/djtheredstoner/DevAuth/_packaging/public/maven/v1"}
}

configurations {
    implementation.extendsFrom shadowImpl
    api.extendsFrom shadowApi

    unmappedMod {
        canBeConsumed = true
        canBeResolved = false
    }
    remappedMod {
        canBeConsumed = true
        canBeResolved = false
    }
}
dependencies {
    minecraft("com.mojang:minecraft:1.8.9")
    mappings("de.oceanlabs.mcp:mcp_stable:22-1.8.9")
    forge("net.minecraftforge:forge:1.8.9-11.15.1.2318-1.8.9")

    def os = platform.getOperatingSystem();
    if (platform.getArchitecture().isArm()) {
        if (os.isLinux() && platform.getArchitecture().is64Bit()) {
            minecraftNatives(files(urlFile("https://github.com/theofficialgman/lwjgl3-binaries-arm64/raw/lwjgl-2.9.4/lwjgl-platform-2.9.4-nightly-20150209-natives-linux.jar", "lwjgl-platform-2.9.4-nightly-20150209-natives-linux-arm64.jar"),
                    urlFile("https://github.com/theofficialgman/lwjgl3-binaries-arm64/raw/lwjgl-2.9.4/jinput-platform-2.0.5-natives-linux.jar", "jinput-platform-2.0.5-natives-linux-arm64.jar")))
        } else if (os.isLinux() && !platform.getArchitecture().is64Bit()) {
            minecraftNatives(files(urlFile("https://github.com/theofficialgman/lwjgl3-binaries-arm32/raw/lwjgl-2.9.4/lwjgl-platform-2.9.4-nightly-20150209-natives-linux.jar", "lwjgl-platform-2.9.4-nightly-20150209-natives-linux-arm32.jar"),
                    urlFile("https://github.com/theofficialgman/lwjgl3-binaries-arm32/raw/lwjgl-2.9.4/jinput-platform-2.0.5-natives-linux.jar", "jinput-platform-2.0.5-natives-linux-arm32.jar")))
        } else if (os.isMacOS()) {
            minecraftNatives(files(urlFile("https://github.com/Dungeons-Guide/lwjgl/releases/download/2.9.4-20150209-mmachina.2-syeyoung.1/lwjgl-platform-2.9.4-nightly-20150209-natives-osx-arm64.jar", "lwjgl-platform-2.9.4-nightly-20150209-natives-osx-arm64.jar"),
                    urlFile("https://github.com/r58Playz/jinput-m1/raw/main/plugins/OSX/bin/jinput-platform-2.0.5.jar", "jinput-platform-2.0.5-natives-osx-arm64.jar")))
        }
    }


    implementation "org.jetbrains:annotations-java5:19.0.0"
    shadowApi "org.json:json:20240303"
    shadowImpl 'io.nayuki:qrcodegen:1.4.0'
    shadowImpl 'org.bouncycastle:bcpg-jdk15on:1.70'
    shadowImpl "com.twelvemonkeys.imageio:imageio-bmp:3.7.0"

    compileOnly "org.projectlombok:lombok:1.18.20"
    annotationProcessor "org.projectlombok:lombok:1.18.16"

    testCompileOnly "org.projectlombok:lombok:1.18.20"
    testAnnotationProcessor "org.projectlombok:lombok:1.18.20"


    modLocalRuntime("me.djtheredstoner:DevAuth-forge-legacy:1.1.0") {}
}


tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

tasks.withType(Jar) {
    manifest {
        attributes["FMLCorePluginContainsFMLMod"] = "true"
        attributes["FMLCorePlugin"] = 'kr.syeyoung.dungeonsguide.launcher.coremod.DGTweaker'
    }
}

tasks.shadowJar {
    archiveFileName = jar.archiveFileName
    exclude "META-INF/versions/**"
    configurations = [project.configurations.shadowImpl, project.configurations.shadowApi]

    enableRelocation = true
    relocationPrefix = "kr.syeyoung.dungeonsguide.libs"
}


tasks.named("remapJar") {
    archiveClassifier = "mapped"
    dependsOn tasks.shadowJar
    inputFile = tasks.shadowJar.archiveFile
}


signing {
    required(project.findProperty("dg.dosign") == "true")
    useGpgCmd()
    sign tasks.remapJar, tasks.shadowJar
}

artifacts {
    remappedMod(tasks.remapJar)
    unmappedMod(tasks.shadowJar)
}