Use path manager for mission selector

This commit is contained in:
Jarrod Doyle 2024-08-25 09:15:42 +01:00
parent cb2f016455
commit 3d41441f2a
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 67 additions and 39 deletions

View File

@ -5,7 +5,7 @@ using System.Linq;
namespace KeepersCompound.LGS;
// TODO: Support FMs resource paths!
// TODO: Merge the two versions of GetXXX and handle null campaign string as OM
public class ResourcePathManager
{
private record CampaignResources
@ -84,6 +84,13 @@ public class ResourcePathManager
return false;
}
public string[] GetCampaignNames()
{
if (!_initialised) return null;
return _fmResources.Keys.ToArray();
}
public string GetMissionPath(string missionName)
{
if (!_initialised) return null;
@ -107,6 +114,24 @@ public class ResourcePathManager
return null;
}
public string[] GetMissionNames()
{
if (!_initialised) return null;
return _omResources.missionPathMap.Keys.ToArray();
}
public string[] GetMissionNames(string campaignName)
{
if (!_initialised) return null;
if (_fmResources.TryGetValue(campaignName, out var campaign))
{
return campaign.missionPathMap.Keys.ToArray();
}
return null;
}
public string GetTexturePath(string textureName)
{
if (!_initialised) return null;

View File

@ -48,11 +48,18 @@ public partial class Mission : Node3D
var extractPath = ProjectSettings.GlobalizePath($"user://extracted/tmp");
_installPaths = new ResourcePathManager(extractPath);
var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector;
missionSelector.LoadMission += (string rootPath, string missionPath) =>
missionSelector.pathManager = _installPaths;
missionSelector.LoadMission += (string campaign, string mission) =>
{
var inited = _installPaths.Init(rootPath);
GD.Print($"Inited paths: {inited}");
FileName = missionPath;
if (campaign == null)
{
FileName = _installPaths.GetMissionPath(mission);
}
else
{
FileName = _installPaths.GetMissionPath(campaign, mission);
}
Build = true;
};
}

View File

@ -1,15 +1,14 @@
using System.IO;
using System.Linq;
using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV.UI;
public partial class MissionSelector : Control
{
[Signal]
public delegate void LoadMissionEventHandler(string rootPath, string missionPath);
public delegate void LoadMissionEventHandler(string campaign, string mission);
private InstallPaths _installPaths;
public ResourcePathManager pathManager;
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
@ -22,7 +21,6 @@ public partial class MissionSelector : Control
public override void _Ready()
{
// TODO: Load initial folderpath from config and prefil everything
_FolderSelect = GetNode<FileDialog>("%FolderSelect");
_FolderPath = GetNode<LineEdit>("%FolderPath");
_BrowseButton = GetNode<Button>("%BrowseButton");
@ -32,8 +30,8 @@ public partial class MissionSelector : Control
_CancelButton = GetNode<Button>("%CancelButton");
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
_FolderSelect.DirSelected += (string dir) => { _FolderPath.Text = dir; BuildCampaignList(dir); };
_FolderPath.TextSubmitted += BuildCampaignList;
_FolderSelect.DirSelected += SetInstallPath;
_FolderPath.TextSubmitted += SetInstallPath;
_Missions.ItemSelected += (long _) => _LoadButton.Disabled = false;
_Campaigns.ItemSelected += BuildMissionList;
_LoadButton.Pressed += EmitLoadMission;
@ -51,19 +49,25 @@ public partial class MissionSelector : Control
}
}
private void BuildCampaignList(string path)
private void SetInstallPath(string path)
{
_installPaths = new InstallPaths(path);
_FolderPath.Text = path;
if (pathManager.Init(path))
{
BuildCampaignList();
}
}
private void BuildCampaignList()
{
_Campaigns.Clear();
_Missions.Clear();
_LoadButton.Disabled = true;
_Campaigns.AddItem("Original Missions");
var paths = Directory.GetDirectories(_installPaths.fmsPath);
foreach (var c in paths.OrderBy(s => s))
foreach (var campaign in pathManager.GetCampaignNames())
{
_Campaigns.AddItem(c.TrimPrefix(_installPaths.fmsPath));
_Campaigns.AddItem(campaign);
}
}
@ -72,39 +76,31 @@ public partial class MissionSelector : Control
_Missions.Clear();
_LoadButton.Disabled = true;
var campaignPath = "";
if (idx == 0)
{
campaignPath = _installPaths.omsPath;
}
else
{
campaignPath = _installPaths.fmsPath + _Campaigns.GetItemText((int)idx);
}
var campaignName = _Campaigns.GetItemText((int)idx);
var missionNames = (idx == 0) ?
pathManager.GetMissionNames() :
pathManager.GetMissionNames(campaignName);
string[] extensions = { "mis", "cow" };
var paths = Directory.GetFiles(campaignPath);
foreach (var f in paths.OrderBy(s => s))
foreach (var mission in missionNames)
{
if (extensions.Contains(f.GetExtension().ToLower()))
{
_Missions.AddItem(f.TrimPrefix(campaignPath));
}
_Missions.AddItem(mission);
}
}
private void EmitLoadMission()
{
var selectedCampaign = _Campaigns.GetSelectedItems()[0];
var selected = _Missions.GetSelectedItems();
if (selected.IsEmpty())
var campaignIdxs = _Campaigns.GetSelectedItems();
var missionIdxs = _Missions.GetSelectedItems();
if (campaignIdxs.IsEmpty() || missionIdxs.IsEmpty())
{
return;
}
var campaignPath = selectedCampaign == 0 ? _installPaths.omsPath : _installPaths.fmsPath + _Campaigns.GetItemText(selectedCampaign);
var path = campaignPath + _Missions.GetItemText(selected[0]);
EmitSignal(SignalName.LoadMission, _installPaths.rootPath, path);
var campaignIdx = campaignIdxs[0];
var missionIdx = missionIdxs[0];
var campaignName = campaignIdx == 0 ? null : _Campaigns.GetItemText(campaignIdx);
var missionName = _Missions.GetItemText(missionIdx);
EmitSignal(SignalName.LoadMission, campaignName, missionName);
Visible = false;
}