Skip to content

Commit

Permalink
Let DS1 PTDE unreferenced textures get found and sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowth117 committed Sep 11, 2023
1 parent 5478ca7 commit 968a1ad
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions AquaModelLibrary/Extra/FromSoft/MSBModelExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static void ExtractMSBMapModels(string outDir, string filePath, byte[] fi
List<AquaNode> unrefAqnList = new List<AquaNode>();
List<string> unrefModelNames = new List<string>();
List<string> texNames = new List<string>();
List<string> unrefTexNames = new List<string>();
List<List<Matrix4x4>> instanceTransformList = new List<List<Matrix4x4>>();
IMsb msb = null;
Dictionary<string, List<Matrix4x4>> objectTransformsDict = new Dictionary<string, List<Matrix4x4>>();
Expand Down Expand Up @@ -109,7 +110,7 @@ public static void ExtractMSBMapModels(string outDir, string filePath, byte[] fi
var id = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(modelFile)).ToLower();
if (objectTransformsDict.ContainsKey(id) || extractUnreferencedMapData)
{
GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texNames, unrefAqpList, unrefAqnList, unrefModelNames, Path.GetExtension(modelFile), Path.GetFileNameWithoutExtension(modelFile).ToLower(), true, File.ReadAllBytes(modelFile));
GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texNames, unrefAqpList, unrefAqnList, unrefModelNames, unrefTexNames, Path.GetExtension(modelFile), Path.GetFileNameWithoutExtension(modelFile).ToLower(), true, File.ReadAllBytes(modelFile));
}
}
foreach (var texFile in tpfFiles)
Expand Down Expand Up @@ -142,14 +143,17 @@ public static void ExtractMSBMapModels(string outDir, string filePath, byte[] fi
var foundKey = objectTransformsDict.ContainsKey(id);
if (foundKey || extractUnreferencedMapData)
{
GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texNames, unrefAqpList, unrefAqnList, unrefModelNames, Path.GetExtension(modelFile), Path.GetFileNameWithoutExtension(modelFile).ToLower(), foundKey, File.ReadAllBytes(modelFile));
GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texNames, unrefAqpList, unrefAqnList, unrefModelNames, unrefTexNames, Path.GetExtension(modelFile), Path.GetFileNameWithoutExtension(modelFile).ToLower(), foundKey, File.ReadAllBytes(modelFile));
}
}
foreach (var texFile in texFilePaths)
{
var id = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(texFile)).ToLower();
//Purposely no extractUnreferencedMapData check. We do NOT want every map texture in Dark Souls 1 dumped here...
if (texNames.Contains(id))
{
GatherTexturesFromTPF(texNames, outPathDirectory, Path.GetExtension(texFile), Path.GetFileNameWithoutExtension(texFile), File.ReadAllBytes(texFile));
} else if(unrefTexNames.Contains(id))
{
GatherTexturesFromTPF(texNames, outPathDirectory, Path.GetExtension(texFile), Path.GetFileNameWithoutExtension(texFile), File.ReadAllBytes(texFile));
}
Expand All @@ -174,7 +178,7 @@ public static void ExtractMSBMapModels(string outDir, string filePath, byte[] fi
var texBdtPath = Path.Combine(rootPath, $"model/map/{fileNameTex}.tpfbdt");
using (var bxf = new BXF4Reader(modelBhdPath, modelBdtPath))
{
GatherModelsFromBinder(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, bxf, texNames, unrefAqpList, unrefAqnList, unrefModelNames);
GatherModelsFromBinder(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, bxf, texNames, unrefAqpList, unrefAqnList, unrefModelNames, unrefTexNames);
}
using (var bxf = new BXF4Reader(texBhdPath, texBdtPath))
{
Expand Down Expand Up @@ -208,7 +212,7 @@ public static void ExtractMSBMapModels(string outDir, string filePath, byte[] fi
var foundKey = objectTransformsDict.ContainsKey(id);
if (foundKey || extractUnreferencedMapData)
{
GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texNames, unrefAqpList, unrefAqnList, unrefModelNames, Path.GetExtension(modelFile), Path.GetFileNameWithoutExtension(modelFile).ToLower(), foundKey, File.ReadAllBytes(modelFile));
GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texNames, unrefAqpList, unrefAqnList, unrefModelNames, unrefTexNames, Path.GetExtension(modelFile), Path.GetFileNameWithoutExtension(modelFile).ToLower(), foundKey, File.ReadAllBytes(modelFile));
}
}
foreach (var bhd in texBhds)
Expand Down Expand Up @@ -274,7 +278,7 @@ public static void ExtractMSBMapModels(string outDir, string filePath, byte[] fi
}
using (var mapBnd = new BND4Reader(fileBytes))
{
GatherModelsFromBinder(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, mapBnd, texNames, unrefAqpList, unrefAqnList, unrefModelNames);
GatherModelsFromBinder(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, mapBnd, texNames, unrefAqpList, unrefAqnList, unrefModelNames, unrefTexNames);
}
}
}
Expand Down Expand Up @@ -346,7 +350,7 @@ private static void ProcessModelsForExport(AquaUtil aqu, List<AquaObject> aqpLis
}
}

