From 3239829e39c7c81dbf31c53be0f3b59886b597c8 Mon Sep 17 00:00:00 2001 From: Deficuet Date: Sat, 21 Jan 2023 14:43:34 -0700 Subject: [PATCH] replace list of objects with map --- .../io/github/deficuet/unitykt/ImportContext.kt | 2 +- .../kotlin/io/github/deficuet/unitykt/PPtrUtils.kt | 4 ++-- .../io/github/deficuet/unitykt/UnityAssetManager.kt | 10 +++++++++- .../github/deficuet/unitykt/file/SerializedFile.kt | 12 +++++------- .../kotlin/io/github/deficuet/unitykt/util/utils.kt | 10 +++++----- src/main/kotlin/io/github/deficuet/unitykt/utils.kt | 4 ++++ 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt b/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt index cb68731f..79d1d0f9 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt @@ -28,7 +28,7 @@ class ImportContext: AssetBundleFile { /** * All [Object] loaded from this file. */ - val objects = mutableListOf() + val objects = mutableMapOf() internal constructor( filePath: String, manager: UnityAssetManager, diff --git a/src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt b/src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt index ef7150ee..3ef59237 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt @@ -6,8 +6,8 @@ import io.github.deficuet.unitykt.data.PPtr inline fun PPtr.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 diff --git a/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt b/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt index e41c9354..5288cc88 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt @@ -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 diff --git a/src/main/kotlin/io/github/deficuet/unitykt/file/SerializedFile.kt b/src/main/kotlin/io/github/deficuet/unitykt/file/SerializedFile.kt index d23238e1..0309c2f7 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/file/SerializedFile.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/file/SerializedFile.kt @@ -189,9 +189,7 @@ class SerializedFile( var buildType = BuildType("") private set val externals: List - val objects: List - val objectDict: Map - get() = objects.associateBy { it.mPathID } + val objects: Map init { if (hVersion >= FormatVersion.Unknown_9) { @@ -324,7 +322,7 @@ class SerializedFile( reader.readStringUntilNull() } else "" //region readObjects - val objectList = mutableListOf() + val objectMap = mutableMapOf() for (info in objectInfoList) { val obj = when (info.type) { ClassIDType.Animation -> Animation(this, info) @@ -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 } diff --git a/src/main/kotlin/io/github/deficuet/unitykt/util/utils.kt b/src/main/kotlin/io/github/deficuet/unitykt/util/utils.kt index e1ed7e27..b3c2a5c9 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/util/utils.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/util/utils.kt @@ -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 @@ -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)) diff --git a/src/main/kotlin/io/github/deficuet/unitykt/utils.kt b/src/main/kotlin/io/github/deficuet/unitykt/utils.kt index 7e17eddb..e3e89a16 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/utils.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/utils.kt @@ -72,4 +72,8 @@ inline fun List.objectFromPathID(pathId: Long): T { return first { it.mPathID == pathId } as T } +inline fun Map.getAs(pathId: Long): T { + return this[pathId].cast() +} + inline fun Any?.cast(): T = this as T \ No newline at end of file