Use new resource manager in mission and textureloader

This commit is contained in:
Jarrod Doyle 2024-08-24 17:10:08 +01:00
parent 62ff5306fe
commit 9fd76815a2
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 42 additions and 75 deletions

View File

@ -1,4 +1,5 @@
using Godot;
using KeepersCompound.LGS;
using KeepersCompound.LGS.Database;
using KeepersCompound.LGS.Database.Chunks;
using KeepersCompound.TMV.UI;
@ -38,17 +39,19 @@ public partial class Mission : Node3D
[Export]
public bool Dump = false;
InstallPaths _installPaths;
ResourcePathManager _installPaths;
DbFile _file;
TextureLoader _textureLoader;
public override void _Ready()
{
var extractPath = ProjectSettings.GlobalizePath($"user://extracted/tmp");
_installPaths = new ResourcePathManager(extractPath);
var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector;
missionSelector.LoadMission += (string rootPath, string missionPath) =>
{
_installPaths = new InstallPaths(rootPath);
var inited = _installPaths.Init(rootPath);
GD.Print($"Inited paths: {inited}");
FileName = missionPath;
Build = true;
};
@ -91,7 +94,8 @@ public partial class Mission : Node3D
{
ClearMap();
_textureLoader = new TextureLoader(_installPaths.famPath, FileName.GetBaseDir());
var fmName = FileName.GetBaseDir().GetFile();
_textureLoader = new TextureLoader(fmName);
_file = new(FileName);
UseChunk<TxList>("TXLIST", LoadTextures);
UseChunk<WorldRep>("WREXT", BuildWrMeshes);
@ -441,7 +445,7 @@ public partial class Mission : Node3D
for (var i = 0; i < count; i++)
{
var item = textureList.Items[i];
var path = "/";
var path = "";
for (var j = 0; j < item.Tokens.Length; j++)
{
var token = item.Tokens[j];
@ -454,7 +458,7 @@ public partial class Mission : Node3D
}
path += item.Name;
if (!_textureLoader.Load(i, path))
if (!_textureLoader.Load(_installPaths, i, path))
{
GD.Print($"Failed to load texture: {path}");
}

View File

@ -1,45 +1,21 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV;
public partial class TextureLoader
{
private readonly string _userTexturesPath;
private readonly string _rootFamPath; // TODO: Load from installation resources
private readonly string _fmPath;
private readonly Dictionary<string, string> _rootTexturePaths = new();
private readonly Dictionary<string, string> _fmTexturePaths = new();
private readonly string _fmName;
private readonly List<ImageTexture> _textureCache = new();
private readonly Dictionary<int, int> _idMap = new();
private readonly Dictionary<string, int> _pathMap = new();
public TextureLoader(string rootFamPath, string fmPath)
public TextureLoader(string fmName)
{
_rootFamPath = rootFamPath;
_fmPath = fmPath;
_userTexturesPath = ProjectSettings.GlobalizePath($"user://textures/tmp");
ExtractRootFamFiles();
_fmName = fmName;
LoadDefaultTexture();
RegisterTexturePaths(_fmPath, _fmTexturePaths);
RegisterTexturePaths(_userTexturesPath, _rootTexturePaths);
}
private void ExtractRootFamFiles()
{
var dir = new DirectoryInfo(_userTexturesPath);
if (dir.Exists)
{
dir.Delete(true);
}
var zip = ZipFile.OpenRead(_rootFamPath);
zip.ExtractToDirectory(_userTexturesPath.PathJoin("fam"));
}
private void LoadDefaultTexture()
@ -50,46 +26,28 @@ public partial class TextureLoader
_textureCache.Add(texture);
}
private static void RegisterTexturePaths(string rootDir, Dictionary<string, string> map)
{
// TODO: Load DDS BMP GIF CEL
string[] validExtensions = { "png", "tga", "pcx", "gif" };
var famOptions = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive };
var textureOptions = new EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true,
};
foreach (var dirPath in Directory.EnumerateDirectories(rootDir, "fam", famOptions))
{
foreach (var path in Directory.EnumerateFiles(dirPath, "*", textureOptions))
{
if (validExtensions.Contains(path.GetExtension().ToLower()))
{
// TODO: This only adds the first one found rather than the highest priority
var key = path.TrimPrefix(rootDir).GetBaseName().ToLower();
map.TryAdd(key, path);
}
}
}
GD.Print($"Registered {map.Count} texture paths at : {rootDir}");
}
public bool Load(int id, string path)
public bool Load(ResourcePathManager installManager, int id, string path)
{
var loaded = false;
if (_fmTexturePaths.TryGetValue(path.ToLower(), out var fmTexPath))
string texPath;
if (_fmName != null)
{
_textureCache.Add(LoadTexture(fmTexPath));
texPath = installManager.GetTexturePath(_fmName, path);
texPath ??= installManager.GetTexturePath(path);
}
else
{
texPath = installManager.GetTexturePath(path);
}
if (texPath != null)
{
var texture = LoadTexture(texPath);
if (texture != null)
{
_textureCache.Add(texture);
loaded = true;
}
else if (_rootTexturePaths.TryGetValue(path.ToLower(), out var rootTexPath))
{
_textureCache.Add(LoadTexture(rootTexPath));
loaded = true;
}
var index = loaded ? _textureCache.Count - 1 : 0;
@ -101,6 +59,9 @@ public partial class TextureLoader
public static ImageTexture LoadTexture(string path)
{
var ext = path.GetExtension().ToLower();
string[] validExtensions = { "png", "tga", "pcx", "gif" };
if (validExtensions.Contains(ext))
{
var texture = ext switch
{
"pcx" => LoadPcx(path),
@ -109,6 +70,8 @@ public partial class TextureLoader
};
return texture;
}
return null;
}
public ImageTexture Get(int id)
{