Skip to content

Commit

Permalink
fix: load items by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
mortik committed May 16, 2024
1 parent 704c96a commit 5e6661e
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 90 deletions.
15 changes: 8 additions & 7 deletions Loader/EntityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class EntityService
{
public string OutputFolder { get; set; }
public string DataRoot { get; set; }
public Dictionary<string, string> referenceToClassNameMap;

Dictionary<string, string> classNameToFilenameMap;
Dictionary<string, string> referenceToClassNameMap;
Dictionary<string, string> classNameToTypeMap;
Dictionary<string, EntityClassDefinition> classNameToEntityMap;
ClassParser<EntityClassDefinition> entityParser = new ClassParser<EntityClassDefinition>();
Expand Down Expand Up @@ -122,12 +122,13 @@ public EntityClassDefinition GetByFilename(string filename)
var entity = LoadEntity(filename);
if (entity == null) return null;

if (!classNameToFilenameMap.ContainsKey(entity.ClassName)) {
classNameToFilenameMap.Add(entity.ClassName, filename);
referenceToClassNameMap.Add(entity.__ref, entity.ClassName);
classNameToEntityMap.Add(entity.ClassName, entity);
classNameToTypeMap.Add(entity.ClassName, entity.Components.SAttachableComponentParams?.AttachDef.Type ?? "");
}
if (!classNameToFilenameMap.ContainsKey(entity.ClassName))
{
classNameToFilenameMap.Add(entity.ClassName, filename);
referenceToClassNameMap.Add(entity.__ref, entity.ClassName);
classNameToEntityMap.Add(entity.ClassName, entity);
classNameToTypeMap.Add(entity.ClassName, entity.Components?.SAttachableComponentParams?.AttachDef.Type ?? "");
}

return entity;
}
Expand Down
76 changes: 38 additions & 38 deletions Loader/ItemBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ List<string> BuildTagList(EntityClassDefinition entity)
{
var tags = new List<string>();

var tagString = entity.Components.SAttachableComponentParams?.AttachDef.Tags;
var tagString = entity.Components?.SAttachableComponentParams?.AttachDef.Tags;
if (tagString != null)
{
foreach (var tag in tagString.Split(" "))
Expand All @@ -109,7 +109,7 @@ List<StandardisedItemPort> BuildPortList(EntityClassDefinition entity)

if (entity?.Components?.SItemPortContainerComponentParams == null) return ports;

foreach (var port in entity.Components.SItemPortContainerComponentParams.Ports)
foreach (var port in entity.Components?.SItemPortContainerComponentParams.Ports)
{
var stdPort = new StandardisedItemPort
{
Expand Down Expand Up @@ -165,7 +165,7 @@ List<string> BuildPortFlags(SItemPortDef port)

StandardisedShield BuildShieldInfo(EntityClassDefinition item)
{
var shield = item.Components.SCItemShieldGeneratorParams;
var shield = item.Components?.SCItemShieldGeneratorParams;
if (shield == null) return null;

return new StandardisedShield
Expand All @@ -188,7 +188,7 @@ StandardisedShield BuildShieldInfo(EntityClassDefinition item)

StandardisedQuantumDrive BuildQuantumDriveInfo(EntityClassDefinition item)
{
var qdComponent = item.Components.SCItemQuantumDriveParams;
var qdComponent = item.Components?.SCItemQuantumDriveParams;
if (qdComponent == null) return null;

return new StandardisedQuantumDrive
Expand Down Expand Up @@ -216,8 +216,8 @@ StandardisedQuantumDrive BuildQuantumDriveInfo(EntityClassDefinition item)

StandardisedDurability BuildDurabilityInfo(EntityClassDefinition item)
{
var degradation = item.Components.SDegradationParams;
var health = item.Components.SHealthComponentParams;
var degradation = item.Components?.SDegradationParams;
var health = item.Components?.SHealthComponentParams;

if (degradation == null && health == null) return null;

Expand All @@ -230,8 +230,8 @@ StandardisedDurability BuildDurabilityInfo(EntityClassDefinition item)

StandardisedPowerPlant BuildPowerPlantInfo(EntityClassDefinition item)
{
if (item.Components.SAttachableComponentParams?.AttachDef.Type != "PowerPlant") return null;
var powerPlant = item.Components.EntityComponentPowerConnection;
if (item.Components?.SAttachableComponentParams?.AttachDef.Type != "PowerPlant") return null;
var powerPlant = item.Components?.EntityComponentPowerConnection;
if (powerPlant == null) return null;

return new StandardisedPowerPlant
Expand All @@ -242,7 +242,7 @@ StandardisedPowerPlant BuildPowerPlantInfo(EntityClassDefinition item)

StandardisedCooler BuildCoolerInfo(EntityClassDefinition item)
{
var cooler = item.Components.SCItemCoolerParams;
var cooler = item.Components?.SCItemCoolerParams;
if (cooler == null) return null;

return new StandardisedCooler
Expand All @@ -253,7 +253,7 @@ StandardisedCooler BuildCoolerInfo(EntityClassDefinition item)

StandardisedThruster BuildThrusterInfo(EntityClassDefinition item)
{
var thruster = item.Components.SCItemThrusterParams;
var thruster = item.Components?.SCItemThrusterParams;
if (thruster == null) return null;

return new StandardisedThruster
Expand All @@ -267,7 +267,7 @@ StandardisedThruster BuildThrusterInfo(EntityClassDefinition item)

StandardisedCargoGrid BuildCargoGridInfo(EntityClassDefinition item)
{
var cargo = item.Components.SCItemCargoGridParams;
var cargo = item.Components?.SCItemCargoGridParams;
if (cargo == null) return null;

return new StandardisedCargoGrid
Expand All @@ -282,10 +282,10 @@ StandardisedCargoGrid BuildCargoGridInfo(EntityClassDefinition item)

StandardisedFuelTank BuildQuantumFuelTankInfo(EntityClassDefinition item)
{
var tank = item.Components.SCItemFuelTankParams;
var tank = item.Components?.SCItemFuelTankParams;
if (tank == null) return null;

if (item.Components.SAttachableComponentParams.AttachDef.Type != "QuantumFuelTank") return null;
if (item.Components?.SAttachableComponentParams.AttachDef.Type != "QuantumFuelTank") return null;

return new StandardisedFuelTank
{
Expand All @@ -295,10 +295,10 @@ StandardisedFuelTank BuildQuantumFuelTankInfo(EntityClassDefinition item)

StandardisedFuelTank BuildHydrogenFuelTankInfo(EntityClassDefinition item)
{
var tank = item.Components.SCItemFuelTankParams;
var tank = item.Components?.SCItemFuelTankParams;
if (tank == null) return null;

if (item.Components.SAttachableComponentParams.AttachDef.Type != "FuelTank") return null;
if (item.Components?.SAttachableComponentParams.AttachDef.Type != "FuelTank") return null;

return new StandardisedFuelTank
{
Expand All @@ -308,7 +308,7 @@ StandardisedFuelTank BuildHydrogenFuelTankInfo(EntityClassDefinition item)

StandardisedFuelIntake BuildHydrogenFuelIntakeInfo(EntityClassDefinition item)
{
var intake = item.Components.SCItemFuelIntakeParams;
var intake = item.Components?.SCItemFuelIntakeParams;
if (intake == null) return null;

return new StandardisedFuelIntake
Expand All @@ -319,7 +319,7 @@ StandardisedFuelIntake BuildHydrogenFuelIntakeInfo(EntityClassDefinition item)

StandardisedArmour BuildArmourInfo(EntityClassDefinition item)
{
var armour = item.Components.SCItemVehicleArmorParams;
var armour = item.Components?.SCItemVehicleArmorParams;
if (armour == null) return null;

return new StandardisedArmour
Expand All @@ -345,7 +345,7 @@ StandardisedArmour BuildArmourInfo(EntityClassDefinition item)

StandardisedEmp BuildEmpInfo(EntityClassDefinition item)
{
var emp = item.Components.SCItemEMPParams;
var emp = item.Components?.SCItemEMPParams;
if (emp == null) return null;

return new StandardisedEmp
Expand All @@ -359,10 +359,10 @@ StandardisedEmp BuildEmpInfo(EntityClassDefinition item)

StandardisedMissileRack BuildMissileRackInfo(EntityClassDefinition item)
{
if (item.Components.SAttachableComponentParams?.AttachDef.Type != "MissileLauncher") return null;
if (item.Components.SAttachableComponentParams?.AttachDef.SubType != "MissileRack") return null;
if (item.Components?.SAttachableComponentParams?.AttachDef.Type != "MissileLauncher") return null;
if (item.Components?.SAttachableComponentParams?.AttachDef.SubType != "MissileRack") return null;

var rootPort = item.Components.SItemPortContainerComponentParams;
var rootPort = item.Components?.SItemPortContainerComponentParams;
if (rootPort == null) return null;

var rackPorts = rootPort.Ports;
Expand All @@ -377,7 +377,7 @@ StandardisedMissileRack BuildMissileRackInfo(EntityClassDefinition item)

StandardisedQig BuildQigInfo(EntityClassDefinition item)
{
var qig = item.Components.SCItemQuantumInterdictionGeneratorParams;
var qig = item.Components?.SCItemQuantumInterdictionGeneratorParams;
if (qig == null) return null;

return new StandardisedQig
Expand All @@ -389,7 +389,7 @@ StandardisedQig BuildQigInfo(EntityClassDefinition item)

StandardisedIfcs BuildIfcsInfo(EntityClassDefinition item)
{
var ifcs = item.Components.IFCSParams;
var ifcs = item.Components?.IFCSParams;
if (ifcs == null) return null;

return new StandardisedIfcs
Expand All @@ -415,7 +415,7 @@ StandardisedIfcs BuildIfcsInfo(EntityClassDefinition item)

StandardisedCoolerConnection BuildHeatConnectionInfo(EntityClassDefinition item)
{
var heat = item.Components.EntityComponentHeatConnection;
var heat = item.Components?.EntityComponentHeatConnection;
if (heat == null) return null;

return new StandardisedCoolerConnection
Expand All @@ -428,7 +428,7 @@ StandardisedCoolerConnection BuildHeatConnectionInfo(EntityClassDefinition item)

StandardisedPowerConnection BuildPowerConnectionInfo(EntityClassDefinition item)
{
var power = item.Components.EntityComponentPowerConnection;
var power = item.Components?.EntityComponentPowerConnection;
if (power == null) return null;

return new StandardisedPowerConnection
Expand All @@ -440,7 +440,7 @@ StandardisedPowerConnection BuildPowerConnectionInfo(EntityClassDefinition item)

StandardisedWeapon BuildWeaponInfo(EntityClassDefinition item)
{
var weapon = item.Components.SCItemWeaponComponentParams;
var weapon = item.Components?.SCItemWeaponComponentParams;
if (weapon == null) return null;

var info = new StandardisedWeapon
Expand Down Expand Up @@ -551,7 +551,7 @@ StandardisedAmmunition BuildAmmunitionInfo(EntityClassDefinition item)
Size = ammo.size,
ImpactDamage = ConvertDamage(impactDamage),
DetonationDamage = ConvertDamage(detonationDamage),
Capacity = item.Components.SAmmoContainerComponentParams?.maxAmmoCount ?? item.Components.SAmmoContainerComponentParams?.maxRestockCount,
Capacity = item.Components?.SAmmoContainerComponentParams?.maxAmmoCount ?? item.Components?.SAmmoContainerComponentParams?.maxRestockCount,
BulletImpulseFalloff = new StandardisedBulletImpulseFalloff{
MinDistance = projectiles?.impulseFalloffParams?.BulletImpulseFalloffParams?.minDistance,
DropFalloff = projectiles?.impulseFalloffParams?.BulletImpulseFalloffParams?.dropFalloff,
Expand Down Expand Up @@ -621,11 +621,11 @@ StandardisedDamage ConvertDamage(DamageInfo damage)
AmmoParams GetAmmoParams(EntityClassDefinition item)
{
// If this a weapon that contains its own ammo, or if it is a magazine, then it will have an SCAmmoContainerComponentParams component.
var ammoRef = item.Components.SAmmoContainerComponentParams?.ammoParamsRecord;
var ammoRef = item.Components?.SAmmoContainerComponentParams?.ammoParamsRecord;
if (ammoRef != null) return ammoSvc.GetByReference(ammoRef);

// Otherwise if this is a weapon then SCItemWeaponComponentParams.ammoContainerRecord should be the reference of a magazine entity
var magRef = item.Components.SCItemWeaponComponentParams?.ammoContainerRecord;
var magRef = item.Components?.SCItemWeaponComponentParams?.ammoContainerRecord;
if (magRef == null) return null;
var mag = entitySvc.GetByReference(magRef);
if (mag == null) return null;
Expand All @@ -636,7 +636,7 @@ AmmoParams GetAmmoParams(EntityClassDefinition item)

StandardisedMissile BuildMissileInfo(EntityClassDefinition item)
{
var missile = item.Components.SCItemMissileParams;
var missile = item.Components?.SCItemMissileParams;
if (missile == null) return null;

var info = new StandardisedMissile
Expand All @@ -649,7 +649,7 @@ StandardisedMissile BuildMissileInfo(EntityClassDefinition item)

StandardisedMissile BuildBombInfo(EntityClassDefinition item)
{
var missile = item.Components.SCItemBombParams;
var missile = item.Components?.SCItemBombParams;
if (missile == null) return null;

var info = new StandardisedMissile
Expand All @@ -662,7 +662,7 @@ StandardisedMissile BuildBombInfo(EntityClassDefinition item)

StandardisedScanner BuildScannerInfo(EntityClassDefinition item)
{
var scanner = item.Components.SSCItemScannerComponentParams;
var scanner = item.Components?.SSCItemScannerComponentParams;
if (scanner == null) return null;

var info = new StandardisedScanner
Expand All @@ -675,7 +675,7 @@ StandardisedScanner BuildScannerInfo(EntityClassDefinition item)

StandardisedRadar BuildRadarInfo(EntityClassDefinition item)
{
var radar = item.Components.SCItemRadarComponentParams;
var radar = item.Components?.SCItemRadarComponentParams;
if (radar == null) return null;

var info = new StandardisedRadar
Expand All @@ -692,11 +692,11 @@ StandardisedRadar BuildRadarInfo(EntityClassDefinition item)
List<StandardisedSignatureDetection> BuildDetectionSignatures(EntityClassDefinition item)
{
var detections = new List<StandardisedSignatureDetection>();
var signatureDetection = item.Components.SCItemRadarComponentParams.signatureDetection;
var signatureDetection = item.Components?.SCItemRadarComponentParams.signatureDetection;

if (signatureDetection == null) return null;

foreach (var detection in item.Components.SCItemRadarComponentParams.signatureDetection)
foreach (var detection in item.Components?.SCItemRadarComponentParams.signatureDetection)
{
detections.Add(new StandardisedSignatureDetection
{
Expand All @@ -711,7 +711,7 @@ List<StandardisedSignatureDetection> BuildDetectionSignatures(EntityClassDefinit

StandardisedPing BuildPingInfo(EntityClassDefinition item)
{
var ping = item.Components.SSCItemPingComponentParams;
var ping = item.Components?.SSCItemPingComponentParams;
if (ping == null) return null;

var info = new StandardisedPing
Expand All @@ -725,7 +725,7 @@ StandardisedPing BuildPingInfo(EntityClassDefinition item)

StandardisedWeaponRegenPool BuildWeaponRegenInfo(EntityClassDefinition item)
{
var regen = item.Components.SCItemWeaponRegenPoolComponentParams;
var regen = item.Components?.SCItemWeaponRegenPoolComponentParams;
if (regen == null) return null;

var info = new StandardisedWeaponRegenPool
Expand All @@ -740,7 +740,7 @@ StandardisedWeaponRegenPool BuildWeaponRegenInfo(EntityClassDefinition item)

StandardisedInventoryContainer BuildPersonalInventoryInfo(EntityClassDefinition item)
{
var inventoryRef = item.Components.SCItemInventoryContainerComponentParams?.containerParams;
var inventoryRef = item.Components?.SCItemInventoryContainerComponentParams?.containerParams;
if (inventoryRef != null) return _inventoryContainerSvc.GetInventoryContainer(inventoryRef);

return null;
Expand Down
Loading

0 comments on commit 5e6661e

Please sign in to comment.