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

Check refmaps exist before adding them to mixin configs (#37) #38

Open
wants to merge 1 commit into
base: 3.4
Choose a base branch
from
Open
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
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()
}
}
}
}
}