Skip to content

Commit

Permalink
Added support for loading stuff within packages with more than one ex…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
Amrsatrio committed Jul 22, 2020
1 parent 705bff1 commit d093fc7
Show file tree
Hide file tree
Showing 7 changed files with 457 additions and 221 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.idea
/build
/run
/.unused
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.tb24'
version '0.2.1'
version '0.2.2'

sourceCompatibility = 1.8

Expand Down Expand Up @@ -42,7 +42,9 @@ repositories {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
implementation 'com.google.code.gson:gson:2.8.6'
// implementation 'com.mojang:brigadier:1.0.17'
implementation 'me.fungames:JFortniteParse:+' // :3.0.2'
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/tb24/blenderumap/AssetUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tb24.blenderumap

import me.fungames.jfortniteparse.ue4.FGuid
import me.fungames.jfortniteparse.ue4.assets.exports.UExport
import me.fungames.jfortniteparse.ue4.assets.exports.UObject
import me.fungames.jfortniteparse.ue4.assets.objects.FPropertyTag
import java.util.*

fun <T> getProp(properties: List<FPropertyTag>, name: String, clazz: Class<T>): T? {
for (prop in properties) {
if (name == prop.name.text) {
return prop.getTagTypeValue(clazz, null) as T?
}
}
return null
}

fun <T> getProps(properties: List<FPropertyTag>, name: String, clazz: Class<T>): Array<T?> {
val collected: MutableList<FPropertyTag> = ArrayList()
var maxIndex = -1
for (prop in properties) {
if (prop.name.text == name) {
collected.add(prop)
maxIndex = Math.max(maxIndex, prop.arrayIndex)
}
}
val out = java.lang.reflect.Array.newInstance(clazz, maxIndex + 1) as Array<T?>
for (prop in collected) {
out[prop.arrayIndex] = prop.getTagTypeValue(clazz, null) as T?
}
return out
}

fun <T> UExport.getProp(name: String, clazz: Class<T>) = baseObject.getProp(name, clazz)
fun <T> UObject.getProp(name: String, clazz: Class<T>) = getProp(properties, name, clazz)
inline operator fun <reified T> UExport.get(name: String): T? = getProp(name, T::class.java)
inline operator fun <reified T> UObject.get(name: String): T? = getProp(name, T::class.java)

fun FGuid?.asString() = if (this == null) null else "%08x%08x%08x%08x".format(part1, part2, part3, part4)
70 changes: 54 additions & 16 deletions src/main/java/com/tb24/blenderumap/JWPSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.google.gson.*
import me.fungames.jfortniteparse.ue4.FGuid
import me.fungames.jfortniteparse.ue4.assets.exports.UDataTable
import me.fungames.jfortniteparse.ue4.assets.exports.UExport
import me.fungames.jfortniteparse.ue4.assets.exports.UObject
import me.fungames.jfortniteparse.ue4.assets.objects.*
import me.fungames.jfortniteparse.ue4.assets.util.FName
import me.fungames.jfortniteparse.util.parseHexBinary
Expand All @@ -20,10 +19,13 @@ import java.util.*
*/
@ExperimentalUnsignedTypes
object JWPSerializer {
@JvmField
var sUseNonstandardFormat = false

@JvmField
val GSON: Gson = GsonBuilder()
.disableHtmlEscaping()
.setPrettyPrinting()
//.setPrettyPrinting()
.serializeNulls()
.registerTypeAdapter(ByteArray::class.java, ByteArraySerializer())
.registerTypeAdapter(UByte::class.java, JsonSerializer<UByte> { src, typeOfSrc, context -> JsonPrimitive(src.toByte()) })
Expand All @@ -42,6 +44,14 @@ object JWPSerializer {
JsonObject().apply {
add("min", context.serialize(src.min))
add("max", context.serialize(src.max))
add("valid", context.serialize(src.isValid))
}
})
.registerTypeAdapter(FBox2D::class.java, JsonSerializer<FBox2D> { src, typeOfSrc, context ->
JsonObject().apply {
add("min", context.serialize(src.min))
add("max", context.serialize(src.max))
add("valid", context.serialize(src.isValid))
}
})
.registerTypeAdapter(FGameplayTagContainer::class.java, JsonSerializer<FGameplayTagContainer> { src, typeOfSrc, context ->
Expand All @@ -62,24 +72,36 @@ object JWPSerializer {
JsonPrimitive(src.text)
})
.registerTypeAdapter(FPackageIndex::class.java, JsonSerializer<FPackageIndex> { src, typeOfSrc, context ->
var out: JsonElement? = null
val importObject = src.importObject
val out: JsonElement

if (src.index >= 0) {
out = JsonObject()
out.addProperty("export", src.index)
} else if (importObject != null) {
if (src.index < 0) {
val importObject = src.importObject
out = JsonArray()
out.add(context.serialize(importObject.objectName))
out.add(context.serialize(importObject!!.objectName))
out.add(context.serialize(src.outerImportObject!!.objectName))

if (src.outerImportObject!!.outerIndex.importObject != null) {
out.add(context.serialize(src.outerImportObject!!.outerIndex.importObject!!.objectName))
}
} else {
out = JsonObject()
out.addProperty("export", src.index)

/*if (src.index > 0) {
out.addProperty("__object_name", src.exportObject!!.objectName.text)
}*/
}

out
})
.registerTypeAdapter(FQuat::class.java, JsonSerializer<FQuat> { src, typeOfSrc, context ->
JsonObject().apply {
addProperty("x", src.x)
addProperty("y", src.y)
addProperty("z", src.z)
addProperty("w", src.w)
}
})
.registerTypeAdapter(FRotator::class.java, JsonSerializer<FRotator> { src, typeOfSrc, context ->
JsonObject().apply {
addProperty("pitch", src.pitch)
Expand Down Expand Up @@ -141,21 +163,37 @@ object JWPSerializer {
.create()

private fun serializeProperties(obj: JsonObject, properties: List<FPropertyTag>, context: JsonSerializationContext) {
for (propertyTag in properties) {
obj.add(propertyTag.name.text, context.serialize(propertyTag.tag))
properties.forEach {
obj.add(it.name.text + (if (it.arrayIndex != 0) "[${it.arrayIndex}]" else ""), context.serialize(it.tag))
}
}

private class ExportSerializer : JsonSerializer<UExport> {
override fun serialize(src: UExport, typeOfSrc: Type, context: JsonSerializationContext): JsonElement? {
val obj = JsonObject()
if (sUseNonstandardFormat && src.export != null) obj.addProperty("object_name", src.export!!.objectName.text)
obj.addProperty("export_type", src.exportType)

if (src is UObject) {
serializeProperties(obj, src.properties, context)
} else if (src is UDataTable) {
for ((key, value) in src.rows) {
obj.add(key.text, context.serialize(value))
if (src !is UDataTable || sUseNonstandardFormat)
serializeProperties(obj, src.baseObject.properties, context)

if (src is UDataTable) {
if (sUseNonstandardFormat) {
obj.add("rows", JsonObject().apply {
for ((key, value) in src.rows) {
add(key.text, JsonObject().apply {
addProperty("export_type", "RowStruct")
serializeProperties(this, value.properties, context)
})
}
})
} else {
for ((key, value) in src.rows) {
obj.add(key.text, JsonObject().apply {
addProperty("export_type", "RowStruct")
serializeProperties(this, value.properties, context)
})
}
}
}

Expand Down
Loading

0 comments on commit d093fc7

Please sign in to comment.