Load textures from root thief resource CRF

This commit is contained in:
Jarrod Doyle 2024-08-13 19:11:03 +01:00
parent 9db1bc6681
commit 6f62d2ca00
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 40 additions and 20 deletions

View File

@ -38,15 +38,18 @@ public partial class Mission : Node3D
[Export] [Export]
public bool Dump = false; public bool Dump = false;
InstallPaths _installPaths;
DbFile _file; DbFile _file;
TextureLoader _textureLoader; TextureLoader _textureLoader;
public override void _Ready() public override void _Ready()
{ {
var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector; var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector;
missionSelector.LoadMission += (string path) => missionSelector.LoadMission += (string rootPath, string missionPath) =>
{ {
FileName = path; _installPaths = new InstallPaths(rootPath);
FileName = missionPath;
Build = true; Build = true;
}; };
} }
@ -88,7 +91,7 @@ public partial class Mission : Node3D
{ {
ClearMap(); ClearMap();
_textureLoader = new TextureLoader("", FileName.GetBaseDir()); _textureLoader = new TextureLoader(_installPaths.famPath, FileName.GetBaseDir());
_file = new(FileName); _file = new(FileName);
var textureList = (TxList)_file.Chunks["TXLIST"]; var textureList = (TxList)_file.Chunks["TXLIST"];

View File

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using Godot; using Godot;
@ -7,21 +8,38 @@ namespace KeepersCompound.TMV;
public partial class TextureLoader public partial class TextureLoader
{ {
private readonly string _rootPath; // TODO: Load from installation resources private readonly string _userTexturesPath;
private readonly string _rootFamPath; // TODO: Load from installation resources
private readonly string _fmPath; private readonly string _fmPath;
private readonly Dictionary<string, string> _rootTexturePaths = new();
private readonly Dictionary<string, string> _fmTexturePaths = 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 rootPath, string fmPath) public TextureLoader(string rootFamPath, string fmPath)
{ {
_rootPath = rootPath; _rootFamPath = rootFamPath;
_fmPath = fmPath; _fmPath = fmPath;
_userTexturesPath = ProjectSettings.GlobalizePath($"user://textures/tmp");
ExtractRootFamFiles();
LoadDefaultTexture(); LoadDefaultTexture();
RegisterFmTexturePaths(); 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()
@ -32,7 +50,7 @@ public partial class TextureLoader
_textureCache.Add(texture); _textureCache.Add(texture);
} }
private void RegisterFmTexturePaths() private static void RegisterTexturePaths(string rootDir, Dictionary<string, string> map)
{ {
// TODO: Load DDS BMP GIF CEL // TODO: Load DDS BMP GIF CEL
string[] validExtensions = { "png", "tga", "pcx" }; string[] validExtensions = { "png", "tga", "pcx" };
@ -44,34 +62,33 @@ public partial class TextureLoader
RecurseSubdirectories = true, RecurseSubdirectories = true,
}; };
foreach (var dirPath in Directory.EnumerateDirectories(_fmPath, "fam", famOptions)) foreach (var dirPath in Directory.EnumerateDirectories(rootDir, "fam", famOptions))
{ {
foreach (var path in Directory.EnumerateFiles(dirPath, "*", textureOptions)) foreach (var path in Directory.EnumerateFiles(dirPath, "*", textureOptions))
{ {
if (validExtensions.Contains(path.GetExtension().ToLower())) if (validExtensions.Contains(path.GetExtension().ToLower()))
{ {
// TODO: This only adds the first one found rather than the highest priority // TODO: This only adds the first one found rather than the highest priority
var key = path.TrimPrefix(_fmPath).GetBaseName().ToLower(); var key = path.TrimPrefix(rootDir).GetBaseName().ToLower();
_fmTexturePaths.TryAdd(key, path); map.TryAdd(key, path);
} }
} }
} }
GD.Print($"Registered {map.Count} texture paths at : {rootDir}");
} }
public bool Load(int id, string path) public bool Load(int id, string path)
{ {
// TODO: Handle lowercase .pcx :)
var userTexturesPath = ProjectSettings.GlobalizePath($"user://textures{path}.PCX");
var loaded = false; var loaded = false;
if (_fmTexturePaths.TryGetValue(path.ToLower(), out var filePath)) if (_fmTexturePaths.TryGetValue(path.ToLower(), out var fmTexPath))
{ {
_textureCache.Add(LoadTexture(filePath)); _textureCache.Add(LoadTexture(fmTexPath));
loaded = true; loaded = true;
} }
else if (File.Exists(userTexturesPath)) else if (_rootTexturePaths.TryGetValue(path.ToLower(), out var rootTexPath))
{ {
_textureCache.Add(LoadTexture(userTexturesPath)); _textureCache.Add(LoadTexture(rootTexPath));
loaded = true; loaded = true;
} }

View File

@ -7,7 +7,7 @@ namespace KeepersCompound.TMV.UI;
public partial class MissionSelector : Control public partial class MissionSelector : Control
{ {
[Signal] [Signal]
public delegate void LoadMissionEventHandler(string path); public delegate void LoadMissionEventHandler(string rootPath, string missionPath);
private InstallPaths _installPaths; private InstallPaths _installPaths;
@ -116,7 +116,7 @@ public partial class MissionSelector : Control
var campaignPath = selectedCampaign == 0 ? _installPaths.omsPath : _installPaths.fmsPath + _Campaigns.GetItemText(selectedCampaign); var campaignPath = selectedCampaign == 0 ? _installPaths.omsPath : _installPaths.fmsPath + _Campaigns.GetItemText(selectedCampaign);
var path = campaignPath + _Missions.GetItemText(selected[0]); var path = campaignPath + _Missions.GetItemText(selected[0]);
EmitSignal(SignalName.LoadMission, path); EmitSignal(SignalName.LoadMission, _installPaths.rootPath, path);
Visible = false; Visible = false;
} }