private static void GatherModelsFromBinder(bool useMetaData, List<AquaObject> aqpList, List<AquaNode> aqnList, List<string> modelNames, List<List<Matrix4x4>> instanceTransformList, Dictionary<string, List<Matrix4x4>> objectTransformsDict, BinderReader bxf, List<string> texFilenameList, List<AquaObject> unrefAqpList, List<AquaNode> unrefAqnList, List<string> unrefModelNames)
private static void GatherModelsFromBinder(bool useMetaData, List<AquaObject> aqpList, List<AquaNode> aqnList, List<string> modelNames, List<List<Matrix4x4>> instanceTransformList, Dictionary<string, List<Matrix4x4>> objectTransformsDict, BinderReader bxf, List<string> texFilenameList, List<AquaObject> unrefAqpList, List<AquaNode> unrefAqnList, List<string> unrefModelNames, List<string> unrefTexFilenameList)
{
foreach (var bxfFile in bxf.Files)
{
Expand All @@ -356,11 +360,11 @@ private static void GatherModelsFromBinder(bool useMetaData, List<AquaObject> aq
bool foundKey = objectTransformsDict.ContainsKey(bFNameSansExt);
var fileBytes = bxf.ReadFile(bxfFile);

GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texFilenameList, unrefAqpList, unrefAqnList, unrefModelNames, ext, bFNameSansExt, foundKey, fileBytes);
GatherModel(useMetaData, aqpList, aqnList, modelNames, instanceTransformList, objectTransformsDict, texFilenameList, unrefAqpList, unrefAqnList, unrefModelNames, unrefTexFilenameList, ext, bFNameSansExt, foundKey, fileBytes);
}
}

private static void GatherModel(bool useMetaData, List<AquaObject> aqpList, List<AquaNode> aqnList, List<string> modelNames, List<List<Matrix4x4>> instanceTransformList, Dictionary<string, List<Matrix4x4>> objectTransformsDict, List<string> texFilenameList, List<AquaObject> unrefAqpList, List<AquaNode> unrefAqnList, List<string> unrefModelNames, string ext, string bFNameSansExt, bool foundKey, byte[] fileBytes)
private static void GatherModel(bool useMetaData, List<AquaObject> aqpList, List<AquaNode> aqnList, List<string> modelNames, List<List<Matrix4x4>> instanceTransformList, Dictionary<string, List<Matrix4x4>> objectTransformsDict, List<string> texFilenameList, List<AquaObject> unrefAqpList, List<AquaNode> unrefAqnList, List<string> unrefModelNames, List<string> unrefTexFilenameList, string ext, string bFNameSansExt, bool foundKey, byte[] fileBytes)
{
if (ext == ".dcx")
{
Expand All @@ -385,17 +389,26 @@ private static void GatherModel(bool useMetaData, List<AquaObject> aqpList, List
}

//Gather textures that are actually referenced by the model
if (foundKey)
if (foundKey || extractUnreferencedMapData)
{
foreach (var mesh in flver.Meshes)
{
var mat = flver.Materials[mesh.MaterialIndex];
foreach (var tex in mat.Textures)
{
var texName = Path.GetFileNameWithoutExtension(tex.Path).ToLower();
if (!texFilenameList.Contains(texName))
if(foundKey)
{
if (!texFilenameList.Contains(texName))
{
texFilenameList.Add(texName);
}
} else
{
texFilenameList.Add(texName);
if (!unrefTexFilenameList.Contains(texName))
{
unrefTexFilenameList.Add(texName);
}
}
}
}
Expand Down

0 comments on commit 968a1ad

Please sign in to comment.