Skip to content

Commit

Permalink
Improve error logging when composing models
Browse files Browse the repository at this point in the history
  • Loading branch information
PassiveModding committed Jan 21, 2025
1 parent 3155682 commit 20c761a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
25 changes: 16 additions & 9 deletions Meddle/Meddle.Plugin/Models/Composer/CharacterComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,23 @@ private void HandleModel(GenderRace genderRace, CustomizeParameter customizePara
var materialBuilders = new MaterialBuilder[modelInfo.Materials.Length];
for (int i = 0; i < modelInfo.Materials.Length; i++)
{
var materialInfo = modelInfo.Materials[i];
try
{
var materialInfo = modelInfo.Materials[i];

progress?.Invoke(new ProgressEvent(modelInfo.GetHashCode(), $"{materialInfo.Path.GamePath}", i + 1, modelInfo.Materials.Length));
var mtrlData = dataProvider.LookupData(materialInfo.Path.FullPath);
if (mtrlData == null)
{
Plugin.Logger?.LogWarning("Failed to load material file: {mtrlPath}", materialInfo.Path.FullPath);
throw new Exception($"Failed to load material file: {materialInfo.Path.FullPath}");
throw new Exception($"Failed to load material file {materialInfo.Path.FullPath} returned null");
}

Plugin.Logger?.LogInformation("Loaded material {mtrlPath}", materialInfo.Path.FullPath);
var mtrlFile = new MtrlFile(mtrlData);
var shpkName = mtrlFile.GetShaderPackageName();
var shpkPath = $"shader/sm5/shpk/{shpkName}";
var shpkData = dataProvider.LookupData(shpkPath);
if (shpkData == null) throw new Exception($"Failed to load shader package file: {shpkPath}");
if (shpkData == null) throw new Exception($"Failed to load shader package file {shpkPath} returned null");
var shpkFile = new ShpkFile(shpkData);
var material = new MaterialSet(mtrlFile, materialInfo.Path.GamePath,
shpkFile,
Expand Down Expand Up @@ -167,7 +166,7 @@ private void HandleModel(GenderRace genderRace, CustomizeParameter customizePara
}
catch (Exception e)
{
Plugin.Logger?.LogError(e, "Failed to load material {MaterialInfo}", JsonSerializer.Serialize(modelInfo.Materials[i], jsonOptions));
Plugin.Logger?.LogError(e, "Failed to load material\n{Message}\n{MaterialInfo}", e.Message, JsonSerializer.Serialize(modelInfo.Materials[i], jsonOptions));
materialBuilders[i] = new MaterialBuilder("error");
}
}
Expand All @@ -179,7 +178,11 @@ private void HandleModel(GenderRace genderRace, CustomizeParameter customizePara
{
// custom pbd may exist
var pbdFileData = dataProvider.LookupData(modelInfo.Deformer.Value.PbdPath);
if (pbdFileData == null) throw new InvalidOperationException($"Failed to get deformer pbd {modelInfo.Deformer.Value.PbdPath}");
if (pbdFileData == null)
{
throw new InvalidOperationException($"Failed to get deformer pbd {modelInfo.Deformer.Value.PbdPath} returned null");
}

deform = ((GenderRace)modelInfo.Deformer.Value.DeformerId, (GenderRace)modelInfo.Deformer.Value.RaceSexId, new RaceDeformer(new PbdFile(pbdFileData), bones));
Plugin.Logger?.LogDebug("Using deformer pbd {Path}", modelInfo.Deformer.Value.PbdPath);
}
Expand Down Expand Up @@ -427,7 +430,7 @@ private bool HandleRootAttach(ParsedCharacterInfo characterInfo, SceneBuilder sc
}
catch (Exception e)
{
Plugin.Logger?.LogError(e, "Failed to handle model {ModelInfo}", JsonSerializer.Serialize(
Plugin.Logger?.LogError(e, "Failed to handle model\n{Message}\n{ModelInfo}", e.Message, JsonSerializer.Serialize(
t, new JsonSerializerOptions
{
IncludeFields = true
Expand Down Expand Up @@ -481,7 +484,7 @@ public void ComposeModels(ParsedModelInfo[] models, GenderRace genderRace, Custo
}
catch (Exception e)
{
Plugin.Logger?.LogError(e, "Failed to handle model {ModelInfo}", JsonSerializer.Serialize(t, jsonOptions));
Plugin.Logger?.LogError(e, "Failed to handle model\n{Message}\n{ModelInfo}", e.Message, JsonSerializer.Serialize(t, jsonOptions));
}
}
}
Expand All @@ -503,11 +506,15 @@ private void EnsureBonesExist(Model model, List<BoneNodeBuilder> bones, BoneNode
{
if (bones.All(b => !b.BoneName.Equals(boneName, StringComparison.Ordinal)))
{
if (root == null)
{
throw new InvalidOperationException($"Root bone not found when generating missing bone {boneName} for {model.Path}");
}

var bone = new BoneNodeBuilder(boneName)
{
IsGenerated = true
};
if (root == null) throw new InvalidOperationException($"Root bone not found when generating missing bone {boneName} for {model.Path}");

var parent = FindLogicalParent(model, bone, bones) ?? root;
parent.AddNode(bone);
Expand Down
9 changes: 7 additions & 2 deletions Meddle/Meddle.Plugin/Models/Composer/InstanceComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class InstanceComposer : IDisposable
private int countProgress;
private bool arrayTexturesSaved;
private static readonly object StaticFileLock = new();

private void SaveArrayTextures()
{
if (arrayTexturesSaved) return;
Expand Down Expand Up @@ -138,7 +139,7 @@ public void Compose(SceneBuilder scene)
}
catch (Exception ex)
{
Plugin.Logger?.LogError(ex, "Failed to compose instance {instanceId} {instanceType}", instance.Id, instance.Type);
Plugin.Logger?.LogError(ex, "Failed to compose instance {instanceId} {instanceType}\n{Message}", instance.Id, instance.Type, ex.Message);
}

//countProgress++;
Expand Down Expand Up @@ -371,7 +372,11 @@ private void ComposeTerrainInstance(ParsedTerrainInstance terrainInstance, Scene
var plateTransform = new Transform(new Vector3(platePos.X, 0, platePos.Y), Quaternion.Identity, Vector3.One);
var mdlPath = $"{terrainInstance.Path.GamePath}/bgplate/{i:D4}.mdl";
var mdlData = dataProvider.LookupData(mdlPath);
if (mdlData == null) throw new Exception($"Failed to load model file: {mdlPath}");
if (mdlData == null)
{
throw new Exception($"Failed to load model file {mdlPath} returned null");
}

Plugin.Logger?.LogInformation("Loaded model {mdlPath}", mdlPath);
var mdlFile = new MdlFile(mdlData);

Expand Down

0 comments on commit 20c761a

Please sign in to comment.