loom {
    accessWidenerPath = file("src/main/resources/architectury.accessWidener")
}

dependencies {
    // We depend on fabric loader here to use the fabric @Environment annotations
    // Do NOT use other classes from fabric loader
    modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
}

architectury {
    common(rootProject.platforms.split(","))
}

/**
 * The following code to generate the access widener is based on the following pull request by Juuxel;
 * https://github.com/Juuxel/
 * https://github.com/FabricMC/fabric/pull/2044/
 * Copyright (c) 2016, 2017, 2018, 2019 FabricMC and Juuxel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.objectweb.asm.ClassReader
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.ClassNode

import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path

task generateAccessWidener {
    doLast {
        List<String> lines = ["", "##############################", "# This section is generated automatically with Gradle task generateAccessWidener!!!", "##############################", ""]
        Path inputJar = loom.namedMinecraftProvider.parentMinecraftProvider.mergedJar

        try (def fs = FileSystems.newFileSystem(URI.create("jar:${inputJar.toUri()}"), [create: false])) {
            generateItemConstructors(lines, fs)
            lines.add("")
            generateBlockConstructors(lines, fs)
            lines.add("")
            generateRenderTypeRelated(lines, fs)
        }

        file('.gradle/generated.accesswidener').text = String.join('\n', lines) + '\n'
    }
}

static def generateBlockConstructors(List<String> lines, FileSystem fs) {
    lines.add("# Constructors of non-abstract block classes")
    Files.list(fs.getPath("net/minecraft/world/level/block"))
            .filter { Files.isRegularFile(it) && it.toString().endsWith(".class") }
            .map { loadClass(it) }
            .sorted(Comparator.comparing { it.name })
            .filter { (it.access & Opcodes.ACC_ABSTRACT) == 0 }
            .forEach { node ->
                for (def method : node.methods) {
                    // Checklist for finding block constructors as of 1.18.2:
                    //  - class directly in net.minecraft.world.level.block (excluding subpackages)
                    //  - method name == <init> (by definition)
                    //  - contains an BlockBehaviour$Properties parameter
                    //  - only taking into account non-abstract classes and non-public constructors

                    // Block constructor...
                    if (method.name == "<init>" && Type.getArgumentTypes(method.desc).any { it.internalName == 'net/minecraft/world/level/block/state/BlockBehaviour$Properties' }) {
                        // ...and non-public
                        if ((method.access & Opcodes.ACC_PUBLIC) == 0) {
                            lines.add("transitive-accessible method $node.name <init> $method.desc")
                        }
                    }
                }
            }
}

static def generateItemConstructors(List<String> lines, FileSystem fs) {
    lines.add("# Constructors of non-abstract item classes")
    Files.list(fs.getPath("net/minecraft/world/item"))
            .filter { Files.isRegularFile(it) && it.toString().endsWith(".class") }
            .map { loadClass(it) }
            .sorted(Comparator.comparing { it.name })
            .filter { (it.access & Opcodes.ACC_ABSTRACT) == 0 }
            .forEach { node ->
                for (def method : node.methods) {
                    // Checklist for finding block constructors as of 1.18.2:
                    //  - class directly in net.minecraft.world.item (excluding subpackages)
                    //  - method name == <init> (by definition)
                    //  - contains an Item$Properties parameter
                    //  - only taking into account non-abstract classes and non-public constructors

                    // Item constructor...
                    if (method.name == "<init>" && Type.getArgumentTypes(method.desc).any { it.internalName == 'net/minecraft/world/item/Item$Properties' }) {
                        // ...and non-public
                        if ((method.access & Opcodes.ACC_PUBLIC) == 0) {
                            lines.add("transitive-accessible method $node.name <init> $method.desc")
                        }
                    }
                }
            }
}

static def generateRenderTypeRelated(List<String> lines, FileSystem fs) {
    lines.add("# RenderStateShard fields")
    def node = loadClass(fs.getPath("net/minecraft/client/renderer/RenderStateShard.class"))
    for (def field : node.fields) {
        if ((field.access & Opcodes.ACC_STATIC) != 0 && (field.access & Opcodes.ACC_FINAL) != 0) {
            if ((field.access & Opcodes.ACC_PUBLIC) == 0) {
                lines.add("transitive-accessible field $node.name $field.name $field.desc")
            }
        }
    }
    for (def innerClass : node.innerClasses) {
        if ((innerClass.access & Opcodes.ACC_PROTECTED) != 0) {
            lines.add("transitive-accessible class $innerClass.name")
        }
    }
}

static ClassNode loadClass(Path path) {
    def node = new ClassNode()

    try (def is = Files.newInputStream(path)) {
        new ClassReader(is).accept(node, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES)
    }

    return node
}

/**
 * End of access widener code.
 */

publishing {
    publications {
        mavenCommon(MavenPublication) {
            artifactId = rootProject.archivesBaseName
            from components.java
        }
    }

    repositories {
        if (System.getenv("MAVEN_PASS") != null) {
            maven {
                url = "https://deploy.shedaniel.me/"
                credentials {
                    username = "shedaniel"
                    password = System.getenv("MAVEN_PASS")
                }
            }
        }
    }
}
