Skip to content

Commit

Permalink
Allow MeshSet to be read by a custom Plugin reader
Browse files Browse the repository at this point in the history
  • Loading branch information
paulov-t committed May 12, 2023
1 parent 798d104 commit 8cb05e2
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 56 deletions.
14 changes: 14 additions & 0 deletions Libraries/FrostySdk/Frostbite/PluginInterfaces/IMeshSetReader.cs
@@ -0,0 +1,14 @@
using FMT.FileTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrostySdk.Frostbite.PluginInterfaces
{
public interface IMeshSetReader
{
public void Read(NativeReader nativeReader, MeshSet meshSet);
}
}
4 changes: 2 additions & 2 deletions Libraries/FrostySdk/FrostbiteProfiles/FIFA19Profile.json
Expand Up @@ -33,8 +33,8 @@
"EditorScreen": "DefaultEditor",
"EBXReader": "EbxReader",
"EBXWriter": "EbxWriter",
"CacheWriter": null,
"CacheReader": null,
"CacheWriter": "BaseCacheWriter",
"CacheReader": "BaseCacheReader",
"SupportedLauncherFileTypes": null,
"ModCompilerFileType": null,
"CanEdit": false,
Expand Down
16 changes: 16 additions & 0 deletions Libraries/FrostySdk/FrostbiteSdk.Managers/AssetManager.cs
Expand Up @@ -260,6 +260,20 @@ public static bool InitialisePlugins()

public static bool CacheUpdate = false;

public object LoadTypeFromPluginByInterface(string interfaceName, params object[] args)
{
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()
.Where(x => x.FullName.Contains("Plugin", StringComparison.OrdinalIgnoreCase)))
{
var t = a.GetTypes().FirstOrDefault(x => x.GetInterface(interfaceName) != null);
if (t != null)
{
return Activator.CreateInstance(t, args: args);
}
}
return null;
}

public object LoadTypeFromPlugin(string className, params object[] args)
{
if (CachedTypes == null)
Expand All @@ -284,6 +298,8 @@ public object LoadTypeFromPlugin(string className, params object[] args)
}
return null;
}


public static object LoadTypeFromPlugin2(string className, params object[] args)
{
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()
Expand Down
80 changes: 46 additions & 34 deletions Libraries/FrostySdk/ResourceTypes/Mesh/MeshSet.cs
@@ -1,6 +1,7 @@
using FMT.FileTools;
using FMT.FileTools.Modding;
using FrostySdk;
using FrostySdk.Frostbite.PluginInterfaces;
using FrostySdk.FrostySdk.Resources;
using FrostySdk.IO;
using FrostySdk.Managers;
Expand All @@ -13,37 +14,35 @@

public class MeshSet
{
private TangentSpaceCompressionType tangentSpaceCompressionType;
public TangentSpaceCompressionType tangentSpaceCompressionType;

private AxisAlignedBox boundingBox;
public string fullName;

private string fullName;
public uint nameHash { get; set; }

private uint nameHash { get; set; }
public uint headerSize { get; set; }

private uint headerSize { get; set; }
public readonly List<uint> unknownUInts = new List<uint>();

private readonly List<uint> unknownUInts = new List<uint>();
public ushort? unknownUShort { get; set; }

private ushort? unknownUShort { get; set; }
public readonly List<ushort> unknownUShorts = new List<ushort>();

private readonly List<ushort> unknownUShorts = new List<ushort>();
public readonly List<long> unknownOffsets = new List<long>();

private readonly List<long> unknownOffsets = new List<long>();
public readonly List<byte[]> unknownBytes = new List<byte[]>();

private readonly List<byte[]> unknownBytes = new List<byte[]>();
public ushort boneCount { get; set; }

private ushort boneCount { get; set; }
public ushort CullBoxCount { get; set; }

private ushort CullBoxCount { get; set; }
public readonly List<ushort> boneIndices = new List<ushort>();

private readonly List<ushort> boneIndices = new List<ushort>();
public readonly List<AxisAlignedBox> boneBoundingBoxes = new List<AxisAlignedBox>();

private readonly List<AxisAlignedBox> boneBoundingBoxes = new List<AxisAlignedBox>();
public readonly List<AxisAlignedBox> partBoundingBoxes = new List<AxisAlignedBox>();

private readonly List<AxisAlignedBox> partBoundingBoxes = new List<AxisAlignedBox>();

private readonly List<LinearTransform> partTransforms = new List<LinearTransform>();
public readonly List<LinearTransform> partTransforms = new List<LinearTransform>();

public TangentSpaceCompressionType TangentSpaceCompressionType
{
Expand All @@ -64,15 +63,15 @@ public TangentSpaceCompressionType TangentSpaceCompressionType
}
}

public AxisAlignedBox BoundingBox => boundingBox;
public AxisAlignedBox BoundingBox { get; set; }

public List<MeshSetLod> Lods { get; } = new List<MeshSetLod>();


public MeshType Type { get; set; }

public EMeshLayout MeshLayout { get; private set; }
public MeshSetLayoutFlags MeshSetLayoutFlags { get; private set; }
public EMeshLayout MeshLayout { get; set; }
public MeshSetLayoutFlags MeshSetLayoutFlags { get; set; }

public string FullName
{
Expand All @@ -90,7 +89,7 @@ public string FullName
}
}

