|
| 1 | +package com.margelo.nitro.rive |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Context |
| 5 | +import android.content.res.Resources |
| 6 | +import android.net.Uri |
| 7 | +import android.util.Log |
| 8 | +import app.rive.runtime.kotlin.core.* |
| 9 | +import com.margelo.nitro.NitroModules |
| 10 | +import kotlinx.coroutines.* |
| 11 | +import java.io.File as JavaFile |
| 12 | +import java.io.IOException |
| 13 | +import java.net.URI |
| 14 | +import java.net.URL |
| 15 | + |
| 16 | +typealias ReferencedAssetCache = MutableMap<String, FileAsset> |
| 17 | + |
| 18 | +class ReferencedAssetLoader { |
| 19 | + private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) |
| 20 | + |
| 21 | + private fun logError(message: String) { |
| 22 | + Log.e("ReferencedAssetLoader", message) |
| 23 | + } |
| 24 | + |
| 25 | + private fun isValidUrl(url: String): Boolean { |
| 26 | + return try { |
| 27 | + URL(url) |
| 28 | + true |
| 29 | + } catch (e: Exception) { |
| 30 | + false |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private fun constructFilePath(filename: String, path: String): String { |
| 35 | + return if (path.endsWith("/")) "$path$filename" else "$path/$filename" |
| 36 | + } |
| 37 | + |
| 38 | + @SuppressLint("DiscouragedApi") |
| 39 | + private fun getResourceId(source: String, context: Context): Int { |
| 40 | + val resourceTypes = listOf("raw", "drawable") |
| 41 | + |
| 42 | + for (type in resourceTypes) { |
| 43 | + val resourceId = context.resources.getIdentifier(source, type, context.packageName) |
| 44 | + if (resourceId != 0) { |
| 45 | + return resourceId |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return 0 |
| 50 | + } |
| 51 | + |
| 52 | + private fun readAssetBytes(context: Context, fileName: String): ByteArray? { |
| 53 | + val assetManager = context.assets |
| 54 | + return try { |
| 55 | + assetManager.open(fileName).use { inputStream -> |
| 56 | + inputStream.readBytes() |
| 57 | + } |
| 58 | + } catch (e: IOException) { |
| 59 | + logError("Unable to read file from assets: $fileName - ${e.message}") |
| 60 | + null |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private fun downloadUrlAsset(url: String, listener: (ByteArray) -> Unit) { |
| 65 | + if (!isValidUrl(url)) { |
| 66 | + logError("Invalid URL: $url") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + scope.launch { |
| 71 | + try { |
| 72 | + val uri = URI(url) |
| 73 | + val bytes = when (uri.scheme) { |
| 74 | + "file" -> { |
| 75 | + val file = JavaFile(uri.path) |
| 76 | + if (!file.exists()) { |
| 77 | + throw IOException("File not found: ${uri.path}") |
| 78 | + } |
| 79 | + if (!file.canRead()) { |
| 80 | + throw IOException("Permission denied: ${uri.path}") |
| 81 | + } |
| 82 | + file.readBytes() |
| 83 | + } |
| 84 | + "http", "https" -> { |
| 85 | + URL(url).readBytes() |
| 86 | + } |
| 87 | + else -> { |
| 88 | + logError("Unsupported URL scheme: ${uri.scheme}") |
| 89 | + return@launch |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + withContext(Dispatchers.Main) { |
| 94 | + listener(bytes) |
| 95 | + } |
| 96 | + } catch (e: Exception) { |
| 97 | + logError("Unable to download asset from URL: $url - ${e.message}") |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private fun loadResourceAsset( |
| 103 | + sourceAssetId: String, |
| 104 | + context: Context, |
| 105 | + listener: (ByteArray) -> Unit |
| 106 | + ) { |
| 107 | + scope.launch { |
| 108 | + try { |
| 109 | + val scheme = runCatching { Uri.parse(sourceAssetId).scheme }.getOrNull() |
| 110 | + |
| 111 | + if (scheme != null) { |
| 112 | + downloadUrlAsset(sourceAssetId, listener) |
| 113 | + return@launch |
| 114 | + } |
| 115 | + |
| 116 | + val resourceId = getResourceId(sourceAssetId, context) |
| 117 | + |
| 118 | + if (resourceId != 0) { |
| 119 | + val bytes = context.resources.openRawResource(resourceId).use { inputStream -> |
| 120 | + inputStream.readBytes() |
| 121 | + } |
| 122 | + withContext(Dispatchers.Main) { |
| 123 | + listener(bytes) |
| 124 | + } |
| 125 | + } else { |
| 126 | + logError("Resource not found: $sourceAssetId") |
| 127 | + } |
| 128 | + } catch (e: IOException) { |
| 129 | + logError("IO Exception while reading resource: $sourceAssetId - ${e.message}") |
| 130 | + } catch (e: Resources.NotFoundException) { |
| 131 | + logError("Resource not found: $sourceAssetId - ${e.message}") |
| 132 | + } catch (e: Exception) { |
| 133 | + logError("Unexpected error while processing resource: $sourceAssetId - ${e.message}") |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private fun loadBundledAsset( |
| 139 | + sourceAsset: String, |
| 140 | + path: String?, |
| 141 | + context: Context, |
| 142 | + listener: (ByteArray) -> Unit |
| 143 | + ) { |
| 144 | + scope.launch { |
| 145 | + try { |
| 146 | + val fullPath = if (path == null) sourceAsset else constructFilePath(sourceAsset, path) |
| 147 | + val bytes = readAssetBytes(context, fullPath) |
| 148 | + |
| 149 | + if (bytes != null) { |
| 150 | + withContext(Dispatchers.Main) { |
| 151 | + listener(bytes) |
| 152 | + } |
| 153 | + } |
| 154 | + } catch (e: Exception) { |
| 155 | + logError("Error loading bundled asset: $sourceAsset - ${e.message}") |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private fun processAssetBytes(bytes: ByteArray, asset: FileAsset) { |
| 161 | + when (asset) { |
| 162 | + is ImageAsset -> asset.image = RiveRenderImage.make(bytes) |
| 163 | + is FontAsset -> asset.font = RiveFont.make(bytes) |
| 164 | + is AudioAsset -> asset.audio = RiveAudio.make(bytes) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private fun loadAsset(assetData: ResolvedReferencedAsset, asset: FileAsset, context: Context) { |
| 169 | + val listener: (ByteArray) -> Unit = { bytes -> |
| 170 | + processAssetBytes(bytes, asset) |
| 171 | + } |
| 172 | + |
| 173 | + when { |
| 174 | + assetData.sourceAssetId != null -> { |
| 175 | + loadResourceAsset(assetData.sourceAssetId, context, listener) |
| 176 | + } |
| 177 | + assetData.sourceUrl != null -> { |
| 178 | + downloadUrlAsset(assetData.sourceUrl, listener) |
| 179 | + } |
| 180 | + assetData.sourceAsset != null -> { |
| 181 | + loadBundledAsset(assetData.sourceAsset, assetData.path, context, listener) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + fun createCustomLoader( |
| 187 | + referencedAssets: ReferencedAssetsType?, |
| 188 | + cache: ReferencedAssetCache |
| 189 | + ): FileAssetLoader? { |
| 190 | + val assetsData = referencedAssets?.data ?: return null |
| 191 | + val context = NitroModules.applicationContext ?: return null |
| 192 | + |
| 193 | + return object : FileAssetLoader() { |
| 194 | + override fun loadContents(asset: FileAsset, inBandBytes: ByteArray): Boolean { |
| 195 | + var key = asset.uniqueFilename.substringBeforeLast(".") |
| 196 | + var assetData = assetsData[key] |
| 197 | + |
| 198 | + if (assetData == null) { |
| 199 | + key = asset.name |
| 200 | + assetData = assetsData[asset.name] |
| 201 | + } |
| 202 | + |
| 203 | + if (assetData == null) { |
| 204 | + return false |
| 205 | + } |
| 206 | + |
| 207 | + cache[key] = asset |
| 208 | + |
| 209 | + loadAsset(assetData, asset, context) |
| 210 | + |
| 211 | + return true |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + fun dispose() { |
| 217 | + scope.cancel() |
| 218 | + } |
| 219 | +} |
0 commit comments