Files
RobustToolbox/Robust.Shared/Serialization/NetFormattedStringSerializer.cs
Pieter-Jan BriersandGitHub d161c3b3b8 Add FormattedString type (#6339)
This is basically a lightweight marker type saying "this string contains markup". Intended to avoid injection accidents if people don't realize they should escape stuff.
2025-12-15 19:36:16 +01:00

51 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using JetBrains.Annotations;
using NetSerializer;
using Robust.Shared.RichText;
namespace Robust.Shared.Serialization;
/// <summary>
/// Special network serializer for <see cref="FormattedString"/> to make sure validation runs for network values.
/// </summary>
internal sealed class NetFormattedStringSerializer : IStaticTypeSerializer
{
public bool Handles(Type type)
{
return type == typeof(FormattedString);
}
public IEnumerable<Type> GetSubtypes(Type type)
{
return [typeof(string)];
}
public MethodInfo GetStaticWriter(Type type)
{
return typeof(NetFormattedStringSerializer).GetMethod("Write", BindingFlags.Static | BindingFlags.NonPublic)!;
}
public MethodInfo GetStaticReader(Type type)
{
return typeof(NetFormattedStringSerializer).GetMethod("Read", BindingFlags.Static | BindingFlags.NonPublic)!;
}
[UsedImplicitly]
private static void Write(Stream stream, FormattedString value)
{
Primitives.WritePrimitive(stream, value.Markup);
}
[UsedImplicitly]
private static void Read(Stream stream, out FormattedString value)
{
Primitives.ReadPrimitive(stream, out string markup);
// Must be valid formed strict markup, do not trust the client!
value = FormattedString.FromMarkup(markup);
}
}