Skip to content

Commit

Permalink
More shortcuts for PPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
Deficuet committed Apr 1, 2022
1 parent a193e39 commit 2f769e5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/main/kotlin/io/github/deficuet/unitykt/PPtrUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.deficuet.unitykt

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

inline fun <reified O: Object> PPtr<O>.getObj(): O? {
if (obj != null) return obj
Expand All @@ -19,3 +18,18 @@ inline fun <reified O: Object> PPtr<O>.getObj(): O? {
return null
}

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

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

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

inline fun <reified T: Object> List<PPtr<*>>.firstObjectOf(): T {
return mapNotNull { it.getObj() }.firstObjectOf()
}
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/deficuet/unitykt/data/PPtr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.github.deficuet.unitykt.file.SerializedFile
import io.github.deficuet.unitykt.util.*
import java.io.File

class PPtr<T: Object> internal constructor(reader: ObjectReader) {
class PPtr<out T: Object> internal constructor(reader: ObjectReader) {
var mFileID = reader.readInt()
internal set
var mPathID = with(reader) { if (formatVersion < FormatVersion.kUnknown_14) readInt().toLong() else readLong() }
Expand All @@ -18,7 +18,7 @@ class PPtr<T: Object> internal constructor(reader: ObjectReader) {
/**
* @see io.github.deficuet.unitykt.getObj
*/
@PublishedApi internal var obj: T? = null
@PublishedApi internal var obj: @UnsafeVariance T? = null

@PublishedApi internal fun getManager(): SerializedFile? {
return if (mFileID == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open class ObjectImpl internal constructor(private val reader: ObjectReader) {
val bytes by lazy { reader.bytes }

init {
println("Object with mPathID $mPathID and type ${reader.type} initialized")
println("Object(${reader.type}) path id $mPathID initialized")
reader.position = 0
if (platform == BuildTarget.NoTarget) reader += 4 //m_ObjectHideFlags: UInt
}
Expand Down

0 comments on commit 2f769e5

Please sign in to comment.