Skip to content

Commit

Permalink
Merge pull request #11 from cvusmo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
blacksheepcosmo authored Aug 22, 2023
2 parents efd9cce + 1c47f06 commit ccab55a
Show file tree
Hide file tree
Showing 27 changed files with 1,302 additions and 563 deletions.
18 changes: 18 additions & 0 deletions FFTProject/Controllers/Interfaces/IListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using KSP.Messages;

namespace FFT.Controllers.Interfaces
{
public interface IListener
{
public interface IGameStateListener
{
event Action<GameStateEnteredMessage> GameStateEntered;
event Action<GameStateLeftMessage> GameStateLeft;
}

public interface IVesselSituationListener
{
event Action<VesselSituationChangedMessage> VesselSituationChanged;
}
}
}
9 changes: 9 additions & 0 deletions FFTProject/Controllers/Interfaces/ILoadModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FFT.Controllers.Interfaces
{
public interface ILoadModule
{
void Boot();
void PreLoad();
void Load();
}
}
7 changes: 7 additions & 0 deletions FFTProject/Controllers/Interfaces/IModuleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FFT.Controllers.Interfaces
{
public interface IModuleController
{
void SetLoadModule(ILoadModule loadModule);
}
}
12 changes: 12 additions & 0 deletions FFTProject/Controllers/Interfaces/IResetModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace FFT.Controllers.Interfaces
{
public interface IResetModule
{
void Reset();
void Unload();
}
}
13 changes: 13 additions & 0 deletions FFTProject/Controllers/Interfaces/IStartModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace FFT.Controllers.Interfaces
{
public interface IStartModule
{
void StartVentValve();
void ActivateModule(ModuleController.ModuleType moduleType);
void DeactivateModule(ModuleController.ModuleType moduleType);
}
}
93 changes: 93 additions & 0 deletions FFTProject/Controllers/LoadModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//|=====================Summary========================|0|
//| Ensures that the correct module gets loaded |1|
//|by cvusmo===========================================|4|
//|====================================================|1|

using BepInEx.Logging;
using FFT.Controllers.Interfaces;
using FFT.Managers;
using FFT.Modules;
using FFT.Utilities;
using Newtonsoft.Json;
using System;

