From 8b9138930ac4806769ee9b0dc31fdc6dbf603acc Mon Sep 17 00:00:00 2001 From: Deficuet Date: Sat, 21 Jan 2023 15:59:54 -0700 Subject: [PATCH] update --- .../github/deficuet/unitykt/ImportContext.kt | 1 + .../io/github/deficuet/unitykt/PPtrUtils.kt | 8 ++--- .../deficuet/unitykt/UnityAssetManager.kt | 8 ++--- .../deficuet/unitykt/file/SerializedFile.kt | 1 + .../io/github/deficuet/unitykt/utils.kt | 32 +++++-------------- 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt b/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt index 79d1d0f9..484fcaad 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt @@ -29,6 +29,7 @@ class ImportContext: AssetBundleFile { * All [Object] loaded from this file. */ val objects = mutableMapOf() + val objectList: Collection get() = objects.values 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 3ef59237..8d5be6aa 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt @@ -27,14 +27,14 @@ inline fun Array>.allObjectsOf(): List { return map { it.getObj() }.filterIsInstance() } -inline fun List>.allObjectsOf(): List { +inline fun Collection>.allObjectsOf(): List { return map { it.getObj() }.filterIsInstance() } inline fun Array>.firstObjectOf(): T { - return mapNotNull { it.getObj() }.firstObjectOf() + return mapNotNull { it.getObj() }.firstObjectOf() } -inline fun List>.firstObjectOf(): T { - return mapNotNull { it.getObj() }.firstObjectOf() +inline fun Collection>.firstObjectOf(): T { + return mapNotNull { it.getObj() }.firstObjectOf() } \ No newline at end of file diff --git a/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt b/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt index 5288cc88..b460bcbb 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/UnityAssetManager.kt @@ -23,12 +23,12 @@ class UnityAssetManager: Closeable { /** * All file context loaded from disk. */ - val contexts = mutableListOf() + val contexts = mutableMapOf() /** * All Objects loaded except [io.github.deficuet.unitykt.data.AssetBundle] */ - val objects get() = contexts.flatMap { context -> + val objects get() = contexts.values.flatMap { context -> sequence { for (obj in context.objects) { if (obj.key != 1L) { @@ -73,7 +73,7 @@ class UnityAssetManager: Closeable { configuration.offsetMode, configuration.manualOffset ).also { - contexts.add(it) + contexts[it.name] = it } } @@ -91,7 +91,7 @@ class UnityAssetManager: Closeable { configuration.offsetMode, configuration.manualOffset ).also { - contexts.add(it) + contexts[it.name] = it } } 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 0309c2f7..2975a5b0 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/file/SerializedFile.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/file/SerializedFile.kt @@ -190,6 +190,7 @@ class SerializedFile( private set val externals: List val objects: Map + val objectList: Collection get() = objects.values init { if (hVersion >= FormatVersion.Unknown_9) { diff --git a/src/main/kotlin/io/github/deficuet/unitykt/utils.kt b/src/main/kotlin/io/github/deficuet/unitykt/utils.kt index e3e89a16..8812872a 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/utils.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/utils.kt @@ -2,8 +2,6 @@ package io.github.deficuet.unitykt import io.github.deficuet.unitykt.data.Object -operator fun List.get(key: String) = find { it.name.contentEquals(key) } - operator fun Collection>.get(key: K): List { return filter { when (val f = it.first) { @@ -40,35 +38,21 @@ operator fun Array>.get(key: K): List { }.map { it.second } } -fun List>.first(key: K) = get(key)[0] +fun Collection>.first(key: K) = get(key)[0] -fun List>.firstOrNull(key: K) = with(get(key)) { if (isEmpty()) null else this[0] } +fun Collection>.firstOrNull(key: K) = with(get(key)) { if (isEmpty()) null else this[0] } -inline fun List.allObjectsOf(): List { - return mutableListOf().apply { - for (obj in this@allObjectsOf) { - if (obj is O) add(obj) - } - } -} +inline fun Collection.firstObjectOf() = filterIsInstance()[0] -inline fun List.firstObjectOf() = allObjectsOf()[0] - -inline fun List.firstOfOrNull(): O? { - return with(allObjectsOf()) { if (isEmpty()) null else this[0] } +inline fun Collection.firstOfOrNull(): O? { + return with(filterIsInstance()) { if (isEmpty()) null else this[0] } } -fun List.allObjectsOf(vararg type: String): List { - return mutableListOf().apply { - for (obj in this@allObjectsOf) { - if (obj.type.name in type) { - add(obj) - } - } - } +fun Collection.allObjectsOf(vararg type: String): List { + return filter { it.type.name in type } } -inline fun List.objectFromPathID(pathId: Long): T { +inline fun Collection.objectFromPathID(pathId: Long): T { return first { it.mPathID == pathId } as T }