Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.gradle/
.vscode/
build/
g=out/
gen/
Expand Down
140 changes: 140 additions & 0 deletions cache/bin/main/dev/openrune/ServerCacheManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package dev.openrune

import dev.openrune.OsrsCacheProvider.*
import dev.openrune.cache.CacheManager
import dev.openrune.cache.filestore.definition.ComponentDecoder
import dev.openrune.cache.filestore.definition.FontDecoder
import dev.openrune.cache.filestore.definition.InterfaceType
import dev.openrune.filesystem.Cache
import java.nio.BufferUnderflowException
import dev.openrune.cache.getOrDefault
import dev.openrune.definition.type.*
import dev.openrune.codec.osrs.ItemDecoder
import dev.openrune.codec.osrs.ObjectDecoder
import dev.openrune.codec.osrs.HealthBarDecoder
import dev.openrune.codec.osrs.InventoryDecoder
import dev.openrune.codec.osrs.NpcDecoder
import dev.openrune.codec.osrs.SequenceDecoder
import dev.openrune.codec.osrs.impl.InventoryServerCodec
import dev.openrune.definition.type.widget.ComponentType
import dev.openrune.server.impl.item.ItemRenderDataManager
import dev.openrune.types.*
import io.github.oshai.kotlinlogging.KotlinLogging
import org.alter.rscm.RSCM.asRSCM
import java.nio.file.Path

object ServerCacheManager {

private val items: MutableMap<Int, ItemServerType> = mutableMapOf()
private val npcs: MutableMap<Int, NpcServerType> = mutableMapOf()
private val objects: MutableMap<Int, ObjectServerType> = mutableMapOf()
private val healthBars: MutableMap<Int, HealthBarServerType> = mutableMapOf()
private val structs: MutableMap<Int, StructType> = mutableMapOf()
private val dbrows: MutableMap<Int, DBRowType> = mutableMapOf()
private val dbtables: MutableMap<Int, DBTableType> = mutableMapOf()
private val enums: MutableMap<Int, EnumType> = mutableMapOf()
private val varbits: MutableMap<Int, VarBitType> = mutableMapOf()
private val varps: MutableMap<Int, VarpType> = mutableMapOf()
private val sequences = mutableMapOf<Int, SequenceServerType>()
private var fonts = mutableMapOf<Int, FontType>()
private var interfaces = mutableMapOf<Int, InterfaceType>()
private var inv = mutableMapOf<Int, InventoryServerType>()

val logger = KotlinLogging.logger {}

fun init(cachePath : Path, rev : Int) {
val cache = Cache.load(cachePath)
init(cache,rev)
}

fun init(cache : Cache, rev : Int) {
ItemRenderDataManager.init()
CacheManager.init(OsrsCacheProvider(cache,rev))

fonts = FontDecoder(cache).loadAllFonts()

try {
EnumDecoder().load(cache, enums)
ObjectDecoder().load(cache, objects)
HealthBarDecoder().load(cache, healthBars)
NpcDecoder().load(cache, npcs)
ItemDecoder().load(cache, items)
InventoryDecoder().load(cache,inv)
SequenceDecoder().load(cache,sequences)
VarBitDecoder().load(cache, varbits)
VarDecoder().load(cache, varps)
StructDecoder().load(cache, structs)
DBRowDecoder().load(cache, dbrows)
DBTableDecoder().load(cache, dbtables)
ComponentDecoder(cache).load(interfaces)
} catch (e: BufferUnderflowException) {
logger.error(e) { "Error reading definitions" }
throw e
}
}

fun getNpc(id: Int) = npcs[id]
fun getFont(id: Int) = fonts[id]
fun getObject(id: Int) = objects[id]
fun getItem(id: Int) = items[id]
fun getVarbit(id: Int) = varbits[id]
fun getVarp(id: Int) = varps[id]
fun getAnim(id: Int) = sequences[id]
fun getEnum(id: Int) = enums[id]
fun getHealthBar(id: Int) = healthBars[id]
fun getStruct(id: Int) = structs[id]
fun getDbrow(id: Int) = dbrows[id]
fun getDbtable(id: Int) = dbtables[id]
fun getInventory(id: Int) = inv[id]

fun getNpcOrDefault(id: Int) = getOrDefault(npcs, id, NpcServerType(), "Npc")
fun getObjectOrDefault(id: Int) = getOrDefault(objects, id, ObjectServerType(), "Object")

fun getItemOrDefault(id: Int) = getOrDefault(items, id, ItemServerType(), "Item")
fun getVarbitOrDefault(id: Int) = getOrDefault(varbits, id, VarBitType(), "Varbit")
fun getVarpOrDefault(id: Int) = getOrDefault(varps, id, VarpType(), "Varp")
fun getEnumOrDefault(id: Int) = getOrDefault(enums, id, EnumType(), "Enum")
fun getHealthBarOrDefault(id: Int) = getOrDefault(healthBars, id, HealthBarServerType(), "HealthBar")
fun getStructOrDefault(id: Int) = getOrDefault(structs, id, StructType(), "Struct")
fun getDbrowOrDefault(id: Int) = getOrDefault(dbrows, id, DBRowType(), "DBRow")
fun getDbtableOrDefault(id: Int) = getOrDefault(dbtables, id, DBTableType(), "DBTable")

// Size methods
fun npcSize() = npcs.size
fun objectSize() = objects.size
fun itemSize() = items.size
fun varbitSize() = varbits.size
fun varpSize() = varps.size
fun enumSize() = enums.size
fun healthBarSize() = healthBars.size
fun structSize() = structs.size
fun animSize() = sequences.size

// Bulk getters
fun getNpcs() = npcs.toMap()
fun getObjects() = objects.toMap()
fun getItems() = items.toMap()
fun getVarbits() = varbits.toMap()
fun getVarps() = varps.toMap()
fun getEnums() = enums.toMap()
fun getHealthBars() = healthBars.toMap()
fun getStructs() = structs.toMap()
fun getAnims() = sequences.toMap()
fun getRows() = dbrows.toMap()

fun fromInterface(id: Int): InterfaceType =
interfaces[id] ?: error("Interface $id not found")

fun fromInterface(id: String): InterfaceType =
fromInterface(id.asRSCM())

fun fromComponent(packed: Int): ComponentType {
val interfaceId = packed ushr 16
val childId = packed and 0xFFFF
return fromInterface(interfaceId).components[childId]
?: error("Component $childId not found in interface $interfaceId")
}

fun fromComponent(id: String) = fromComponent(id.asRSCM())

}
12 changes: 12 additions & 0 deletions cache/bin/main/dev/openrune/codec/osrs/CodecsOsrs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.openrune.codec.osrs

