summaryrefslogtreecommitdiff
path: root/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt
blob: 8c009e08af4120bb0f56cb0a05d7cfaf4c7a986b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package moe.nea.archenemy.mojang

import moe.nea.archenemy.MCSide
import net.minecraftforge.artifactural.gradle.GradleRepositoryAdapter
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ModuleDependency
import java.io.File
import java.net.URI

abstract class ArchenemyMojangExtension(val project: Project) {
	val sharedExtension = project.rootProject.extensions.getByType(ArchenemySharedExtension::class.java)

	private val _registerMinecraftProvider by lazy {
		GradleRepositoryAdapter.add(
			project.repositories,
			"Minecraft Provider",
			sharedExtension.getLocalCacheDirectory().resolve("minecraft-provider"),
			sharedExtension.minecraftProvider
		)
		GradleRepositoryAdapter.add(
			project.repositories,
			"Minecraft Mapped Provider",
			getLocalCacheDirectory().resolve("minecraft-mapped-provider"),
			mappedRepositoryProvider
		)
		GradleRepositoryAdapter.add(
			project.repositories,
			"Minecraft Merged Provider",
			getLocalCacheDirectory().resolve("minecraft-merged-provider"),
			mergedRepositoryProvider
		)
		project.repositories.maven {
			it.name = "Minecraft Libraries"
			it.url = URI("https://libraries.minecraft.net/")
		}
	}

	private val mappedRepositoryProvider = MappedRepositoryProvider(this)
	private val mergedRepositoryProvider = MergedRepositoryProvider(this)

	fun yarnMappings(dependency: Dependency): MappingDependency {
		dependency as ModuleDependency
		return YarnMappingDependency(this, dependency)
	}

	fun officialMappings(version: String, side: MCSide): MappingDependency {
		_registerMinecraftProvider
		val dependency by lazy {
			realize(
				sharedExtension.minecraftProvider.getMappingsDependencyCoordinate(
					MinecraftProvider.MinecraftCoordinate(
						version,
						side
					)
				)
			)
		}
		return OfficialMappingDependency(side, version, project.providers.provider { dependency }).also { it.get() }
	}

	fun natives(version: String): ExtractNativesTask {
		val taskName = "extractNatives$version"
		var extractNativesTask = project.tasks.findByName(taskName)
		if (extractNativesTask != null) return extractNativesTask as ExtractNativesTask

		extractNativesTask = project.tasks.create(taskName, ExtractNativesTask::class.java) {
			it.natives.set(sharedExtension.getDownloadVersionMetadataTask(version)
				               .getVersionMetadataNow().getFilteredLibraries())
			it.nativeDirectory.set(getLocalCacheDirectory().resolve("extractedNatives/$version"))
		}
		return extractNativesTask
	}

	fun libraries(version: String): List<String> {
		return sharedExtension.getDownloadVersionMetadataTask(version)
			.getVersionMetadataNow().getFilteredLibraries()
			.map {
				it.getArtifactCoordinate()
			}
	}

	fun intermediaryMappings(version: String): MappingDependency {
		return yarnMappings(project.dependencies.create("net.fabricmc:intermediary:$version:v2"))
	}

	fun mergeJar(
		base: Dependency,
		overlay: Dependency,
	): Dependency {
		base as ModuleDependency
		overlay as ModuleDependency
		_registerMinecraftProvider
		return realize(
			mergedRepositoryProvider.getCoordinate(
				MergedRepositoryProvider.Coordinate(base, overlay)
			)
		)
	}

	fun mapJar(
		dependency: Dependency,
		mappings: MappingDependency,
		sourceNamespace: String,
		destinationNamespace: String
	): Dependency {
		dependency as ModuleDependency
		_registerMinecraftProvider
		return realize(
			mappedRepositoryProvider.getDependencyCoordiante(
				MappedRepositoryProvider.MappedCoordinates(
					dependency, mappings, sourceNamespace, destinationNamespace
				)
			)
		)
	}

	fun minecraft(version: String, side: MCSide): Dependency {
		_registerMinecraftProvider
		return realize(
			sharedExtension.minecraftProvider.getDependencyCoordinate(
				MinecraftProvider.MinecraftCoordinate(
					version,
					side,
				)
			)
		)
	}

	fun realize(dependency: String): Dependency {
		return realize(project.dependencies.create(dependency))
	}

	fun realize(dependency: Dependency): Dependency {
		project.configurations.detachedConfiguration(dependency).resolve()
		return dependency
	}

	fun getLocalCacheDirectory(): File {
		return sharedExtension.getLocalCacheDirectory().resolve("projectspecific")
			.resolve(if (project == project.rootProject) "__root" else project.path.replace(":", "_"))
	}

}