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
19 changes: 19 additions & 0 deletions BinaryWizard.Tests/BinaryWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text;

namespace BinaryWizard.Tests;

public static class BinaryWriterExtensions {
public static void WriteNullTerminatedString(this BinaryWriter writer, string text, Encoding? encoding = null) {
encoding ??= Encoding.UTF8;

if (string.IsNullOrEmpty(text)) {
writer.Write("\0"u8);

return;
}

var textBytes = encoding.GetBytes(text);
writer.Write(textBytes);
writer.Write(encoding is UnicodeEncoding ? "\0\0"u8 : "\0"u8);
}
}
2 changes: 1 addition & 1 deletion BinaryWizard.Tests/Samples/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ namespace BinaryWizard.Tests.Samples;
[BinarySerializable]
public partial struct Entity {
public int Id;
// public string Name;
public string Name;
public Vector3 Position;
}
5 changes: 3 additions & 2 deletions BinaryWizard.Tests/SerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void Entity_CorrectlySerialized() {
using var stream = new MemoryStream();
using (var writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true)) {
writer.Write(69); // Id
// writer.Write("glomdom"); // Name
writer.WriteNullTerminatedString("glomdom"); // Name

// Position: Vector3
writer.Write(1); // X
Expand All @@ -88,7 +88,8 @@ public void Entity_CorrectlySerialized() {

var actual = Entity.FromBinary(reader);
var expected = new Entity {
Id = 69, /* Name = "glomdom", */
Id = 69,
Name = "glomdom",
Position = new Vector3 {
X = 1, Y = 2, Z = 3,
},
Expand Down
8 changes: 5 additions & 3 deletions BinaryWizard/Analysis/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal static string GetBinaryPrimitiveReader(this ITypeSymbol sym, Endianness
internal static int GetByteSize(this ITypeSymbol primitive) {
return primitive.SpecialType switch {
SpecialType.System_Boolean => 1,
SpecialType.System_Char => 1,
SpecialType.System_Char => 2,
SpecialType.System_SByte => 1,
SpecialType.System_Byte => 1,
SpecialType.System_Int16 => 2,
Expand All @@ -53,10 +53,12 @@ internal static int GetByteSize(this ITypeSymbol primitive) {
SpecialType.System_UInt32 => 4,
SpecialType.System_Int64 => 8,
SpecialType.System_UInt64 => 8,
SpecialType.System_Single => 4,
SpecialType.System_Double => 8,
SpecialType.System_Decimal => 16,
SpecialType.System_Double => 32,
SpecialType.System_String => -1,

_ => throw new InvalidOperationException("Unexpected case encountered."),
_ => throw new InvalidOperationException($"Unexpected case encountered for SpecialType: {primitive.SpecialType}"),
};
}

Expand Down
31 changes: 31 additions & 0 deletions BinaryWizard/Embedded/SerializeWithAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2026 glomdom
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;

namespace BinaryWizard;

[AttributeUsage(AttributeTargets.Field)]
public sealed class SerializeWithAttribute : Attribute {
/// <summary>
/// The type of the function to use when serializing the attached field.
/// </summary>
public Type Function { get; set; }

public SerializeWithAttribute(Type function) {
Function = function;
}
}
31 changes: 29 additions & 2 deletions BinaryWizard/Emission/CodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,22 @@ internal static void Generate(SourceProductionContext spc, ClassSerializationMet
switch (seg) {
case FixedSegment fixedSeg: ProcessFixedSegment(++segmentCount, fixedSeg, bodyBuilder, bodyIndent, meta); break;
case DynamicSegment dynSeg: ProcessDynamicSegment(dynSeg, bodyBuilder, bodyIndent, meta); break;
case NestedObjectSegment nestedSeg: bodyBuilder.AppendLine($"{bodyIndent}result.{nestedSeg.FieldName} = {nestedSeg.TypeName}.FromBinary(reader);"); break;
case NestedObjectSegment nestedSeg:
bodyBuilder.AppendLine($"{bodyIndent}result.{nestedSeg.FieldName} = {nestedSeg.TypeName}.FromBinary(reader);"); break;
case DynamicStringSegment stringSeg:
bodyBuilder.AppendLine($$"""
{{bodyIndent}}{
{{bodyIndent}} var stringBytes = new System.Collections.Generic.List<byte>();
{{bodyIndent}} while (true) {
{{bodyIndent}} byte b = reader.ReadByte();
{{bodyIndent}} if (b == 0) break;
{{bodyIndent}} stringBytes.Add(b);
{{bodyIndent}} }
{{bodyIndent}} result.{{stringSeg.Field.Name}} = System.Text.Encoding.UTF8.GetString(stringBytes.ToArray());
{{bodyIndent}}}
""");

break;
}
}

Expand Down Expand Up @@ -117,7 +132,19 @@ private static void ProcessFixedSegment(int segmentIndex, FixedSegment seg, Stri
continue;
}

sb.AppendLine($"{indent}result.{field.Name} = {field.TypeModel.Type.GetBinaryPrimitiveReader(meta.Endianness)}({bufName}.Slice({localOffset}, {field.ByteSize}));");
if (field.TypeModel.IsString) {
sb.AppendLine($$"""
{{indent}}{
{{indent}} var textSpan = {{bufName}}.Slice({{localOffset}}, {{field.ByteSize}});
{{indent}} var nullIdx = textSpan.IndexOf((byte)0);
{{indent}} result.{{field.Name}} = System.Text.Encoding.UTF8.GetString(nullIdx >= 0 ? textSpan.Slice(0, nullIdx) : textSpan);
{{indent}}}
""");
} else {
sb.AppendLine(
$"{indent}result.{field.Name} = {field.TypeModel.Type.GetBinaryPrimitiveReader(meta.Endianness)}({bufName}.Slice({localOffset}, {field.ByteSize}));"
);
}

if (field.HasMagic) {
var magicStr = field.Magic;
Expand Down
1 change: 1 addition & 0 deletions BinaryWizard/Models/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal sealed record TypeModel {
internal int? InnerTypeByteSize { get; set; }
internal bool IsFixedArray => FixedArraySize is not null && InnerType is not null;
internal bool IsDynamicArray => FixedArraySize is null && InnerType is not null;
internal bool IsString => Type.SpecialType == SpecialType.System_String;

internal TypeModel(ITypeSymbol type, int? fixedArraySize = null) {
Type = type;
Expand Down
28 changes: 28 additions & 0 deletions BinaryWizard/Segmenting/DynamicStringSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2026 glomdom
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Collections.Generic;
using BinaryWizard.Models;

namespace BinaryWizard.Segmenting;

internal record DynamicStringSegment : Segment {
internal FieldDef Field { get; set; }

internal DynamicStringSegment(FieldDef field) {
Field = field;
}
}
16 changes: 12 additions & 4 deletions BinaryWizard/Segmenting/SegmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ internal sealed class SegmentManager {

internal void AddField(FieldDef field, string? lengthRef = null) {
if (field.IsDynamic) {
if (string.IsNullOrEmpty(lengthRef)) throw new ArgumentNullException(nameof(lengthRef), "Length reference was not provided when field is dynamic");

CommitFixed();

_segments.Add(new DynamicSegment([field], lengthRef!));
if (field.TypeModel.IsString) {
_segments.Add(new DynamicStringSegment(field));

Debug.WriteLine($"Added dynamic string segment for {field.Name}");
} else {
if (string.IsNullOrEmpty(lengthRef)) {
throw new ArgumentNullException(nameof(lengthRef), $"Length reference was not provided for dynamic array field '{field.Name}'");
}

_segments.Add(new DynamicSegment([field], lengthRef!));

Debug.WriteLine($"Added dynamic segment for {field.Name} (dep. {lengthRef})");
Debug.WriteLine($"Added dynamic segment for {field.Name} (dep. {lengthRef})");
}
} else {
_currentFields.Add(field);

Expand Down