From 63591c228b5d4feef057ee0f619c8176fd83c7b2 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sun, 8 Sep 2024 13:24:10 +0100 Subject: [PATCH] Add CampaignName to Context --- project/code/TMV/Context.cs | 2 ++ project/code/TMV/Mission.cs | 16 ++++++++-------- project/code/TMV/Model.cs | 3 ++- project/code/TMV/ModelLoader.cs | 3 ++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/project/code/TMV/Context.cs b/project/code/TMV/Context.cs index 939bc49..228491e 100644 --- a/project/code/TMV/Context.cs +++ b/project/code/TMV/Context.cs @@ -9,12 +9,14 @@ public partial class Context : Node public ResourcePathManager PathManager { get; private set; } public ModelLoader ModelLoader { get; private set; } + public string CampaignName { get; set; } public override void _Ready() { var extractPath = ProjectSettings.GlobalizePath($"user://extracted/tmp"); PathManager = new ResourcePathManager(extractPath); ModelLoader = new ModelLoader(); + CampaignName = ""; Instance = this; } diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index 1b472f4..a00468a 100644 --- a/project/code/TMV/Mission.cs +++ b/project/code/TMV/Mission.cs @@ -41,7 +41,6 @@ public partial class Mission : Node3D public bool Dump = false; public int LightmapLayers = 33; - string _campaignName; string _missionName; DbFile _file; TextureLoader _textureLoader; @@ -59,10 +58,10 @@ public partial class Mission : Node3D var resourceSelector = GetNode("%ResourceSelector") as ResourceSelector; resourceSelector.ResourceSelected += (string campaign, string mission) => { - _campaignName = campaign; + var context = Context.Instance; + context.CampaignName = campaign; _missionName = mission; - var pathManager = Context.Instance.PathManager; - var (newCampaign, missionPath) = pathManager.GetResourcePath(ResourceType.Mission, campaign, mission); + var (newCampaign, missionPath) = context.PathManager.GetResourcePath(ResourceType.Mission, campaign, mission); if (newCampaign != campaign) { GD.Print($"Didn't find campaign mission: ({campaign}, {mission})"); @@ -121,7 +120,7 @@ public partial class Mission : Node3D { ClearMap(); - _textureLoader = new TextureLoader(_campaignName); + _textureLoader = new TextureLoader(Context.Instance.CampaignName); Timing.TimeStage("DbFile Parse", () => _file = new(FileName)); Timing.TimeStage("Register Textures", () => UseChunk("TXLIST", RegisterTextures)); Timing.TimeStage("Build WR", () => UseChunk("WREXT", BuildWrMeshes)); @@ -268,7 +267,7 @@ public partial class Mission : Node3D var scale = scaleProp == null ? Vector3.One : scaleProp.value.ToGodotVec3(false); var model = Timing.TimeStage("Get Models", () => { - return Context.Instance.ModelLoader.Load(_campaignName, modelName); + return Context.Instance.ModelLoader.Load(modelName); }); if (model != null) { @@ -287,17 +286,18 @@ public partial class Mission : Node3D path = prop.value; var pathManager = Context.Instance.PathManager; + var campaign = Context.Instance.CampaignName; if (path.StartsWith("fam", StringComparison.OrdinalIgnoreCase)) { var resType = ResourceType.Texture; - path = pathManager.GetResourcePath(resType, _campaignName, prop.value).Item2; + path = pathManager.GetResourcePath(resType, campaign, prop.value).Item2; } else { var resType = ResourceType.ObjectTexture; var convertedValue = ResourcePathManager.ConvertSeparator(prop.value); var resName = Path.GetFileNameWithoutExtension(convertedValue); - path = pathManager.GetResourcePath(resType, _campaignName, resName).Item2; + path = pathManager.GetResourcePath(resType, campaign, resName).Item2; } return path != null; diff --git a/project/code/TMV/Model.cs b/project/code/TMV/Model.cs index 0d10193..46791b2 100644 --- a/project/code/TMV/Model.cs +++ b/project/code/TMV/Model.cs @@ -18,7 +18,8 @@ public partial class Model : Node3D node.QueueFree(); } - var model = Context.Instance.ModelLoader.Load(campaignName, modelPath); + Context.Instance.CampaignName = campaignName; + var model = Context.Instance.ModelLoader.Load(modelPath); AddChild(model); } } diff --git a/project/code/TMV/ModelLoader.cs b/project/code/TMV/ModelLoader.cs index 3877c15..cfff4c6 100644 --- a/project/code/TMV/ModelLoader.cs +++ b/project/code/TMV/ModelLoader.cs @@ -9,8 +9,9 @@ public class ModelLoader { private readonly Dictionary<(string, string), MeshInstance3D> _cache = new(); - public MeshInstance3D Load(string campaignName, string modelName, bool forceLoad = false) + public MeshInstance3D Load(string modelName, bool forceLoad = false) { + var campaignName = Context.Instance.CampaignName; campaignName ??= ""; if (!forceLoad)