Skip to content

Commit 4f09dba

Browse files
committed
- Item Convert On Import initial work
- Perf fixes for item loading.
1 parent 7a344c1 commit 4f09dba

File tree

8 files changed

+209
-49
lines changed

8 files changed

+209
-49
lines changed

xivModdingFramework/Cache/XivDependencyGraph.cs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
using xivModdingFramework.Models.DataContainers;
1818
using xivModdingFramework.Models.FileTypes;
1919
using xivModdingFramework.Mods;
20+
using xivModdingFramework.Mods.DataContainers;
21+
using xivModdingFramework.Mods.FileTypes;
2022
using xivModdingFramework.Resources;
23+
using xivModdingFramework.SqPack.DataContainers;
2124
using xivModdingFramework.SqPack.FileTypes;
2225
using xivModdingFramework.Variants.FileTypes;
2326
using static xivModdingFramework.Cache.XivCache;
@@ -492,14 +495,25 @@ public override int GetHashCode()
492495
/// Gets all the model files in this dependency chain.
493496
/// </summary>
494497
/// <returns></returns>
495-
public async Task<List<string>> GetModelFiles()
498+
public async Task<List<string>> GetModelFiles(IndexFile index = null, ModList modlist = null)
496499
{
497500
// Some chains have no meta entries, and jump straight to models.
498501
// Try to resolve Meta files first.
499502
if (Info.PrimaryType == XivItemType.equipment || Info.PrimaryType == XivItemType.accessory)
500503
{
501504
var _eqp = new Eqp(XivCache.GameInfo.GameDirectory);
502-
var races = await _eqp.GetAvailableRacialModels(Info.PrimaryId, Info.Slot, false, true);
505+
506+
List<XivRace> races = null;
507+
if (index != null)
508+
{
509+
var metadata = await ItemMetadata.GetFromCachedIndex(this, index);
510+
races = metadata.EqdpEntries.Where(x => x.Value.bit1).Select(x => x.Key).ToList();
511+
}
512+
else
513+
{
514+
races = await _eqp.GetAvailableRacialModels(Info.PrimaryId, Info.Slot, false, true);
515+
}
516+
503517
var models = new List<string>();
504518
foreach(var race in races)
505519
{
@@ -523,8 +537,13 @@ public async Task<List<string>> GetModelFiles()
523537
if (Info.PrimaryType == XivItemType.human && Info.SecondaryType != XivItemType.hair && Info.SecondaryId / 100 >= 1)
524538
{
525539
// For human types, if their model is missing, the version 00xx is used instead.
526-
var index = new Index(XivCache.GameInfo.GameDirectory);
527-
if(!(await index.FileExists(modelPath)))
540+
if(index == null)
541+
{
542+
var _index = new Index(XivCache.GameInfo.GameDirectory);
543+
index = await _index.GetIndexFile(IOUtil.GetDataFileFromPath(modelPath), false, true);
544+
}
545+
546+
if(!(index.FileExists(modelPath)))
528547
{
529548
var replacementNumber = (Info.SecondaryId % 100);
530549
var alteredRoot = new XivDependencyRoot(Info.PrimaryType, Info.PrimaryId, Info.SecondaryType, replacementNumber, Info.Slot);
@@ -546,8 +565,9 @@ public async Task<List<string>> GetModelFiles()
546565
/// Subsets of this data may be accessed with XivDependencyGraph::GetChildFiles(internalFilePath).
547566
/// </summary>
548567
/// <returns></returns>
549-
public async Task<List<string>> GetMaterialFiles(int materialVariant = -1)
568+
public async Task<List<string>> GetMaterialFiles(int materialVariant = -1, IndexFile index = null, ModList modlist = null)
550569
{
570+
var useCache = index == null;
551571

552572
var materials = new HashSet<string>();
553573
if (Info.PrimaryType == XivItemType.human && Info.SecondaryType == XivItemType.body)
@@ -560,8 +580,13 @@ public async Task<List<string>> GetMaterialFiles(int materialVariant = -1)
560580
var path= $"chara/human/c{primary}/obj/body/b{body}/material/v0001/mt_c{primary}b{body}_a.mtrl";
561581

562582
// Just validate it exists and call it a day.
563-
var index = new Index(XivCache.GameInfo.GameDirectory);
564-
var exists = await index.FileExists(path);
583+
if (index == null)
584+
{
585+
var _index = new Index(XivCache.GameInfo.GameDirectory);
586+
index = await _index.GetIndexFile(IOUtil.GetDataFileFromPath(path), false, true);
587+
}
588+
589+
var exists = index.FileExists(path);
565590
if(exists)
566591
{
567592
materials.Add(path);
@@ -582,7 +607,7 @@ public async Task<List<string>> GetMaterialFiles(int materialVariant = -1)
582607
}
583608
else
584609
{
585-
var models = await GetModelFiles();
610+
var models = await GetModelFiles(index, modlist);
586611
if (models != null && models.Count > 0)
587612
{
588613
var dataFile = IOUtil.GetDataFileFromPath(models[0]);
@@ -598,7 +623,15 @@ public async Task<List<string>> GetMaterialFiles(int materialVariant = -1)
598623

599624
foreach (var model in models)
600625
{
601-
var mdlMats = await XivCache.GetChildFiles(model);
626+
List<string> mdlMats = null;
627+
if (useCache)
628+
{
629+
mdlMats = await XivCache.GetChildFiles(model);
630+
} else
631+
{
632+
mdlMats = await _mdl.GetReferencedMaterialPaths(model, -1, false, false, index, modlist);
633+
}
634+
602635
if (materialVariant <= 0)
603636
{
604637
foreach (var mat in mdlMats)
@@ -638,11 +671,15 @@ public async Task<List<string>> GetMaterialFiles(int materialVariant = -1)
638671
}
639672

640673
// Here we get to get a little fancy.
641-
var _modding = new Modding(XivCache.GameInfo.GameDirectory);
642-
var modList = _modding.GetModList();
674+
if (modlist == null)
675+
{
676+
var _modding = new Modding(XivCache.GameInfo.GameDirectory);
677+
modlist = _modding.GetModList();
678+
}
679+
643680
var rootFolder = Info.GetRootFolder();
644681
var variantRep = "v" + materialVariant.ToString().PadLeft(4, '0');
645-
foreach (var mod in modList.Mods)
682+
foreach (var mod in modlist.Mods)
646683
{
647684
if (!mod.enabled) continue;
648685

@@ -673,15 +710,25 @@ public async Task<List<string>> GetMaterialFiles(int materialVariant = -1)
673710
/// Subsets of this data may be accessed with XivDependencyGraph::GetChildFiles(internalFilePath).
674711
/// </summary>
675712
/// <returns></returns>
676-
public async Task<List<string>> GetTextureFiles(int materialVariant = -1)
713+
public async Task<List<string>> GetTextureFiles(int materialVariant = -1, IndexFile index = null, ModList modlist = null)
677714
{
678-
var materials = await GetMaterialFiles(materialVariant);
715+
var materials = await GetMaterialFiles(materialVariant, index, modlist);
679716
var textures = new HashSet<string>();
680717
if (materials != null && materials.Count > 0)
681718
{
682719
foreach (var mat in materials)
683720
{
684-
var mtrlTexs = await XivCache.GetChildFiles(mat);
721+
List<string> mtrlTexs = null;
722+
if (index == null)
723+
{
724+
mtrlTexs = await XivCache.GetChildFiles(mat);
725+
} else
726+
{
727+
var dataFile = IOUtil.GetDataFileFromPath(mat);
728+
var _mtrl = new Mtrl(XivCache.GameInfo.GameDirectory, dataFile, XivCache.GameInfo.GameLanguage);
729+
mtrlTexs = await _mtrl.GetTexturePathsFromMtrlPath(mat, false, false, index, modlist);
730+
}
731+
685732
foreach (var tex in mtrlTexs)
686733
{
687734
textures.Add(tex);

xivModdingFramework/Materials/FileTypes/Mtrl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ public async Task<XivMtrl> GetMtrlData(string mtrlFile, int materialSet = -1, in
295295
/// </summary>
296296
/// <param name="mtrlPath"></param>
297297
/// <returns></returns>
298-
public async Task<List<string>> GetTexturePathsFromMtrlPath(string mtrlPath, bool includeDummies = false, bool forceOriginal = false)
298+
public async Task<List<string>> GetTexturePathsFromMtrlPath(string mtrlPath, bool includeDummies = false, bool forceOriginal = false, IndexFile index = null, ModList modlist = null)
299299
{
300300
var dat = new Dat(_gameDirectory);
301-
var mtrlData = await dat.GetType2Data(mtrlPath, forceOriginal);
301+
var mtrlData = await dat.GetType2Data(mtrlPath, forceOriginal, index, modlist);
302302
var uniqueTextures = new HashSet<string>();
303303
var texRegex = new Regex(".*\\.tex$");
304304

xivModdingFramework/Models/FileTypes/Mdl.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
using System.Text.RegularExpressions;
5757
using xivModdingFramework.Materials.DataContainers;
5858
using xivModdingFramework.Cache;
59+
using xivModdingFramework.SqPack.DataContainers;
60+
using xivModdingFramework.Mods.DataContainers;
5961

6062
namespace xivModdingFramework.Models.FileTypes
6163
{
@@ -1691,18 +1693,24 @@ public async Task<XivMdl> GetRawMdlData(string mdlPath, bool getOriginal = false
16911693
/// <param name="mdlPath"></param>
16921694
/// <param name="getOriginal"></param>
16931695
/// <returns></returns>
1694-
public async Task<List<string>> GetReferencedMaterialPaths(string mdlPath, int materialVariant = -1, bool getOriginal = false, bool includeSkin = true)
1696+
public async Task<List<string>> GetReferencedMaterialPaths(string mdlPath, int materialVariant = -1, bool getOriginal = false, bool includeSkin = true, IndexFile index = null, ModList modlist = null)
16951697
{
16961698
// Language is irrelevant here.
16971699
var dataFile = IOUtil.GetDataFileFromPath(mdlPath);
16981700
var _mtrl = new Mtrl(_gameDirectory, dataFile, XivLanguage.None);
16991701
var _imc = new Imc(_gameDirectory);
1700-
var _index = new Index(_gameDirectory);
1702+
if (index == null)
1703+
{
1704+
var _index = new Index(_gameDirectory);
1705+
var _modding = new Modding(_gameDirectory);
1706+
index = await _index.GetIndexFile(dataFile, false, true);
1707+
modlist = await _modding.GetModListAsync();
1708+
}
17011709

17021710
var materials = new List<string>();
17031711

17041712
// Read the raw Material names from the file.
1705-
var materialNames = await GetReferencedMaterialNames(mdlPath, getOriginal);
1713+
var materialNames = await GetReferencedMaterialNames(mdlPath, getOriginal, index, modlist);
17061714
if(materialNames.Count == 0)
17071715
{
17081716
return materials;
@@ -1729,7 +1737,7 @@ public async Task<List<string>> GetReferencedMaterialPaths(string mdlPath, int m
17291737
// We need to get the IMC info for this MDL so that we can pull every possible Material Variant.
17301738
try
17311739
{
1732-
var info = await _imc.GetFullImcInfo(imcPath);
1740+
var info = await _imc.GetFullImcInfo(imcPath, index, modlist);
17331741
var slotRegex = new Regex("_([a-z]{3}).mdl$");
17341742
var slot = "";
17351743
var m = slotRegex.Match(mdlPath);
@@ -1802,19 +1810,30 @@ public async Task<List<string>> GetReferencedMaterialPaths(string mdlPath, int m
18021810
/// <param name="mdlPath"></param>
18031811
/// <param name="getOriginal"></param>
18041812
/// <returns></returns>
1805-
public async Task<List<string>> GetReferencedMaterialNames(string mdlPath, bool getOriginal = false)
1813+
public async Task<List<string>> GetReferencedMaterialNames(string mdlPath, bool getOriginal = false, IndexFile index = null, ModList modlist = null)
18061814
{
18071815
var materials = new List<string>();
1808-
var index = new Index(_gameDirectory);
18091816
var dat = new Dat(_gameDirectory);
18101817
var modding = new Modding(_gameDirectory);
1811-
var mod = await modding.TryGetModEntry(mdlPath);
1812-
var offset = await index.GetDataOffset(mdlPath);
18131818

1814-
var modded = mod != null && mod.enabled;
1819+
1820+
if (index == null)
1821+
{
1822+
var _index = new Index(_gameDirectory);
1823+
index = await _index.GetIndexFile(IOUtil.GetDataFileFromPath(mdlPath), false, true);
1824+
1825+
}
1826+
1827+
var offset = index.Get8xDataOffset(mdlPath);
18151828
if (getOriginal)
18161829
{
1817-
if (modded)
1830+
if(modlist == null)
1831+
{
1832+
modlist = await modding.GetModListAsync();
1833+
}
1834+
1835+
var mod = modlist.Mods.FirstOrDefault(x => x.fullPath == mdlPath);
1836+
if(mod != null)
18181837
{
18191838
offset = mod.data.originalOffset;
18201839
}

xivModdingFramework/Mods/FileTypes/ItemMetadata.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,36 @@ public static async Task<ItemMetadata> GetMetadata(XivDependencyRoot root, bool
172172
}
173173
}
174174

175+
/// <summary>
176+
/// Retrieves the item metadata from a cached index setup (or raw)
177+
/// </summary>
178+
/// <param name="root"></param>
179+
/// <param name="index"></param>
180+
/// <returns></returns>
181+
public static async Task<ItemMetadata> GetFromCachedIndex(XivDependencyRoot root, IndexFile index)
182+
{
183+
184+
var _dat = new Dat(XivCache.GameInfo.GameDirectory);
185+
var df = IOUtil.GetDataFileFromPath(root.Info.GetRootFile());
186+
187+
long offset = 0;
188+
if (index != null)
189+
{
190+
offset = index.Get8xDataOffset(root.Info.GetRootFile());
191+
}
192+
193+
ItemMetadata mData = null;
194+
if (offset == 0)
195+
{
196+
mData = await ItemMetadata.GetMetadata(root);
197+
}
198+
else
199+
{
200+
var data = await _dat.GetType2Data(offset, df);
201+
mData = await ItemMetadata.Deserialize(data);
202+
}
203+
return mData;
204+
}
175205

176206
/// <summary>
177207
/// Creates a new ItemMetaData entry from the constituent files around the FFXIV file system.

0 commit comments

Comments
 (0)