Closed ArchLeaders closed 1 year ago
Update: at some point it was failing when loading a BYML, but currently it fails after loading all the fields and instead crashes loading the havok objects.
I tried for a while to fix it, and got pretty close too, but I still eventually ran into a memory exception.
Here's the code that got me closest. I removed all the object storage and just wrote the file as soon as the information was gathered. But based on the debugger it still looks like something isn't being deallocated.
Anything stick out to you here?
public static void CacheFieldCollision(string cacheDir)
{
Directory.CreateDirectory(cacheDir);
foreach (string fieldName in GlobalData.FieldNames) {
foreach (string sectionName in GlobalData.SectionNames) {
MapCollisionLoader[] collisionLoaders = new MapCollisionLoader[4];
for (int i = 0; i < 4; i++) {
string path = PluginConfig.GetContentPath($"Physics/StaticCompound/{fieldName}/{sectionName}-{i}.shksc");
MapCollisionLoader loader = new();
using FileStream fs = File.OpenRead(path);
loader.Load(fs, Path.GetFileName(path));
collisionLoaders[i] = loader;
}
foreach (string muuntEnding in GlobalData.MuuntEndings) {
string path = PluginConfig.GetContentPath($"Map/{fieldName}/{sectionName}/{sectionName}_{muuntEnding}.smubin");
using FileStream fs = File.OpenRead(path);
STFileLoader.Settings mubinSettings = STFileLoader.TryDecompressFile(fs, Path.GetFileName(path));
if (mubinSettings.Stream == null) {
StudioLogger.WriteError($"Trouble reading {path}!");
continue;
}
BymlFile mubin = BymlFile.FromBinary(mubinSettings.Stream);
mubinSettings.Stream.Dispose();
ProcessMapUnit(cacheDir, mubin, collisionLoaders);
}
GC.Collect();
}
}
}
private static void ProcessMapUnit(string cacheDir, BymlFile mubin, MapCollisionLoader[] collisionLoaders)
{
List<BymlNode> objects = mubin.RootNode.Hash["Objs"].Array;
foreach (var obj in objects) {
string actorName = obj.Hash["UnitConfigName"].String;
if (!_cached.Contains(actorName)) {
for (int i = 0; i < 4; i++) {
MapCollisionLoader collisionLoader = new();
collisionLoader.CreateForCaching();
BakedCollisionShapeCacheable[] infoGroup = collisionLoaders[i].GetCacheables(obj.Hash["HashId"].UInt);
if (infoGroup == null) {
continue;
}
foreach (var info in infoGroup) {
collisionLoader.AddShape(info, 0, Vector3.Zero, Quaternion.Identity, Vector3.One);
}
using FileStream fs = File.Create(
Path.Combine(cacheDir, $"{obj.Hash["UnitConfigName"].String}.hksc"));
collisionLoader.Save(fs);
}
_cached.Add(actorName);
}
}
}
Hopefully this has nothing to do with it xD
I have a lot of memory (about 64GB), but I get this error when clicking "Cache Baked Collision" on the
MubinToolSettings
menu.Looks like this might just be an issue with allocating every byml in every field... but, Idk how it works so I can't do much to try and fix it without breaking it.