Skip to content

Commit

Permalink
Check refmaps exist before adding them to mixin configs (architectury#37
Browse files Browse the repository at this point in the history
)

Idea formatted automatically, that's why the diff looks weird
  • Loading branch information
Jamalam360 committed Oct 31, 2023
1 parent 0a37a6b commit d33fcb0
Showing 1 changed file with 53 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,58 @@ import dev.architectury.transformer.util.Logger
import java.io.ByteArrayInputStream

class AddRefmapName : AssetEditTransformer {
val gson = GsonBuilder().setPrettyPrinting().create()
override fun doEdit(context: TransformerContext, output: FileAccess) {
val mixins = mutableSetOf<String>()
output.handle { path, bytes ->
// Check JSON file in root directory
if (path.endsWith(".json") && !Transform.trimLeadingSlash(path)
.contains("/") && !Transform.trimLeadingSlash(path).contains("\\")
) {
Logger.debug("Checking whether $path is a mixin config.")
try {
val json = gson.fromJson(ByteArrayInputStream(bytes).reader(), JsonObject::class.java)
if (json != null) {
val hasMixins = json.has("mixins") && json["mixins"].isJsonArray
val hasClient = json.has("client") && json["client"].isJsonArray
val hasServer = json.has("server") && json["server"].isJsonArray
if (json.has("package") && (hasMixins || hasClient || hasServer)) {
if (!json.has("refmap") || !json.has("minVersion")) {
mixins.add(path)
}
}
}
} catch (_: Exception) {
}
}
}
if (mixins.isNotEmpty()) {
Logger.debug("Found mixin config(s): " + java.lang.String.join(",", mixins))
}
val refmap = System.getProperty(BuiltinProperties.REFMAP_NAME)
mixins.forEach { path ->
output.modifyFile(path) {
val json: JsonObject = gson.fromJson<JsonObject>(
ByteArrayInputStream(it).reader(),
JsonObject::class.java
)
val gson = GsonBuilder().setPrettyPrinting().create()
override fun doEdit(context: TransformerContext, output: FileAccess) {
val refmap = System.getProperty(BuiltinProperties.REFMAP_NAME)
var refmapFound = false
val mixins = mutableSetOf<String>()
output.handle { path, bytes ->
// Check JSON file in root directory
if (path.endsWith(".json") && !Transform.trimLeadingSlash(path)
.contains("/") && !Transform.trimLeadingSlash(path).contains("\\")
) {
Logger.debug("Checking whether $path is a mixin config.")
try {
val json = gson.fromJson(ByteArrayInputStream(bytes).reader(), JsonObject::class.java)
if (json != null) {
val hasMixins = json.has("mixins") && json["mixins"].isJsonArray
val hasClient = json.has("client") && json["client"].isJsonArray
val hasServer = json.has("server") && json["server"].isJsonArray
if (json.has("package") && (hasMixins || hasClient || hasServer)) {
if (!json.has("refmap") || !json.has("minVersion")) {
mixins.add(path)
}
}
}
} catch (_: Exception) {
}

if (!json.has("refmap")) {
Logger.debug("Injecting $refmap to $path")
json.addProperty("refmap", refmap)
}
Logger.debug("Checking whether $path is a mixin refmap.")
if (path.endsWith(refmap)) {
Logger.debug("Found mixin refmap: $path")
refmapFound = true
}
}
}
if (mixins.isNotEmpty()) {
Logger.debug("Found mixin config(s): " + java.lang.String.join(",", mixins))
}
if (refmapFound) {
mixins.forEach { path ->
output.modifyFile(path) {
val json: JsonObject = gson.fromJson<JsonObject>(
ByteArrayInputStream(it).reader(),
JsonObject::class.java
)

gson.toJson(json).toByteArray()
}
}
}
}
if (!json.has("refmap")) {
Logger.debug("Injecting $refmap to $path")
json.addProperty("refmap", refmap)
}

gson.toJson(json).toByteArray()
}
}
}
}
}

0 comments on commit d33fcb0

Please sign in to comment.