Skip to main content
 Warning: Beta Version
Version: Beta ⚠️

Asset & Artifact Entries

Assets and artifacts are both handled through similar interfaces that extend StaticEntry. Assets are static files like images, sounds, or other external resources, while artifacts are assets that are generated by the server at runtime.

AssetEntry

The AssetEntry is primarily used for handling static assets. Assets can include various types of files such as images, data, or other external resources that can be used by an extension.

ArtifactEntry

The ArtifactEntry is a specialized interface derived from AssetEntry. Its primary purpose is to handle artifacts, which are assets generated by the extensions. Unlike standard assets, artifacts are usually dynamic and created during runtime and stored between restarts. This makes them particularly useful for storing data arbitrary configuration data. As artifacts loaded in on reload, they are not suitable for storing real-time data.

Usage

Defining an AssetEntry

ExampleAssetEntry.kt
@Entry("example_asset", "An example asset entry.", Colors.BLUE, "material-symbols:home-storage-rounded")
class ExampleAssetEntry(
override val id: String = "",
override val name: String = "",
override val path: String = "",
) : AssetEntry

Defining an ArtifactEntry

ExampleArtifactEntry.kt
@Entry("example_artifact", "An example artifact entry.", Colors.BLUE, "material-symbols:home-storage-rounded")
class ExampleArtifactEntry(
override val id: String = "",
override val name: String = "",
override val artifactId: String = "",
) : ArtifactEntry

Accessing Asset & Artifact Content

Using AssetManager (Traditional Approach)

String Content

ExampleAssetEntry.kt
suspend fun accessAssetData(ref: Ref<out AssetEntry>) {
val assetManager = KoinJavaComponent.get<AssetManager>(AssetManager::class.java)
val entry = ref.get() ?: return
val stringContent: String? = assetManager.fetchStringAsset(entry)
// Do something with the string content
}

Binary Content

ExampleAssetEntry.kt
suspend fun accessBinaryAssetData(ref: Ref<out AssetEntry>) {
val assetManager = KoinJavaComponent.get<AssetManager>(AssetManager::class.java)
val entry = ref.get() ?: return
val binaryContent: ByteArray? = assetManager.fetchBinaryAsset(entry)
// Do something with the binary content
}

Both AssetEntry and ArtifactEntry also provide convenient helper methods for accessing and storing data:

ExampleAssetEntry.kt
suspend fun accessAssetDataUsingHelpers(ref: Ref<out AssetEntry>) {
val entry = ref.get() ?: return

// Check if asset has data
if (entry.hasData()) {
// Access string data using helper method
val stringContent: String? = entry.stringData()

// Access binary data using helper method
val binaryContent: ByteArray? = entry.binaryData()
}
}

The same helper methods work for artifacts:

ExampleArtifactEntry.kt
suspend fun accessArtifactDataUsingHelpers(ref: Ref<out ArtifactEntry>) {
val entry = ref.get() ?: return

// Check if artifact has data
if (entry.hasData()) {
// Access string data using helper method
val stringContent: String? = entry.stringData()

// Access binary data using helper method
val binaryContent: ByteArray? = entry.binaryData()

// Store string data
entry.stringData("Updated artifact content")

// Store binary data
entry.binaryData(byteArrayOf(1, 2, 3, 4, 5))
}
}