Skip to content

Commit

Permalink
Major re-factoring
Browse files Browse the repository at this point in the history
- Use KSPDev for loading global settings.
- Use BasicFieldList to load KSPFields.
- Move some static settings around
  • Loading branch information
ihsoft committed May 2, 2016
1 parent 04a3328 commit eb096a8
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 240 deletions.
284 changes: 58 additions & 226 deletions Plugins/Source/KISAddonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@
using System;
using UnityEngine;


using System.Text;

namespace KIS {

[KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
[PersistentFieldsFile("KIS/settings.cfg", "KISConfig")]
class KISAddonConfig : MonoBehaviour {
[PersistentField("StackableItemOverride/partName", isCollection = true)]
public static List<string> stackableList = new List<string>();

[PersistentField("StackableModule/moduleName", isCollection = true)]
public static List<string> stackableModules = new List<string>();

[PersistentField("Global/breathableAtmoPressure")]
public static float breathableAtmoPressure = 0.5f;

[PersistentField("Editor/PodInventory/addToAllSeats", isCollection = true)]
public static List<String> defaultItemsForAllSeats = new List<string>();
[PersistentField("Editor/PodInventory/addToTheFirstSeatOnly", isCollection = true)]
public static List<String> defaultItemsForTheFirstSeat = new List<string>();

const string MaleKerbalEva = "kerbalEVA";
const string FemaleKerbalEva = "kerbalEVAfemale";
const string RdKerbalEva = "kerbalEVA_RD";

public void Awake() {
ConfigAccessor.ReadFieldsInType(GetType(), this);

ConfigAccessor.ReadFieldsInType(typeof(ModuleKISInventory), instance: null);

// Set inventory module for every eva kerbal
Logger.logInfo("Set KIS config...");
ConfigNode nodeSettings = GameDatabase.Instance.GetConfigNode("KIS/settings/KISConfig");
Expand All @@ -30,250 +38,74 @@ public void Awake() {
return;
}

// Set global settings
ConfigNode nodeGlobal = nodeSettings.GetNode("Global");
if (nodeGlobal.HasValue("itemDebug")) {
ModuleKISInventory.debugContextMenu = bool.Parse(nodeGlobal.GetValue("itemDebug"));
}
if (nodeGlobal.HasValue("breathableAtmoPressure")) {
breathableAtmoPressure = float.Parse(nodeGlobal.GetValue("breathableAtmoPressure"));
}

ConfigNode nodeEvaInventory = nodeSettings.GetNode("EvaInventory");
LoadEvaInventoryConfig(nodeEvaInventory);

ConfigNode nodeEvaPickup = nodeSettings.GetNode("EvaPickup");
ConfigNode nodeStackable = nodeSettings.GetNode("StackableItemOverride");
ConfigNode nodeStackableModule = nodeSettings.GetNode("StackableModule");

// Set stackable items list
stackableList.Clear();
foreach (string partName in nodeStackable.GetValues("partName")) {
stackableList.Add(partName);
}

// Set stackable module list
stackableModules.Clear();
foreach (string moduleName in nodeStackableModule.GetValues("moduleName")) {
stackableModules.Add(moduleName);
}

//-------Male Kerbal
// Adding module to EVA cause an unknown error but work
Part evaPrefab = PartLoader.getPartInfoByName("kerbalEVA").partPrefab;
try {
evaPrefab.AddModule("ModuleKISInventory");
} catch {
}
try {
evaPrefab.AddModule("ModuleKISPickup");
} catch {
}

// Set inventory module for eva
ModuleKISInventory evaInventory = evaPrefab.GetComponent<ModuleKISInventory>();
if (evaInventory) {
if (nodeGlobal.HasValue("kerbalDefaultMass"))
evaInventory.kerbalDefaultMass = float.Parse(nodeGlobal.GetValue("kerbalDefaultMass"));
SetInventoryConfig(nodeEvaInventory, evaInventory);
evaInventory.invType = ModuleKISInventory.InventoryType.Eva;
Logger.logInfo("Eva inventory module loaded successfully");
}

// Set pickup module for eva
ModuleKISPickup evaPickup = evaPrefab.GetComponent<ModuleKISPickup>();
if (evaPickup) {
if (nodeEvaPickup.HasValue("grabKey")) {
KISAddonPickup.grabKey = nodeEvaPickup.GetValue("grabKey");
}
if (nodeEvaPickup.HasValue("attachKey")) {
KISAddonPickup.attachKey = nodeEvaPickup.GetValue("attachKey");
}
if (nodeEvaPickup.HasValue("allowPartAttach")) {
evaPickup.allowPartAttach = bool.Parse(nodeEvaPickup.GetValue("allowPartAttach"));
}
if (nodeEvaPickup.HasValue("allowStaticAttach")) {
evaPickup.allowStaticAttach = bool.Parse(nodeEvaPickup.GetValue("allowStaticAttach"));
}
if (nodeEvaPickup.HasValue("allowPartStack")) {
evaPickup.allowPartStack = bool.Parse(nodeEvaPickup.GetValue("allowPartStack"));
}
if (nodeEvaPickup.HasValue("maxDistance")) {
evaPickup.maxDistance = float.Parse(nodeEvaPickup.GetValue("maxDistance"));
}
if (nodeEvaPickup.HasValue("grabMaxMass")) {
evaPickup.grabMaxMass = float.Parse(nodeEvaPickup.GetValue("grabMaxMass"));
}
if (nodeEvaPickup.HasValue("dropSndPath")) {
evaPickup.dropSndPath = nodeEvaPickup.GetValue("dropSndPath");
}
if (nodeEvaPickup.HasValue("attachPartSndPath")) {
evaPickup.attachPartSndPath = nodeEvaPickup.GetValue("attachPartSndPath");
}
if (nodeEvaPickup.HasValue("detachPartSndPath")) {
evaPickup.detachPartSndPath = nodeEvaPickup.GetValue("detachPartSndPath");
}
if (nodeEvaPickup.HasValue("attachStaticSndPath")) {
evaPickup.attachStaticSndPath = nodeEvaPickup.GetValue("attachStaticSndPath");
}
if (nodeEvaPickup.HasValue("detachStaticSndPath")) {
evaPickup.detachStaticSndPath = nodeEvaPickup.GetValue("detachStaticSndPath");
}
if (nodeEvaPickup.HasValue("draggedIconResolution")) {
KISAddonPickup.draggedIconResolution =
int.Parse(nodeEvaPickup.GetValue("draggedIconResolution"));
}
Logger.logInfo("Eva pickup module loaded successfully");
}

//-------Female Kerbal
// Adding module to EVA cause an unknown error but work
Part evaFemalePrefab = PartLoader.getPartInfoByName("kerbalEVAfemale").partPrefab;
try {
evaFemalePrefab.AddModule("ModuleKISInventory");
} catch {
}
try {
evaFemalePrefab.AddModule("ModuleKISPickup");
} catch {
}

// Set inventory module for eva
ModuleKISInventory evaFemaleInventory = evaFemalePrefab.GetComponent<ModuleKISInventory>();
if (evaFemaleInventory) {
if (nodeGlobal.HasValue("kerbalDefaultMass")) {
evaFemaleInventory.kerbalDefaultMass =
float.Parse(nodeGlobal.GetValue("kerbalDefaultMass"));
}
SetInventoryConfig(nodeEvaInventory, evaFemaleInventory);
evaFemaleInventory.invType = ModuleKISInventory.InventoryType.Eva;
Logger.logInfo("Eva inventory module loaded successfully");
}

// Set pickup module for eva
ModuleKISPickup evaFemalePickup = evaFemalePrefab.GetComponent<ModuleKISPickup>();
if (evaFemalePickup) {
if (nodeEvaPickup.HasValue("grabKey")) {
KISAddonPickup.grabKey = nodeEvaPickup.GetValue("grabKey");
}
if (nodeEvaPickup.HasValue("attachKey")) {
KISAddonPickup.attachKey = nodeEvaPickup.GetValue("attachKey");
}
if (nodeEvaPickup.HasValue("allowPartAttach")) {
evaFemalePickup.allowPartAttach = bool.Parse(nodeEvaPickup.GetValue("allowPartAttach"));
}
if (nodeEvaPickup.HasValue("allowStaticAttach")) {
evaFemalePickup.allowStaticAttach = bool.Parse(nodeEvaPickup.GetValue("allowStaticAttach"));
}
if (nodeEvaPickup.HasValue("allowPartStack")) {
evaFemalePickup.allowPartStack = bool.Parse(nodeEvaPickup.GetValue("allowPartStack"));
}
if (nodeEvaPickup.HasValue("maxDistance")) {
evaFemalePickup.maxDistance = float.Parse(nodeEvaPickup.GetValue("maxDistance"));
}
if (nodeEvaPickup.HasValue("grabMaxMass")) {
evaFemalePickup.grabMaxMass = float.Parse(nodeEvaPickup.GetValue("grabMaxMass"));
}
if (nodeEvaPickup.HasValue("dropSndPath")) {
evaFemalePickup.dropSndPath = nodeEvaPickup.GetValue("dropSndPath");
}
if (nodeEvaPickup.HasValue("attachPartSndPath")) {
evaFemalePickup.attachPartSndPath = nodeEvaPickup.GetValue("attachPartSndPath");
}
if (nodeEvaPickup.HasValue("detachPartSndPath")) {
evaFemalePickup.detachPartSndPath = nodeEvaPickup.GetValue("detachPartSndPath");
}
if (nodeEvaPickup.HasValue("attachStaticSndPath")) {
evaFemalePickup.attachStaticSndPath = nodeEvaPickup.GetValue("attachStaticSndPath");
}
if (nodeEvaPickup.HasValue("detachStaticSndPath")) {
evaFemalePickup.detachStaticSndPath = nodeEvaPickup.GetValue("detachStaticSndPath");
}
if (nodeEvaPickup.HasValue("draggedIconResolution")) {
KISAddonPickup.draggedIconResolution =
int.Parse(nodeEvaPickup.GetValue("draggedIconResolution"));
}
Logger.logInfo("Eva pickup module loaded successfully");
}

// Set inventory module for every pod with crew capacity
Logger.logInfo("Loading pod inventory...");
// Male Kerbal.
UpdateEvaPrefab(PartLoader.getPartInfoByName(MaleKerbalEva), nodeSettings);
// Female Kerbal.
UpdateEvaPrefab(PartLoader.getPartInfoByName(FemaleKerbalEva), nodeSettings);

// Set inventory module for every pod with crew capacity.
Logger.logInfo("Loading pod inventories...");
foreach (AvailablePart avPart in PartLoader.LoadedPartsList) {
if (avPart.name == "kerbalEVA" || avPart.name == "kerbalEVAfemale"
|| avPart.name == "kerbalEVA_RD"
if (avPart.name == MaleKerbalEva || avPart.name == FemaleKerbalEva
|| avPart.name == RdKerbalEva
|| !avPart.partPrefab || avPart.partPrefab.CrewCapacity < 1) {
continue;
}
Logger.logInfo("Found part with CrewCapacity: {0}", avPart.name);

for (int i = 0; i < avPart.partPrefab.CrewCapacity; i++) {
try {
var moduleInventory =
avPart.partPrefab.AddModule("ModuleKISInventory") as ModuleKISInventory;
SetInventoryConfig(nodeEvaInventory, moduleInventory);
SetInventoryConfig(moduleInventory, nodeSettings);
moduleInventory.podSeat = i;
moduleInventory.invType = ModuleKISInventory.InventoryType.Pod;
Logger.logInfo("Pod inventory module(s) for seat {0} loaded successfully", i);
} catch {
Logger.logWarning("Pod inventory module(s) for seat {0} can't be loaded!", i);
Logger.logError("Pod inventory module(s) for seat {0} can't be loaded!", i);
}
}
}
}

private void SetInventoryConfig(ConfigNode node, ModuleKISInventory moduleInventory) {
// TODO: Load values into static members and move code into LoadEvaInventoryConfig().
// FIXME: Re-factor to ReadCfgSetting() method.
if (node.HasValue("inventoryKey")) {
moduleInventory.evaInventoryKey = node.GetValue("inventoryKey");
}
if (node.HasValue("rightHandKey")) {
moduleInventory.evaRightHandKey = node.GetValue("rightHandKey");
}
if (node.HasValue("helmetKey")) {
moduleInventory.evaHelmetKey = node.GetValue("helmetKey");
void SetInventoryConfig(Component moduleInventory, ConfigNode nodeSettings) {
var nodeEvaInventory = nodeSettings.GetNode("EvaInventory");
if (nodeEvaInventory != null) {
var baseFields = new BaseFieldList(moduleInventory);
baseFields.Load(nodeEvaInventory);
}
if (node.HasValue("slotsX")) {
moduleInventory.slotsX = int.Parse(node.GetValue("slotsX"));
}
if (node.HasValue("slotsY")) {
moduleInventory.slotsY = int.Parse(node.GetValue("slotsY"));
}
if (node.HasValue("slotSize")) {
moduleInventory.slotSize = int.Parse(node.GetValue("slotSize"));
}
if (node.HasValue("itemIconResolution")) {
moduleInventory.itemIconResolution = int.Parse(node.GetValue("itemIconResolution"));
}
if (node.HasValue("selfIconResolution")) {
moduleInventory.selfIconResolution = int.Parse(node.GetValue("selfIconResolution"));
}

void UpdateEvaPrefab(AvailablePart avPart, ConfigNode nodeSettings) {
var prefab = avPart.partPrefab;
// Adding module to EVA may cause an NPE excetption but work module update will still work.
try {
prefab.AddModule(typeof(ModuleKISInventory).Name);
} catch (Exception ex) {
Logger.logInfo("Ignoring error adding ModuleKISInventory to {0}: {1}", prefab, ex);
}
if (node.HasValue("maxVolume")) {
moduleInventory.maxVolume = float.Parse(node.GetValue("maxVolume"));
try {
prefab.AddModule(typeof(ModuleKISPickup).Name);
} catch (Exception ex) {
Logger.logInfo("Ignoring error adding ModuleKISPickup to {0}: {1}", prefab, ex);
}
if (node.HasValue("openSndPath")) {
moduleInventory.openSndPath = node.GetValue("openSndPath");

// Setup inventory module for eva.
var evaInventory = prefab.GetComponent<ModuleKISInventory>();
if (evaInventory) {
SetInventoryConfig(evaInventory, nodeSettings);
evaInventory.invType = ModuleKISInventory.InventoryType.Eva;
Logger.logInfo("Eva inventory module loaded successfully");
}
if (node.HasValue("closeSndPath")) {
moduleInventory.closeSndPath = node.GetValue("closeSndPath");

// Load KSP fields for ModuleKISPickup module.
var nodeEvaPickup = nodeSettings.GetNode("EvaPickup");
var evaPickup = prefab.GetComponent<ModuleKISPickup>();
if (evaPickup && nodeEvaPickup != null) {
var fields = new BaseFieldList(evaPickup);
fields.Load(nodeEvaPickup);
Logger.logInfo("Eva pickup module loaded successfully");
}
}

/// <summary>Loads config settings for EvaInventory.</summary>
/// <param name="node">A config node to load data from.</param>
private void LoadEvaInventoryConfig(ConfigNode node) {
// Inventory hotkeys.
KIS_Shared.ReadCfgSetting(node, "slotHotkeysEnabled",
ref ModuleKISInventory.inventoryKeysEnabled);
KIS_Shared.ReadCfgSetting(node, "slotHotkey1", ref ModuleKISInventory.slotHotkey1);
KIS_Shared.ReadCfgSetting(node, "slotHotkey2", ref ModuleKISInventory.slotHotkey2);
KIS_Shared.ReadCfgSetting(node, "slotHotkey3", ref ModuleKISInventory.slotHotkey3);
KIS_Shared.ReadCfgSetting(node, "slotHotkey4", ref ModuleKISInventory.slotHotkey4);
KIS_Shared.ReadCfgSetting(node, "slotHotkey5", ref ModuleKISInventory.slotHotkey5);
KIS_Shared.ReadCfgSetting(node, "slotHotkey6", ref ModuleKISInventory.slotHotkey6);
KIS_Shared.ReadCfgSetting(node, "slotHotkey7", ref ModuleKISInventory.slotHotkey7);
KIS_Shared.ReadCfgSetting(node, "slotHotkey8", ref ModuleKISInventory.slotHotkey8);
}
}

} // namespace
11 changes: 10 additions & 1 deletion Plugins/Source/KISAddonPickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void OnDestroy() {
}
}

// Icons paths.
const string GrabIcon = "KIS/Textures/grab";
const string GrabOkIcon = "KIS/Textures/grabOk";
const string ForbiddenIcon = "KIS/Textures/forbidden";
Expand Down Expand Up @@ -110,14 +111,22 @@ void OnDestroy() {
[PersistentField("Editor/partGrabModifiers")]
static KeyModifiers editorGrabPartModifiers = KeyModifiers.None;

[PersistentField("EvaPickup/grabKey")]
public static string grabKey = "g";

[PersistentField("EvaPickup/attachKey")]
public static string attachKey = "h";

[PersistentField("EvaPickup/redockKey")]
public static string redockKey = "y";

[PersistentField("EvaPickup/draggedIconResolution")]
public static int draggedIconResolution = 64;

public static KIS_IconViewer icon;
public static Part draggedPart;
public static KIS_Item draggedItem;
public static int draggedIconSize = 50;
public static int draggedIconResolution = 64;
public static Part movingPart;
public static KISAddonPickup instance;
public bool grabActive = false;
Expand Down
Loading

0 comments on commit eb096a8

Please sign in to comment.