add config file

This commit is contained in:
bird_egop
2026-06-07 00:41:16 +03:00
parent 4e45bd236c
commit 8bed73fb5a
10 changed files with 169 additions and 35 deletions
@@ -1,6 +1,7 @@
using System.Numerics;
using MshLib;
using NResLib;
using NResUI.Abstractions;
using NResUI.Rendering.Viewport.Meshes;
using Silk.NET.OpenGL;
@@ -11,7 +12,7 @@ public static class MshViewportLoader
private const int DefaultModelState = 0;
private const int DefaultLod = 0;
public static MshViewportLoadResult LoadFromFile(GL gl, string path)
public static MshViewportLoadResult LoadFromFile(GL gl, string path, IConfigProvider configProvider)
{
try
{
@@ -30,7 +31,7 @@ public static class MshViewportLoader
}
fs.Seek(0, SeekOrigin.Begin);
var pieces = LoadModelPieces(gl, fs, archive, path);
var pieces = LoadModelPieces(gl, fs, archive, path, configProvider);
if (pieces.Count == 0)
return MshViewportLoadResult.Failure("MSH was parsed, but no renderable geometry pieces were found.", path);
@@ -42,7 +43,9 @@ public static class MshViewportLoader
}
}
private static List<ViewportPiece> LoadModelPieces(GL gl, FileStream fs, NResArchive archive, string path)
private static List<ViewportPiece> LoadModelPieces(
GL gl, FileStream fs, NResArchive archive, string path, IConfigProvider configProvider
)
{
var nodes = Msh0x01.ReadComponent(fs, archive);
var geometry = Msh0x02.ReadComponent(fs, archive);
@@ -54,7 +57,7 @@ public static class MshViewportLoader
var restPoses = MshRestPoseBuilder.BuildRestPose(nodes, animationDescriptors);
var mshToViewportTransform = Matrix4x4.CreateRotationX(-MathF.PI * 0.5f);
var names = TryReadNames(fs, archive);
var materialLibrary = WeaMaterialLibrary.TryLoadForMsh(gl, path);
var materialLibrary = WeaMaterialLibrary.TryLoadForMsh(gl, path, configProvider);
var pieces = new List<ViewportPiece>();
@@ -1,5 +1,6 @@
using MaterialLib;
using NResLib;
using NResUI.Abstractions;
using NResUI.Rendering.Viewport.OpenGL;
using Silk.NET.OpenGL;
using TexmLib;
@@ -24,7 +25,7 @@ public sealed class WeaMaterialLibrary
: null;
}
public static WeaMaterialLibrary TryLoadForMsh(GL gl, string mshPath)
public static WeaMaterialLibrary TryLoadForMsh(GL gl, string mshPath, IConfigProvider configProvider)
{
var weaPath = FindMatchingWeaPath(mshPath);
if (!File.Exists(weaPath))
@@ -34,9 +35,13 @@ public sealed class WeaMaterialLibrary
if (materialRefs.Count == 0)
return Empty;
// TODO: Hardcoded for now
var materialLibFs = "C:\\IronStrategy\\Material.lib";
var materialLibFs = Path.Combine(configProvider.GetConfig().GameBasePath, "Material.lib");
if (!File.Exists(materialLibFs))
{
return new WeaMaterialLibrary([]);
}
using var materialFs = new FileStream(materialLibFs, FileMode.Open, FileAccess.Read, FileShare.Read);
var parseResult = NResParser.ReadFile(materialFs);
if (parseResult.Archive == null)
@@ -46,7 +51,7 @@ public sealed class WeaMaterialLibrary
foreach (var materialRef in materialRefs)
{
var texture = TryLoadMaterialTexture(gl, materialFs, parseResult.Archive, materialRef.Name, out var texm);
var texture = TryLoadMaterialTexture(gl, materialFs, parseResult.Archive, materialRef.Name, configProvider, out var texm);
result[materialRef.Id] = texture != null
? new ViewportMaterial(materialRef.Name, texture.Value, texm)
: new ViewportMaterial(materialRef.Name);
@@ -194,7 +199,8 @@ public sealed class WeaMaterialLibrary
}
private static uint? TryLoadMaterialTexture(
GL gl, FileStream materialFs, NResArchive materialArchive, string materialName, out TexmFile? texm
GL gl, FileStream materialFs, NResArchive materialArchive, string materialName, IConfigProvider configProvider,
out TexmFile? texm
)
{
texm = null;
@@ -212,7 +218,12 @@ public sealed class WeaMaterialLibrary
var texture = materialFile.Stages[0].TextureName;
var textureLibFs = "C:\\IronStrategy\\Textures.lib";
var textureLibFs = Path.Combine(configProvider.GetConfig().GameBasePath, "Textures.lib");
if (!File.Exists(textureLibFs))
{
return null;
}
using var textureFs = new FileStream(textureLibFs, FileMode.Open, FileAccess.Read, FileShare.Read);
var textureResult = NResParser.ReadFile(textureFs);
@@ -269,27 +280,5 @@ public sealed class WeaMaterialLibrary
Normalize(x.FileName) == normalized);
}
private static string? FindTexturesLib(string? startDirectory)
{
if (string.IsNullOrWhiteSpace(startDirectory))
return null;
var directory = new DirectoryInfo(startDirectory);
while (directory != null)
{
var candidate = Path.Combine(directory.FullName, "Textures.lib");
if (Directory.Exists(candidate))
return candidate;
candidate = Path.Combine(directory.FullName, "textures.lib");
if (Directory.Exists(candidate))
return candidate;
directory = directory.Parent;
}
return null;
}
private readonly record struct WeaMaterialRef(int Id, string Name);
}