Skip to content

Commit

Permalink
Merge branch 'master' into appearanceRefCounts
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle authored Nov 8, 2024
2 parents c18de10 + 7fe3aba commit c63f765
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 72 deletions.
14 changes: 0 additions & 14 deletions DMCompiler/DM/Builders/DMASTFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,6 @@ private DMASTExpression FoldExpression(DMASTExpression? expression) {

break;
}
case DMASTLeftShift leftShift: {
if (leftShift is { LHS: DMASTConstantInteger lhsInt, RHS: DMASTConstantInteger rhsInt }) {
return new DMASTConstantInteger(expression.Location, lhsInt.Value << rhsInt.Value);
}

break;
}
case DMASTRightShift rightShift: {
if (rightShift is { LHS: DMASTConstantInteger lhsInt, RHS: DMASTConstantInteger rhsInt }) {
return new DMASTConstantInteger(expression.Location, lhsInt.Value >> rhsInt.Value);
}

break;
}
case DMASTBinaryAnd binaryAnd: {
if (binaryAnd is { LHS: DMASTConstantInteger lhsInt, RHS: DMASTConstantInteger rhsInt }) {
return new DMASTConstantInteger(expression.Location, lhsInt.Value & rhsInt.Value);
Expand Down
53 changes: 53 additions & 0 deletions DMCompiler/Optimizer/PeepholeOptimizations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,57 @@ public void Apply(List<IAnnotatedBytecode> input, int index) {
new AnnotatedBytecodeInstruction(DreamProcOpcode.PushFloat, 1, args));
}
}

// PushFloat [constant]
// PushFloat [constant]
// BitshiftLeft
// -> PushFloat [result]
internal sealed class ConstFoldBitshiftLeft : IPeepholeOptimization {
public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
return [
DreamProcOpcode.PushFloat,
DreamProcOpcode.PushFloat,
DreamProcOpcode.BitShiftLeft,
];
}

public void Apply(List<IAnnotatedBytecode> input, int index) {
var firstInstruction = IPeepholeOptimization.GetInstructionAndValue(input[index], out var pushVal1);
IPeepholeOptimization.GetInstructionAndValue(input[index + 1], out var pushVal2);

// At runtime, given "A << B" we pop B then A
// In the peephole optimizer, index is "A", index+1 is "B"
var args = new List<IAnnotatedBytecode>(1) {new AnnotatedBytecodeFloat(((int)pushVal1 << (int)pushVal2), firstInstruction.Location)};

IPeepholeOptimization.ReplaceInstructions(input, index, 3,
new AnnotatedBytecodeInstruction(DreamProcOpcode.PushFloat, 1, args));
}
}

// PushFloat [constant]
// PushFloat [constant]
// BitshiftRight
// -> PushFloat [result]
internal sealed class ConstFoldBitshiftRight : IPeepholeOptimization {
public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
return [
DreamProcOpcode.PushFloat,
DreamProcOpcode.PushFloat,
DreamProcOpcode.BitShiftRight,
];
}

public void Apply(List<IAnnotatedBytecode> input, int index) {
var firstInstruction = IPeepholeOptimization.GetInstructionAndValue(input[index], out var pushVal1);
IPeepholeOptimization.GetInstructionAndValue(input[index + 1], out var pushVal2);

// At runtime, given "A >> B" we pop B then A
// In the peephole optimizer, index is "A", index+1 is "B"
var args = new List<IAnnotatedBytecode>(1) {new AnnotatedBytecodeFloat(((int)pushVal1 >> (int)pushVal2), firstInstruction.Location)};

IPeepholeOptimization.ReplaceInstructions(input, index, 3,
new AnnotatedBytecodeInstruction(DreamProcOpcode.PushFloat, 1, args));
}
}

