Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra maps #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ modsDotGroovy {
platforms 'forge', 'quilt'
}

tasks.configureEach {
if (it.name == 'modsDotGroovyToToml') {
it.mixinConfigs.put('mymod.mixins.json', 'mymod.refmap.json')
}
}

afterEvaluate {
println(sourceSets.main.ext.refMapFile)
}

repositories {
mavenLocal()
}
19 changes: 17 additions & 2 deletions Test/src/main/resources/mods.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ModsDotGroovy.make {
version = '1.190'

contributors = [
Owner: 'GroovyMC',
Owner: ['GroovyMC'],
Author: ['Matyrobbrt', 'Paint_Ninja', 'lukebemish']
]

Expand Down Expand Up @@ -69,6 +69,21 @@ ModsDotGroovy.make {
}

onQuilt {
mixin = "no.mixin.json"
mixin = 'mymod.mixins.json'
}

packMcMeta {
packFormat = 6
forgeDataPackFormat = 4
description = 'The resources of a lovely mod'
}

mixinConfig {
compatibilityLevel = 17
packageName = 'com.matyrobbrt.test.mixins'
mixins {
common 'ServerPlayerMixin'
client 'client.MinecraftMixin'
}
}
}
59 changes: 56 additions & 3 deletions src/lib/groovy/ModsDotGroovy.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import groovy.transform.stc.ClosureParams
import groovy.transform.stc.SimpleType
import groovyjarjarantlr4.v4.runtime.misc.Nullable
import modsdotgroovy.ImmutableModInfo
import modsdotgroovy.MixinConfigBuilder
import modsdotgroovy.ModInfoBuilder
import modsdotgroovy.ModsBuilder
import modsdotgroovy.PackMcMetaBuilder
import modsdotgroovy.VersionRange

