Skip to content
Open
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
16 changes: 0 additions & 16 deletions DMCompiler/Bytecode/StringFormatEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,10 @@ public static bool Decode(char c, [NotNullWhen(true)] out FormatSuffix? suffix)
return true;
}

public static bool Decode(char c) {
ushort bytes = c;
return (bytes & FormatPrefix) == FormatPrefix; // Could also check that the lower byte is a valid enum but... ehhhhh
}

/// <returns>true if argument is a marker for an interpolated value, one of them [] things. false if not.</returns>
public static bool IsInterpolation(FormatSuffix suffix) {
//This logic requires that all the interpolated-value enums keep separated from the others.
//I'd write some type-engine code to catch a discrepancy in that but alas, this language is just not OOPy enough.
return suffix <= FormatSuffix.ReferenceOfValue;
}

/// <returns>A new version of the string, with all formatting characters removed.</returns>
public static string RemoveFormatting(string input) {
StringBuilder ret = new StringBuilder(input.Length); // Trying to keep it to one malloc here
foreach(char c in input) {
if(!Decode(c))
ret.Append(c);
}

return ret.ToString();
}
}
6 changes: 3 additions & 3 deletions OpenDreamClient/Input/ContextMenu/ContextMenuPopup.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void RepopulateEntities(ScalingViewport viewport, Vector2 relativePos, Sc
continue;

var reference = new ClientObjectReference(_entityManager.GetNetEntity(uid));
var name = _appearanceSystem.GetName(reference);
var name = _appearanceSystem.GetNameUnformatted(reference);
ContextMenu.AddChild(new ContextMenuItem(this, reference, name, sprite.Icon));
}

Expand All @@ -74,15 +74,15 @@ public void RepopulateEntities(ScalingViewport viewport, Vector2 relativePos, Sc
if (_spriteQuery.TryGetComponent(uid, out var sprite) &&
sprite.Icon.Appearance?.MouseOpacity != MouseOpacity.Transparent) {
var reference = new ClientObjectReference(_entityManager.GetNetEntity(uid));
var name = _appearanceSystem.GetName(reference);
var name = _appearanceSystem.GetNameUnformatted(reference);
ContextMenu.AddChild(new ContextMenuItem(this, reference, name, sprite.Icon));
}
}

