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; 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 public class ResourcePathManager
{ {
private record CampaignResources private record CampaignResources
@ -84,6 +84,13 @@ public class ResourcePathManager
return false; return false;
} }
public string[] GetCampaignNames()
{
if (!_initialised) return null;
return _fmResources.Keys.ToArray();
}
public string GetMissionPath(string missionName) public string GetMissionPath(string missionName)
{ {
if (!_initialised) return null; if (!_initialised) return null;
@ -107,6 +114,24 @@ public class ResourcePathManager
return null; 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) public string GetTexturePath(string textureName)
{ {
if (!_initialised) return null; if (!_initialised) return null;

View File

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

View File

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