<commit_before>package rs.emulate.editor.legacy.config.npc

import org.joml.Vector3f
import rs.emulate.editor.core.content.capabilities.ResourceViewerSupport
import rs.emulate.editor.core.workbench.viewer.ResourceView
import rs.emulate.editor.core.workbench.viewer.component.SceneComponent
import rs.emulate.editor.vfs.LegacyFileId
import rs.emulate.editor.vfs.LegacyFileLoader
import rs.emulate.editor.vfs.VirtualFileLoader
import rs.emulate.legacy.config.npc.NpcDefinitionDecoder
import rs.emulate.legacy.model.Face
import rs.emulate.legacy.model.Model
import rs.emulate.legacy.model.ModelDecoder
import rs.emulate.legacy.model.Vertex
import rs.emulate.scene3d.Mesh
import rs.emulate.util.compression.gunzip
import java.awt.Color

class NpcViewerSupport : ResourceViewerSupport<LegacyFileId.ConfigEntry> {

    override fun createViewer(
        id: LegacyFileId.ConfigEntry,
        loader: VirtualFileLoader<LegacyFileId.ConfigEntry>
    ): ResourceView {
        loader as LegacyFileLoader // TODO yuck

        val npcdef = loader.load(id)!!
        val definition = NpcDefinitionDecoder.decode(id.file, npcdef)
        val models = definition.models.map {
            val buffer = loader.load(LegacyFileId.FileEntry(1, it))!!
            ModelDecoder(buffer.gunzip()).decode()
        }

        val mesh = meshFromModels(models)

        return object : ResourceView() {
            override val root = SceneComponent()

            override fun onFocusGained() {
                root.activeProperty.set(true)
            }

            override fun onFocusLost() {
                root.activeProperty.set(false)
            }

            init {
                root.addChild(mesh)
            }
        }
    }

    // TODO temporary
    private fun Vertex.toVec3f() = Vector3f(x * 0.01f, -y * 0.01f, z * 0.01f)

    private fun meshFromModels(models: List<Model>): Mesh {
        val positions = mutableListOf<Vector3f>()
        val colors = mutableListOf<Vector3f>()

        models.forEach { (_, faces, vertices) ->
            val sorted = faces.sortedByDescending(Face::renderPriority)

            positions += sorted.flatMap {
                listOf(
                    vertices[it.a].toVec3f(),
                    vertices[it.b].toVec3f(),<commit_msg>Add hack to center models onscreen<commit_after>package rs.emulate.editor.legacy.config.npc

import org.joml.Vector3f
import rs.emulate.editor.core.content.capabilities.ResourceViewerSupport
import rs.emulate.editor.core.workbench.viewer.ResourceView
import rs.emulate.editor.core.workbench.viewer.component.SceneComponent
import rs.emulate.editor.vfs.LegacyFileId
import rs.emulate.editor.vfs.LegacyFileLoader
import rs.emulate.editor.vfs.VirtualFileLoader
import rs.emulate.legacy.config.npc.NpcDefinitionDecoder
import rs.emulate.legacy.model.Face
import rs.emulate.legacy.model.Model
import rs.emulate.legacy.model.ModelDecoder
import rs.emulate.legacy.model.Vertex
import rs.emulate.scene3d.Mesh
import rs.emulate.util.compression.gunzip
import java.awt.Color

class NpcViewerSupport : ResourceViewerSupport<LegacyFileId.ConfigEntry> {

    override fun createViewer(
        id: LegacyFileId.ConfigEntry,
        loader: VirtualFileLoader<LegacyFileId.ConfigEntry>
    ): ResourceView {
        loader as LegacyFileLoader // TODO yuck

        val npcdef = loader.load(id)!!
        val definition = NpcDefinitionDecoder.decode(id.file, npcdef)
        val models = definition.models.map {
            val buffer = loader.load(LegacyFileId.FileEntry(1, it))!!
            ModelDecoder(buffer.gunzip()).decode()
        }

        val mesh = meshFromModels(models)

        return object : ResourceView() {
            override val root = SceneComponent()

            override fun onFocusGained() {
                root.activeProperty.set(true)
            }

            override fun onFocusLost() {
                root.activeProperty.set(false)
            }

            init {
                root.addChild(mesh)
            }
        }
    }

    // TODO temporary
    private fun Vertex.toVec3f() = Vector3f(-x * 0.01f, -y * 0.01f - 3f, z * 0.01f)

    private fun meshFromModels(models: List<Model>): Mesh {
        val positions = mutableListOf<Vector3f>()
        val colors = mutableListOf<Vector3f>()

        models.forEach { (_, faces, vertices) ->
            val sorted = faces.sortedByDescending(Face::renderPriority)

            positions += sorted.flatMap {
                listOf(
                    vertices[it.a].toVec3f(),
                    vertices[it.b].toVec3f(),
{'fraction_non_alphanumeric': 0.09844672938087946, 'fraction_numerical': 0.008094508860205645, 'mean_word_length': 2.5943396226415096, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '985389', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1339, 'n_words': 237}