From 048b216c1e9f1a0b6eb1cf5fd815536c7f6ffb4b Mon Sep 17 00:00:00 2001 From: Majiir Paktu Date: Tue, 2 Sep 2014 21:58:08 -0400 Subject: [PATCH] Hide internal resources dictionary behind indexer --- Plugin/KethaneData.cs | 19 ++++++++++++------- Plugin/MapOverlay.cs | 10 +++++----- Plugin/PartModules/KethaneDetector.cs | 4 ++-- Plugin/Scenarios/KethaneScanningTutorial.cs | 2 +- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Plugin/KethaneData.cs b/Plugin/KethaneData.cs index 0809a3c..c333679 100644 --- a/Plugin/KethaneData.cs +++ b/Plugin/KethaneData.cs @@ -28,20 +28,25 @@ public static KethaneData Current } } - public Dictionary ResourceData = new Dictionary(); + private Dictionary resources = new Dictionary(); + + public ResourceData this[string resourceName] + { + get { return resources[resourceName]; } + } public ICellResource GetCellDeposit(string resourceName, CelestialBody body, Cell cell) { - if (resourceName == null || body == null || !ResourceData.ContainsKey(resourceName)) { return null; } + if (resourceName == null || body == null || !resources.ContainsKey(resourceName)) { return null; } - return ResourceData[resourceName][body].GetCellDeposit(cell); + return resources[resourceName][body].GetCellDeposit(cell); } public override void OnLoad(ConfigNode config) { var timer = System.Diagnostics.Stopwatch.StartNew(); - ResourceData.Clear(); + resources.Clear(); var resourceNodes = config.GetNodes("Resource"); @@ -49,7 +54,7 @@ public override void OnLoad(ConfigNode config) { var resourceName = resource.Resource; var resourceNode = resourceNodes.SingleOrDefault(n => n.GetValue("Resource") == resourceName) ?? new ConfigNode(); - ResourceData[resourceName] = Kethane.ResourceData.Load(resource, resourceNode); + resources[resourceName] = Kethane.ResourceData.Load(resource, resourceNode); } timer.Stop(); @@ -58,7 +63,7 @@ public override void OnLoad(ConfigNode config) public void ResetGeneratorConfig(ResourceDefinition resource) { - ResourceData[resource.Resource] = Kethane.ResourceData.Load(resource, new ConfigNode()); + resources[resource.Resource] = Kethane.ResourceData.Load(resource, new ConfigNode()); } public override void OnSave(ConfigNode configNode) @@ -67,7 +72,7 @@ public override void OnSave(ConfigNode configNode) configNode.AddValue("Version", System.Reflection.Assembly.GetExecutingAssembly().GetInformationalVersion()); - foreach (var resource in ResourceData) + foreach (var resource in resources) { var resourceNode = new ConfigNode("Resource"); resourceNode.AddValue("Resource", resource.Key); diff --git a/Plugin/MapOverlay.cs b/Plugin/MapOverlay.cs index e8d5550..4ff6102 100644 --- a/Plugin/MapOverlay.cs +++ b/Plugin/MapOverlay.cs @@ -243,8 +243,8 @@ private void refreshCellColors() private void refreshCellColor(Cell cell, CelestialBody body, Color32[] colors, KethaneData data) { var deposit = data.GetCellDeposit(resource.Resource, body, cell); - var scanned = data.ResourceData[resource.Resource][body].IsCellScanned(cell); - var bodyResources = data.ResourceData[resource.Resource][body]; + var scanned = data[resource.Resource][body].IsCellScanned(cell); + var bodyResources = data[resource.Resource][body]; var color = (revealAll ? deposit != null : scanned) ? getDepositColor(resource, bodyResources, deposit) : colorUnknown; setCellColor(cell, color, colors); } @@ -380,7 +380,7 @@ private void mouseWindow(int windowId) GUILayout.Label(String.Format("{0}:", definition.Resource)); GUILayout.FlexibleSpace(); - if (revealAll || KethaneData.Current.ResourceData[definition.Resource][body].IsCellScanned(cell)) + if (revealAll || KethaneData.Current[definition.Resource][body].IsCellScanned(cell)) { var deposit = KethaneData.Current.GetCellDeposit(definition.Resource, body, cell); GUILayout.Label(deposit != null ? String.Format("{0:N1}", deposit.Quantity) : "(none)"); @@ -452,7 +452,7 @@ private void controlWindow(int windowID) if (GUILayout.Button("Reset " + (body ? body.name : "[null]") + " Data")) { - KethaneData.Current.ResourceData[resource.Resource].ResetBodyData(body); + KethaneData.Current[resource.Resource].ResetBodyData(body); refreshCellColors(); } @@ -497,7 +497,7 @@ private static void export() { foreach (var cell in Cell.AtLevel(MapOverlay.GridLevel)) { - var scanned = KethaneData.Current.ResourceData[resource.Resource][body].IsCellScanned(cell); + var scanned = KethaneData.Current[resource.Resource][body].IsCellScanned(cell); var deposit = KethaneData.Current.GetCellDeposit(resource.Resource, body, cell); sb.Append(String.Format("{0},{1},", body.name, resource.Resource)); diff --git a/Plugin/PartModules/KethaneDetector.cs b/Plugin/PartModules/KethaneDetector.cs index 8ff7bdf..92d9221 100644 --- a/Plugin/PartModules/KethaneDetector.cs +++ b/Plugin/PartModules/KethaneDetector.cs @@ -163,10 +163,10 @@ public override void OnFixedUpdate() { var detected = false; var cell = MapOverlay.GetCellUnder(vessel.mainBody, vessel.transform.position); - if (resources.All(r => KethaneData.Current.ResourceData[r][vessel.mainBody].IsCellScanned(cell))) { return; } + if (resources.All(r => KethaneData.Current[r][vessel.mainBody].IsCellScanned(cell))) { return; } foreach (var resource in resources) { - KethaneData.Current.ResourceData[resource][vessel.mainBody].ScanCell(cell); + KethaneData.Current[resource][vessel.mainBody].ScanCell(cell); if (KethaneData.Current.GetCellDeposit(resource, vessel.mainBody, cell) != null) { detected = true; diff --git a/Plugin/Scenarios/KethaneScanningTutorial.cs b/Plugin/Scenarios/KethaneScanningTutorial.cs index 769879d..322afbf 100644 --- a/Plugin/Scenarios/KethaneScanningTutorial.cs +++ b/Plugin/Scenarios/KethaneScanningTutorial.cs @@ -187,7 +187,7 @@ protected override void OnTutorialSetup() timewarp.OnUpdate = () => { - surfaceCoverage = (float)Cell.AtLevel(MapOverlay.GridLevel).Count(c => KethaneData.Current.ResourceData["Kethane"][FlightGlobals.currentMainBody].IsCellScanned(c)) / Cell.CountAtLevel(MapOverlay.GridLevel); + surfaceCoverage = (float)Cell.AtLevel(MapOverlay.GridLevel).Count(c => KethaneData.Current["Kethane"][FlightGlobals.currentMainBody].IsCellScanned(c)) / Cell.CountAtLevel(MapOverlay.GridLevel); if (surfaceCoverage >= 0.02f) { Tutorial.GoToNextPage();