Skip to content

Commit

Permalink
Add location for all DCSAircraft
Browse files Browse the repository at this point in the history
With this they can be loaded from either aircraft_modules or common_modules.
  • Loading branch information
jdahlblom committed Apr 11, 2024
1 parent 1af04a6 commit 446c52f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 36 deletions.
70 changes: 46 additions & 24 deletions src/ClassLibraryCommon/DCSAircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
using System.IO;
using System.Linq;

public enum DCSBIOSLocation
{
AircraftModules,
CommonModules,
None
}

/// <summary>
/// Holds information about currently selected aircraft / module (DCS aircraft/helicopter).
/// This class reads all modules from dcs-bios_modules.txt and the user can then select between these
Expand All @@ -17,20 +24,16 @@ public class DCSAircraft

private static readonly object Lock = new();
private static List<DCSAircraft> _modulesList = new();
public const int MetaModuleIDStart = 500;

public static DCSAircraft SelectedAircraft { get; set; }
public const string DCSBIOS_META_DATA_START_FILE_NAME = "MetadataStart.json";
public const string DCSBIOS_META_DATA_END_FILE_NAME = "MetadataEnd.json";
public const string DCSBIOS_COMMON_DATA_FILE_NAME = "CommonData.json";
private const string DCSBIOS_META_DATA_START_MODULE = "MetadataStart";
private const string DCSBIOS_META_DATA_END_MODULE = "MetadataEnd";
private const string DCSBIOS_COMMON_DATA_MODULE = "CommonData";

private DCSAircraft(int id, string description, string jsonFilename)
private DCSAircraft(int id, string description, string jsonFilename, DCSBIOSLocation dcsbiosLocation)
{
ID = id;
JSONFilename = jsonFilename;
Description = description;
DCSBIOSLocation = dcsbiosLocation;
}

public static void Init()
Expand All @@ -42,15 +45,14 @@ public static void Init()

public string JSONFilename { get; }

public DCSBIOSLocation DCSBIOSLocation { get;}

public string LuaFilename
{
get => JSONFilename.Replace(".json", ".lua");
}

public bool IsMetaModule
{
get => JSONFilename.Contains(DCSBIOS_META_DATA_END_MODULE) || JSONFilename.Contains(DCSBIOS_META_DATA_START_MODULE) || JSONFilename.Contains(DCSBIOS_COMMON_DATA_MODULE);
}
public bool IsMetaModule => ID >= MetaModuleIDStart;