import java.net.http.HttpClient
Expand All @@ -29,18 +31,26 @@ class ModsDotGroovy {
protected Map data

protected ModsDotGroovy() {
this.data = switch (platform) {
case Platform.QUILT -> ["schema_version": 1, "quilt_loader": [:]]
case Platform.FORGE -> [:]
if (platform === Platform.QUILT) {
data = ['schema_version': 1, 'quilt_loader': [:]]
} else {
data = [:]
}
}

protected static Platform platform
protected static final Map<String, String> mixinRefMaps = [:]

protected static void setPlatform(String name) {
platform = Platform.valueOf(name.toUpperCase(Locale.ROOT))
}

protected static void setMixinRefMap(String configName, String refMap) {
if (!refMap.isBlank()) {
mixinRefMaps[configName] = refMap
}
}

void propertyMissing(String name, Object value) {
put(name, value)
}
Expand Down Expand Up @@ -242,6 +252,45 @@ class ModsDotGroovy {
}
}

void packMcMeta(@DelegatesTo(value = PackMcMetaBuilder, strategy = DELEGATE_FIRST)
@ClosureParams(value = SimpleType, options = 'modsdotgroovy.PackMcMetaBuilder') final Closure closure) {
final builder = new PackMcMetaBuilder()
closure.delegate = builder
closure.resolveStrategy = DELEGATE_FIRST
closure.call(builder)
extraMaps.put('packMcMeta', builder as Map)
}

void mixinConfig(String configId, @DelegatesTo(value = MixinConfigBuilder, strategy = DELEGATE_FIRST)
@ClosureParams(value = SimpleType, options = 'modsdotgroovy.MixinConfigBuilder') final Closure closure) {
final builder = new MixinConfigBuilder()
builder.setRefMap(mixinRefMaps[configId])
closure.delegate = builder
closure.resolveStrategy = DELEGATE_FIRST
closure.call(builder)
extraMaps.put('mixinConfig_' + configId, builder as Map)

onQuilt {
final old = data['mixin']
if (old === null) {
data['mixin'] = [configId]
} else if (old instanceof List) {
old.add(configId)
} else {
data['mixin'] = [configId, old]
}
}
}

void mixinConfig(@DelegatesTo(value = MixinConfigBuilder, strategy = DELEGATE_FIRST)
@ClosureParams(value = SimpleType, options = 'modsdotgroovy.MixinConfigBuilder') final Closure closure) {
mixinRefMaps.forEach { cfg, refmap ->
if (!extraMaps.containsKey('mixinConfig_' + cfg)) {
mixinConfig(cfg, closure)
}
}
}

private static String combineAsString(List<String> parts) {
String fullString = ''
switch (parts.size()) {
Expand All @@ -268,6 +317,10 @@ class ModsDotGroovy {
sanitizeMap(data)
}

private Map getExtraMaps() {
(Map)data.computeIfAbsent('extraMaps') { new HashMap<>() }
}

private static void sanitizeMap(Map data) {
final copy = new LinkedHashMap(data) // cannot use Map.copyOf as we wish to remove null values
copy.forEach { Object key, Object value ->
Expand Down
19 changes: 19 additions & 0 deletions src/lib/groovy/modsdotgroovy/MapBackend.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package modsdotgroovy

import groovy.transform.CompileStatic

@CompileStatic
class MapBackend {
protected final Map data = [:]
void put(String key, Object value) {
data[key] = value
}
void propertyMissing(String name, Object value) {
data[name] = value
}

<T> T asType(Class<T> type) {
if (type == Map.class) return (T)data
return type.cast(this)
}
}
87 changes: 87 additions & 0 deletions src/lib/groovy/modsdotgroovy/MixinConfigBuilder.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package modsdotgroovy

import groovy.transform.CompileStatic
import groovy.transform.stc.ClosureParams
import groovy.transform.stc.SimpleType

import static groovy.lang.Closure.DELEGATE_FIRST

@CompileStatic
class MixinConfigBuilder extends MapBackend {

MixinConfigBuilder() {
setMinVersion('0.8')
injectors {
setDefaultRequire(1)
}
setRequired(true)
}

/**
* Sets the refmap
* @param refMap refmap
*/
void setRefMap(String refMap) {
put('refmap', refMap)
}

void setRequired(boolean required) {
put('required', required)
}

void setPackageName(String packageName) {
put('package', packageName)
}

void setCompatibilityLevel(int javaVersion) {
put('compatibilityLevel', 'JAVA_' + javaVersion)
}

void setMinVersion(String minVersion) {
put('minVersion', minVersion)
}

void injectors(@DelegatesTo(value = Injectors, strategy = DELEGATE_FIRST)
@ClosureParams(value = SimpleType, options = 'modsdotgroovy.MixinConfigBuilder$Injectors') final Closure closure) {
final builder = new Injectors()
closure.delegate = builder
closure.resolveStrategy = DELEGATE_FIRST
closure.call(builder)
put('injectors', builder.data)
}

void mixins(@DelegatesTo(value = Mixins, strategy = DELEGATE_FIRST)
@ClosureParams(value = SimpleType, options = 'modsdotgroovy.MixinConfigBuilder$Mixins') final Closure closure) {
final builder = new Mixins()
closure.delegate = builder
closure.resolveStrategy = DELEGATE_FIRST
closure.call(builder)
this['mixins'] = builder.common
this['client'] = builder.client
this['server'] = builder.server
}

static final class Injectors extends MapBackend {
void setDefaultRequire(int defaultRequire) {
put('defaultRequire', defaultRequire)
}
}

static final class Mixins {
final List<String> common = []
final List<String> client = []
final List<String> server = []

void common(String... commonMixins) {
this.common.addAll(commonMixins)
}

void client(String... clientMixins) {
this.client.addAll(clientMixins)
}

void server(String... serverMixins) {
this.server.addAll(serverMixins)
}
}
}
31 changes: 31 additions & 0 deletions src/lib/groovy/modsdotgroovy/PackMcMetaBuilder.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2022 GroovyMC
* SPDX-License-Identifier: MIT
*/

package modsdotgroovy

import groovy.transform.CompileStatic

@CompileStatic
class PackMcMetaBuilder extends MapBackend {
void setDescription(String description) {
packMap['description'] = description
}

void setPackFormat(int packFormat) {
packMap['pack_format'] = packFormat
}

void setForgeResourcePackFormat(int packFormat) {
packMap['forge:resource_pack_format'] = packFormat
}

void setForgeDataPackFormat(int packFormat) {
packMap['forge:data_pack_format'] = packFormat
}

private Map getPackMap() {
(Map)data.computeIfAbsent('pack') { new HashMap<>() }
}
}
Loading