|
| 1 | +using System.Numerics; |
| 2 | +using Dalamud.Bindings.ImGui; |
| 3 | +using Dalamud.Game.ClientState.Objects.Types; |
| 4 | +using Dalamud.Interface.Textures; |
| 5 | +using Dalamud.Interface.Utility.Raii; |
| 6 | +using Dalamud.Plugin.Services; |
| 7 | +using FFXIVClientStructs.FFXIV.Client.Game.Character; |
| 8 | +using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; |
| 9 | +using FFXIVClientStructs.FFXIV.Client.Graphics.Render; |
| 10 | +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; |
| 11 | +using FFXIVClientStructs.Interop; |
| 12 | +using Meddle.Plugin.Models; |
| 13 | +using Meddle.Plugin.Services; |
| 14 | +using Meddle.Plugin.Utils; |
| 15 | +using Microsoft.Extensions.Logging; |
| 16 | + |
| 17 | +namespace Meddle.Plugin.UI; |
| 18 | + |
| 19 | +public class ColorTableTester : ITab |
| 20 | +{ |
| 21 | + private readonly ILogger<ColorTableTester> log; |
| 22 | + private readonly Configuration config; |
| 23 | + private readonly CommonUi commonUi; |
| 24 | + private readonly TextureCache textureCache; |
| 25 | + private readonly ITextureProvider textureProvider; |
| 26 | + private ICharacter? selectedCharacter; |
| 27 | + private OnRenderMaterialOutput? output; |
| 28 | + public string Name => "ColorTable Tester"; |
| 29 | + public int Order => 100; |
| 30 | + public MenuType MenuType => MenuType.Debug; |
| 31 | + |
| 32 | + public ColorTableTester( |
| 33 | + ILogger<ColorTableTester> log, |
| 34 | + Configuration config, |
| 35 | + CommonUi commonUi, TextureCache textureCache, ITextureProvider textureProvider) |
| 36 | + { |
| 37 | + this.log = log; |
| 38 | + this.config = config; |
| 39 | + this.commonUi = commonUi; |
| 40 | + this.textureCache = textureCache; |
| 41 | + this.textureProvider = textureProvider; |
| 42 | + } |
| 43 | + |
| 44 | + public unsafe void Draw() |
| 45 | + { |
| 46 | + commonUi.DrawCharacterSelect(ref selectedCharacter, CharacterValidationFlags.IsVisible); |
| 47 | + if (output != null) |
| 48 | + { |
| 49 | + var serialized = System.Text.Json.JsonSerializer.Serialize(output, new System.Text.Json.JsonSerializerOptions |
| 50 | + { |
| 51 | + WriteIndented = true |
| 52 | + }); |
| 53 | + ImGui.TextWrapped(serialized); |
| 54 | + if (output.DecalTexture != null) |
| 55 | + { |
| 56 | + var wrap = textureCache.GetOrAdd($"{output.DecalTexture.GetHashCode()}", () => |
| 57 | + { |
| 58 | + var textureData = output.DecalTexture.Bitmap.GetPixelSpan(); |
| 59 | + var wrap = textureProvider.CreateFromRaw( |
| 60 | + RawImageSpecification.Rgba32(output.DecalTexture.Width, output.DecalTexture.Height), textureData, |
| 61 | + $"Meddle_Decal_{output.DecalTexture.GetHashCode()}"); |
| 62 | + return wrap; |
| 63 | + }); |
| 64 | + var availableWidth = ImGui.GetContentRegionAvail().X; |
| 65 | + float displayWidth = output.DecalTexture.Width; |
| 66 | + float displayHeight = output.DecalTexture.Height; |
| 67 | + if (displayWidth > availableWidth) |
| 68 | + { |
| 69 | + var ratio = availableWidth / displayWidth; |
| 70 | + displayWidth *= ratio; |
| 71 | + displayHeight *= ratio; |
| 72 | + } |
| 73 | + ImGui.Image(wrap.Handle, new Vector2(displayWidth, displayHeight)); |
| 74 | + } |
| 75 | + |
| 76 | + if (ImGui.Button("Clear Output")) |
| 77 | + { |
| 78 | + output = null; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (selectedCharacter == null) |
| 83 | + { |
| 84 | + ImGui.Text("No character selected"); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + var character = (Character*)selectedCharacter.Address; |
| 89 | + var drawObject = character->GameObject.DrawObject; |
| 90 | + if (drawObject == null) |
| 91 | + { |
| 92 | + ImGui.Text("Draw object is null"); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (drawObject->GetObjectType() != ObjectType.CharacterBase) |
| 97 | + { |
| 98 | + ImGui.Text("Draw object is not a character base"); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + var cBase = (CharacterBase*)drawObject; |
| 103 | + var modelType = cBase->GetModelType(); |
| 104 | + if (modelType != CharacterBase.ModelType.Human) |
| 105 | + { |
| 106 | + ImGui.Text("Model is not human"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + // cPtr.Value->ColorTableTexturesSpan[(slotIdx * CSCharacterBase.MaterialsPerSlot) + materialIdx]; |
| 112 | + var human = (Human*)cBase; |
| 113 | + // var colorTableContexts = new List<(Pointer<Model> Model, Pointer<Material> Material, Pointer<Texture> Texture, int ColorTableIdx)>(); |
| 114 | + var colorTableDict = new Dictionary<int, (Pointer<Model> Model, Pointer<Material> Material)>(); |
| 115 | + for (var modelIdx = 0; modelIdx < human->ModelsSpan.Length; modelIdx++) |
| 116 | + { |
| 117 | + var model = human->ModelsSpan[modelIdx]; |
| 118 | + if (model == null || model.Value == null) |
| 119 | + { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + for (var materialIdx = 0; materialIdx < model.Value->MaterialsSpan.Length; materialIdx++) |
| 124 | + { |
| 125 | + var material = model.Value->MaterialsSpan[materialIdx]; |
| 126 | + if (material == null || material.Value == null) |
| 127 | + { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + var index = ParseMaterialUtil.ResolveColorTableSetIndex((int)model.Value->SlotIndex, materialIdx); |
| 132 | + colorTableDict[index] = (model, material); |
| 133 | + } |
| 134 | + } |
| 135 | + for (var colorTableIdx = 0; colorTableIdx < human->ColorTableTexturesSpan.Length; colorTableIdx++) |
| 136 | + { |
| 137 | + var colorTableTexture = human->ColorTableTexturesSpan[colorTableIdx]; |
| 138 | + if (colorTableTexture == null || colorTableTexture.Value == null) |
| 139 | + { |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + if (!colorTableDict.TryGetValue(colorTableIdx, out var modelMaterial)) |
| 144 | + { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + var modelName = modelMaterial.Model.Value->ModelResourceHandle->FileName; |
| 149 | + var materialName = modelMaterial.Material.Value->MaterialResourceHandle->FileName; |
| 150 | + var shortMaterialName = Path.GetFileName(materialName.ToString()); |
| 151 | + |
| 152 | + if (ImGui.CollapsingHeader($"Color Table Texture {colorTableIdx} - {shortMaterialName} - {colorTableTexture.Value->ActualWidth}x{colorTableTexture.Value->ActualHeight}")) |
| 153 | + { |
| 154 | + var colorTable = ParseMaterialUtil.ParseColorTableTexture(colorTableTexture); |
| 155 | + UiUtil.DrawColorTable(colorTable); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + public void Dispose() |
| 162 | + { |
| 163 | + // TODO release managed resources here |
| 164 | + } |
| 165 | +} |
0 commit comments