-
Notifications
You must be signed in to change notification settings - Fork 7
WIP feat: experimental iOS / Android POC #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
582f156
5916624
a845f29
6ebf265
32e742c
7dc2964
7ff04e8
e418b4b
31c3115
cf062f2
caff580
04d7a20
046becf
2c25174
aebbc51
beb5827
8196fed
3e42312
a01fc48
920dfb4
c68ec58
436d243
fc628e5
7e984b2
4fd2755
96abedd
5b13844
91c487b
06e7401
7f5904d
503a981
efd8521
47f4a51
f1b851e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package com.margelo.nitro.rive | ||
|
|
||
| import android.util.Log | ||
| import app.rive.AudioAsset | ||
| import app.rive.FontAsset | ||
| import app.rive.ImageAsset | ||
| import app.rive.core.CommandQueue | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| object ExperimentalAssetLoader { | ||
| private const val TAG = "ExperimentalAssetLoader" | ||
|
|
||
| fun registerAssets( | ||
| referencedAssets: ReferencedAssetsType?, | ||
| riveWorker: CommandQueue | ||
| ) { | ||
| val assetsData = referencedAssets?.data ?: return | ||
| val scope = CoroutineScope(Dispatchers.IO) | ||
|
|
||
| for ((name, assetData) in assetsData) { | ||
| val source = DataSourceResolver.resolve(assetData) ?: continue | ||
| scope.launch { | ||
| try { | ||
| val loader = source.createLoader() | ||
| val data = loader.load(source) | ||
| val type = inferAssetType(name, data) | ||
| registerAsset(data, name, type, riveWorker) | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to load asset '$name'", e) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun updateAssets( | ||
| referencedAssets: ReferencedAssetsType, | ||
| riveWorker: CommandQueue | ||
| ) { | ||
| val assetsData = referencedAssets.data ?: return | ||
| val scope = CoroutineScope(Dispatchers.IO) | ||
|
|
||
| for ((name, assetData) in assetsData) { | ||
| val source = DataSourceResolver.resolve(assetData) ?: continue | ||
| scope.launch { | ||
| try { | ||
| val loader = source.createLoader() | ||
| val data = loader.load(source) | ||
| val type = inferAssetType(name, data) | ||
| registerAsset(data, name, type, riveWorker) | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to update asset '$name'", e) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private suspend fun registerAsset( | ||
| data: ByteArray, | ||
| name: String, | ||
| type: AssetType, | ||
| riveWorker: CommandQueue | ||
| ) { | ||
| Log.i(TAG, "Registering $type asset '$name' (${data.size} bytes)") | ||
| when (type) { | ||
| AssetType.IMAGE -> { | ||
| riveWorker.unregisterImage(name) | ||
| val result = ImageAsset.fromBytes(riveWorker, data) | ||
| if (result is app.rive.Result.Success) { | ||
| result.value.register(name) | ||
| Log.i(TAG, "Image '$name' registered") | ||
| } | ||
| } | ||
| AssetType.FONT -> { | ||
| riveWorker.unregisterFont(name) | ||
| val result = FontAsset.fromBytes(riveWorker, data) | ||
| if (result is app.rive.Result.Success) { | ||
| result.value.register(name) | ||
| Log.i(TAG, "Font '$name' registered") | ||
| } | ||
| } | ||
| AssetType.AUDIO -> { | ||
| riveWorker.unregisterAudio(name) | ||
| val result = AudioAsset.fromBytes(riveWorker, data) | ||
| if (result is app.rive.Result.Success) { | ||
| result.value.register(name) | ||
| Log.i(TAG, "Audio '$name' registered") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun inferAssetType(name: String, data: ByteArray): AssetType { | ||
| val ext = name.substringAfterLast('.', "").lowercase() | ||
| return when (ext) { | ||
| "png", "jpg", "jpeg", "webp", "gif", "bmp", "svg" -> AssetType.IMAGE | ||
| "ttf", "otf", "woff", "woff2" -> AssetType.FONT | ||
| "wav", "mp3", "ogg", "flac", "aac", "m4a" -> AssetType.AUDIO | ||
| else -> inferFromMagicBytes(data) | ||
| } | ||
| } | ||
|
|
||
| private fun inferFromMagicBytes(data: ByteArray): AssetType { | ||
| if (data.size < 4) return AssetType.IMAGE | ||
|
|
||
| // PNG: 89 50 4E 47 | ||
| if (data[0] == 0x89.toByte() && data[1] == 0x50.toByte() && | ||
| data[2] == 0x4E.toByte() && data[3] == 0x47.toByte()) return AssetType.IMAGE | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:if-else-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:multiline-if-else reported by reviewdog 🐶 |
||
| // JPEG: FF D8 FF | ||
| if (data[0] == 0xFF.toByte() && data[1] == 0xD8.toByte() && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶 |
||
| data[2] == 0xFF.toByte()) return AssetType.IMAGE | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:if-else-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:multiline-if-else reported by reviewdog 🐶 |
||
| // RIFF container: WebP (RIFF....WEBP) or WAV (RIFF....WAVE) | ||
| if (data[0] == 0x52.toByte() && data[1] == 0x49.toByte() && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶 |
||
| data[2] == 0x46.toByte() && data[3] == 0x46.toByte()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:wrapping reported by reviewdog 🐶 |
||
| if (data.size >= 12 && | ||
| data[8] == 0x57.toByte() && data[9] == 0x41.toByte() && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶 |
||
| data[10] == 0x56.toByte() && data[11] == 0x45.toByte()) return AssetType.AUDIO // "WAVE" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:if-else-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:multiline-if-else reported by reviewdog 🐶 |
||
| return AssetType.IMAGE // assume WebP for other RIFF | ||
| } | ||
| // ID3 (MP3): 49 44 33 | ||
| if (data[0] == 0x49.toByte() && data[1] == 0x44.toByte() && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶 |
||
| data[2] == 0x33.toByte()) return AssetType.AUDIO | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:if-else-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:multiline-if-else reported by reviewdog 🐶 |
||
| // TrueType: 00 01 00 00 | ||
| if (data[0] == 0x00.toByte() && data[1] == 0x01.toByte() && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶 |
||
| data[2] == 0x00.toByte() && data[3] == 0x00.toByte()) return AssetType.FONT | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:if-else-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:multiline-if-else reported by reviewdog 🐶 |
||
| // OpenType: 4F 54 54 4F ("OTTO") | ||
| if (data[0] == 0x4F.toByte() && data[1] == 0x54.toByte() && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶 |
||
| data[2] == 0x54.toByte() && data[3] == 0x4F.toByte()) return AssetType.FONT | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:if-else-wrapping reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [ktlint] standard:multiline-if-else reported by reviewdog 🐶 |
||
|
|
||
| return AssetType.IMAGE | ||
| } | ||
|
|
||
| enum class AssetType { IMAGE, FONT, AUDIO } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [ktlint] standard:condition-wrapping reported by reviewdog 🐶
Newline expected before operand in multiline condition