namespace FFT.Controllers
{
[JsonObject(MemberSerialization.OptIn)]
public class LoadModule : ILoadModule
{
[JsonProperty]
public bool EnableVFX { get; private set; } = true;

private static readonly object _lock = new object();
private readonly ManualLogSource _logger = BepInEx.Logging.Logger.CreateLogSource("LoadModule: ");

internal RefreshVesselData.RefreshActiveVessel RefreshActiveVessel => RefreshVesselData.Instance.RefreshActiveVesselInstance;

private Manager _manager;
private MessageManager _messageManager;
private ConditionsManager _conditionsManager;
private ModuleController _moduleController;
private StartModule _startModule;
private ResetModule _resetModule;
private Module_VentValve _moduleVentValve;

public event Action ModuleResetRequested;

private static LoadModule _instance;
private static readonly Lazy<LoadModule> _lazyInstance = new Lazy<LoadModule>(() => new LoadModule());
public static LoadModule Instance
{
get
{
if (_instance == null)
{
_instance = _lazyInstance.Value;
_instance.InitializeDependencies();
}
return _instance;
}
}
private LoadModule() { }
public void InitializeDependencies()
{
_manager = Manager.Instance;
_messageManager = MessageManager.Instance;
_conditionsManager = ConditionsManager.Instance;
_moduleController = ModuleController.Instance;
_startModule = StartModule.Instance;
_resetModule = ResetModule.Instance;
_moduleVentValve = new Module_VentValve();
}
public void Boot()
{
Utility.RefreshGameManager();
if (RefreshActiveVessel.IsFlightActive && EnableVFX)
{
_logger.LogInfo("Booting Module_VentValve");
_moduleController.SetModuleState(ModuleController.ModuleType.ModuleVentValve, true);
PreLoad();
}
}
public void PreLoad()
{
Utility.RefreshGameManager();
if (_moduleController.GetModuleState(ModuleController.ModuleType.ModuleVentValve))
{
_logger.LogInfo("Preloading Module_VentValve");
_moduleVentValve = new Module_VentValve();
Load();
}
}
public void Load()
{
Utility.RefreshGameManager();
if (RefreshActiveVessel.IsFlightActive && _moduleController.GetModuleState(ModuleController.ModuleType.ModuleVentValve))
{
_logger.LogInfo("Loading Module_VentValve");
_messageManager.SubscribeToMessages();
_moduleController.IsModuleLoaded = true;
}
}
}
}
88 changes: 88 additions & 0 deletions FFTProject/Controllers/ModuleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//|=====================Summary========================|0|
//| Dictionary for module types |1|
//|by cvusmo===========================================|4|
//|====================================================|1|
using FFT.Controllers.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FFT.Controllers
{
public sealed class ModuleController
{
private readonly Dictionary<ModuleType, bool> moduleStates = new Dictionary<ModuleType, bool>();
private static ModuleController _instance;
private static readonly object _lock = new object();
public enum ModuleType
{
Default = 0,
ModuleVentValve = 1,
ModuleOne = 2,
ModuleTwo = 3,
ModuleThree = 4,
ModuleFour = 5
}
internal bool IsModuleLoaded { get; set; }
internal bool ShouldResetModule { get; set; }
internal static ModuleController Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new ModuleController();
}
}
}
return _instance;
}
}
private ModuleController()
{
InitializeModuleStates();
}
private void InitializeModuleStates()
{
foreach (ModuleType moduleType in Enum.GetValues(typeof(ModuleType)))
{
moduleStates[moduleType] = false;
}
}
internal void SetModuleState(ModuleType type, bool state)
{
if (!moduleStates.ContainsKey(type))
{
throw new ArgumentException($"Unsupported ModuleType: {type}");
}

moduleStates[type] = state;
}
internal bool GetModuleState(ModuleType type)
{
if (moduleStates.TryGetValue(type, out bool state))
{
return state;
}
throw new ArgumentException($"Unsupported ModuleType: {type}");
}
internal void SetVentValveState(bool state)
{
SetModuleState(ModuleType.ModuleVentValve, state);
}
internal void ResetAllModuleStates()
{
foreach (var key in moduleStates.Keys.ToList())
{
moduleStates[key] = false;
}

IsModuleLoaded = false;
ShouldResetModule = false;
}
}
}
89 changes: 89 additions & 0 deletions FFTProject/Controllers/ResetModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//|=====================Summary========================|0|
//| Resets the ConditionsManager to its default states |1|
//|by cvusmo===========================================|4|
//|====================================================|1|
using FFT.Controllers.Interfaces;
using FFT.Managers;
using FFT.Utilities;
using Newtonsoft.Json;

namespace FFT.Controllers
{
[JsonObject(MemberSerialization.OptIn)]
public class ResetModule : IResetModule
{
public event Action ModuleResetRequested = delegate { };

private readonly ConditionsManager _conditionsManager;
private readonly Manager _manager;
private readonly ModuleController _moduleController;
private readonly RefreshVesselData _vesselData;
private bool? _isFlightActiveCache;

private static readonly object _lock = new object();
private static ResetModule _instance;
public ResetModule(
ConditionsManager conditionsManager,
Manager manager,
ModuleController moduleController,
RefreshVesselData vesselData)
{
_conditionsManager = conditionsManager;
_manager = manager;
_moduleController = moduleController;
_vesselData = vesselData;
}
public static ResetModule Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new ResetModule(ConditionsManager.Instance, Manager.Instance, ModuleController.Instance, new RefreshVesselData());
}
}
}
return _instance;
}
}
public void Reset()
{
if (!_moduleController.ShouldResetModule) return;

if (Utility.VesselSituations == KSP.Sim.impl.VesselSituations.PreLaunch ||
Utility.VesselSituations == KSP.Sim.impl.VesselSituations.Landed ||
Utility.VesselSituations == KSP.Sim.impl.VesselSituations.Flying)
{
ModuleResetRequested.Invoke();
}

_moduleController.ShouldResetModule = false;
_moduleController.IsModuleLoaded = false;
}
public void Unload()
{
bool isFlightCurrentlyActive = _vesselData.RefreshActiveVesselInstance.IsFlightActive;

if (_isFlightActiveCache.HasValue && !_isFlightActiveCache.Value ||
!_isFlightActiveCache.HasValue && !isFlightCurrentlyActive)
{
_isFlightActiveCache = false;
_manager.Logger.LogInfo("Unloading Module");

if (_moduleController.GetModuleState(ModuleController.ModuleType.ModuleVentValve))
{
Reset();
_manager.Logger.LogInfo("Reset Module_VentValve");
}
}
else
{
_isFlightActiveCache = true;
}
}
}
}
Loading

0 comments on commit ccab55a

Please sign in to comment.