Skip to content

Commit

Permalink
Core: Code cleanup, diffable-cs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Jul 23, 2024
1 parent c720a52 commit a227bef
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Linq;
using Cpp2IL.Core.Extensions;
using Cpp2IL.Core.Model.Contexts;
using Cpp2IL.Core.Utils;

namespace Cpp2IL.Core.Tests;

public class AccessibilityUtilsTests
public class AccessibilityExtensionsTests
{
[Test]
public void AccessibilityTests()
Expand Down
7 changes: 3 additions & 4 deletions Cpp2IL.Core/Cpp2IlApi.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;
using AssetRipper.Primitives;
using Cpp2IL.Core.Exceptions;
using Cpp2IL.Core.Logging;
Expand All @@ -14,6 +11,8 @@
using LibCpp2IL;
using LibCpp2IL.Logging;

[assembly: InternalsVisibleTo("Cpp2IL.Core.Tests")]

namespace Cpp2IL.Core
{
public static class Cpp2IlApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using Cpp2IL.Core.Model.Contexts;
using LibCpp2IL.Metadata;

namespace Cpp2IL.Core.Utils;
namespace Cpp2IL.Core.Extensions;

public static class AccessibilityUtils
internal static class AccessibilityExtensions
{
public static bool IsAccessibleTo(this TypeAnalysisContext referenceType, TypeAnalysisContext referencingType)
{
Expand Down
69 changes: 69 additions & 0 deletions Cpp2IL.Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Cpp2IL.Core.Extensions;

internal static class EnumerableExtensions
{
public static IEnumerable<T> MaybeAppend<T>(this IEnumerable<T> enumerable, T? item) where T : struct
{
if (item is not null)
{
return enumerable.Append(item.Value);
}

return enumerable;
}

public static MemoryEnumerable<T> AsEnumerable<T>(this Memory<T> memory) => new(memory);

public static MemoryEnumerator<T> GetEnumerator<T>(this Memory<T> memory) => new(memory);

public class MemoryEnumerable<T> : IEnumerable<T>
{
private readonly Memory<T> _memory;

public MemoryEnumerable(Memory<T> memory)
{
_memory = memory;
}

public IEnumerator<T> GetEnumerator() => new MemoryEnumerator<T>(_memory);

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class MemoryEnumerator<T> : IEnumerator<T>
{
private readonly Memory<T> _memory;

private int _index = -1;

public MemoryEnumerator(Memory<T> memory)
{
_memory = memory;
}

public bool MoveNext()
{
_index++;
return _index < _memory.Length;
}

public void Reset()
{
_index = -1;
}

public T Current => _memory.Span[_index];

object? IEnumerator.Current => Current;

public void Dispose()
{
// Nothing to dispose
}
}
}
14 changes: 13 additions & 1 deletion Cpp2IL.Core/Extensions/MiscExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Cpp2IL.Core.ISIL;
Expand Down Expand Up @@ -167,5 +167,17 @@ public static unsafe uint ReadUInt(this Span<byte> span, int start)
fixed (byte* ptr = &span[start])
return *(uint*)ptr;
}

public static bool BitsAreEqual(this BitArray first, BitArray second)
{
if (first.Count != second.Count)
return false;

bool areDifferent = false;
for (int i = 0; i < first.Count && !areDifferent; i++)
areDifferent = first.Get(i) != second.Get(i);

return !areDifferent;
}
}
}
13 changes: 13 additions & 0 deletions Cpp2IL.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Cpp2IL.Core.Extensions;

public static class StringExtensions
{
public static string EscapeString(this string str)
=> str
.Replace("\\", @"\\")
.Replace("\"", "\\\"")
.Replace("\n", "\\n")
.Replace("\r", "\\r")
.Replace("\t", "\\t")
.Replace("\0", "\\0");
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override void ReadFromV29Blob(BinaryReader reader, ApplicationAnalysisCon
public override string ToString()
{
if(PrimitiveValue is string s)
return $"\"{s}\"";
return $"\"{s.EscapeString()}\"";

return PrimitiveValue?.ToString(CultureInfo.InvariantCulture) ?? "null";
}
Expand Down
4 changes: 2 additions & 2 deletions Cpp2IL.Core/OldGraphs/InstructionGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Cpp2IL.Core.Extensions;
using Cpp2IL.Core.Il2CppApiFunctions;
using Cpp2IL.Core.Utils;
using Iced.Intel;

namespace Cpp2IL.Core.Graphs;
Expand Down Expand Up @@ -509,4 +509,4 @@ public string Print(bool instructions = false)
public List<IControlFlowNode> INodes => Nodes.Cast<IControlFlowNode>().ToList();

public int Count => nodeSet.Count;
}
}
10 changes: 5 additions & 5 deletions Cpp2IL.Core/OutputFormats/DiffableCsOutputFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ private static void AppendField(StringBuilder sb, FieldAnalysisContext field, in
sb.Append("; //Field offset: 0x");
sb.Append(field.Offset.ToString("X"));

if ((field.Attributes & FieldAttributes.HasFieldRVA) != 0)
if ((field.Attributes & FieldAttributes.HasFieldRVA) != 0 && field.BackingData != null)
{
sb.Append(" || Has Field RVA: 0x");
var (dataIndex, _) = LibCpp2IlMain.TheMetadata!.GetFieldDefaultValue(field.BackingData!.Field.FieldIndex);
var pointer = LibCpp2IlMain.TheMetadata!.GetDefaultValueFromIndex(dataIndex);
sb.Append(pointer.ToString("X8"));
sb.Append(" || Has Field RVA (address hidden for diffability)");
// var (dataIndex, _) = LibCpp2IlMain.TheMetadata!.GetFieldDefaultValue(field.BackingData.Field.FieldIndex);
// var pointer = LibCpp2IlMain.TheMetadata!.GetDefaultValueFromIndex(dataIndex);
// sb.Append(pointer.ToString("X8"));

var actualValue = field.BackingData.Field.StaticArrayInitialValue;
if (actualValue is { Length: > 0 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using Cpp2IL.Core.Api;
using Cpp2IL.Core.Extensions;
using Cpp2IL.Core.ISIL;
using Cpp2IL.Core.Model.Contexts;
using Cpp2IL.Core.Utils;
Expand Down
19 changes: 0 additions & 19 deletions Cpp2IL.Core/Utils/EnumerableExtensions.cs

This file was deleted.

62 changes: 0 additions & 62 deletions Cpp2IL.Core/Utils/MiscUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,6 @@ public static ulong GetAddressOfNextFunctionStart(ulong current)
return ret;
}

public static bool BitsAreEqual(this BitArray first, BitArray second)
{
if (first.Count != second.Count)
return false;

bool areDifferent = false;
for (int i = 0; i < first.Count && !areDifferent; i++)
areDifferent = first.Get(i) != second.Get(i);

return !areDifferent;
}

public static void ExecuteSerial<T>(IEnumerable<T> enumerable, Action<T> what)
{
foreach (var item in enumerable)
Expand Down Expand Up @@ -317,55 +305,5 @@ public static string CleanPathElement(string input)

return InvalidPathElements.Contains(input) ? $"__invalidwin32name_{input}__" : input;
}

public static MemoryEnumerator<T> GetEnumerator<T>(this Memory<T> memory) => new(memory);

public static MemoryEnumerable<T> AsEnumerable<T>(this Memory<T> memory) => new(memory);

public class MemoryEnumerable<T> : IEnumerable<T>
{
private readonly Memory<T> _memory;

public MemoryEnumerable(Memory<T> memory)
{
_memory = memory;
}

public IEnumerator<T> GetEnumerator() => new MemoryEnumerator<T>(_memory);

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class MemoryEnumerator<T> : IEnumerator<T>
{
private readonly Memory<T> _memory;

private int _index = -1;

public MemoryEnumerator(Memory<T> memory)
{
_memory = memory;
}

public bool MoveNext()
{
_index++;
return _index < _memory.Length;
}

public void Reset()
{
_index = -1;
}

public T Current => _memory.Span[_index];

object? IEnumerator.Current => Current;

public void Dispose()
{
// Nothing to dispose
}
}
}
}

0 comments on commit a227bef

Please sign in to comment.