Skip to content

Commit

Permalink
Supports resource pack obfuscation.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Dec 15, 2024
1 parent 5832266 commit 8ca91d2
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ public interface ConfigManager {
CoreShadersOption shaders();
boolean useCoreShaders();
boolean showMeHealthBar();
boolean resourcePackObfuscation();
}
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allprojects {
apply(plugin = "kotlin")
apply(plugin = "org.jetbrains.dokka")
group = "kr.toxicity.healthbar"
version = "3.7.2"
version = "3.7.3"
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
Expand Down Expand Up @@ -79,8 +79,8 @@ val dist = getApiDependencyProject("dist").spigot()
.dependency("io.github.arcaneplugins:levelledmobs-plugin:4.0.3.1")
.dependency("me.clip:placeholderapi:2.11.6")
.dependency("com.alessiodp.parties:parties-bukkit:3.2.16")
.dependency("io.github.toxicity188:BetterHud-standard-api:1.10.1")
.dependency("io.github.toxicity188:BetterHud-bukkit-api:1.10.1")
.dependency("io.github.toxicity188:BetterHud-standard-api:1.10.3")
.dependency("io.github.toxicity188:BetterHud-bukkit-api:1.10.3")
.dependency("net.citizensnpcs:citizens-main:2.0.35-SNAPSHOT")
.dependency("com.github.SkriptLang:Skript:2.9.5")
.dependency("com.nexomc:nexo:0.4.0")
Expand Down Expand Up @@ -165,7 +165,7 @@ tasks {
version("1.21.1") //TODO set this to 'minecraft' when other plugins support the latest version.
pluginJars(fileTree("plugins"))
downloadPlugins {
hangar("BetterHud", "1.10.1")
hangar("BetterHud", "1.10.3")
hangar("PlaceholderAPI", "2.11.6")
hangar("Skript", "2.9.5")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class BetterHealthBarImpl : BetterHealthBar() {
private val scheduler = if (isFolia) FoliaScheduler() else StandardScheduler()

private val managers = listOf(
EncodeManager,
CompatibilityManager,
ConfigManagerImpl,
ListenerManagerImpl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kr.toxicity.healthbar.api.layout.ImageLayout
import kr.toxicity.healthbar.api.listener.HealthBarListener
import kr.toxicity.healthbar.api.renderer.ImageRenderer
import kr.toxicity.healthbar.data.BitmapData
import kr.toxicity.healthbar.manager.EncodeManager
import kr.toxicity.healthbar.manager.ImageManagerImpl
import kr.toxicity.healthbar.manager.ListenerManagerImpl
import kr.toxicity.healthbar.pack.PackResource
Expand Down Expand Up @@ -41,7 +42,7 @@ class ImageLayoutImpl(
val componentMap = HashMap<BitmapData, WidthComponent>()
image.images().forEach {
val list = ArrayList<PixelComponent>()
val dir = "${parent.name}/image/${layer()}/${it.name}"
val dir = "${parent.name}/image/${layer()}/${it.name}".encodeFile(EncodeManager.EncodeNamespace.TEXTURES)
resource.textures.add(dir) {
it.image.image.withOpacity(layer()).toByteArray()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kr.toxicity.healthbar.api.layout.ImageLayout
import kr.toxicity.healthbar.api.layout.LayoutGroup
import kr.toxicity.healthbar.api.layout.TextLayout
import kr.toxicity.healthbar.manager.ConfigManagerImpl
import kr.toxicity.healthbar.manager.EncodeManager
import kr.toxicity.healthbar.pack.PackResource
import kr.toxicity.healthbar.util.*
import net.kyori.adventure.key.Key
Expand All @@ -21,7 +22,8 @@ class LayoutGroupImpl(

var index = ADVENTURE_START_INT

private val imageKey = Key.key(NAMESPACE, "$name/images")
private val encodedName = encodeKey(EncodeManager.EncodeNamespace.FONT, "$name/images")
private val imageKey = createAdventureKey(encodedName)
private val group = section.getString("group")

private var i = 0
Expand Down Expand Up @@ -68,7 +70,7 @@ class LayoutGroupImpl(
images.forEach {
it.build(resource, min, count, json)
}
resource.font.add("$name/images.json") {
resource.font.add("$encodedName.json") {
JsonObject().apply {
add("providers", json)
}.save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kr.toxicity.healthbar.api.placeholder.PlaceholderOption
import kr.toxicity.healthbar.api.renderer.TextRenderer
import kr.toxicity.healthbar.api.text.TextAlign
import kr.toxicity.healthbar.data.BitmapData
import kr.toxicity.healthbar.manager.EncodeManager
import kr.toxicity.healthbar.manager.TextManagerImpl
import kr.toxicity.healthbar.pack.PackResource
import kr.toxicity.healthbar.util.*
Expand Down Expand Up @@ -70,7 +71,7 @@ class TextLayoutImpl(
val dataList = ArrayList<JsonData>()
val fileParent = "${parent.name}/text/${layer()}"
text.bitmap().forEachIndexed { index, textBitmap ->
val fileName = "$fileParent/${index + 1}.png"
val fileName = "${encodeKey(EncodeManager.EncodeNamespace.TEXTURES, "$fileParent/${index + 1}")}.png"
dataList.add(JsonData(
"$NAMESPACE:$fileName",
textBitmap.array
Expand All @@ -83,7 +84,7 @@ class TextLayoutImpl(
val map = HashMap<BitmapData, WidthKey>()
for (i in 0..<count) {
val y = y() + groupY() * i + max
val keyName = "${parent.name}/$name/${i + 1}"
val keyName = encodeKey(EncodeManager.EncodeNamespace.FONT, "${parent.name}/$name/${i + 1}")
keys.add(map.computeIfAbsent(BitmapData(keyName, y, height)) {
resource.font.add("$keyName.json") {
JsonObject().apply {
Expand All @@ -106,7 +107,7 @@ class TextLayoutImpl(
})
}.save()
}
WidthKey(Key.key(NAMESPACE, keyName), x() + groupX() * i)
WidthKey(createAdventureKey(keyName), x() + groupX() * i)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ object ConfigManagerImpl : ConfigManager, BetterHealthBerManager {
private var shaders = CoreShadersOption.DEFAULT
private var useCoreShaders = true
private var showMeHealthBar = true
private var resourcePackObfuscation = false

private var bstats: Metrics? = null

override fun preReload() {
runWithHandleException("Unable to load config.yml") {
val config = PluginConfiguration.CONFIG.create()
debug = config.getBoolean("debug")
resourcePackObfuscation = config.getBoolean("resource-pack-obfuscation", false)

packType = if (CompatibilityManager.usePackTypeNone) PackType.NONE else config.getString("pack-type")?.let {
runCatching {
PackType.valueOf(it.uppercase())
Expand Down Expand Up @@ -125,4 +128,5 @@ object ConfigManagerImpl : ConfigManager, BetterHealthBerManager {
override fun shaders(): CoreShadersOption = shaders
override fun useCoreShaders(): Boolean = useCoreShaders
override fun showMeHealthBar(): Boolean = showMeHealthBar
override fun resourcePackObfuscation(): Boolean = resourcePackObfuscation
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package kr.toxicity.healthbar.manager

import kr.toxicity.healthbar.pack.PackResource
import java.util.*

object EncodeManager : BetterHealthBerManager {

private val encodeMap = EnumMap<EncodeNamespace, MutableMap<String, Int>>(EncodeNamespace::class.java)

override fun reload(resource: PackResource) {

}

override fun postReload() {
encodeMap.clear()
}
enum class EncodeNamespace {
FONT,
TEXTURES
}
fun generateKey(namespace: EncodeNamespace, name: String): String {
val map = synchronized(encodeMap) {
encodeMap.computeIfAbsent(namespace) {
mutableMapOf()
}
}
return synchronized(map) {
map.computeIfAbsent(name) {
map.size
}.encode()
}
}

private fun Int.encode(): String {
var i = this
val size = chars.size
return buildString {
while (i >= size) {
append(chars.first())
i -= size
}
append(chars[i])
}
}

private val chars = listOf(
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'm', 'n', 'l', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kr.toxicity.healthbar.api.healthbar.HealthBar
import kr.toxicity.healthbar.api.manager.HealthBarManager
import kr.toxicity.healthbar.healthbar.HealthBarImpl
import kr.toxicity.healthbar.pack.PackResource
import kr.toxicity.healthbar.util.forEachAllYamlAsync
import kr.toxicity.healthbar.util.forEachAllYaml
import kr.toxicity.healthbar.util.putSync
import kr.toxicity.healthbar.util.runWithHandleException
import kr.toxicity.healthbar.util.subFolder
Expand All @@ -28,7 +28,7 @@ object HealthBarManagerImpl : HealthBarManager, BetterHealthBerManager {
override fun reload(resource: PackResource) {
healthBarMap.clear()
uuidSet.clear()
resource.dataFolder.subFolder("healthbars").forEachAllYamlAsync { file, s, section ->
resource.dataFolder.subFolder("healthbars").forEachAllYaml { file, s, section ->
runWithHandleException("Unable to load this health bar: $s in ${file.path}") {
var uuid = UUID.randomUUID()
while (!uuidSet.add(uuid)) uuid = UUID.randomUUID()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object ImageManagerImpl : ImageManager, BetterHealthBerManager {
override fun reload(resource: PackResource) {
imageMap.clear()
val images = resource.dataFolder.subFolder("assets")
resource.dataFolder.subFolder("images").forEachAllYamlAsync { file, s, section ->
resource.dataFolder.subFolder("images").forEachAllYaml { file, s, section ->
runWithHandleException("Unable to load this image: $s in ${file.path}") {
val typeValue = section.getString("type").ifNull("Unable to find 'type' configuration.")
val image = when (val type = ImageType.valueOf(typeValue.uppercase())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kr.toxicity.healthbar.api.layout.LayoutGroup
import kr.toxicity.healthbar.api.manager.LayoutManager
import kr.toxicity.healthbar.layout.LayoutGroupImpl
import kr.toxicity.healthbar.pack.PackResource
import kr.toxicity.healthbar.util.forEachAllYamlAsync
import kr.toxicity.healthbar.util.forEachAllYaml
import kr.toxicity.healthbar.util.putSync
import kr.toxicity.healthbar.util.runWithHandleException
import kr.toxicity.healthbar.util.subFolder
Expand All @@ -29,7 +29,7 @@ object LayoutManagerImpl : LayoutManager, BetterHealthBerManager {
layoutMap.clear()
groupData.clear()

resource.dataFolder.subFolder("layouts").forEachAllYamlAsync { file, s, configurationSection ->
resource.dataFolder.subFolder("layouts").forEachAllYaml { file, s, configurationSection ->
runWithHandleException("Unable to load this layout: $s in ${file.path}") {
val layout = LayoutGroupImpl(
file.path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kr.toxicity.healthbar.api.mob.MobProvider
import kr.toxicity.healthbar.entity.HealthBarEntityImpl
import kr.toxicity.healthbar.mob.MobConfigurationImpl
import kr.toxicity.healthbar.pack.PackResource
import kr.toxicity.healthbar.util.forEachAllYamlAsync
import kr.toxicity.healthbar.util.forEachAllYaml
import kr.toxicity.healthbar.util.putSync
import kr.toxicity.healthbar.util.runWithHandleException
import kr.toxicity.healthbar.util.subFolder
Expand All @@ -25,7 +25,7 @@ object MobManagerImpl : BetterHealthBerManager, MobManager {

override fun reload(resource: PackResource) {
mobConfigurationMap.clear()
resource.dataFolder.subFolder("mobs").forEachAllYamlAsync { file, s, configurationSection ->
resource.dataFolder.subFolder("mobs").forEachAllYaml { file, s, configurationSection ->
runWithHandleException("Unable to load mob: $s in ${file.path}") {
val config = MobConfigurationImpl(file.path, configurationSection)
mobConfigurationMap.putSync("mob", s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ object TextManagerImpl : TextManager, BetterHealthBerManager {
textMap.clear()
textMap["default"] = default
val fonts = resource.dataFolder.subFolder("fonts")
resource.dataFolder.subFolder("texts").forEachAllYamlAsync { file, s, configurationSection ->
resource.dataFolder.subFolder("texts").forEachAllYaml { file, s, configurationSection ->
runWithHandleException("Unable to read this text: $s in ${file.path}") {
val font = Font.createFont(Font.TRUETYPE_FONT, File(fonts, configurationSection.getString("file").ifNull("Unable to find 'file' configuration.").replace('/', File.separatorChar)).apply {
if (!exists()) throw RuntimeException("Unable to find this font: $path")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kr.toxicity.healthbar.util
import kr.toxicity.healthbar.api.component.PixelComponent
import kr.toxicity.healthbar.api.component.WidthComponent
import kr.toxicity.healthbar.manager.ConfigManagerImpl
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.TextDecoration
import net.kyori.adventure.text.minimessage.MiniMessage
Expand Down Expand Up @@ -42,7 +41,7 @@ val MINI_MESSAGE = MiniMessage.builder()
.build()

val SPACE_KEY
get() = Key.key(NAMESPACE, "space")
get() = createAdventureKey("space")

val NEGATIVE_ONE_SPACE_COMPONENT
get() = WidthComponent(0, Component.text().font(SPACE_KEY).content((ADVENTURE_START_INT - 1).parseChar()))
Expand Down
15 changes: 15 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/healthbar/util/Encodes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.toxicity.healthbar.util

import kr.toxicity.healthbar.manager.ConfigManagerImpl
import kr.toxicity.healthbar.manager.EncodeManager
import net.kyori.adventure.key.Key

fun String.encodeFile(path: EncodeManager.EncodeNamespace): String {
val split = split('.')
if (split.size != 2) throw RuntimeException("Invaild file name: $this")
return "${encodeKey(path, split[0])}.${split[1]}"
}

fun encodeKey(path: EncodeManager.EncodeNamespace, name: String) = if (ConfigManagerImpl.resourcePackObfuscation()) EncodeManager.generateKey(path, name) else name

fun createAdventureKey(path: String) = Key.key(NAMESPACE, path)
32 changes: 7 additions & 25 deletions dist/src/main/kotlin/kr/toxicity/healthbar/util/Files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun File.requireExists() = apply {
if (!exists()) throw RuntimeException("This file doesn't exist: $path")
}

fun File.forEachAllYamlAsync(block: (File, String, ConfigurationSection) -> Unit) {
fun File.forEachAllYaml(block: (File, String, ConfigurationSection) -> Unit) {
fun getAll(file: File): List<File> {
return if (file.isDirectory) {
file.listFiles()?.map { subFile ->
Expand All @@ -21,34 +21,16 @@ fun File.forEachAllYamlAsync(block: (File, String, ConfigurationSection) -> Unit
listOf(file)
}
}
val list = getAll(this).filter {
getAll(this).filter {
it.extension == "yml"
}.mapNotNull {
runCatching {
}.forEach {
runWithHandleException("Unable to load this yml file: ${it.name}") {
val yaml = it.toYaml()
val list = ArrayList<Pair<String, ConfigurationSection>>()
yaml.getKeys(false).forEach {
yaml.getConfigurationSection(it)?.let { section ->
list.add(it to section)
yaml.getKeys(false).forEach { key ->
yaml.getConfigurationSection(key)?.let { section ->
block(it, key, section)
}
}
if (list.isNotEmpty()) it to list else null
}.getOrElse { e ->
warn(
"Unable to load this yml file: ${it.name}",
"Reason: ${e.message}"
)
null
}
}
if (list.isEmpty()) return
list.map {
{
it.second.forEach { pair ->
block(it.first, pair.first, pair.second)
}
}
}.forEachAsync {
it()
}
}

0 comments on commit 8ca91d2

Please sign in to comment.