// Append the turf to the end of the context menu
var turfUnderMouse = _mouseInputSystem.GetTurfUnderMouse(mapCoords, out var turfId)?.Atom;
if (turfUnderMouse is not null && turfId is not null) {
var name = _appearanceSystem.GetName(turfUnderMouse.Value);
var name = _appearanceSystem.GetNameUnformatted(turfUnderMouse.Value);
var icon = _appearanceSystem.GetTurfIcon(turfId.Value);

ContextMenu.AddChild(new ContextMenuItem(this, turfUnderMouse.Value, name, icon));
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamClient/Interface/Controls/ControlMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public override bool TryGetProperty(string property, [NotNullWhen(true)] out IDM
private void UpdateAtomUnderMouse(ClientObjectReference? atom, Vector2 relativePos, Vector2i iconPos) {
if (!_atomUnderMouse.Equals(atom)) {
_entitySystemManager.Resolve(ref _appearanceSystem);
var name = (atom != null) ? _appearanceSystem.GetName(atom.Value) : string.Empty;
var name = (atom != null) ? _appearanceSystem.GetNameUnformatted(atom.Value) : string.Empty;
Window?.SetStatus(name);

if (_atomUnderMouse != null)
Expand Down
4 changes: 2 additions & 2 deletions OpenDreamClient/Rendering/ClientAppearanceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public bool TryGetAppearance(ClientObjectReference reference, [NotNullWhen(true)
return false;
}

public string GetName(ClientObjectReference reference) {
public string GetNameUnformatted(ClientObjectReference reference) {
switch (reference.Type) {
case ClientObjectReference.RefType.Client:
return _playerManager.LocalSession?.Name ?? "<unknown>";
Expand All @@ -359,7 +359,7 @@ public string GetName(ClientObjectReference reference) {
if (!TryGetAppearance(reference, out var appearance))
break;

return appearance.Name;
return StringFormatDecoder.RemoveFormatting(appearance.Name);
}

return "<unknown>";
Expand Down
3 changes: 2 additions & 1 deletion OpenDreamClient/Rendering/MapTextRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.Contracts;
using System.Text;
using OpenDreamClient.Interface.Html;
using OpenDreamShared.Dream;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.RichText;
Expand All @@ -26,7 +27,7 @@ public void RenderToTarget(DrawingHandleWorld handle, IRenderTexture texture, st
handle.SetTransform(DreamViewOverlay.CreateRenderTargetFlipMatrix(texture.Size, Vector2.Zero));

var message = new FormattedMessage();
HtmlParser.Parse(maptext, message);
HtmlParser.Parse(StringFormatDecoder.RemoveFormatting(maptext), message);

var (height, lineBreaks) = ProcessWordWrap(message, texture.Size.X);
var lineHeight = _defaultFont.GetLineHeight(Scale);
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/DreamConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void OutputDreamValue(DreamValue value) {

// Prune any remaining formatting
var message = value.Stringify();
message = StringFormatEncoder.RemoveFormatting(message);
message = StringFormatDecoder.RemoveFormatting(message);

OutputControl(message, null);
}
Expand Down
5 changes: 3 additions & 2 deletions OpenDreamRuntime/Objects/DreamObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OpenDreamRuntime.Objects.Types;
using OpenDreamRuntime.Rendering;
using OpenDreamRuntime.Resources;
using OpenDreamShared.Dream;
using Robust.Server.GameObjects;
using Robust.Server.GameStates;
using Robust.Shared.Map;
Expand Down Expand Up @@ -371,7 +372,7 @@ public virtual string GetDisplayName(StringFormatEncoder.FormatSuffix? suffix =

var name = GetRawName();
bool isProper = StringIsProper(name);
name = StringFormatEncoder.RemoveFormatting(name); // TODO: Care about other formatting macros for obj names beyond \proper & \improper
name = StringFormatDecoder.RemoveFormatting(name); // TODO: Care about other formatting macros for obj names beyond \proper & \improper
if(!isProper) {
return name;
}
Expand All @@ -390,7 +391,7 @@ public virtual string GetDisplayName(StringFormatEncoder.FormatSuffix? suffix =
/// Similar to <see cref="GetDisplayName"/> except it just returns the name as plaintext, with formatting removed. No article or anything.
/// </summary>
public string GetNameUnformatted() {
return StringFormatEncoder.RemoveFormatting(GetRawName());
return StringFormatDecoder.RemoveFormatting(GetRawName());
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/Procs/DMOpcodeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public static ProcStatus FormatString(DMProcState state) {
if (interps[nextInterpIndex].TryGetValueAsDreamObject<DreamObject>(out var dreamObject)) {
formattedString.Append(dreamObject.GetNameUnformatted());
} else if (interps[nextInterpIndex].TryGetValueAsString(out var interpStr)) {
formattedString.Append(StringFormatEncoder.RemoveFormatting(interpStr));
formattedString.Append(StringFormatDecoder.RemoveFormatting(interpStr));
}

// NOTE probably should put this above the TryGetAsDreamObject function and continue if formatting has occured
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ private static void JsonEncode(Utf8JsonWriter writer, DreamValue value) {
writer.WriteEndObject();
}
} else if (value.TryGetValueAsString(out var text))
writer.WriteStringValue(text);
writer.WriteStringValue(StringFormatDecoder.RemoveFormatting(text));
else if (value.TryGetValueAsType(out var type))
writer.WriteStringValue(type.Path);
else if (value.TryGetValueAsProc(out var proc))
Expand Down
4 changes: 2 additions & 2 deletions OpenDreamRuntime/Resources/ConsoleOutputResource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DMCompiler.Bytecode;
using OpenDreamRuntime.Procs.DebugAdapter;
using OpenDreamShared.Dream;

namespace OpenDreamRuntime.Resources;

Expand All @@ -22,7 +22,7 @@ public void WriteConsole(LogLevel logLevel, string sawmill, string message) {
public override void Output(DreamValue value) {
// Prune any remaining formatting
var message = value.Stringify();
message = StringFormatEncoder.RemoveFormatting(message);
message = StringFormatDecoder.RemoveFormatting(message);

WriteConsole(LogLevel.Info, "world.log", message);
}
Expand Down
5 changes: 3 additions & 2 deletions OpenDreamRuntime/Resources/DreamResource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.IO;
using DMCompiler.Bytecode;
// ReSharper disable once RedundantUsingDirective
using System.Runtime.CompilerServices;
using System.Text;
using OpenDreamShared.Dream;

namespace OpenDreamRuntime.Resources;

Expand Down Expand Up @@ -80,7 +81,7 @@ public virtual void Output(DreamValue value) {
}

// Prune any remaining formatting
text = StringFormatEncoder.RemoveFormatting(text);
text = StringFormatDecoder.RemoveFormatting(text);

CreateDirectory();
File.AppendAllText(ResourcePath, text + "\r\n");
Expand Down
25 changes: 25 additions & 0 deletions OpenDreamShared/Dream/StringFormatDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Text;

namespace OpenDreamShared.Dream;

public static class StringFormatDecoder {
/// <summary>
/// Refer to DMCompiler.Bytecode.StringFormatEncoder for documentation (can't cref cross-project)
/// </summary>
private static readonly ushort FormatPrefix = 0xFF00;

private static readonly StringBuilder UnformattedStringBuilder = new();

/// <returns>A new version of the string, with all formatting characters removed.</returns>
public static string RemoveFormatting(string input) {
UnformattedStringBuilder.Clear();
UnformattedStringBuilder.EnsureCapacity(input.Length); // Trying to keep it to one malloc here
foreach(char c in input) {
ushort bytes = c;
if((bytes & FormatPrefix) != FormatPrefix)
UnformattedStringBuilder.Append(c);
}

return UnformattedStringBuilder.ToString();
}
}
Loading