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

View File

@ -1,45 +1,21 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using Godot; using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV; namespace KeepersCompound.TMV;
public partial class TextureLoader public partial class TextureLoader
{ {
private readonly string _userTexturesPath; private readonly string _fmName;
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 List<ImageTexture> _textureCache = new(); private readonly List<ImageTexture> _textureCache = new();
private readonly Dictionary<int, int> _idMap = new(); private readonly Dictionary<int, int> _idMap = new();
private readonly Dictionary<string, int> _pathMap = new(); private readonly Dictionary<string, int> _pathMap = new();
public TextureLoader(string rootFamPath, string fmPath) public TextureLoader(string fmName)
{ {
_rootFamPath = rootFamPath; _fmName = fmName;
_fmPath = fmPath;
_userTexturesPath = ProjectSettings.GlobalizePath($"user://textures/tmp");
ExtractRootFamFiles();
LoadDefaultTexture(); 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() private void LoadDefaultTexture()
@ -50,46 +26,28 @@ public partial class TextureLoader
_textureCache.Add(texture); _textureCache.Add(texture);
} }
private static void RegisterTexturePaths(string rootDir, Dictionary<string, string> map) public bool Load(ResourcePathManager installManager, int id, string path)
{
// 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)
{ {
var loaded = false; 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);
loaded = true; texPath ??= installManager.GetTexturePath(path);
} }
else if (_rootTexturePaths.TryGetValue(path.ToLower(), out var rootTexPath)) else
{ {
_textureCache.Add(LoadTexture(rootTexPath)); texPath = installManager.GetTexturePath(path);
loaded = true; }
if (texPath != null)
{
var texture = LoadTexture(texPath);
if (texture != null)
{
_textureCache.Add(texture);
loaded = true;
}
} }
var index = loaded ? _textureCache.Count - 1 : 0; var index = loaded ? _textureCache.Count - 1 : 0;
@ -101,13 +59,18 @@ public partial class TextureLoader
public static ImageTexture LoadTexture(string path) public static ImageTexture LoadTexture(string path)
{ {
var ext = path.GetExtension().ToLower(); var ext = path.GetExtension().ToLower();
var texture = ext switch string[] validExtensions = { "png", "tga", "pcx", "gif" };
if (validExtensions.Contains(ext))
{ {
"pcx" => LoadPcx(path), var texture = ext switch
"gif" => LoadGif(path), {
_ => ImageTexture.CreateFromImage(Image.LoadFromFile(path)), "pcx" => LoadPcx(path),
}; "gif" => LoadGif(path),
return texture; _ => ImageTexture.CreateFromImage(Image.LoadFromFile(path)),
};
return texture;
}
return null;
} }
public ImageTexture Get(int id) public ImageTexture Get(int id)