Skip to content

Commit

Permalink
replace list of objects with map
Browse files Browse the repository at this point in the history
  • Loading branch information
Deficuet authored and Deficuet committed Jan 21, 2023
1 parent c39bee6 commit 3239829
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ImportContext: AssetBundleFile {
/**
* All [Object] loaded from this file.
*/
val objects = mutableListOf<Object>()
val objects = mutableMapOf<Long, Object>()

internal constructor(
filePath: String, manager: UnityAssetManager,
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import io.github.deficuet.unitykt.data.PPtr
inline fun <reified O: Object> PPtr<O>.getObj(): O? {
if (obj != null) return obj
getManager()?.let { manager ->
if (mPathID in manager.objectDict) {
val objFound = manager.objectDict.getValue(mPathID)
if (mPathID in manager.objects) {
val objFound = manager.objects.getValue(mPathID)
return if (objFound is O) {
obj = objFound
obj
Expand Down
10 changes: 9 additions & 1 deletion src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ class UnityAssetManager: Closeable {
/**
* All Objects loaded except [io.github.deficuet.unitykt.data.AssetBundle]
*/
val objects get() = contexts.flatMap { context -> context.objects.filter { it.mPathID != 1L } }
val objects get() = contexts.flatMap { context ->
sequence {
for (obj in context.objects) {
if (obj.key != 1L) {
yield(obj.value)
}
}
}
}

/**
* Multi-dictionary of objects associated with their mPathID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ class SerializedFile(
var buildType = BuildType("")
private set
val externals: List<FileIdentifier>
val objects: List<Object>
val objectDict: Map<Long, Object>
get() = objects.associateBy { it.mPathID }
val objects: Map<Long, Object>

init {
if (hVersion >= FormatVersion.Unknown_9) {
Expand Down Expand Up @@ -324,7 +322,7 @@ class SerializedFile(
reader.readStringUntilNull()
} else ""
//region readObjects
val objectList = mutableListOf<Object>()
val objectMap = mutableMapOf<Long, Object>()
for (info in objectInfoList) {
val obj = when (info.type) {
ClassIDType.Animation -> Animation(this, info)
Expand Down Expand Up @@ -358,10 +356,10 @@ class SerializedFile(
ClassIDType.ResourceManager -> ResourceManager(this, info)
else -> Object(this, info)
}
objectList.add(obj)
objectMap[obj.mPathID] = obj
}
objects = objectList
root.objects.addAll(objects)
objects = objectMap
root.objects.putAll(objects)
objectInfoList.clear()
//endregion
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/io/github/deficuet/unitykt/util/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal fun ByteArray.decodeToString(charset: Charset = Charsets.UTF_8) =
internal fun ByteArray.toHalf(): Float {
if (size != 2) throw IllegalStateException("There should be 2 bytes only")
val intValue = this[0].toIntBits().shl(8).or(this[1].toIntBits())
var mantissa = intValue and 0x03FF
var exp = intValue and 0x7C00
var mantissa = intValue.and(0x03FF)
var exp = intValue.and(0x7C00)
if (exp == 0x7C00) exp = 0x3FC00
else if (exp != 0) {
exp += 0x1C000
Expand All @@ -35,10 +35,10 @@ internal fun ByteArray.toHalf(): Float {
} else if (mantissa != 0) {
exp = 0x1C400
do {
mantissa = mantissa shl 1
mantissa = mantissa.shl(1)
exp -= 0x400
} while (mantissa and 0x400 == 0)
mantissa = mantissa and 0x3FF
} while (mantissa.and(0x400) == 0)
mantissa = mantissa.and(0x3FF)
}
return Float.fromBits(
intValue.and(0x8000).shl(16).or(exp.or(mantissa).shl(13))
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/io/github/deficuet/unitykt/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ inline fun <reified T: Object> List<Object>.objectFromPathID(pathId: Long): T {
return first { it.mPathID == pathId } as T
}

inline fun <reified T: Object> Map<Long, Object>.getAs(pathId: Long): T {
return this[pathId].cast()
}

inline fun <reified T> Any?.cast(): T = this as T

0 comments on commit 3239829

Please sign in to comment.