-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix] Borg and Golem LayDown #906
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System.Linq; | ||
using Content.Server.Administration; | ||
using Content.Server.Silicons.Laws; | ||
using Content.Shared.Administration; | ||
using Content.Shared.Silicons.Laws; | ||
using Content.Shared.Silicons.Laws.Components; | ||
using Robust.Shared.Toolshed; | ||
using Robust.Shared.Toolshed.TypeParsers; | ||
|
||
namespace Content.Server.Backmen.Administration.Commands.Toolshed; | ||
|
||
[ToolshedCommand, AdminCommand(AdminFlags.Admin)] | ||
public sealed class LawsCommand : ToolshedCommand | ||
{ | ||
private SiliconLawSystem? _law; | ||
|
||
[CommandImplementation("list")] | ||
public IEnumerable<EntityUid> List() | ||
{ | ||
var query = EntityManager.EntityQueryEnumerator<SiliconLawBoundComponent>(); | ||
while (query.MoveNext(out var uid, out _)) | ||
{ | ||
yield return uid; | ||
} | ||
} | ||
|
||
[CommandImplementation("get")] | ||
public IEnumerable<string> Get([PipedArgument] EntityUid lawbound) | ||
{ | ||
_law ??= GetSys<SiliconLawSystem>(); | ||
|
||
foreach (var law in _law.GetLaws(lawbound).Laws) | ||
{ | ||
yield return $"law {law.LawIdentifierOverride ?? law.Order.ToString()}: {Loc.GetString(law.LawString)}"; | ||
} | ||
} | ||
|
||
[CommandImplementation("set")] | ||
public EntityUid? SetLaws( | ||
[CommandInvocationContext] IInvocationContext ctx, | ||
[PipedArgument] EntityUid input, | ||
[CommandArgument] Prototype<SiliconLawsetPrototype> siliconLawSetPrototype | ||
) | ||
{ | ||
_law ??= GetSys<SiliconLawSystem>(); | ||
|
||
_law.SetLaws(input, siliconLawSetPrototype.Value); | ||
return input; | ||
} | ||
|
||
[CommandImplementation("set")] | ||
public IEnumerable<EntityUid> SetLaws( | ||
[CommandInvocationContext] IInvocationContext ctx, | ||
[PipedArgument] IEnumerable<EntityUid> input, | ||
[CommandArgument] Prototype<SiliconLawsetPrototype> siliconLawSetPrototype | ||
) | ||
{ | ||
return input.Where(ent => SetLaws(ctx, ent, siliconLawSetPrototype) != null); | ||
} | ||
|
||
[CommandImplementation("override")] | ||
public EntityUid? OverrideLaw( | ||
[CommandInvocationContext] IInvocationContext ctx, | ||
[PipedArgument] EntityUid input, | ||
[CommandArgument] int index, | ||
[CommandArgument] string lawString | ||
) | ||
{ | ||
if (index < 0) | ||
return null; | ||
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate Index Before Inserting New Law When inserting a new law into Apply this diff to fix the issue: -if (index < 0)
+if (index < 0 || index > laws.Laws.Count)
return null; This ensures that the index is within the valid range before insertion. Also applies to: 77-84 |
||
_law ??= GetSys<SiliconLawSystem>(); | ||
|
||
var laws = _law.GetLaws(input); | ||
var law = laws.Laws.FirstOrDefault(x => x.Order == index); | ||
if (law == null) | ||
{ | ||
laws.Laws.Insert(index, | ||
new SiliconLaw() | ||
{ | ||
Order = index, | ||
LawString = lawString, | ||
} | ||
); | ||
} | ||
else | ||
{ | ||
law.LawString = lawString; | ||
} | ||
|
||
return input; | ||
} | ||
|
||
[CommandImplementation("override")] | ||
public IEnumerable<EntityUid> OverrideLaw( | ||
[CommandInvocationContext] IInvocationContext ctx, | ||
[PipedArgument] IEnumerable<EntityUid> input, | ||
[CommandArgument] int index, | ||
[CommandArgument] string lawString | ||
) | ||
{ | ||
return input.Where(uid => OverrideLaw(ctx, uid, index, lawString) != null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -269,6 +269,25 @@ public SiliconLawset GetLawset(ProtoId<SiliconLawsetPrototype> lawset) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return laws; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//backmen-start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void SetLaws(EntityUid uid, SiliconLawsetPrototype prototype) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (_prototype.TryIndex<SiliconLawsetPrototype>(prototype, out var lawSet)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var laws = lawSet.Laws | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.Select(x => _prototype.Index<SiliconLawPrototype>(x)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.Select(x => new SiliconLaw() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Order = x.Order, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LawString = x.LawString, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LawIdentifierOverride = x.LawIdentifierOverride, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.ToList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SetLaws(laws, uid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+273
to
+288
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling and improve consistency with existing patterns The current implementation has several issues that need to be addressed:
Apply these improvements: - public void SetLaws(EntityUid uid, SiliconLawsetPrototype prototype)
+ /// <summary>
+ /// Sets the laws for a silicon entity using a lawset prototype.
+ /// </summary>
+ /// <param name="uid">The entity to set laws for</param>
+ /// <param name="protoId">The ID of the lawset prototype</param>
+ /// <exception cref="ArgumentException">Thrown when the prototype is not found</exception>
+ public void SetLaws(EntityUid uid, ProtoId<SiliconLawsetPrototype> protoId)
{
- if (_prototype.TryIndex<SiliconLawsetPrototype>(prototype, out var lawSet))
- {
- var laws = lawSet.Laws
- .Select(x => _prototype.Index<SiliconLawPrototype>(x))
- .Select(x => new SiliconLaw()
- {
- Order = x.Order,
- LawString = x.LawString,
- LawIdentifierOverride = x.LawIdentifierOverride,
- })
- .ToList();
- SetLaws(laws, uid);
- }
+ var lawSet = _prototype.Index(protoId);
+ var laws = lawSet.Laws
+ .Select(x => _prototype.Index(x))
+ .Select(x => new SiliconLaw
+ {
+ Order = x.Order,
+ LawString = x.LawString,
+ LawIdentifierOverride = x.LawIdentifierOverride,
+ })
+ .ToList();
+ SetLaws(laws, uid);
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//backmen-end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Set the laws of a silicon entity while notifying the player. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -299,30 +318,3 @@ protected override void OnUpdaterInsert(Entity<SiliconLawUpdaterComponent> ent, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ToolshedCommand, AdminCommand(AdminFlags.Admin)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public sealed class LawsCommand : ToolshedCommand | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private SiliconLawSystem? _law; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[CommandImplementation("list")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public IEnumerable<EntityUid> List() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var query = EntityManager.EntityQueryEnumerator<SiliconLawBoundComponent>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (query.MoveNext(out var uid, out _)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
yield return uid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[CommandImplementation("get")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public IEnumerable<string> Get([PipedArgument] EntityUid lawbound) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_law ??= GetSys<SiliconLawSystem>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach (var law in _law.GetLaws(lawbound).Laws) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
yield return $"law {law.LawIdentifierOverride ?? law.Order.ToString()}: {Loc.GetString(law.LawString)}"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Potential Thread-Safety Issue with Lazy Initialization of
_law
The
_law
field is lazily initialized in multiple methods without synchronization. In a multithreaded context, this could lead to race conditions. Consider initializing_law
once in the constructor or at declaration time to ensure thread safety.Apply this diff to fix the issue: