Skip to content

Commit

Permalink
Merge branch 'mtrl-preview-extension'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Sep 21, 2023
2 parents 40b6c60 + 3f439ba commit 2c06506
Show file tree
Hide file tree
Showing 42 changed files with 153 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Penumbra.GameData
11 changes: 5 additions & 6 deletions Penumbra/Api/IpcTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using OtterGui;
using OtterGui.Raii;
using Penumbra.Mods;
using System.Globalization;
using Dalamud.Utility;
using Penumbra.Api.Enums;
using Penumbra.Api.Helpers;
Expand All @@ -17,7 +16,7 @@
using Penumbra.Collections.Manager;
using Dalamud.Plugin.Services;
using Penumbra.GameData.Enums;
using System.Diagnostics;
using Penumbra.GameData.Structs;

namespace Penumbra.Api;

Expand Down Expand Up @@ -1450,7 +1449,7 @@ public void Draw()

_lastCallDuration = _stopwatch.Elapsed;
_lastGameObjectResourcePaths = gameObjects
.Select(GameObjectToString)
.Select(i => GameObjectToString(i))
.Zip(resourcePaths)
.ToArray();

Expand Down Expand Up @@ -1482,7 +1481,7 @@ public void Draw()

_lastCallDuration = _stopwatch.Elapsed;
_lastGameObjectResourcesOfType = gameObjects
.Select(GameObjectToString)
.Select(i => GameObjectToString(i))
.Zip(resourcesOfType)
.ToArray();

Expand Down Expand Up @@ -1630,9 +1629,9 @@ private ushort[] GetSelectedGameObjects()
.SelectWhere(index => (ushort.TryParse(index.Trim(), out var i), i))
.ToArray();

