Files
ss14-wega/Content.Shared/Construction/MachinePartSystem.cs
Nemanja d2ac15c76f Fix flatpacker exploit ignoring board costs (#42445)
Fix flatpacks ignoring costs and board requirements
2026-01-16 00:22:24 +00:00

138 lines
5.6 KiB
C#

using System.Linq;
using Content.Shared.Construction.Components;
using Content.Shared.Examine;
using Content.Shared.Lathe;
using Content.Shared.Materials;
using Robust.Shared.Prototypes;
namespace Content.Shared.Construction
{
/// <summary>
/// Deals with machine parts and machine boards.
/// </summary>
public sealed class MachinePartSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly SharedLatheSystem _lathe = default!;
[Dependency] private readonly SharedConstructionSystem _construction = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MachineBoardComponent, ExaminedEvent>(OnMachineBoardExamined);
}
private void OnMachineBoardExamined(EntityUid uid, MachineBoardComponent component, ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
using (args.PushGroup(nameof(MachineBoardComponent)))
{
args.PushMarkup(Loc.GetString("machine-board-component-on-examine-label"));
foreach (var (material, amount) in component.StackRequirements)
{
var stack = _prototype.Index(material);
var name = _prototype.Index(stack.Spawn).Name;
args.PushMarkup(Loc.GetString("machine-board-component-required-element-entry-text",
("amount", amount),
("requiredElement", Loc.GetString(name))));
}
foreach (var (_, info) in component.ComponentRequirements)
{
var examineName = _construction.GetExamineName(info);
args.PushMarkup(Loc.GetString("machine-board-component-required-element-entry-text",
("amount", info.Amount),
("requiredElement", examineName)));
}
foreach (var (_, info) in component.TagRequirements)
{
var examineName = _construction.GetExamineName(info);
args.PushMarkup(Loc.GetString("machine-board-component-required-element-entry-text",
("amount", info.Amount),
("requiredElement", examineName)));
}
}
}
public bool TryGetMachineBoardMaterialCost(Entity<MachineBoardComponent> entity, out Dictionary<string, int> materials, int coefficient = 1)
{
var (_, comp) = entity;
materials = new Dictionary<string, int>();
foreach (var (stackId, amount) in comp.StackRequirements)
{
var stackProto = _prototype.Index(stackId);
var defaultProto = _prototype.Index(stackProto.Spawn);
if (defaultProto.TryGetComponent<PhysicalCompositionComponent>(out var physComp, EntityManager.ComponentFactory))
{
foreach (var (mat, matAmount) in physComp.MaterialComposition)
{
materials.TryAdd(mat, 0);
materials[mat] += matAmount * amount * coefficient;
}
}
else if (_lathe.TryGetRecipesFromEntity(stackProto.Spawn, out var recipes))
{
var partRecipe = recipes[0];
if (recipes.Count > 1)
partRecipe = recipes.MinBy(p => p.Materials.Values.Sum());
foreach (var (mat, matAmount) in partRecipe!.Materials)
{
materials.TryAdd(mat, 0);
materials[mat] += matAmount * amount * coefficient;
}
}
else
{
// The item has no material cost, so we cannot get the full cost.
return false;
}
}
var genericPartInfo = comp.ComponentRequirements.Values.Concat(comp.TagRequirements.Values);
foreach (var info in genericPartInfo)
{
var amount = info.Amount;
var defaultProtoId = info.DefaultPrototype;
if (_lathe.TryGetRecipesFromEntity(defaultProtoId, out var recipes))
{
var partRecipe = recipes[0];
if (recipes.Count > 1)
partRecipe = recipes.MinBy(p => p.Materials.Values.Sum());
foreach (var (mat, matAmount) in partRecipe!.Materials)
{
materials.TryAdd(mat, 0);
materials[mat] += matAmount * amount * coefficient;
}
}
else if (_prototype.Resolve(defaultProtoId, out var defaultProto) &&
defaultProto.TryGetComponent<PhysicalCompositionComponent>(out var physComp, EntityManager.ComponentFactory))
{
foreach (var (mat, matAmount) in physComp.MaterialComposition)
{
materials.TryAdd(mat, 0);
materials[mat] += matAmount * amount * coefficient;
}
}
else
{
// The item has no material cost, so we cannot get the full cost.
return false;
}
}
// We were able to construct all elements of the recipe.
return true;
}
}
}