From 65ddda8bd7cac2734348080b382e541ad634c996 Mon Sep 17 00:00:00 2001 From: Deficuet Date: Thu, 9 May 2024 17:58:35 -0600 Subject: [PATCH] update --- .../github/deficuet/unitykt/classes/Sprite.kt | 18 +----- .../unitykt/internal/impl/AssetBundleImpl.kt | 2 - .../unitykt/internal/impl/PPtrImpl.kt | 57 ++++++------------- .../unitykt/internal/impl/SpriteImpl.kt | 8 +-- 4 files changed, 20 insertions(+), 65 deletions(-) diff --git a/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt b/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt index a735efe2..b8377604 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt @@ -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 { diff --git a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/AssetBundleImpl.kt b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/AssetBundleImpl.kt index 9d38ba59..541157b8 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/AssetBundleImpl.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/AssetBundleImpl.kt @@ -50,8 +50,6 @@ internal class AssetBundleImpl( return fmPathFlags } - internal var dependenciesLoaded = false - override fun read() { super.read() fmPreloadTable = reader.readArrayOf { PPtrImpl(this) } diff --git a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/PPtrImpl.kt b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/PPtrImpl.kt index cdc84c60..b2880b29 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/PPtrImpl.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/PPtrImpl.kt @@ -8,27 +8,13 @@ import io.github.deficuet.unitykt.pptr.PPtr import io.github.deficuet.unitykt.util.ObjectReader @PublishedApi -internal class PPtrImpl: PPtr { - override var mFileID: Int - override var mPathID: Long +internal class PPtrImpl internal constructor( + override var mFileID: Int, + override var mPathID: Long, + private val assetFile: SerializedFile +): PPtr { 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 @@ -43,27 +29,7 @@ internal class PPtrImpl: PPtr { 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) { @@ -89,6 +55,17 @@ internal class PPtrImpl: PPtr { } fun getObjFrom(assetFile: SerializedFile) = assetFile.objectMap[mPathID] + + internal companion object { + internal operator fun invoke(reader: ObjectReader): PPtrImpl { + 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 diff --git a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt index 589cef40..5087d36a 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt @@ -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] @@ -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 ) }