private unsafe string GameObjectToString(ushort gameObjectIndex)
private unsafe string GameObjectToString(ObjectIndex gameObjectIndex)
{
var gameObject = _objects[gameObjectIndex];
var gameObject = _objects[gameObjectIndex.Index];

return gameObject != null
? $"[{gameObjectIndex}] {gameObject.Name} ({gameObject.ObjectKind})"
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Api/PenumbraApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Penumbra.Interop.Structs;
using Penumbra.Meta.Manipulations;
using Penumbra.Mods;
using System.Diagnostics.CodeAnalysis;
using FFXIVClientStructs.FFXIV.Client.Graphics.Scene;
using OtterGui.Compression;
using Penumbra.Api.Enums;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Collections/Cache/ImcCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using Penumbra.Meta;
using Penumbra.Meta.Files;
using Penumbra.Meta.Manipulations;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Collections/Cache/MetaCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using Penumbra.GameData.Enums;
using Penumbra.Interop.Services;
using Penumbra.Interop.Structs;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Collections/Manager/CollectionStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using Dalamud.Interface.Internal.Notifications;
using OtterGui;
using OtterGui.Filesystem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using Dalamud.Game.ClientState.Objects.Enums;
using Dalamud.Game.ClientState.Objects.Types;
using Penumbra.GameData.Actors;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Collections/Manager/IndividualCollections.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using Dalamud.Game.ClientState.Objects.Enums;
using OtterGui.Filesystem;
using Penumbra.GameData.Actors;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Collections/Manager/TempCollectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using Penumbra.Api;
using Penumbra.Communication;
using Penumbra.GameData.Actors;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Collections/ModCollection.Cache.Access.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using OtterGui.Classes;
using Penumbra.GameData.Enums;
using Penumbra.Mods;
using System.Diagnostics.CodeAnalysis;
using Penumbra.Interop.Structs;
using Penumbra.Meta.Files;
using Penumbra.Meta.Manipulations;
Expand Down
5 changes: 5 additions & 0 deletions Penumbra/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
global using System.Collections.Concurrent;
global using System.Collections.Generic;
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;
global using System.Globalization;
global using System.IO;
global using System.IO.Compression;
global using System.Linq;
global using System.Numerics;
global using System.Reflection;
global using System.Runtime.CompilerServices;
global using System.Runtime.InteropServices;
global using System.Security.Cryptography;
global using System.Text;
global using System.Text.RegularExpressions;
global using System.Threading;
global using System.Threading.Tasks;
1 change: 0 additions & 1 deletion Penumbra/Import/Structs/MetaFileInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Penumbra.GameData.Enums;
using System.Text.RegularExpressions;
using Penumbra.GameData;

namespace Penumbra.Import.Structs;
Expand Down
2 changes: 0 additions & 2 deletions Penumbra/Import/TexToolsImport.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Text;
using Newtonsoft.Json;
using OtterGui.Compression;
using Penumbra.Import.Structs;
using Penumbra.Mods;
using Penumbra.Mods.Editor;
using Penumbra.Mods.Manager;
using FileMode = System.IO.FileMode;
Expand Down
2 changes: 1 addition & 1 deletion Penumbra/Import/TexToolsImporter.Archives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Archives.SevenZip;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using SharpCompress.Readers;
using ZipArchive = SharpCompress.Archives.Zip.ZipArchive;

namespace Penumbra.Import;

Expand Down
2 changes: 1 addition & 1 deletion Penumbra/Import/TexToolsImporter.ModPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Penumbra.Mods;
using Penumbra.Mods.Subclasses;
using Penumbra.Util;
using SharpCompress.Archives.Zip;
using ZipArchive = SharpCompress.Archives.Zip.ZipArchive;

namespace Penumbra.Import;

Expand Down
1 change: 0 additions & 1 deletion Penumbra/Import/TexToolsMeta.Export.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text;
using Penumbra.GameData.Enums;
using Penumbra.GameData.Structs;
using Penumbra.Meta;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Import/TexToolsMeta.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text;
using Penumbra.GameData;
using Penumbra.Import.Structs;
using Penumbra.Meta;
Expand Down
88 changes: 38 additions & 50 deletions Penumbra/Interop/MaterialPreview/MaterialInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,28 @@
using FFXIVClientStructs.FFXIV.Client.Game.Character;
using FFXIVClientStructs.FFXIV.Client.Graphics.Render;
using FFXIVClientStructs.FFXIV.Client.Graphics.Scene;
using Penumbra.GameData.Structs;
using Penumbra.Interop.ResourceTree;
using Penumbra.String;

namespace Penumbra.Interop.MaterialPreview;

public enum DrawObjectType
{
PlayerCharacter,
PlayerMainhand,
PlayerOffhand,
PlayerVfx,
MinionCharacter,
MinionUnk1,
MinionUnk2,
MinionUnk3,
Character,
Mainhand,
Offhand,
Vfx,
};

public readonly record struct MaterialInfo(DrawObjectType Type, int ModelSlot, int MaterialSlot)
public readonly record struct MaterialInfo(ObjectIndex ObjectIndex, DrawObjectType Type, int ModelSlot, int MaterialSlot)
{
public nint GetCharacter(IObjectTable objects)
=> GetCharacter(Type, objects);

public static nint GetCharacter(DrawObjectType type, IObjectTable objects)
=> type switch
{
DrawObjectType.PlayerCharacter => objects.GetObjectAddress(0),
DrawObjectType.PlayerMainhand => objects.GetObjectAddress(0),
DrawObjectType.PlayerOffhand => objects.GetObjectAddress(0),
DrawObjectType.PlayerVfx => objects.GetObjectAddress(0),
DrawObjectType.MinionCharacter => objects.GetObjectAddress(1),
DrawObjectType.MinionUnk1 => objects.GetObjectAddress(1),
DrawObjectType.MinionUnk2 => objects.GetObjectAddress(1),
DrawObjectType.MinionUnk3 => objects.GetObjectAddress(1),
_ => nint.Zero,
};
=> objects.GetObjectAddress(ObjectIndex.Index);

public nint GetDrawObject(nint address)
=> GetDrawObject(Type, address);

public static nint GetDrawObject(DrawObjectType type, IObjectTable objects)
=> GetDrawObject(type, GetCharacter(type, objects));

public static unsafe nint GetDrawObject(DrawObjectType type, nint address)
{
var gameObject = (Character*)address;
Expand All @@ -52,18 +32,17 @@ public static unsafe nint GetDrawObject(DrawObjectType type, nint address)

return type switch
{
DrawObjectType.PlayerCharacter => (nint)gameObject->GameObject.GetDrawObject(),
DrawObjectType.PlayerMainhand => *((nint*)&gameObject->DrawData.MainHand + 1),
DrawObjectType.PlayerOffhand => *((nint*)&gameObject->DrawData.OffHand + 1),
DrawObjectType.PlayerVfx => *((nint*)&gameObject->DrawData.UnkF0 + 1),
DrawObjectType.MinionCharacter => (nint)gameObject->GameObject.GetDrawObject(),
DrawObjectType.MinionUnk1 => *((nint*)&gameObject->DrawData.MainHand + 1),
DrawObjectType.MinionUnk2 => *((nint*)&gameObject->DrawData.OffHand + 1),
DrawObjectType.MinionUnk3 => *((nint*)&gameObject->DrawData.UnkF0 + 1),
_ => nint.Zero,
DrawObjectType.Character => (nint)gameObject->GameObject.GetDrawObject(),
DrawObjectType.Mainhand => *((nint*)&gameObject->DrawData.MainHand + 1),
DrawObjectType.Offhand => *((nint*)&gameObject->DrawData.OffHand + 1),
DrawObjectType.Vfx => *((nint*)&gameObject->DrawData.UnkF0 + 1),
_ => nint.Zero,
};
}

public unsafe Material* GetDrawObjectMaterial(IObjectTable objects)
=> GetDrawObjectMaterial((CharacterBase*)GetDrawObject(GetCharacter(objects)));

public unsafe Material* GetDrawObjectMaterial(CharacterBase* drawObject)
{
if (drawObject == null)
Expand All @@ -82,33 +61,42 @@ public static unsafe nint GetDrawObject(DrawObjectType type, nint address)
return model->Materials[MaterialSlot];
}

public static unsafe List<MaterialInfo> FindMaterials(IObjectTable objects, string materialPath)
public static unsafe List<MaterialInfo> FindMaterials(IEnumerable<nint> gameObjects, string materialPath)
{
var needle = ByteString.FromString(materialPath.Replace('\\', '/'), out var m, true) ? m : ByteString.Empty;

var result = new List<MaterialInfo>(Enum.GetValues<DrawObjectType>().Length);
foreach (var type in Enum.GetValues<DrawObjectType>())
foreach (var objectPtr in gameObjects)
{
var drawObject = (CharacterBase*)GetDrawObject(type, objects);
if (drawObject == null)
var gameObject = (Character*)objectPtr;
if (gameObject == null)
continue;

for (var i = 0; i < drawObject->SlotCount; ++i)
var index = (ObjectIndex) gameObject->GameObject.ObjectIndex;

foreach (var type in Enum.GetValues<DrawObjectType>())
{
var model = drawObject->Models[i];
if (model == null)
var drawObject = (CharacterBase*)GetDrawObject(type, objectPtr);
if (drawObject == null)
continue;

for (var j = 0; j < model->MaterialCount; ++j)
for (var i = 0; i < drawObject->SlotCount; ++i)
{
var material = model->Materials[j];
if (material == null)
var model = drawObject->Models[i];
if (model == null)
continue;

var mtrlHandle = material->MaterialResourceHandle;
var path = ResolveContext.GetResourceHandlePath((Structs.ResourceHandle*)mtrlHandle);
if (path == needle)
result.Add(new MaterialInfo(type, i, j));
for (var j = 0; j < model->MaterialCount; ++j)
{
var material = model->Materials[j];
if (material == null)
continue;

var mtrlHandle = material->MaterialResourceHandle;
var path = ResolveContext.GetResourceHandlePath((Structs.ResourceHandle*)mtrlHandle);
if (path == needle)
result.Add(new MaterialInfo(index, type, i, j));
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Interop/PathResolving/PathResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using FFXIVClientStructs.FFXIV.Client.System.Resource;
using Penumbra.Api.Enums;
using Penumbra.Collections;
Expand Down
1 change: 0 additions & 1 deletion Penumbra/Interop/ResourceLoading/CreateFileWHook.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text;
using Dalamud.Hooking;
using Penumbra.String;
using Penumbra.String.Classes;
Expand Down
Loading

0 comments on commit 2c06506

Please sign in to comment.