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 Jul 21, 2023
1 parent 3ae350b commit bfef20d
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 118 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/deficuet/unitykt/ImportContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class ImportContext: AssetBundleFile {
/**
* All [Object] loaded from this file.
*/
val objects = mutableMapOf<Long, Object>()
val objectList: Collection<Object> get() = objects.values
val objectMap = mutableMapOf<Long, Object>()
val objectList: Collection<Object> get() = objectMap.values

internal constructor(
filePath: String, manager: UnityAssetManager,
Expand Down
30 changes: 23 additions & 7 deletions src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.deficuet.unitykt
import io.github.deficuet.unitykt.data.Object
import io.github.deficuet.unitykt.data.PPtr

inline fun <reified O: Object> PPtr<O>.getObj(): O? {
inline fun <reified O: Object> PPtr<O>.safeGetObj(): O? {
if (obj != null) return obj
getManager()?.let { manager ->
if (mPathID in manager.objects) {
Expand All @@ -19,22 +19,38 @@ inline fun <reified O: Object> PPtr<O>.getObj(): O? {
return null
}

inline fun <reified O: Object> PPtr<O>.getObj(): O {
return safeGetObj()!!
}

inline fun <reified O: Object> PPtr<*>.safeGetObjAs(): O? {
return safeGetObj() as? O
}

inline fun <reified T: Object> PPtr<*>.getObjAs(): T {
return getObj() as T
return safeGetObj() as T
}

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

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

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

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

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

inline fun <reified T: Object> Collection<PPtr<*>>.firstOfOrNull(): T? {
return mapNotNull { it.safeGetObj() }.firstOfOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class UnityAssetManager: Closeable {
/**
* All Objects loaded except [io.github.deficuet.unitykt.data.AssetBundle]
*/
val objects get() = contexts.values.flatMap { context ->
val objectList get() = contexts.values.flatMap { context ->
sequence {
for (obj in context.objects) {
for (obj in context.objectMap) {
if (obj.key != 1L) {
yield(obj.value)
}
Expand All @@ -41,7 +41,7 @@ class UnityAssetManager: Closeable {
/**
* Multi-dictionary of objects associated with their mPathID
*/
val objectDict get() = objects.groupBy { it.mPathID }
val objectMap get() = objectList.groupBy { it.mPathID }

data class Configuration internal constructor(
/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/github/deficuet/unitykt/data/PPtr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PPtr<out T: Object> internal constructor(reader: ObjectReader) {
val assetFile = reader.assetFile

/**
* @see io.github.deficuet.unitykt.getObj
* @see io.github.deficuet.unitykt.safeGetObj
*/
@PublishedApi internal var obj: @UnsafeVariance T? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.deficuet.unitykt.dataImpl

import io.github.deficuet.unitykt.data.*
import io.github.deficuet.unitykt.getObj
import io.github.deficuet.unitykt.safeGetObj
import io.github.deficuet.unitykt.util.ObjectReader
import io.github.deficuet.unitykt.util.compareTo

Expand Down Expand Up @@ -32,7 +32,7 @@ class GameObjectImpl internal constructor(reader: ObjectReader): EditorExtension
val animators = mutableListOf<Animator>()
val animations = mutableListOf<Animation>()
for (pptr in mComponents) {
val obj = pptr.getObj()
val obj = pptr.safeGetObj()
if (obj != null) {
when (obj) {
is Transform -> transforms.add(obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class SerializedFile(
objectMap[obj.mPathID] = obj
}
objects = objectMap
root.objects.putAll(objects)
root.objectMap.putAll(objects)
objectInfoList.clear()
//endregion
}
Expand Down
Loading

0 comments on commit bfef20d

Please sign in to comment.