Skip to content

Commit

Permalink
Merge pull request #19 from Gerry1135/kidsfix
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
CYBUTEK committed Feb 8, 2015
2 parents 35d09dc + 3b38c0d commit c937a3d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 75 deletions.
1 change: 1 addition & 0 deletions KerbalEngineer/Editor/BuildAdvanced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ protected void Awake()
this.bodiesList.DrawCallback = this.DrawBodiesList;
this.Load();

SimManager.UpdateModSettings();
SimManager.OnReady -= this.GetStageInfo;
SimManager.OnReady += this.GetStageInfo;
}
Expand Down
4 changes: 4 additions & 0 deletions KerbalEngineer/Flight/FlightEngineerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace KerbalEngineer.Flight
using Sections;
using Settings;
using UnityEngine;
using VesselSimulator;

#endregion

Expand Down Expand Up @@ -268,6 +269,9 @@ private void Awake()
this.SectionWindows = new List<SectionWindow>();
this.SectionEditors = new List<SectionEditor>();
this.UpdatableModules = new List<IUpdatable>();

SimManager.UpdateModSettings();

Logger.Log("FlightEngineerCore->Awake");
}
catch (Exception ex)
Expand Down
6 changes: 3 additions & 3 deletions KerbalEngineer/VesselSimulator/EngineSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public bool SetResourceDrains(List<PartSim> allParts, List<PartSim> allFuelLines
switch (ResourceContainer.GetResourceFlowMode(type))
{
case ResourceFlowMode.NO_FLOW:
if (this.partSim.resources[type] > SimManager.RESOURCE_MIN)
if (this.partSim.resources[type] > SimManager.RESOURCE_MIN && this.partSim.resourceFlowStates[type] != 0)
{
sourcePartSet = new HashSet<PartSim>();
//MonoBehaviour.print("SetResourceDrains(" + name + ":" + partId + ") setting sources to just this");
Expand All @@ -254,7 +254,7 @@ public bool SetResourceDrains(List<PartSim> allParts, List<PartSim> allFuelLines
case ResourceFlowMode.ALL_VESSEL:
foreach (PartSim aPartSim in allParts)
{
if (aPartSim.resources[type] > SimManager.RESOURCE_MIN)
if (aPartSim.resources[type] > SimManager.RESOURCE_MIN && aPartSim.resourceFlowStates[type] != 0)
{
if (sourcePartSet == null)
{
Expand All @@ -270,7 +270,7 @@ public bool SetResourceDrains(List<PartSim> allParts, List<PartSim> allFuelLines
var stagePartSets = new Dictionary<int, HashSet<PartSim>>();
var maxStage = -1;

Logger.Log(type);
//Logger.Log(type);
foreach (var aPartSim in allParts)
{
if (aPartSim.resources[type] <= SimManager.RESOURCE_MIN || aPartSim.resourceFlowStates[type] == 0) continue;
Expand Down
13 changes: 13 additions & 0 deletions KerbalEngineer/VesselSimulator/PartSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,19 @@ public double TimeToDrainResource()
return time;
}

public bool EmptyOf(HashSet<int> types)
{
foreach (int type in types)
{
if (this.resources.HasType(type) && this.resourceFlowStates[type] != 0 && (double)this.resources[type] > SimManager.RESOURCE_MIN)
{
return false;
}
}

return true;
}

public int DecouplerCount()
{
int count = 0;
Expand Down
167 changes: 96 additions & 71 deletions KerbalEngineer/VesselSimulator/SimManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ public class SimManager
private static readonly object locker = new object();
private static readonly Stopwatch timer = new Stopwatch();

// Support for RealFuels using reflection to check localCorrectThrust without dependency

private static FieldInfo RF_ModuleEngineConfigs_locaCorrectThrust;
private static FieldInfo RF_ModuleHybridEngine_locaCorrectThrust;
private static FieldInfo RF_ModuleHybridEngines_locaCorrectThrust;
private static bool bRequested;
private static bool bRunning;
private static TimeSpan delayBetweenSims;
private static bool hasCheckedForRealFuels;
private static bool hasInstalledRealFuels;

// Support for RealFuels using reflection to check localCorrectThrust without dependency

private static bool hasCheckedForMods;
private static bool hasInstalledRealFuels;
private static FieldInfo RF_ModuleEngineConfigs_localCorrectThrust;
private static FieldInfo RF_ModuleHybridEngine_localCorrectThrust;
private static FieldInfo RF_ModuleHybridEngines_localCorrectThrust;
private static bool hasInstalledKIDS;
private static MethodInfo KIDS_Utils_GetIspMultiplier;
private static bool bKIDSThrustISP = false;
#endregion

#region Delegates
Expand Down Expand Up @@ -89,56 +92,114 @@ public class SimManager

#region Methods

public static bool DoesEngineUseCorrectedThrust(Part theEngine)
private static void CheckForMods()
{
if (!hasInstalledRealFuels /*|| HighLogic.LoadedSceneIsFlight*/)
{
return false;
}
hasCheckedForMods = true;

// Look for any of the Real Fuels engine modules and call the relevant method to find out
if (RF_ModuleEngineConfigs_locaCorrectThrust != null && theEngine.Modules.Contains("ModuleEngineConfigs"))
foreach (var assembly in AssemblyLoader.loadedAssemblies)
{
var modEngineConfigs = theEngine.Modules["ModuleEngineConfigs"];
if (modEngineConfigs != null)
MonoBehaviour.print("Assembly:" + assembly.assembly);

var name = assembly.assembly.ToString().Split(',')[0];

if (name == "RealFuels")
{
// Check the localCorrectThrust
if ((bool)RF_ModuleEngineConfigs_locaCorrectThrust.GetValue(modEngineConfigs))
MonoBehaviour.print("Found RealFuels mod");

var RF_ModuleEngineConfigs_Type = assembly.assembly.GetType("RealFuels.ModuleEngineConfigs");
if (RF_ModuleEngineConfigs_Type != null)
{
RF_ModuleEngineConfigs_localCorrectThrust = RF_ModuleEngineConfigs_Type.GetField("localCorrectThrust");
}

var RF_ModuleHybridEngine_Type = assembly.assembly.GetType("RealFuels.ModuleHybridEngine");
if (RF_ModuleHybridEngine_Type != null)
{
return true;
RF_ModuleHybridEngine_localCorrectThrust = RF_ModuleHybridEngine_Type.GetField("localCorrectThrust");
}

var RF_ModuleHybridEngines_Type = assembly.assembly.GetType("RealFuels.ModuleHybridEngines");
if (RF_ModuleHybridEngines_Type != null)
{
RF_ModuleHybridEngines_localCorrectThrust = RF_ModuleHybridEngines_Type.GetField("localCorrectThrust");
}

hasInstalledRealFuels = true;
break;
}
else if (name == "KerbalIspDifficultyScaler")
{
var KIDS_Utils_Type = assembly.assembly.GetType("KerbalIspDifficultyScaler.KerbalIspDifficultyScalerUtils");
if (KIDS_Utils_Type != null)
{
KIDS_Utils_GetIspMultiplier = KIDS_Utils_Type.GetMethod("GetIspMultiplier");
}

hasInstalledKIDS = true;
}
}
}

if (RF_ModuleHybridEngine_locaCorrectThrust != null && theEngine.Modules.Contains("ModuleHybridEngine"))
public static bool DoesEngineUseCorrectedThrust(Part theEngine)
{
if (hasInstalledRealFuels)
{
var modHybridEngine = theEngine.Modules["ModuleHybridEngine"];
if (modHybridEngine != null)
// Look for any of the Real Fuels engine modules and call the relevant method to find out
if (RF_ModuleEngineConfigs_localCorrectThrust != null && theEngine.Modules.Contains("ModuleEngineConfigs"))
{
// Check the localCorrectThrust
if ((bool)RF_ModuleHybridEngine_locaCorrectThrust.GetValue(modHybridEngine))
var modEngineConfigs = theEngine.Modules["ModuleEngineConfigs"];
if (modEngineConfigs != null)
{
return true;
// Return the localCorrectThrust
return (bool)RF_ModuleEngineConfigs_localCorrectThrust.GetValue(modEngineConfigs);
}
}
}

if (RF_ModuleHybridEngines_locaCorrectThrust != null && theEngine.Modules.Contains("ModuleHybridEngines"))
{
var modHybridEngines = theEngine.Modules["ModuleHybridEngines"];
if (modHybridEngines != null)
if (RF_ModuleHybridEngine_localCorrectThrust != null && theEngine.Modules.Contains("ModuleHybridEngine"))
{
var modHybridEngine = theEngine.Modules["ModuleHybridEngine"];
if (modHybridEngine != null)
{
// Return the localCorrectThrust
return (bool)RF_ModuleHybridEngine_localCorrectThrust.GetValue(modHybridEngine);
}
}

if (RF_ModuleHybridEngines_localCorrectThrust != null && theEngine.Modules.Contains("ModuleHybridEngines"))
{
// Check the localCorrectThrust
if ((bool)RF_ModuleHybridEngines_locaCorrectThrust.GetValue(modHybridEngines))
var modHybridEngines = theEngine.Modules["ModuleHybridEngines"];
if (modHybridEngines != null)
{
return true;
// Return the localCorrectThrust
return (bool)RF_ModuleHybridEngines_localCorrectThrust.GetValue(modHybridEngines);
}
}
}

if (hasInstalledKIDS && HighLogic.LoadedSceneIsEditor)
{
return bKIDSThrustISP;
}

return false;
}

public static void UpdateModSettings()
{
if (!hasCheckedForMods)
{
CheckForMods();
}

if (hasInstalledKIDS)
{
// (out ispMultiplierVac, out ispMultiplierAtm, out extendToZeroIsp, out thrustCorrection, out ispCutoff, out thrustCutoff);
object[] parameters = new object[6];
KIDS_Utils_GetIspMultiplier.Invoke(null, parameters);
bKIDSThrustISP = (bool)parameters[3];
}
}

public static String GetVesselTypeString(VesselType vesselType)
{
switch (vesselType)
Expand Down Expand Up @@ -171,9 +232,9 @@ public static String GetVesselTypeString(VesselType vesselType)

public static void RequestSimulation()
{
if (!hasCheckedForRealFuels)
if (!hasCheckedForMods)
{
GetRealFuelsTypes();
CheckForMods();
}

lock (locker)
Expand Down Expand Up @@ -217,42 +278,6 @@ private static void ClearResults()
LastStage = null;
}

private static void GetRealFuelsTypes()
{
hasCheckedForRealFuels = true;

foreach (var assembly in AssemblyLoader.loadedAssemblies)
{
MonoBehaviour.print("Assembly:" + assembly.assembly);

if (assembly.assembly.ToString().Split(',')[0] == "RealFuels")
{
MonoBehaviour.print("Found RealFuels mod");

var RF_ModuleEngineConfigs_Type = assembly.assembly.GetType("RealFuels.ModuleEngineConfigs");
if (RF_ModuleEngineConfigs_Type != null)
{
RF_ModuleEngineConfigs_locaCorrectThrust = RF_ModuleEngineConfigs_Type.GetField("localCorrectThrust");
}

var RF_ModuleHybridEngine_Type = assembly.assembly.GetType("RealFuels.ModuleHybridEngine");
if (RF_ModuleHybridEngine_Type != null)
{
RF_ModuleHybridEngine_locaCorrectThrust = RF_ModuleHybridEngine_Type.GetField("localCorrectThrust");
}

var RF_ModuleHybridEngines_Type = assembly.assembly.GetType("RealFuels.ModuleHybridEngines");
if (RF_ModuleHybridEngines_Type != null)
{
RF_ModuleHybridEngines_locaCorrectThrust = RF_ModuleHybridEngines_Type.GetField("localCorrectThrust");
}

hasInstalledRealFuels = true;
break;
}
}
}

private static void RunSimulation(object simObject)
{
try
Expand Down
2 changes: 1 addition & 1 deletion KerbalEngineer/VesselSimulator/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ private bool AllowedToStage()
}
//buffer.AppendFormat("isSepratron = {0}\n", partSim.isSepratron ? "true" : "false");

if (!partSim.isSepratron && !partSim.Resources.EmptyOf(this.drainingResources))
if (!partSim.isSepratron && !partSim.EmptyOf(this.drainingResources))
{
if (SimManager.logOutput)
{
Expand Down

0 comments on commit c937a3d

Please sign in to comment.