Skip to content

Commit

Permalink
Pushing current stuff for a baseline.
Browse files Browse the repository at this point in the history
  • Loading branch information
Waterpicker committed Dec 2, 2024
1 parent 1b75f8e commit dd44a3b
Show file tree
Hide file tree
Showing 22 changed files with 914 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import dev.architectury.event.events.client.ClientReloadShadersEvent;
import dev.architectury.registry.item.ItemPropertiesRegistry;
import dev.architectury.registry.menu.MenuRegistry;
import generations.gg.generations.core.generationscore.common.GenerationsCore;
Expand All @@ -27,6 +28,7 @@
import generations.gg.generations.core.generationscore.common.client.render.rarecandy.MinecraftClientGameProvider;
import generations.gg.generations.core.generationscore.common.client.render.rarecandy.ModelRegistry;
import generations.gg.generations.core.generationscore.common.client.render.rarecandy.Pipelines;
import generations.gg.generations.core.generationscore.common.client.render.rarecandy.VanillShaders;
import generations.gg.generations.core.generationscore.common.client.screen.container.*;
import generations.gg.generations.core.generationscore.common.world.container.GenerationsContainers;
import generations.gg.generations.core.generationscore.common.world.entity.GenerationsBoatEntity;
Expand Down Expand Up @@ -62,6 +64,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Position;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceProvider;
import net.minecraft.util.Mth;
import net.minecraft.util.Tuple;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -92,13 +95,15 @@
public class GenerationsCoreClient {

public static void onInitialize(Minecraft minecraft) {
if (GenerationsCore.CONFIG.client.useRenderDoc) {
try {
System.loadLibrary("renderdoc");
} catch (UnsatisfiedLinkError e) {
LOGGER.warn("Attempted to use renderdoc without renderdoc installed.");
}
}
// if (GenerationsCore.CONFIG.client.useRenderDoc) {
// try {
// System.loadLibrary("renderdoc");
// } catch (Exception e) {
// LOGGER.warn("Attempted to use renderdoc without renderdoc installed.");
// }
// }



ModelRegistry.init();

Expand All @@ -116,6 +121,8 @@ public static void onInitialize(Minecraft minecraft) {
GenerationsCoreClient.setupClient(minecraft);
RareCandy.DEBUG_THREADS = true;

ClientReloadShadersEvent.EVENT.register(VanillShaders::onShaderRegister);

PokeBallModelRepository.INSTANCE.inbuilt("strange_ball", PokeBallModel::new);

VaryingModelRepository.Companion.registerFactory(".pk", (resourceLocation, resource) -> new Tuple<>(new ResourceLocation(resourceLocation.getNamespace(), new File(resourceLocation.getPath()).getName()), b -> (Bone) new ModelPart(RareCandyBone.Companion.getCUBE_LIST(), Map.of("root", new RareCandyBone(resourceLocation)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,41 @@ package generations.gg.generations.core.generationscore.common.client
import com.cobblemon.mod.common.util.asResource
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.mojang.blaze3d.platform.NativeImage
import com.mojang.blaze3d.platform.GlStateManager
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.serialization.Codec
import generations.gg.generations.core.generationscore.common.GenerationsCore
import generations.gg.generations.core.generationscore.common.client.model.SpriteRegistry
import generations.gg.generations.core.generationscore.common.client.render.rarecandy.ITextureWithResourceLocation
import generations.gg.generations.core.generationscore.common.util.GenerationsUtils
import gg.generations.rarecandy.pokeutils.reader.ITextureLoader
import gg.generations.rarecandy.renderer.loading.ITexture
import net.minecraft.Util
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.texture.AbstractTexture
import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite
import net.minecraft.client.renderer.texture.SimpleTexture
import net.minecraft.client.renderer.texture.PreloadedTexture
import net.minecraft.resources.FileToIdConverter
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.packs.resources.ResourceManager
import org.lwjgl.opengl.GL13C
import java.io.ByteArrayInputStream
import java.io.IOException
import kotlin.random.Random

object GenerationsTextureLoader : ITextureLoader() {
val REGULAR = mutableMapOf<String, ResourceLocation>()
val CODEC = Codec.unboundedMap(Codec.STRING, ResourceLocation.CODEC)

// val missingTexture: ITexture
// get() = MinecraftTexture.also { it.resourceLocation = MissingTextureAtlasSprite.getLocation() }

val PROXY_ID: ResourceLocation = GenerationsCore.id("proxy_rarecandy")
val REGULAR = mutableMapOf<String, ITexture>()
val CODEC = Codec.unboundedMap(Codec.STRING, ResourceLocation.CODEC.xmap({ "${it.namespace}:textures/${it.path}.png".asResource() }, { throw RuntimeException("Don't use.")}))
val RARE_CANDY = FileToIdConverter("textures", "rare_candy_texture.json")
val gson = Gson()


object MissingTextureProxy : ITexture {

object MinecraftTexture : ITexture {
override fun close() {
}
override fun bind(slot: Int) {
var texture = Minecraft.getInstance().textureManager.getTexture(MissingTextureAtlasSprite.getLocation())
val texture = Minecraft.getInstance().textureManager.getTexture(MissingTextureAtlasSprite.getLocation())

RenderSystem.activeTexture(GL13C.GL_TEXTURE0 + slot)
RenderSystem.bindTexture(texture.id)
Expand All @@ -47,100 +49,137 @@ object GenerationsTextureLoader : ITextureLoader() {

}

object ProxyTexture : AbstractTexture() {
lateinit var iTexture: Texture

override fun load(resourceManager: ResourceManager) {

}

override fun releaseId() {

}

override fun bind() {
if (!RenderSystem.isOnRenderThreadOrInit()) {
RenderSystem.recordRenderCall {
GlStateManager._bindTexture(this.getId())
}
} else {
GlStateManager._bindTexture(this.getId())
}
}

override fun getId(): Int {
return iTexture.id
}
}

fun initialize(manager: ResourceManager) {
clear()
Minecraft.getInstance().textureManager.register(PROXY_ID, ProxyTexture)
try {
RARE_CANDY.listMatchingResourceStacks(manager).forEach { name, list ->
RARE_CANDY.listMatchingResourceStacks(manager).forEach { (name, list) ->
list.forEach { resource ->
val obj = resource.openAsReader().use { SpriteRegistry.GSON.fromJson(it, JsonObject::class.java) }
val map = GenerationsUtils.decode(CODEC, obj)

if(map.isNotEmpty()) {
map.forEach { (key, value) ->
register(key, SimpleTextureEnhanced(value.let { "${it.namespace}:textures/${it.path}.png" }.asResource()))
if (map.isNotEmpty()) {
map.forEach { (key, location) ->
manager.getResource(location).ifPresent({ it ->
register(key, Texture.read(it.open().readAllBytes()))
})
}
}
}
}

// RARE_CANDY.listMatchingResources(manager).values.forEach { resouce ->
// resouce.openAsReader().use { GsonHelper.fromJson(gson, it, RARE_CANDY_TYPE) }.forEach { (key, value) ->
// register(key, SimpleTextureEnhanced(value.asResource().let { "${it.namespace}:textures/${it.path}.png" }.asResource()))
// }
// }
} catch (e: Exception) { throw RuntimeException(e)
}
}

override fun getTexture(s: String?): ITexture? {
val texture = REGULAR.getOrDefault(s, null)?.let { Minecraft.getInstance().textureManager.getTexture(it, null) }.takeIf { it is ITextureWithResourceLocation } ?: return MissingTextureProxy

return texture as ITexture
}
override fun getTexture(s: String?): ITexture = REGULAR[s] ?: MinecraftTexture

override fun register(s: String, iTexture: ITexture) {
if(iTexture is ITextureWithResourceLocation) REGULAR.putIfAbsent(s, iTexture.location)
REGULAR.putIfAbsent(s, iTexture)
}

override fun register(id: String, name: String, data: ByteArray) {
SimpleTextureIndependentData.read(data, name)?.let { register(id, it) }
Texture.read(data)?.let { register(id, it) }
}


override fun remove(s: String) {
REGULAR.remove(s)?.run { Minecraft.getInstance().textureManager.release(this) }
REGULAR.remove(s)?.run { this.close() }
}

fun clear() {
val iterator = REGULAR.iterator()
while(iterator.hasNext()) {
val entry = iterator.next()
iterator.remove()
Minecraft.getInstance().textureManager.release(entry.value)

var texture = entry.value

if(texture is SimpleTextureIndependentData) {
Minecraft.getInstance().textureManager.release(texture.imageId)
} else {
texture.close()
}
}
}

override fun getDarkFallback(): ITexture? = getTexture("dark")
override fun getDarkFallback(): ITexture = getTexture("dark")

override fun getBrightFallback(): ITexture? = getTexture("bright")
override fun getBrightFallback(): ITexture = getTexture("bright")

override fun getNuetralFallback(): ITexture? = getTexture("neutral")
override fun getNuetralFallback(): ITexture = getTexture("neutral")

override fun getTextureEntries(): Set<String> = REGULAR.keys

fun has(texture: String): Boolean = REGULAR.containsKey(texture)

fun getLocation(material: String): ResourceLocation? = REGULAR.getOrDefault(material, null)
fun getLocation(material: String): ResourceLocation? {
val texture = REGULAR[material] ?: return null

private class SimpleTextureEnhanced(override var location: ResourceLocation) : SimpleTexture(location), ITextureWithResourceLocation {
// if(texture is SimpleTextureIndependentData) {
// return texture.imageId
// }

init {
Minecraft.getInstance().textureManager.register(location, this)
}
ProxyTexture.iTexture = texture as Texture

override fun bind(slot: Int) {
RenderSystem.activeTexture(GL13C.GL_TEXTURE0 + slot)
RenderSystem.bindTexture(id)
}
return PROXY_ID

override fun width(): Int {
TODO("Not yet implemented")
}

override fun height(): Int {
TODO("Not yet implemented")
}
// ProxyTexture.iTexture = texture as Texture
// return PROXY_ID
}

private class SimpleTextureIndependentData(override var location: ResourceLocation, private val texture: ByteArray?) : SimpleTexture(location), ITextureWithResourceLocation {

init {
Minecraft.getInstance().textureManager.register(location, this)
}

override fun bind(slot: Int) {
RenderSystem.activeTexture(GL13C.GL_TEXTURE0 + slot)
RenderSystem.bindTexture(id)
// private class SimpleTextureEnhanced : SimpleTexture(), ITexture {
//
// override fun bind(slot: Int) {
// RenderSystem.activeTexture(GL13C.GL_TEXTURE0 + slot)
// RenderSystem.bindTexture(id)
// }
//
// override fun width(): Int {
// TODO("Not yet implemented")
// }
//
// override fun height(): Int {
// TODO("Not yet implemented")
// }
// }

private class SimpleTextureIndependentData(textureManager: ResourceManager, location: ResourceLocation) : PreloadedTexture(textureManager, location, Util.backgroundExecutor()), ITexture {
override fun bind(p0: Int) {
if (!RenderSystem.isOnRenderThreadOrInit()) {
RenderSystem.recordRenderCall {
GlStateManager.glActiveTexture(p0)
GlStateManager._bindTexture(this.getId())
}
} else {
GlStateManager.glActiveTexture(p0)
GlStateManager._bindTexture(this.getId())
}
}

override fun width(): Int {
Expand All @@ -151,52 +190,6 @@ object GenerationsTextureLoader : ITextureLoader() {
TODO("Not yet implemented")
}

override fun getTextureImage(resourceManager: ResourceManager): TextureImage {
return load()!!
}

fun load(): TextureImage? {
return try {
val inputStream = ByteArrayInputStream(texture)
val nativeImage: NativeImage = try {
NativeImage.read(inputStream)
} catch (var9: Throwable) {
try {
inputStream.close()
} catch (var7: Throwable) {
var9.addSuppressed(var7)
}
throw var9
}

inputStream.close()
// var textureMetadataSection: TextureMetadataSection? = null //TODO: See if implmenting is viable
// try {
// textureMetadataSection = resource.metadata().getSection(TextureMetadataSection.SERIALIZER)
// .orElse(null as Any?) as TextureMetadataSection
// } catch (var8: java.lang.RuntimeException) {
// LOGGER.warn("Failed reading metadata of: {}", location, var8)
// }
TextureImage(null, nativeImage)
} catch (var10: IOException) {
TextureImage(var10)
}
}

companion object {
@Throws(IOException::class)
fun read(imageBytes: ByteArray?, name: String): SimpleTextureIndependentData? {
if(imageBytes == null) return null

val resource = if(name.contains(":")) {
name.lowercase().asResource()
} else {
GenerationsCore.id((1..4).joinToString("") { Random.nextInt(0, 10).toString() } + name.lowercase())
}

return SimpleTextureIndependentData(resource, imageBytes)
}

}
val imageId: ResourceLocation = location
}
}
Loading

0 comments on commit dd44a3b

Please sign in to comment.