Skip to content

Commit 3b91f83

Browse files
authored
Fix for(var/V in list) for special lists (#2368)
1 parent bc172c9 commit 3b91f83

File tree

13 files changed

+138
-115
lines changed

13 files changed

+138
-115
lines changed

OpenDreamRuntime/AtomManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public void SetAppearanceVar(MutableAppearance appearance, string varName, Dream
385385
appearance.Verbs.Clear();
386386

387387
if (value.TryGetValueAsDreamList(out var valueList)) {
388-
foreach (DreamValue verbValue in valueList.GetValues()) {
388+
foreach (DreamValue verbValue in valueList.EnumerateValues()) {
389389
if (!verbValue.TryGetValueAsProc(out var verb))
390390
continue;
391391

OpenDreamRuntime/ByondApi/ByondApi.Functions.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,19 @@ private static byte Byond_ReadList(CByondValue* loc, CByondValue* list, uint* le
303303
return 0;
304304
}
305305

306-
var srcDreamVals = srcList.GetValues();
307-
int length = srcDreamVals.Count;
306+
int length = srcList.GetLength();
308307
*len = (uint)length;
309308
if (list == null || providedBufLen < length) {
310309
return 0;
311310
}
312311

313312
try {
314-
for (int i = 0; i < length; i++) {
315-
list[i] = ValueToByondApi(srcDreamVals[i]);
313+
int i = 0;
314+
foreach (var value in srcList.EnumerateValues()) {
315+
if (i >= length)
316+
throw new Exception($"List {srcList} had more elements than the expected {length}");
317+
318+
list[i++] = ValueToByondApi(value);
316319
}
317320
} catch (Exception) {
318321
return 0;
@@ -706,7 +709,7 @@ private static byte Byond_Block(CByondXYZ* corner1, CByondXYZ* corner2, CByondVa
706709
corner1->x, corner1->y, corner1->z,
707710
corner2->x, corner2->y, corner2->z);
708711

709-
foreach (var turf in turfs.GetValues()) {
712+
foreach (var turf in turfs.EnumerateValues()) {
710713
list.Add(ValueToByondApi(turf));
711714
}
712715
} catch (Exception) {
@@ -879,15 +882,10 @@ private static byte Byond_NewArglist(CByondValue* cType, CByondValue* cArglist,
879882
var objectDef = treeEntry.ObjectDefinition;
880883

881884
var arglistVal = ValueFromDreamApi(*cArglist);
882-
if (!arglistVal.TryGetValueAsDreamList(out DreamList? arglist)) return 0;
885+
if (!arglistVal.TryGetValueAsIDreamList(out var arglist)) return 0;
883886

884887
// Copy the arglist's values to a new array to ensure no shenanigans
885-
var argListValues = arglist.GetValues();
886-
var argValues = new DreamValue[argListValues.Count];
887-
for (int i = 0; i < argListValues.Count; i++) {
888-
argValues[i] = argListValues[i];
889-
}
890-
888+
var argValues = arglist.CopyToArray();
891889
var args = new DreamProcArguments(argValues);
892890

893891
// TODO: This is code duplicated with DMOpcodeHandlers.CreateObject()

OpenDreamRuntime/DreamConnection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ public Task<DreamValue> Prompt(DreamValueType types, string title, string messag
273273
return task;
274274
}
275275

276-
public async Task<DreamValue> PromptList(DreamValueType types, DreamList list, string title, string message, DreamValue defaultValue) {
277-
List<DreamValue> listValues = list.GetValues();
276+
public async Task<DreamValue> PromptList(DreamValueType types, IDreamList list, string title, string message, DreamValue defaultValue) {
277+
DreamValue[] listValues = list.CopyToArray();
278278

279-
List<string> promptValues = new(listValues.Count);
279+
List<string> promptValues = new(listValues.Length);
280280
foreach (var value in listValues) {
281281
if (types.HasFlag(DreamValueType.Obj) && !value.TryGetValueAsDreamObject<DreamObjectMovable>(out _))
282282
continue;
@@ -307,7 +307,7 @@ public async Task<DreamValue> PromptList(DreamValueType types, DreamList list, s
307307

308308
// The client returns the index of the selected item, this needs turned back into the DreamValue.
309309
var selectedIndex = await task;
310-
if (selectedIndex.TryGetValueAsInteger(out int index) && index < listValues.Count) {
310+
if (selectedIndex.TryGetValueAsInteger(out int index) && index < listValues.Length) {
311311
return listValues[index];
312312
}
313313

0 commit comments

Comments
 (0)