Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Deficuet authored and Deficuet committed Jan 21, 2023
1 parent 3239829 commit 8b91389
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ImportContext: AssetBundleFile {
* All [Object] loaded from this file.
*/
val objects = mutableMapOf<Long, Object>()
val objectList: Collection<Object> get() = objects.values

internal constructor(
filePath: String, manager: UnityAssetManager,
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ inline fun <reified T: Object> Array<out PPtr<*>>.allObjectsOf(): List<T> {
return map { it.getObj() }.filterIsInstance<T>()
}

inline fun <reified T: Object> List<PPtr<*>>.allObjectsOf(): List<T> {
inline fun <reified T: Object> Collection<PPtr<*>>.allObjectsOf(): List<T> {
return map { it.getObj() }.filterIsInstance<T>()
}

inline fun <reified T: Object> Array<out PPtr<*>>.firstObjectOf(): T {
return mapNotNull { it.getObj() }.firstObjectOf()
return mapNotNull { it.getObj() }.firstObjectOf<T>()
}

inline fun <reified T: Object> List<PPtr<*>>.firstObjectOf(): T {
return mapNotNull { it.getObj() }.firstObjectOf()
inline fun <reified T: Object> Collection<PPtr<*>>.firstObjectOf(): T {
return mapNotNull { it.getObj() }.firstObjectOf<T>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class UnityAssetManager: Closeable {
/**
* All file context loaded from disk.
*/
val contexts = mutableListOf<ImportContext>()
val contexts = mutableMapOf<String, ImportContext>()

/**
* 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) {
Expand Down Expand Up @@ -73,7 +73,7 @@ class UnityAssetManager: Closeable {
configuration.offsetMode,
configuration.manualOffset
).also {
contexts.add(it)
contexts[it.name] = it
}
}

Expand All @@ -91,7 +91,7 @@ class UnityAssetManager: Closeable {
configuration.offsetMode,
configuration.manualOffset
).also {
contexts.add(it)
contexts[it.name] = it
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class SerializedFile(
private set
val externals: List<FileIdentifier>
val objects: Map<Long, Object>
val objectList: Collection<Object> get() = objects.values

init {
if (hVersion >= FormatVersion.Unknown_9) {
Expand Down
32 changes: 8 additions & 24 deletions src/main/kotlin/io/github/deficuet/unitykt/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package io.github.deficuet.unitykt

import io.github.deficuet.unitykt.data.Object

operator fun List<ImportContext>.get(key: String) = find { it.name.contentEquals(key) }

operator fun <K, V> Collection<Pair<K, V>>.get(key: K): List<V> {
return filter {
when (val f = it.first) {
Expand Down Expand Up @@ -40,35 +38,21 @@ operator fun <K, V> Array<Pair<K, V>>.get(key: K): List<V> {
}.map { it.second }
}

fun <K, V> List<Pair<K, V>>.first(key: K) = get(key)[0]
fun <K, V> Collection<Pair<K, V>>.first(key: K) = get(key)[0]

fun <K, V> List<Pair<K, V>>.firstOrNull(key: K) = with(get(key)) { if (isEmpty()) null else this[0] }
fun <K, V> Collection<Pair<K, V>>.firstOrNull(key: K) = with(get(key)) { if (isEmpty()) null else this[0] }

inline fun <reified O: Object> List<Object>.allObjectsOf(): List<O> {
return mutableListOf<O>().apply {
for (obj in this@allObjectsOf) {
if (obj is O) add(obj)
}
}
}
inline fun <reified O: Object> Collection<Object>.firstObjectOf() = filterIsInstance<O>()[0]

inline fun <reified O: Object> List<Object>.firstObjectOf() = allObjectsOf<O>()[0]

inline fun <reified O: Object> List<Object>.firstOfOrNull(): O? {
return with(allObjectsOf<O>()) { if (isEmpty()) null else this[0] }
inline fun <reified O: Object> Collection<Object>.firstOfOrNull(): O? {
return with(filterIsInstance<O>()) { if (isEmpty()) null else this[0] }
}

fun List<Object>.allObjectsOf(vararg type: String): List<Object> {
return mutableListOf<Object>().apply {
for (obj in this@allObjectsOf) {
if (obj.type.name in type) {
add(obj)
}
}
}
fun Collection<Object>.allObjectsOf(vararg type: String): List<Object> {
return filter { it.type.name in type }
}

inline fun <reified T: Object> List<Object>.objectFromPathID(pathId: Long): T {
inline fun <reified T: Object> Collection<Object>.objectFromPathID(pathId: Long): T {
return first { it.mPathID == pathId } as T
}

Expand Down

0 comments on commit 8b91389

Please sign in to comment.