Skip to content

Commit

Permalink
Inject it into my veinssss
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Oct 17, 2024
1 parent a1f14b5 commit 045ddb3
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 166 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ def ENV = System.getenv()
version = mod_version
group = maven_group

repositories {
maven {
name = "Andante's Maven"
url = "https://maven.andante.dev/releases/"
}
}

dependencies {
minecraft "com.mojang:minecraft:$minecraft_version"
mappings "net.fabricmc:yarn:$minecraft_version+build.$yarn_build:v2"
Expand All @@ -19,6 +26,7 @@ dependencies {
modImplementation "net.fabricmc:fabric-language-kotlin:$fabric_kotlin_version"

include modImplementation ("com.github.yundom:kache:$kache_version")
include modImplementation ("net.mcbrawls:inject:$inject_version")
}

sourceSets {
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ 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.0

# Mod Properties
mod_version=2.11.1
mod_version=2.12
maven_group=dev.andante
3 changes: 3 additions & 0 deletions src/main/kotlin/dev/andante/audience/AudienceInitializer.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.andante.audience

import dev.andante.audience.resource.ResourcePackHandler
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.minecraft.server.MinecraftServer
Expand All @@ -15,5 +16,7 @@ object AudienceInitializer : ModInitializer {
override fun onInitialize() {
// register event to capture server
ServerLifecycleEvents.SERVER_STARTING.register { _minecraftServer = it }

ResourcePackHandler.register()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.google.common.hash.Hashing
/**
* A complete resource pack.
*/
data class ResourcePack(
data class ByteResourcePack(
/**
* All bytes composting the resource pack.
*/
Expand All @@ -20,7 +20,7 @@ data class ResourcePack(
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ResourcePack
other as ByteResourcePack

return hash == other.hash
}
Expand All @@ -33,7 +33,6 @@ data class ResourcePack(
/**
* Hashes a byte array to a sha1 string.
*/
@Suppress("DEPRECATION")
private fun hashSha1(bytes: ByteArray): String {
return Hashing.sha1().hashBytes(bytes).toString()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.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.matches(SHA1_REGEX)) {
return response
}

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

response.writeHeader("Content-Type", "application/zip")
response.writeBytes(pack)

return response
}

/**
* Adds a resource pack to be served.
*/
fun add(pack: ByteResourcePack) {
resourcePacks[pack.hash] = pack.bytes
}

/**
* Removes a resource pack from service.
* @return whether a pack was removed
*/
fun remove(pack: ByteResourcePack): Boolean {
return remove(pack.hash)
}

/**
* Removes a resource pack from service.
* @return whether a pack was removed
*/
fun remove(hash: String): Boolean {
return resourcePacks.remove(hash) != null
}
}

This file was deleted.

This file was deleted.

7 changes: 3 additions & 4 deletions src/test/kotlin/dev/andante/audience/test/AudienceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ 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.ResourcePack
import dev.andante.audience.resource.ResourcePackServer
import dev.andante.audience.resource.ByteResourcePack
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
Expand Down Expand Up @@ -41,7 +40,7 @@ object AudienceTest : ModInitializer {
throw exception
}

val resourcePack = ResourcePack(byteArray)
val resourcePack = ByteResourcePack(byteArray)

val otherByteArray = try {
Path.of("resources2.zip").readBytes()
Expand All @@ -50,7 +49,7 @@ object AudienceTest : ModInitializer {
throw exception
}

val otherResourcePack = ResourcePack(otherByteArray)
val otherResourcePack = ByteResourcePack(otherByteArray)

val resourcePackServer = ResourcePackServer("localhost", 25566).apply {
registerResourcePack(resourcePack)
Expand Down

0 comments on commit 045ddb3

Please sign in to comment.