Skip to content

Commit

Permalink
Fix everything
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Oct 17, 2024
1 parent ea27cf2 commit 0b3065a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ loader_version=0.16.7
fabric_version=0.106.0+1.21.1
fabric_kotlin_version=1.10.20+kotlin.1.9.24
kache_version=1.0.5
inject_version=1.1
inject_version=1.2.1

# Mod Properties
mod_version=2.12
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
package dev.andante.audience.resource

import io.netty.channel.ChannelHandlerContext
import net.mcbrawls.inject.InjectorContext
import net.mcbrawls.inject.http.HttpByteBuf
import net.mcbrawls.inject.http.HttpRequest
import net.mcbrawls.inject.http.HttpInjector
import net.mcbrawls.inject.http.HttpRequest
import net.mcbrawls.inject.http.httpBuffer

object ResourcePackHandler : HttpInjector() {
private val resourcePacks: MutableMap<String, ByteArray> = mutableMapOf()

private val SHA1_REGEX = Regex("^[a-fA-F0-9]{40}$")

override fun isRelevant(ctx: InjectorContext, request: HttpRequest): Boolean = true

override fun intercept(ctx: ChannelHandlerContext, request: HttpRequest): HttpByteBuf {
val response = ctx.httpBuffer()

val path = request.requestURI.removePrefix("/")

if (path.isEmpty()) {
return response
}

if (!path.matches(SHA1_REGEX)) {
return response
}

val pack = resourcePacks[path] ?: return response
response.writeStatusLine("1.1", 200, "OK")

response.writeStatusLine("1.1", 200, "OK")
response.writeHeader("Content-Type", "application/zip")
response.writeBytes(pack)

Expand All @@ -39,8 +25,10 @@ object ResourcePackHandler : HttpInjector() {
/**
* Adds a resource pack to be served.
*/
fun add(pack: ByteResourcePack) {
resourcePacks[pack.hash] = pack.bytes
fun add(vararg packs: ByteResourcePack) {
packs.forEach { pack ->
resourcePacks[pack.hash] = pack.bytes
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/test/kotlin/dev/andante/audience/test/AudienceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import com.mojang.serialization.JsonOps
import dev.andante.audience.Audience
import dev.andante.audience.player.PlayerSet
import dev.andante.audience.player.StandalonePlayerReference
import dev.andante.audience.resource.ByteResourcePack
import dev.andante.audience.resource.ResourcePackHandler
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
import org.slf4j.LoggerFactory
import kotlin.io.path.Path
import kotlin.io.path.readBytes
import kotlin.math.sin
import kotlin.time.measureTimedValue

object AudienceTest : ModInitializer {
private val logger = LoggerFactory.getLogger("Audience Test")

val resourcePackBytes = Path("resources.zip").readBytes()
val resourcePackBytesTwo = Path("resources2.zip").readBytes()

override fun onInitialize() {
logger.info("Initializing")

Expand Down Expand Up @@ -46,5 +53,12 @@ object AudienceTest : ModInitializer {

val measuredApi = measureTimedValue(inputReference::getMojangApiPlayerName)
println("Took ${measuredApi.duration.inWholeMilliseconds} ms to fetch \"${measuredApi.value}\"")

val packOne = ByteResourcePack(resourcePackBytes)
val packTwo = ByteResourcePack(resourcePackBytesTwo)
ResourcePackHandler.add(packOne)
ResourcePackHandler.add(packTwo)
println(packOne.hash)
println(packTwo.hash)
}
}

0 comments on commit 0b3065a

Please sign in to comment.