public string Name { get; private set; }
public string Name { get; set; }

public int HeaderSize => BitConverter.ToUInt16(Meta, 12);

Expand All @@ -100,23 +99,29 @@ public string FullName

public ushort MeshCount { get; set; }

public MeshType FIFA23_Type2 { get; private set; }
public byte[] FIFA23_TypeUnknownBytes { get; private set; }
public byte[] FIFA23_SkinnedUnknownBytes { get; private set; }
public MeshType FIFA23_Type2 { get; set; }
public byte[] FIFA23_TypeUnknownBytes { get; set; }
public byte[] FIFA23_SkinnedUnknownBytes { get; set; }

public long SkinnedBoneCountUnkLong1 { get; private set; }
public long SkinnedBoneCountUnkLong2 { get; private set; }
public long SkinnedBoneCountUnkLong1 { get; set; }
public long SkinnedBoneCountUnkLong2 { get; set; }

public ShaderDrawOrder ShaderDrawOrder { get; private set; }
public ShaderDrawOrder ShaderDrawOrder { get; set; }

public ShaderDrawOrderUserSlot ShaderDrawOrderUserSlot { get; private set; }
public ShaderDrawOrderUserSlot ShaderDrawOrderUserSlot { get; set; }

public ShaderDrawOrderSubOrder ShaderDrawOrderSubOrder { get; private set; }
public ShaderDrawOrderSubOrder ShaderDrawOrderSubOrder { get; set; }

public long UnknownPostLODCount { get; private set; }
public long UnknownPostLODCount { get; set; }

public List<ushort> LodFade { get; } = new List<ushort>();

// Create Empty MeshSet
public MeshSet()
{

}