import dev.openrune.cache.filestore.definition.ConfigDefinitionDecoder
import dev.openrune.codec.osrs.impl.*
import dev.openrune.types.*

class ObjectDecoder : ConfigDefinitionDecoder<ObjectServerType>(ObjectServerCodec(), 55)
class HealthBarDecoder : ConfigDefinitionDecoder<HealthBarServerType>(HealthBarServerCodec(), 56)
class SequenceDecoder : ConfigDefinitionDecoder<SequenceServerType>(SequenceServerCodec(), 57)
class NpcDecoder : ConfigDefinitionDecoder<NpcServerType>(NpcServerCodec(), 58)
class ItemDecoder : ConfigDefinitionDecoder<ItemServerType>(ItemServerCodec(), 59)
class InventoryDecoder : ConfigDefinitionDecoder<InventoryServerType>(InventoryServerCodec(), 60)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.openrune.codec.osrs.impl

import dev.openrune.definition.opcode.DefinitionOpcode
import dev.openrune.definition.opcode.OpcodeDefinitionCodec
import dev.openrune.definition.opcode.OpcodeList
import dev.openrune.definition.opcode.OpcodeType
import dev.openrune.definition.type.HealthBarType
import dev.openrune.types.HealthBarServerType


class HealthBarServerCodec(val health: Map<Int, HealthBarType>? = null) : OpcodeDefinitionCodec<HealthBarServerType>() {

override val definitionCodec = OpcodeList<HealthBarServerType>().apply {
add(DefinitionOpcode(1, OpcodeType.BYTE, HealthBarServerType::width))
}

override fun HealthBarServerType.createData() {
if (health == null) return

val health = health[id]?: return

width = health.width
}

override fun createDefinition(): HealthBarServerType = HealthBarServerType()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.openrune.codec.osrs.impl

import dev.openrune.definition.opcode.DefinitionOpcode
import dev.openrune.definition.opcode.OpcodeDefinitionCodec
import dev.openrune.definition.opcode.OpcodeList
import dev.openrune.definition.opcode.OpcodeType
import dev.openrune.definition.opcode.OpcodeType.BOOLEAN.enumType
import dev.openrune.definition.opcode.propertyChain
import dev.openrune.definition.type.InventoryType
import dev.openrune.server.impl.item.WeaponTypes
import dev.openrune.types.HealthBarServerType
import dev.openrune.types.InvScope
import dev.openrune.types.InvStackType
import dev.openrune.types.InvStock
import dev.openrune.types.InventoryServerType
import dev.openrune.types.InventoryServerType.Companion.pack
import dev.openrune.types.ItemServerType
import dev.openrune.types.Weapon


class InventoryServerCodec(
val types: Map<Int, InventoryType>? = null,
val custom : Map<Int, InventoryServerType>? = emptyMap()

) : OpcodeDefinitionCodec<InventoryServerType>() {

override val definitionCodec = OpcodeList<InventoryServerType>().apply {
add(DefinitionOpcode(1, OpcodeType.USHORT, InventoryServerType::size))
add(DefinitionOpcode(2, OpcodeType.USHORT, InventoryServerType::flags))
add(DefinitionOpcode(3, enumType<InvStackType>(), InventoryServerType::stack))
add(DefinitionOpcode(4, enumType<InvScope>(), InventoryServerType::scope))
}

override fun InventoryServerType.createData() {
if (types == null) return
val inventoryType = types[id]?: return
size = inventoryType.size
val customData = custom!![id]

if (customData != null) {
scope = customData.scope
stack = customData.stack
flags = customData.flags
}

}


override fun createDefinition(): InventoryServerType = InventoryServerType()

}
Loading