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 May 9, 2024
1 parent 4d97ec8 commit 65ddda8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 65 deletions.
18 changes: 1 addition & 17 deletions src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,7 @@ interface Sprite: NamedObject {
*
* i.e. usually up-side-down.
*/
fun getImage(strategy: SpriteCropStrategy = SpriteCropStrategy.USE_TEXTURE_RECT): BufferedImage?
}

/**
* Has effect only when cropping the image using [mRD][Sprite.mRD].
*
* i.e. when the `PPtr` [mSpriteAtlas][Sprite.mSpriteAtlas] gets `null`.
*/
enum class SpriteCropStrategy {
/**
* Use [textureRect][SpriteRenderData.textureRect]
*/
USE_TEXTURE_RECT,
/**
* Use [mRect][Sprite.mRect]
*/
USE_RECT
fun getImage(): BufferedImage?
}

interface SecondarySpriteTexture {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ internal class AssetBundleImpl(
return fmPathFlags
}

internal var dependenciesLoaded = false

override fun read() {
super.read()
fmPreloadTable = reader.readArrayOf { PPtrImpl(this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,13 @@ import io.github.deficuet.unitykt.pptr.PPtr
import io.github.deficuet.unitykt.util.ObjectReader

@PublishedApi
internal class PPtrImpl<out T: UnityObject>: PPtr<T> {
override var mFileID: Int
override var mPathID: Long
internal class PPtrImpl<out T: UnityObject> internal constructor(
override var mFileID: Int,
override var mPathID: Long,
private val assetFile: SerializedFile
): PPtr<T> {
override val isNull: Boolean
get() = mPathID == 0L || mFileID < 0
private val assetFile: SerializedFile

internal constructor(reader: ObjectReader) {
mFileID = reader.readInt32()
mPathID = with(reader) {
if (formatVersion < FormatVersion.Unknown_14) reader.readInt32().toLong()
else reader.readInt64()
}
assetFile = reader.assetFile
}

internal constructor(fileId: Int, pathId: Long, assetFile: SerializedFile) {
mFileID = fileId
mPathID = pathId
this.assetFile = assetFile
}

private var obj: T? = null

Expand All @@ -43,27 +29,7 @@ internal class PPtrImpl<out T: UnityObject>: PPtr<T> {
if (mFileID < 0 || fileIndex >= assetFile.externals.size) {
return null
}
val manager = assetFile.root.manager
val result = manager.assetFiles[assetFile.externals[fileIndex].name.lowercase()]
if (result != null) return result
if (manager.assetRootFolder == null) return null
val bundle = assetFile.objectMap[1L] ?: return null
if (bundle !is AssetBundleImpl) return null
if (!bundle.dependenciesLoaded) {
for (dependencyName in bundle.mDependencies) {
try {
manager.loadFile(
manager.assetRootFolder.resolve(dependencyName),
manager.defaultReaderConfig
)
} catch (e: Exception) {
println("An error occurred during loading dependency file ${dependencyName}: ${e.message}")
continue
}
}
bundle.dependenciesLoaded = true
}
return manager.assetFiles[assetFile.externals[fileIndex].name.lowercase()]
return assetFile.root.manager.assetFiles[assetFile.externals[fileIndex].name.lowercase()]
}

internal fun setObj(other: @UnsafeVariance T) {
Expand All @@ -89,6 +55,17 @@ internal class PPtrImpl<out T: UnityObject>: PPtr<T> {
}

fun getObjFrom(assetFile: SerializedFile) = assetFile.objectMap[mPathID]

internal companion object {
internal operator fun <T: UnityObject> invoke(reader: ObjectReader): PPtrImpl<T> {
val fileId = reader.readInt32()
val pathId = with(reader) {
if (formatVersion < FormatVersion.Unknown_14) reader.readInt32().toLong()
else reader.readInt64()
}
return PPtrImpl(fileId, pathId, reader.assetFile)
}
}
}

@PublishedApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ internal class SpriteImpl(
}
}

override fun getImage(strategy: SpriteCropStrategy): BufferedImage? {
override fun getImage(): BufferedImage? {
val spriteAtlas = mSpriteAtlas?.safeGetObj()
if (spriteAtlas != null) {
val spriteAtlasData = spriteAtlas.mRenderDataMap[mRenderDataKey]
Expand All @@ -133,14 +133,10 @@ internal class SpriteImpl(
}
} else {
val tex = mRD.texture.safeGetObj()
val rect = when (strategy) {
SpriteCropStrategy.USE_TEXTURE_RECT -> mRD.textureRect
SpriteCropStrategy.USE_RECT -> mRect
}
if (tex != null) {
return with(mRD) {
cutImage(
tex, rect, //textureRectOffset,
tex, mRD.textureRect, //textureRectOffset,
downScaleMultiplier, settingsRaw
)
}
Expand Down

0 comments on commit 65ddda8

Please sign in to comment.