Skip to content

Commit

Permalink
Major refactoring of the config loading
Browse files Browse the repository at this point in the history
1. Load Eva and Pickup settinsg via persistent fields.
2. Drop the unused loading progress code in the load inventory method.
3. Simplify the loading screen handling (drop unneeded sts and
checkings).
  • Loading branch information
ihsoft committed Mar 7, 2018
1 parent 0d58f46 commit 30ed803
Showing 1 changed file with 27 additions and 55 deletions.
82 changes: 27 additions & 55 deletions Source/KISAddonConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using KSPDev.ConfigUtils;
using KSPDev.LogUtils;
using KSPDev.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace KIS {
Expand All @@ -19,48 +19,46 @@ sealed class KISAddonConfig : MonoBehaviour {
[PersistentField("Global/breathableAtmoPressure")]
public readonly static float breathableAtmoPressure = 0.5f;

[PersistentField("EvaInventory")]
readonly static PersistentConfigNode evaInventory = new PersistentConfigNode();

[PersistentField("EvaPickup")]
readonly static PersistentConfigNode evaPickup = new PersistentConfigNode();

const string MaleKerbalEva = "kerbalEVA";
const string FemaleKerbalEva = "kerbalEVAfemale";
const string MaleKerbalEvaVintage = "kerbalEVAVintage";
const string FemaleKerbalEvaVintage = "kerbalEVAfemaleVintage";
const string RdKerbalEva = "kerbalEVA_RD";

static ConfigNode nodeSettings;

/// <summary>Instantly loads the KIS global settings.</summary>
class KISConfigLoader: LoadingSystem {

public override bool IsReady() {
return true;
}

ConfigAccessor.ReadFieldsInType(owner.GetType(), owner);
public override void StartLoad() {
DebugEx.Info("Loading KIS global settings...");
ConfigAccessor.ReadFieldsInType(typeof(KISAddonConfig), instance: null);
ConfigAccessor.ReadFieldsInType(typeof(ModuleKISInventory), instance: null);

// Set inventory module for every eva kerbal
DebugEx.Info("Set KIS config...");
nodeSettings = GameDatabase.Instance.GetConfigNode("KIS/settings/KISConfig");
if (nodeSettings == null) {
DebugEx.Error("KIS settings.cfg not found or invalid !");
}
}
}

public bool done;

/// <summary>Adds EVA inventories to every pod.</summary>
class KISPodInventoryLoader: LoadingSystem {
int loadedPartCount;
int loadedPartIndex;
public override bool IsReady() {
return true;
}

void LoadInventories() {
public override void StartLoad() {
// Kerbal parts.
UpdateEvaPrefab(MaleKerbalEva, nodeSettings);
UpdateEvaPrefab(FemaleKerbalEva, nodeSettings);
UpdateEvaPrefab(MaleKerbalEva);
UpdateEvaPrefab(FemaleKerbalEva);

// Set inventory module for every pod with crew capacity.
DebugEx.Info("Loading pod inventories...");
for (loadedPartIndex = 0; loadedPartIndex < loadedPartCount; loadedPartIndex++) {
AvailablePart avPart = PartLoader.LoadedPartsList[loadedPartIndex];
for (var i = 0; i < PartLoader.LoadedPartsList.Count; i++) {
var avPart = PartLoader.LoadedPartsList[i];
if (!(avPart.name == MaleKerbalEva || avPart.name == FemaleKerbalEva
|| avPart.name == MaleKerbalEvaVintage || avPart.name == FemaleKerbalEvaVintage
|| avPart.name == RdKerbalEva
Expand All @@ -70,64 +68,39 @@ void LoadInventories() {
}
}
}

public override bool IsReady() {
return done;
}

public override float ProgressFraction() {
return (float)loadedPartIndex / (float)loadedPartCount;
}

public override string ProgressTitle() {
return "Kerbal Inventory System";
}

done = true;
public override void StartLoad() {
loadedPartCount = PartLoader.LoadedPartsList.Count;
LoadInventories();
}
}

public void Awake() {
List<LoadingSystem> list = LoadingScreen.Instance.loaders;
if (list != null) {
for (int i = 0; i < list.Count; i++) {
if (list[i] is KISConfigLoader) {
(list[i] as KISConfigLoader).owner = this;
}
if (list[i] is KISPodInventoryLoader) {
(list[i] as KISPodInventoryLoader).done = false;
}
for (var i = 0; i < list.Count; i++) {
if (list[i] is PartLoader) {
GameObject go = new GameObject();
var go = new GameObject();

var invLoader = go.AddComponent<KISPodInventoryLoader>();
// Cause the pod inventory loader to run AFTER the part loader.
list.Insert (i + 1, invLoader);
list.Insert(i + 1, invLoader);

var cfgLoader = go.AddComponent<KISConfigLoader>();
cfgLoader.owner = this;
// Cause the config loader to run BEFORE the part loader this ensures
// that the KIS configs are loaded after Module Manager has run but
// before any parts are loaded so KIS aware part modules can add
// pod inventories as necessary.
list.Insert (i, cfgLoader);
list.Insert(i, cfgLoader);
break;
}
}
}
}

public static void AddPodInventories(Part part, int crewCapacity) {
for (int i = 0; i < crewCapacity; i++) {
for (var i = 0; i < crewCapacity; i++) {
try {
var moduleInventory =
part.AddModule(typeof(ModuleKISInventory).Name) as ModuleKISInventory;
KIS_Shared.AwakePartModule(moduleInventory);
var baseFields = new BaseFieldList(moduleInventory);
baseFields.Load(nodeSettings.GetNode("EvaInventory"));
baseFields.Load(evaInventory);
moduleInventory.podSeat = i;
moduleInventory.invType = ModuleKISInventory.InventoryType.Pod;
DebugEx.Fine("Pod inventory module(s) for seat {0} loaded successfully", i);
Expand All @@ -140,11 +113,10 @@ public static void AddPodInventories(Part part, int crewCapacity) {
/// <summary>Load config of EVA modules for the requested part name.</summary>
static void UpdateEvaPrefab(string partName) {
var prefab = PartLoader.getPartInfoByName(partName).partPrefab;
if (LoadModuleConfig(prefab, typeof(ModuleKISInventory),
nodeSettings.GetNode("EvaInventory"))) {
if (LoadModuleConfig(prefab, typeof(ModuleKISInventory), evaInventory)) {
prefab.GetComponent<ModuleKISInventory>().invType = ModuleKISInventory.InventoryType.Eva;
}
LoadModuleConfig(prefab, typeof(ModuleKISPickup), nodeSettings.GetNode("EvaPickup"));
LoadModuleConfig(prefab, typeof(ModuleKISPickup), evaPickup);
}

/// <summary>Loads config values for the part's module fro the provided config node.</summary>
Expand Down

0 comments on commit 30ed803

Please sign in to comment.