public MeshSet(Stream stream)
{
if (stream == null)
Expand All @@ -131,6 +136,13 @@ public MeshSet(Stream stream)
// useful for resetting when live debugging
nativeReader.Position = 0;

var meshSetReader = AssetManager.Instance.LoadTypeFromPluginByInterface(typeof(IMeshSetReader).FullName);
if(meshSetReader != null)
{
((IMeshSetReader)meshSetReader).Read(nativeReader, this);
return;
}

if (ProfileManager.Game == EGame.FIFA23)
ReadFIFA23(nativeReader);
else if (ProfileManager.Game == EGame.NFSUnbound)
Expand All @@ -142,11 +154,11 @@ public MeshSet(Stream stream)

public List<ushort> PositionsOfLodMeshSet { get; } = new List<ushort>();

private void ReadNFSUnbound(FileReader nativeReader)
private void ReadNFSUnbound(NativeReader nativeReader)
{
_ = nativeReader.Position;

boundingBox = nativeReader.ReadAxisAlignedBox();
BoundingBox = nativeReader.ReadAxisAlignedBox();
long[] lodOffsets = new long[MaxLodCount];
for (int i2 = 0; i2 < MaxLodCount; i2++)
{
Expand Down Expand Up @@ -191,7 +203,7 @@ private void ReadNFSUnbound(FileReader nativeReader)

public void ReadFIFA23(FileReader nativeReader)
{
boundingBox = nativeReader.ReadAxisAlignedBox();
BoundingBox = nativeReader.ReadAxisAlignedBox();
long[] array = new long[MaxLodCount];
for (int i2 = 0; i2 < MaxLodCount; i2++)
{
Expand Down Expand Up @@ -545,7 +557,7 @@ private void Write(NativeWriter writer, MeshContainer meshContainer)
{
throw new ArgumentNullException("meshContainer");
}
writer.WriteAxisAlignedBox(boundingBox);
writer.WriteAxisAlignedBox(BoundingBox);
for (int i = 0; i < MaxLodCount; i++)
{
if (i < Lods.Count)
Expand Down
12 changes: 6 additions & 6 deletions Libraries/FrostySdk/ResourceTypes/Mesh/MeshSetLod.cs
Expand Up @@ -102,12 +102,12 @@ public int IndexUnitSize

public long UnknownLongAfterNameHash { get; set; }

public MeshSetLod(FileReader reader, MeshSet meshSet)
public MeshSetLod(NativeReader reader, MeshSet meshSet)
{
Type = (MeshType)reader.ReadUInt32LittleEndian();
maxInstances = reader.ReadUInt32LittleEndian();
uint sectionCount = reader.ReadUInt32LittleEndian();
var sectionOffset = reader.ReadInt64LittleEndian();
Type = (MeshType)reader.ReadUInt();
maxInstances = reader.ReadUInt();
uint sectionCount = reader.ReadUInt();
var sectionOffset = reader.ReadLong();
long categoryOffset = reader.Position;
reader.Position = sectionOffset;
for (uint sectionIndex = 0u; sectionIndex < sectionCount; sectionIndex++)
Expand Down Expand Up @@ -463,7 +463,7 @@ public void ClearCategories()
}
}

public void ReadInlineData(FileReader reader)
public void ReadInlineData(NativeReader reader)
{
if (reader == null)
{
Expand Down
16 changes: 6 additions & 10 deletions Libraries/FrostySdk/ResourceTypes/Mesh/MeshSetSection.cs
Expand Up @@ -77,11 +77,7 @@ public byte BonesPerVertex

public ushort BoneCount { get; private set; }

internal MeshSetSection()
{
}

private void ReadBones(FileReader reader, long bonePositions)
private void ReadBones(NativeReader reader, long bonePositions)
{
long startPosition = reader.Position;
reader.Position = bonePositions;
Expand All @@ -92,7 +88,7 @@ private void ReadBones(FileReader reader, long bonePositions)
reader.Position = startPosition;
}

public MeshSetSection(FileReader reader, int index)
public MeshSetSection(NativeReader reader, int index)
{
switch(ProfileManager.Game)
{
Expand Down Expand Up @@ -126,7 +122,7 @@ public MeshSetSection(FileReader reader, int index)

}

private void ReadNFSU(FileReader reader, int index)
private void ReadNFSU(NativeReader reader, int index)
{
var startPosition = reader.Position;

Expand Down Expand Up @@ -192,7 +188,7 @@ private void ReadNFSU(FileReader reader, int index)
private uint FIFA23_UnknownInt1;
private uint FIFA23_UnknownInt2;

public void Read23(FileReader reader, int index)
public void Read23(NativeReader reader, int index)
{
var startPosition = reader.Position;

Expand Down Expand Up @@ -259,7 +255,7 @@ public void Read23(FileReader reader, int index)
reader.Position = position3;
}

public void Read22(FileReader reader, int index)
public void Read22(NativeReader reader, int index)
{
var startPosition = reader.Position;

Expand Down Expand Up @@ -323,7 +319,7 @@ public void Read22(FileReader reader, int index)
reader.Position = position3;
}

public void Read21(FileReader reader, int index)
public void Read21(NativeReader reader, int index)
{
sectionIndex = index;
offset1 = reader.ReadInt64LittleEndian();
Expand Down
Expand Up @@ -2,9 +2,9 @@
{
public enum ShaderDrawOrderUserSlot
{
ShaderDrawOrderUserSlot_UnkField_953737077 = 79,
ShaderDrawOrderUserSlot_UnkField_576963545 = 127,
ShaderDrawOrderUserSlot_UnkField_724134446 = 175,
ShaderDrawOrderUserSlot_UnkField_657418520 = 223
ShaderDrawOrderUserSlot_Low = 79,
ShaderDrawOrderUserSlot_Default = 127,
ShaderDrawOrderUserSlot_Medium = 175,
ShaderDrawOrderUserSlot_High = 223
}
}
12 changes: 12 additions & 0 deletions Plugins/FIFA23Plugin/Meshes/FIFA23CompositeMeshReader.cs
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FIFA23Plugin.Meshes
{
public class FIFA23CompositeMeshReader
{
}
}

0 comments on commit 8cb05e2

Please sign in to comment.