Skip to content
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

List append self #2153

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Content.Tests/DMProject/Tests/List/ListAppendSelf.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/proc/RunTest()
L1 = list(1, 2)
L1 += L1
ASSERT(L1 ~= list(1, 2, 1, 2))

L2 = list(a=1, b=2)
L2 += L2
ASSERT(L2 ~= list(a=1, b=2, "a", "b"))
23 changes: 13 additions & 10 deletions OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,14 @@ public override DreamValue OperatorOr(DreamValue b, DMProcState state) {

public override DreamValue OperatorAppend(DreamValue b) {
if (b.TryGetValueAsDreamList(out var bList)) {
foreach (DreamValue value in bList.GetValues()) {
if (bList._associativeValues?.TryGetValue(value, out var assocValue) is true) {
SetValue(value, assocValue);
} else {
AddValue(value);
var values = bList.GetValues();
var valueCount = values.Count; // Some lists return a reference to their internal values list which could change with each loop
for (int i = 0; i < valueCount; i++) {
var value = values[i];
AddValue(value); // Always add the value
if (bList._associativeValues?.TryGetValue(value, out var assocValue) is true) { // Ensure the associated value is correct
_associativeValues ??= new();
_associativeValues[value] = assocValue;
}
}
} else {
Expand Down Expand Up @@ -574,9 +577,9 @@ public override int GetLength() {
// atom's verbs list
// Keeps track of an appearance's verbs (atom.verbs, mutable_appearance.verbs, etc)
public sealed class VerbsList(DreamObjectTree objectTree, AtomManager atomManager, ServerVerbSystem? verbSystem, DreamObjectAtom atom) : DreamList(objectTree.List.ObjectDefinition, 0) {
public override DreamValue GetValue(DreamValue key) {
public override DreamValue GetValue(DreamValue key) {
if (verbSystem == null)
return DreamValue.Null;
return DreamValue.Null;
if (!key.TryGetValueAsInteger(out var index))
throw new Exception($"Invalid index into verbs list: {key}");

Expand Down Expand Up @@ -820,12 +823,12 @@ public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth
public override void AddValue(DreamValue value) {
EntityUid entity;
if (value.TryGetValueAsDreamObject<DreamObjectMovable>(out var movable)) {
if(_visContents.Contains(movable))
if (_visContents.Contains(movable))
return; // vis_contents cannot contain duplicates
_visContents.Add(movable);
entity = movable.Entity;
} else if (value.TryGetValueAsDreamObject<DreamObjectTurf>(out var turf)) {
if(_visContents.Contains(turf))
if (_visContents.Contains(turf))
return; // vis_contents cannot contain duplicates
_visContents.Add(turf);
entity = EntityUid.Invalid; // TODO: Support turfs in vis_contents
Expand Down Expand Up @@ -934,7 +937,7 @@ public override List<DreamValue> GetValues() {
}

public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth = false) {
if (!value.TryGetValueAsDreamObject<DreamObjectFilter>(out var filterObject) &&!value.IsNull)
if (!value.TryGetValueAsDreamObject<DreamObjectFilter>(out var filterObject) && !value.IsNull)
throw new Exception($"Cannot set value of filter list to {value}");
if (!key.TryGetValueAsInteger(out var filterIndex) || filterIndex < 1)
throw new Exception($"Invalid index into filter list: {key}");
Expand Down
Loading