Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
66 changes: 38 additions & 28 deletions OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using OpenDreamRuntime.Rendering;
using OpenDreamShared.Dream;
using Robust.Server.GameStates;
using Robust.Shared.Serialization.Manager;
using Dependency = Robust.Shared.IoC.DependencyAttribute;

namespace OpenDreamRuntime.Objects.Types;
Expand Down Expand Up @@ -980,19 +979,9 @@ public override int FindValue(DreamValue value, int start = 1, int end = 0) {

// atom.filters list
// Operates on an object's appearance
public sealed class DreamFilterList : DreamList {
[Dependency] private readonly AtomManager _atomManager = default!;
[Dependency] private readonly ISerializationManager _serializationManager = default!;

private readonly DreamObject _owner;

public DreamFilterList(DreamObjectDefinition listDef, DreamObject owner) : base(listDef, 0) {
IoCManager.InjectDependencies(this);
_owner = owner;
}

public sealed class DreamFilterList(DreamObjectDefinition listDef, DreamObject owner) : DreamList(listDef, 0) {
public override void Cut(int start = 1, int end = 0) {
_atomManager.UpdateAppearance(_owner, appearance => {
AtomManager.UpdateAppearance(owner, appearance => {
int filterCount = appearance.Filters.Count + 1;
if (end == 0 || end > filterCount) end = filterCount;

Expand All @@ -1002,18 +991,26 @@ public override void Cut(int start = 1, int end = 0) {

public int GetIndexOfFilter(DreamFilter filter) {
ImmutableAppearance appearance = GetAppearance();
int i = 0;
while(i < appearance.Filters.Length) {
if(appearance.Filters[i] == filter)
for (int i = 0; i < appearance.Filters.Length; i++) {
if (appearance.Filters[i] == filter)
return i;
}

return -1;
}

public int GetIndexOfFilter(string filterName) {
ImmutableAppearance appearance = GetAppearance();
for (int i = 0; i < appearance.Filters.Length; i++) {
if (appearance.Filters[i].FilterName == filterName)
return i;
i++;
}

return -1;
}

public void SetFilter(int index, DreamFilter? filter) {
_atomManager.UpdateAppearance(_owner, appearance => {
AtomManager.UpdateAppearance(owner, appearance => {
if (index < 1 || index > appearance.Filters.Count)
throw new Exception($"Cannot index {index} on filter list");

Expand All @@ -1031,8 +1028,16 @@ public void SetFilter(int index, DreamFilter? filter) {
}

public override DreamValue GetValue(DreamValue key) {
if (!key.TryGetValueAsInteger(out var filterIndex) || filterIndex < 1)
int filterIndex;
if (key.TryGetValueAsString(out var filterName)) {
filterIndex = GetIndexOfFilter(filterName) + 1; // We're 1-indexed while GetIndexOfFilter() is not
} else {
key.TryGetValueAsInteger(out filterIndex);
}

if (filterIndex < 1) { // The key failed to resolve to a valid index
throw new Exception($"Invalid index into filter list: {key}");
}

ImmutableAppearance appearance = GetAppearance();
if (filterIndex > appearance.Filters.Length)
Expand Down Expand Up @@ -1077,23 +1082,28 @@ public override void AddValue(DreamValue value) {
//This is dynamic to prevent the compiler from optimising the SerializationManager.CreateCopy() call to the DreamFilter type
//so we can preserve the subclass information. Setting it to DreamFilter instead will cause filter parameters to stop working.
dynamic filter = filterObject.Filter;
DreamFilter copy = _serializationManager.CreateCopy(filter, notNullableOverride: true); // Adding a filter creates a copy
DreamFilter copy = SerializationManager.CreateCopy(filter, notNullableOverride: true); // Adding a filter creates a copy

DreamObjectFilter.FilterAttachedTo[copy] = this;
_atomManager.UpdateAppearance(_owner, appearance => {
AtomManager.UpdateAppearance(owner, appearance => {
appearance.Filters.Add(copy);
});
}

public override void RemoveValue(DreamValue value) {
if (!value.TryGetValueAsDreamObject<DreamObjectFilter>(out var filterObject))
return;
int filterIndex = -1;
if (value.TryGetValueAsString(out var filterName)) { // You can also do atom.filters -= "name"
filterIndex = GetIndexOfFilter(filterName);
} else if (value.TryGetValueAsDreamObject<DreamObjectFilter>(out var filterObject)) {
filterIndex = GetIndexOfFilter(filterObject.Filter);
}

var filter = filterObject.Filter;
if (filterIndex < 0) // Failed to find the filter in the list
return;

DreamObjectFilter.FilterAttachedTo.Remove(filter);
_atomManager.UpdateAppearance(_owner, appearance => {
appearance.Filters.Remove(filter);
AtomManager.UpdateAppearance(owner, appearance => {
DreamObjectFilter.FilterAttachedTo.Remove(appearance.Filters[filterIndex]);
appearance.Filters.RemoveAt(filterIndex);
});
}

Expand All @@ -1106,7 +1116,7 @@ public override int FindValue(DreamValue value, int start = 1, int end = 0) {
}

private ImmutableAppearance GetAppearance() {
ImmutableAppearance? appearance = _atomManager.MustGetAppearance(_owner);
ImmutableAppearance? appearance = AtomManager.MustGetAppearance(owner);
if (appearance == null)
throw new Exception("Atom has no appearance");

Expand Down
4 changes: 2 additions & 2 deletions OpenDreamRuntime/Procs/DMProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ public DreamProcArguments CreateProcArguments(ReadOnlySpan<DreamValue> values, D
string argumentName = key.MustGetValueAsString();
int argumentIndex = proc.ArgumentNames.IndexOf(argumentName);
if (argumentIndex == -1)
throw new Exception($"{proc} has no argument named {argumentName}");
throw new Exception($"{proc} has no argument named \"{argumentName}\"");

arguments[argumentIndex] = value;
}
Expand Down Expand Up @@ -1110,7 +1110,7 @@ public DreamProcArguments CreateProcArguments(ReadOnlySpan<DreamValue> values, D

int argumentIndex = proc.ArgumentNames.IndexOf(argumentName);
if (argumentIndex == -1)
throw new Exception($"{proc} has no argument named {argumentName}");
throw new Exception($"{proc} has no argument named \"{argumentName}\"");

arguments[argumentIndex] = argList.GetValue(value);
} else { //Ordered argument
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ public static DreamValue NativeProc_file2text(NativeProc.Bundle bundle, DreamObj

[DreamProc("filter")]
[DreamProcParameter("type", Type = DreamValueTypeFlag.String)] // Must be from a valid list
[DreamProcParameter("name", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("size", Type = DreamValueTypeFlag.Float)]
[DreamProcParameter("color", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("x", Type = DreamValueTypeFlag.Float)]
Expand Down
5 changes: 4 additions & 1 deletion OpenDreamShared/Dream/DreamFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ public partial record DreamFilter {
/// </summary>
public bool Used = false;

[ViewVariables, DataField("type")]
[ViewVariables(VVAccess.ReadOnly), DataField("type")]
public string FilterType;

[ViewVariables(VVAccess.ReadOnly), DataField("name")]
public string? FilterName;

public static Type? GetType(string filterType) {
return filterType switch {
"alpha" => typeof(DreamFilterAlpha),
Expand Down
Loading