/// <summary>
/// This is not exact science
Expand Down Expand Up @@ -115,18 +117,18 @@ private static void AddInternalModules()
{
if (!_modulesList.Exists(o => o.ID == 1))
{
var module = new DCSAircraft(1, "NoFrameLoadedYet", "NOFRAMELOADEDYET");
var module = new DCSAircraft(1, "NoFrameLoadedYet", "NOFRAMELOADEDYET", DCSBIOSLocation.None);
_modulesList.Add(module);
}

if (!_modulesList.Exists(o => o.ID == 2))
{
var module = new DCSAircraft(2, "Key Emulation", "KEYEMULATOR");
var module = new DCSAircraft(2, "Key Emulation", "KEYEMULATOR", DCSBIOSLocation.None);
_modulesList.Add(module);
}
if (!_modulesList.Exists(o => o.ID == 3))
{
var module = new DCSAircraft(3, "Key Emulation with SRS support", "KEYEMULATOR_SRS");
var module = new DCSAircraft(3, "Key Emulation with SRS support", "KEYEMULATOR_SRS", DCSBIOSLocation.None);
_modulesList.Add(module);
}
}
Expand Down Expand Up @@ -159,15 +161,7 @@ public static void FillModulesListFromDcsBios(string dcsbiosJsonFolder, bool loa

var stringArray = File.ReadAllLines(dcsbiosConfigFile);

//Inject these static DCS-BIOS modules

lock (Lock)
{
_modulesList.Add(new DCSAircraft(500, DCSBIOS_META_DATA_END_MODULE, DCSBIOS_META_DATA_END_FILE_NAME));
_modulesList.Add(new DCSAircraft(501, DCSBIOS_META_DATA_START_MODULE, DCSBIOS_META_DATA_START_FILE_NAME));
_modulesList.Add(new DCSAircraft(502, DCSBIOS_COMMON_DATA_MODULE, DCSBIOS_COMMON_DATA_FILE_NAME));
}


// A-10C|5|A-10C Thunderbolt/II
foreach (var s in stringArray)
{
Expand All @@ -182,10 +176,23 @@ public static void FillModulesListFromDcsBios(string dcsbiosJsonFolder, bool loa
var json = parts[0] + ".json";
var id = int.Parse(parts[1]);
var properName = parts[2];
var dcsbiosLocation = DCSBIOSLocation.None;

if (parts.Length == 3)
{
//Not all modules have the property set, for those it is assumed they are located in AircraftModules.
dcsbiosLocation = DCSBIOSLocation.AircraftModules;
}
else
{
var enumParseResult = Enum.TryParse<DCSBIOSLocation>(parts[3], true, out var dcsbiosLocationParse);
if (enumParseResult) dcsbiosLocation = dcsbiosLocationParse;
}


lock (Lock)
{
_modulesList.Add(new DCSAircraft(id, properName, json));
_modulesList.Add(new DCSAircraft(id, properName, json, dcsbiosLocation));
}
}

Expand Down Expand Up @@ -243,6 +250,21 @@ public static DCSAircraft GetKeyEmulator()
return module;
}

public static string GetMetaDataEndJSONFilename()
{
return _modulesList.First(o => o.JSONFilename.Contains("MetadataEnd")).JSONFilename;
}

public static string GetMetaDataStartJSONFilename()
{
return _modulesList.First(o => o.JSONFilename.Contains("MetadataStart")).JSONFilename;
}

public static string GetCommonDataJSONFilename()
{
return _modulesList.First(o => o.JSONFilename.Contains("CommonData")).JSONFilename;
}

public static DCSAircraft GetKeyEmulatorSRS()
{
var module = Modules.FirstOrDefault(IsKeyEmulatorSRS);
Expand Down
7 changes: 5 additions & 2 deletions src/ClassLibraryCommon/dcs-bios_modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Mi-8MT|29|Mi-8MT
Mig-15bis|30|MiG-15bis
Mig-19P|31|MiG-19P Farmer
Mig-21Bis|32|MiG-21bis
NS430|33|NS 430 GPS
NS430|33|NS-430 Navigation System|CommonModules
P-47D|34|P-47D Thunderbolt
P-51D|35|TF/P-51D Mustang
SA342|36|SA342 Gazelle
Expand All @@ -48,4 +48,7 @@ F-15E|44|F-15E Strike Eagle
Mosquito|45|Mosquito FB Mk. VI
AH-64D|46|AH-64D Apache
MH-60R|47|MH-60R SeaHawk
NS-430|48|NS-430 Navigation System

MetadataEnd|500|MetadataEnd|CommonModules
MetadataStart|501|MetadataStart|CommonModules
CommonData|502|CommonData|CommonModules
23 changes: 14 additions & 9 deletions src/DCS-BIOS/ControlLocator/DCSBIOSControlLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace DCS_BIOS.ControlLocator
using Newtonsoft.Json;
using NLog;


/// <summary>
/// Reads the aircraft's / helicopter's JSON file containing the cockpit controls.
/// Whenever a class needs a specific DCS-BIOS control it asks for the control using
Expand Down Expand Up @@ -208,9 +209,13 @@ public static List<DCSBIOSControl> GetModuleControlsFromJson(string filename, bo
/// </summary>
public static List<DCSBIOSControl> GetMetaControls()
{
var controlList = GetModuleControlsFromJson(DCSAircraft.DCSBIOS_META_DATA_START_FILE_NAME, true);
controlList.AddRange(GetModuleControlsFromJson(DCSAircraft.DCSBIOS_META_DATA_END_FILE_NAME, true));
controlList.AddRange(GetModuleControlsFromJson(DCSAircraft.DCSBIOS_COMMON_DATA_FILE_NAME, true));
var controlList = new List<DCSBIOSControl>();

foreach (var dcsAircraft in DCSAircraft.Modules.Where(o => o.ID >= DCSAircraft.MetaModuleIDStart))
{
controlList.AddRange(GetModuleControlsFromJson(dcsAircraft.JSONFilename, true));
}

return controlList;
}

Expand Down Expand Up @@ -404,7 +409,7 @@ private static List<DCSBIOSControl> ReadControlsFromDocJson(string fileFullPath)

private static void LoadMetaDataEnd(string jsonDirectory)
{
if (DCSBIOSAircraftLoadStatus.IsLoaded(DCSAircraft.DCSBIOS_META_DATA_END_FILE_NAME) || Common.IsEmulationModesFlagSet(EmulationMode.KeyboardEmulationOnly) || DCSAircraft.IsNoFrameLoadedYet(_dcsAircraft))
if (DCSBIOSAircraftLoadStatus.IsLoaded(DCSAircraft.GetMetaDataEndJSONFilename()) || Common.IsEmulationModesFlagSet(EmulationMode.KeyboardEmulationOnly) || DCSAircraft.IsNoFrameLoadedYet(_dcsAircraft))
{
return;
}
Expand All @@ -413,9 +418,9 @@ private static void LoadMetaDataEnd(string jsonDirectory)
{
lock (LockObject)
{
_dcsbiosControls.AddRange(ReadControlsFromDocJson(jsonDirectory + $"\\{DCSAircraft.DCSBIOS_META_DATA_END_FILE_NAME}"));
_dcsbiosControls.AddRange(ReadControlsFromDocJson(jsonDirectory + $"\\{DCSAircraft.GetMetaDataEndJSONFilename()}"));

DCSBIOSAircraftLoadStatus.SetLoaded(DCSAircraft.DCSBIOS_META_DATA_END_FILE_NAME, true);
DCSBIOSAircraftLoadStatus.SetLoaded(DCSAircraft.GetMetaDataEndJSONFilename(), true);
}
}
catch (Exception ex)
Expand All @@ -426,7 +431,7 @@ private static void LoadMetaDataEnd(string jsonDirectory)

private static void LoadCommonData(string jsonDirectory)
{
if (DCSBIOSAircraftLoadStatus.IsLoaded(DCSAircraft.DCSBIOS_COMMON_DATA_FILE_NAME) || Common.IsEmulationModesFlagSet(EmulationMode.KeyboardEmulationOnly) || DCSAircraft.IsNoFrameLoadedYet(_dcsAircraft))
if (DCSBIOSAircraftLoadStatus.IsLoaded(DCSAircraft.GetCommonDataJSONFilename()) || Common.IsEmulationModesFlagSet(EmulationMode.KeyboardEmulationOnly) || DCSAircraft.IsNoFrameLoadedYet(_dcsAircraft))
{
return;
}
Expand All @@ -435,8 +440,8 @@ private static void LoadCommonData(string jsonDirectory)
{
lock (LockObject)
{
_dcsbiosControls.AddRange(ReadControlsFromDocJson(jsonDirectory + $"\\{DCSAircraft.DCSBIOS_COMMON_DATA_FILE_NAME}"));
DCSBIOSAircraftLoadStatus.SetLoaded(DCSAircraft.DCSBIOS_COMMON_DATA_FILE_NAME, true);
_dcsbiosControls.AddRange(ReadControlsFromDocJson(jsonDirectory + $"\\{DCSAircraft.GetCommonDataJSONFilename()}"));
DCSBIOSAircraftLoadStatus.SetLoaded(DCSAircraft.GetCommonDataJSONFilename(), true);
}
}
catch (Exception ex)
Expand Down
7 changes: 6 additions & 1 deletion src/DCS-BIOS/misc/LuaAssistant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class LuaAssistant
private static DCSAircraft _dcsAircraft;
private static string _jsonDirectory;
private static string _dcsbiosAircraftLuaLocation;
private static string _dcsbiosCommonModulesLuaLocation;
private static string _dcsbiosModuleLuaFilePath;
private static readonly object LockObject = new();
private const string DCSBIOS_LUA_NOT_FOUND_ERROR_MESSAGE = "Error loading DCS-BIOS lua.";
Expand Down Expand Up @@ -44,6 +45,7 @@ internal static string JSONDirectory
{
_jsonDirectory = value;
_dcsbiosAircraftLuaLocation = $@"{value}\..\..\lib\modules\aircraft_modules\";
_dcsbiosCommonModulesLuaLocation = $@"{value}\..\..\lib\modules\common_modules\";
_dcsbiosModuleLuaFilePath = $@"{value}\..\..\lib\modules\Module.lua";
}
}
Expand Down Expand Up @@ -232,7 +234,10 @@ private static void ReadLuaCommandsFromLua()
{
lock (LockObject)
{
ReadControlsFromLua(_dcsAircraft, $"{_dcsbiosAircraftLuaLocation}{_dcsAircraft.LuaFilename}");
var location = _dcsAircraft.DCSBIOSLocation == DCSBIOSLocation.AircraftModules
? _dcsbiosAircraftLuaLocation
: _dcsbiosCommonModulesLuaLocation;
ReadControlsFromLua(_dcsAircraft, $"{location}{_dcsAircraft.LuaFilename}");
ReadModuleFunctionSignatures(_dcsbiosModuleLuaFilePath);
}
}
Expand Down

0 comments on commit 446c52f

Please sign in to comment.