#endregion
1 change: 0 additions & 1 deletion OpenDreamClient/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace OpenDreamClient;
public sealed class EntryPoint : GameClient {
[Dependency] private readonly IDreamInterfaceManager _dreamInterface = default!;
[Dependency] private readonly IDreamResourceManager _dreamResource = default!;
[Dependency] private readonly IDreamSoundEngine _soundEngine = default!;
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly ILightManager _lightManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamClient/Input/ContextMenu/ContextMenuPopup.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void RepopulateEntities(ClientObjectReference[] entities, int? turfId) {
foreach (var objectReference in entities) {
if (objectReference.Type == ClientObjectReference.RefType.Entity) {
var entity = _entityManager.GetEntity(objectReference.Entity);
if (_xformQuery.TryGetComponent(entity, out TransformComponent? transform) && !_mapManager.IsGrid(_transformSystem.GetParent(transform)!.Owner)) // Not a child of another entity
if (_xformQuery.TryGetComponent(entity, out TransformComponent? transform) && !_mapManager.IsGrid(_transformSystem.GetParentUid(entity))) // Not a child of another entity
continue;
if (!_spriteQuery.TryGetComponent(entity, out DMISpriteComponent? sprite)) // Has a sprite
continue;
Expand Down
8 changes: 4 additions & 4 deletions OpenDreamClient/Interface/BrowsePopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace OpenDreamClient.Interface;

internal sealed class BrowsePopup {
public event Action Closed;
public event Action? Closed;

public ControlBrowser Browser;
public ControlWindow WindowElement;
public readonly ControlBrowser Browser;
public readonly ControlWindow WindowElement;

private OSWindow _window;
private readonly OSWindow _window;

public BrowsePopup(
string name,
Expand Down
8 changes: 4 additions & 4 deletions OpenDreamClient/Interface/Controls/ControlChild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected override Control CreateUIElement() {
protected override void UpdateElementDescriptor() {
base.UpdateElementDescriptor();

var newLeftElement = ChildDescriptor.Left.Value != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Left.Value, out var leftWindow)
var newLeftElement = _interfaceManager.Windows.TryGetValue(ChildDescriptor.Left.Value, out var leftWindow)
? leftWindow.UIElement
: null;
var newRightElement = ChildDescriptor.Right.Value != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Right.Value, out var rightWindow)
var newRightElement = _interfaceManager.Windows.TryGetValue(ChildDescriptor.Right.Value, out var rightWindow)
? rightWindow.UIElement
: null;

Expand All @@ -45,9 +45,9 @@ protected override void UpdateElementDescriptor() {
}

public override void Shutdown() {
if (ChildDescriptor.Left.Value != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Left.Value, out var left))
if (_interfaceManager.Windows.TryGetValue(ChildDescriptor.Left.Value, out var left))
left.Shutdown();
if (ChildDescriptor.Right.Value != null && _interfaceManager.Windows.TryGetValue(ChildDescriptor.Right.Value, out var right))
if (_interfaceManager.Windows.TryGetValue(ChildDescriptor.Right.Value, out var right))
right.Shutdown();
}

Expand Down
27 changes: 13 additions & 14 deletions OpenDreamClient/Interface/Controls/ControlTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@ protected override void UpdateElementDescriptor() {

_tabs.Clear();
_tab.RemoveAllChildren();
if (TabDescriptor.Tabs.Value != null) {
var tabIds = TabDescriptor.Tabs.Value.Split(',',
StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);

foreach (var tabId in tabIds) {
if (!_interfaceManager.Windows.TryGetValue(tabId, out var pane))
continue;

TabContainer.SetTabTitle(pane.UIElement, pane.Title);
_tab.AddChild(pane.UIElement);
_tabs.Add(pane);
if (TabDescriptor.CurrentTab.Value == pane.Title)
_tab.CurrentTab = pane.UIElement.GetPositionInParent();
}

var tabIds = TabDescriptor.Tabs.Value.Split(',',
StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);

foreach (var tabId in tabIds) {
if (!_interfaceManager.Windows.TryGetValue(tabId, out var pane))
continue;

TabContainer.SetTabTitle(pane.UIElement, pane.Title);
_tab.AddChild(pane.UIElement);
_tabs.Add(pane);
if (TabDescriptor.CurrentTab.Value == pane.Title)
_tab.CurrentTab = pane.UIElement.GetPositionInParent();
}
}

Expand Down
4 changes: 2 additions & 2 deletions OpenDreamClient/Interface/Controls/ControlWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class ControlWindow : InterfaceControl {

public readonly List<InterfaceControl> ChildControls = new();

public string Title => WindowDescriptor.Title.Value ?? (WindowDescriptor.IsDefault.Value ? "OpenDream World" : WindowDescriptor.Id.AsRaw());
public string Title => WindowDescriptor.Title.Value;
public InterfaceMacroSet Macro => _interfaceManager.MacroSets[WindowDescriptor.Macro.AsRaw()];

private WindowDescriptor WindowDescriptor => (WindowDescriptor)ElementDescriptor;
Expand All @@ -37,7 +37,7 @@ protected override void UpdateElementDescriptor() {
// Don't call base.UpdateElementDescriptor();

_menuContainer.RemoveAllChildren();
if (WindowDescriptor.Menu.Value != null && _interfaceManager.Menus.TryGetValue(WindowDescriptor.Menu.Value, out var menu)) {
if (_interfaceManager.Menus.TryGetValue(WindowDescriptor.Menu.Value, out var menu)) {
_menuContainer.AddChild(menu.MenuBar);
_menuContainer.Visible = true;
} else {
Expand Down
65 changes: 48 additions & 17 deletions OpenDreamClient/Interface/DMF/IDMFProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IDMFProperty {
public string AsJson();
public string AsJsonDM();
public string AsRaw();

/// winget() calls do not act the same as embedded winget calls, and the default behaviour is some whacky combination of AsEscaped() and AsRaw(). This proc handles that. Fucking BYOND.
public string AsSnowflake();
}
Expand All @@ -40,16 +40,14 @@ Value is formatted as a DM-escaped string with surrounding quotes.
Does not change the value's text representation in any way; assumes it's already formatted correctly for the purpose. This is similar to as arg but does no escaping and no quotes.
*/

public struct DMFPropertyString(string value) : IDMFProperty {
public string? Value = value;
public struct DMFPropertyString(string? value) : IDMFProperty {
public string Value = value ?? string.Empty;

public string AsArg() {
return Value != null ? "\""+AsEscaped()+"\"" : "\"\"";
return "\""+AsEscaped()+"\"";
}

public string AsEscaped() {
if(Value == null)
return "";
return Value
.Replace("\\", "\\\\")
.Replace("\"", "\\\"");
Expand All @@ -60,10 +58,7 @@ public string AsString() {
}

public string AsParams() {
if(Value == null)
return "";
else
return System.Web.HttpUtility.UrlEncode(Value);
return System.Web.HttpUtility.UrlEncode(Value);
}

public string AsJson() {
Expand All @@ -80,13 +75,13 @@ public string AsJsonDM() {
}

public string AsRaw() {
return Value ?? "";
return Value;
}

public string AsSnowflake() {
return AsRaw();
}

public override string ToString() {
return AsRaw();
}
Expand Down Expand Up @@ -140,7 +135,7 @@ public string AsRaw() {
public string AsSnowflake() {
return AsRaw();
}

public override string ToString() {
return AsRaw();
}
Expand All @@ -151,7 +146,7 @@ public bool Equals(string comparison) {
}
}

public struct DMFPropertyVec2 : IDMFProperty {
public struct DMFPropertyVec2 : IDMFProperty, IEquatable<DMFPropertyVec2> {
public int X;
public int Y;
public char Delim = ',';
Expand Down Expand Up @@ -226,9 +221,21 @@ public bool Equals(string comparison) {
DMFPropertyVec2 comparisonVec = new(comparison);
return comparisonVec.X == X && comparisonVec.Y == Y;
}

public bool Equals(DMFPropertyVec2 other) {
return X == other.X && Y == other.Y && Delim == other.Delim;
}

public override bool Equals(object? obj) {
return obj is DMFPropertyVec2 other && Equals(other);
}

public override int GetHashCode() {
return HashCode.Combine(X, Y, Delim);
}
}

public struct DMFPropertySize : IDMFProperty {
public struct DMFPropertySize : IDMFProperty, IEquatable<DMFPropertySize> {
private DMFPropertyVec2 _value;
public int X {get => _value.X; set => _value.X = value;}
public int Y {get => _value.Y; set => _value.Y = value;}
Expand Down Expand Up @@ -295,11 +302,23 @@ public bool Equals(string comparison) {
return _value.Equals(comparison);
}

public bool Equals(DMFPropertySize other) {
return _value.Equals(other._value);
}

public override bool Equals(object? obj) {
return obj is DMFPropertySize other && Equals(other);
}

public override int GetHashCode() {
return _value.GetHashCode();
}

public static bool operator ==(DMFPropertySize a, DMFPropertySize b) => a.Vector == b.Vector;
public static bool operator !=(DMFPropertySize a, DMFPropertySize b) => a.Vector != b.Vector;
}

public struct DMFPropertyPos : IDMFProperty {
public struct DMFPropertyPos : IDMFProperty, IEquatable<DMFPropertyPos> {
private DMFPropertyVec2 _value;
public int X => _value.X;
public int Y => _value.Y;
Expand Down Expand Up @@ -366,6 +385,18 @@ public bool Equals(string comparison) {
return _value.Equals(comparison);
}

public bool Equals(DMFPropertyPos other) {
return _value.Equals(other._value);
}

public override bool Equals(object? obj) {
return obj is DMFPropertyPos other && Equals(other);
}

public override int GetHashCode() {
return _value.GetHashCode();
}

public static bool operator ==(DMFPropertyPos a, DMFPropertyPos b) => a.Vector == b.Vector;
public static bool operator !=(DMFPropertyPos a, DMFPropertyPos b) => a.Vector != b.Vector;
}
Expand Down Expand Up @@ -479,7 +510,7 @@ public string AsRaw() {
public string AsSnowflake() {
return Value ? "true" : "false";
}

public override string ToString() {
return AsRaw();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DMFPropertyString Id {
init => _id = value;
}

public DMFPropertyString Name => new(_name.Value ?? Id.AsRaw());
public DMFPropertyString Name => new(_name.Value);

public DMFPropertyString Type {
get => _type;
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamClient/Interface/DreamInterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public void RunCommand(string fullCommand) {
}

default: {
string[] argsRaw = fullCommand.Split(' ', 2, StringSplitOptions.TrimEntries);
string[] argsRaw = fullCommand!.Split(' ', 2, StringSplitOptions.TrimEntries);
string command = argsRaw[0].ToLowerInvariant(); // Case-insensitive

if (!_entitySystemManager.TryGetEntitySystem(out ClientVerbSystem? verbSystem))
Expand Down
Loading

0 comments on commit c63f765

Please sign in to comment.