Compare commits

..
11 Commits
Author SHA1 Message Date
ds5678 4bcc823371 Bump version 2024-04-20 15:55:53 -07:00
ds5678 7630da6da0 Improve parsing of version zero 2024-04-20 15:55:14 -07:00
ds5678 e5802e39ee Parse with only the major version 2024-04-20 15:44:19 -07:00
ds5678 c663c68124 Bump version 2024-04-15 19:51:42 -07:00
dependabot[bot] fa2458cfae Bump Microsoft.NET.Test.Sdk from 17.8.0 to 17.9.0
Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.8.0 to 17.9.0.
- [Release notes](https://github.com/microsoft/vstest/releases)
- [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md)
- [Commits](https://github.com/microsoft/vstest/compare/v17.8.0...v17.9.0)

---
updated-dependencies:
- dependency-name: Microsoft.NET.Test.Sdk
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
2024-04-15 08:04:23 -07:00
dependabot[bot] ee923d8e9b Bump coverlet.collector from 6.0.0 to 6.0.2
Bumps [coverlet.collector](https://github.com/coverlet-coverage/coverlet) from 6.0.0 to 6.0.2.
- [Release notes](https://github.com/coverlet-coverage/coverlet/releases)
- [Commits](https://github.com/coverlet-coverage/coverlet/compare/v6.0.0...v6.0.2)

---
updated-dependencies:
- dependency-name: coverlet.collector
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
2024-04-15 08:04:14 -07:00
dependabot[bot] 8b23a0c671 Bump NUnit.Analyzers from 3.10.0 to 4.1.0
Bumps [NUnit.Analyzers](https://github.com/nunit/nunit.analyzers) from 3.10.0 to 4.1.0.
- [Release notes](https://github.com/nunit/nunit.analyzers/releases)
- [Changelog](https://github.com/nunit/nunit.analyzers/blob/master/CHANGES.txt)
- [Commits](https://github.com/nunit/nunit.analyzers/compare/3.10.0...4.1.0)

---
updated-dependencies:
- dependency-name: NUnit.Analyzers
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
2024-04-15 08:04:04 -07:00
ds5678 f56feaf217 Remove incorrect code for Unity 6 2024-04-14 22:20:30 -07:00
ds5678 bd575f0063 Bump Version
* Expand support for UnityGuid and Utf8String to .NET Standard
2024-01-15 00:19:28 -05:00
dependabot[bot] 0a6b003bf2 Bump BenchmarkDotNet from 0.13.11 to 0.13.12
Bumps [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) from 0.13.11 to 0.13.12.
- [Release notes](https://github.com/dotnet/BenchmarkDotNet/releases)
- [Commits](https://github.com/dotnet/BenchmarkDotNet/compare/v0.13.11...v0.13.12)

---
updated-dependencies:
- dependency-name: BenchmarkDotNet
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
2024-01-09 09:38:48 -05:00
dependabot[bot] f4026adfd0 Bump PolySharp from 1.14.0 to 1.14.1
Bumps [PolySharp](https://github.com/Sergio0694/PolySharp) from 1.14.0 to 1.14.1.
- [Release notes](https://github.com/Sergio0694/PolySharp/releases)
- [Commits](https://github.com/Sergio0694/PolySharp/compare/1.14.0...1.14.1)

---
updated-dependencies:
- dependency-name: PolySharp
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
2023-12-18 15:14:12 -05:00
13 changed files with 139 additions and 87 deletions

No files matched your search

@@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>
</Project>
@@ -9,14 +9,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
</ItemGroup>
<ItemGroup>
@@ -72,20 +72,4 @@ public class ComparisonTests
Assert.That(new UnityVersion(2019, 1, 0, UnityVersionType.Final, 1).LessThanOrEquals(2019, 1, 0));
});
}
[TestCase(2017)]
[TestCase(2018)]
[TestCase(2019)]
[TestCase(2020)]
[TestCase(2021)]
[TestCase(2022)]
[TestCase(2023)]
public void YearVersioningIsBetweenFiveAndSix(int year)
{
Assert.Multiple(() =>
{
Assert.That(new UnityVersion((ushort)year).GreaterThan(5));
Assert.That(new UnityVersion((ushort)year).LessThan(6));
});
}
}
+6 -1
View File
@@ -9,8 +9,13 @@ public class ParsingTests
[TestCase("4.2.2f1\n1", 4, 2, 2, UnityVersionType.Final, 1, "\n1", "Custom Unity 4 Version")]
[TestCase("2343.4.5f7", 2343, 4, 5, UnityVersionType.Final, 7, null, "Fictitious Future Version")]
[TestCase("2019.2.2f1c2\n2", 2019, 2, 2, UnityVersionType.China, 2, "\n2", "Fake Custom Chinese Version")]
[TestCase("2019", 2019, 0, 0, UnityVersionType.Final, 1, null, "Major Only")]
[TestCase("2019.4", 2019, 4, 0, UnityVersionType.Final, 1, null, "Major Minor Only")]
[TestCase("2019.4.3", 2019, 4, 3, UnityVersionType.Final, 1, null, "Major Minor Build Only")]
[TestCase("0", 0, 0, 0, UnityVersionType.Alpha, 0, null, "Zero, Major Only")]
[TestCase("0.0", 0, 0, 0, UnityVersionType.Alpha, 0, null, "Zero, Major Minor Only")]
[TestCase("0.0.0", 0, 0, 0, UnityVersionType.Alpha, 0, null, "Zero, Major Minor Build Only")]
[TestCase("0.0.0b2", 0, 0, 0, UnityVersionType.Beta, 2, null, "Zero, Full")]
[TestCase("2019.2.2f1-letters", 2019, 2, 2, UnityVersionType.Final, 1, "-letters", "Issue #40 - Appended Custom Characters")]
public void UnityVersionParsesCorrectly(string versionString, int major, int minor, int build, UnityVersionType type, int typeNumber, string? customEngine, string name)
{
@@ -20,7 +25,7 @@ public class ParsingTests
{
Assert.That(parsedCustomEngine, Is.EqualTo(customEngine), $"The custom engine boolean is incorrect for '{name}'");
Assert.That(parsedVersion, Is.EqualTo(expected), $"The parsed version is incorrect for '{name}'");
if (customEngine is not null || typeNumber is not 1)
if (customEngine is not null || typeNumber is not 0 and not 1)
{
Assert.That(parsedVersion.ToString(UnityVersionFormatFlags.Default, parsedCustomEngine ?? ""), Is.EqualTo(versionString));
}
@@ -1,4 +1,4 @@
#if NET7_0_OR_GREATER
#if NET5_0_OR_GREATER
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -83,7 +83,7 @@ public class UnityGuidTests
{
Guid originalGuid = Guid.NewGuid();
UnityGuid tobyteArrayGuid = new UnityGuid(originalGuid.ToByteArray());
UnityGuid memoryMarshallGuid = new UnityGuid(MemoryMarshal.Cast<Guid, byte>(new ReadOnlySpan<Guid>(in originalGuid)));
UnityGuid memoryMarshallGuid = Unsafe.As<Guid, UnityGuid>(ref originalGuid);
Assert.That(memoryMarshallGuid, Is.EqualTo(tobyteArrayGuid));
}
}
@@ -1,4 +1,4 @@
#if NET7_0_OR_GREATER
#if NET5_0_OR_GREATER
namespace AssetRipper.Primitives.Tests;
public class Utf8StringTests
@@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<Authors>ds5678</Authors>
<Company>AssetRipper</Company>
<Version>2.1.0</Version>
<Version>3.1.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<PackageId>AssetRipper.Primitives</PackageId>
<PackageTags>C# assetripper unity unity3d</PackageTags>
@@ -30,10 +30,40 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PolySharp" Version="1.14.0">
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.HashCode">
<Version>1.1.1</Version>
</PackageReference>
<PackageReference Include="System.Memory">
<Version>4.5.5</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>8.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="System.Text.Json">
<Version>8.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Text.Json">
<Version>8.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="System.Text.Json">
<Version>8.0.1</Version>
</PackageReference>
</ItemGroup>
</Project>
+59 -10
View File
@@ -1,6 +1,8 @@
#if NET7_0_OR_GREATER
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
@@ -13,9 +15,13 @@ public readonly record struct UnityGuid
{
public UnityGuid(Guid guid)
{
#if NET5_0_OR_GREATER
Span<byte> guidData = stackalloc byte[16];
bool success = guid.TryWriteBytes(guidData);
Debug.Assert(success);
#else
Span<byte> guidData = guid.ToByteArray();
#endif
ConvertSystemOrUnityBytes(guidData, guidData);
Data0 = ReadUInt32LittleEndian(guidData, 0);
Data1 = ReadUInt32LittleEndian(guidData, 1);
@@ -44,10 +50,24 @@ public readonly record struct UnityGuid
//This is not an acceptable way to convert between Unity and System Guids.
//We only do it this way to efficiently get 16 random bytes.
//We don't care about official Guid validity because Unity does not care either.
Guid guid = Guid.NewGuid();
ReadOnlySpan<Guid> guidSpan = new ReadOnlySpan<Guid>(in guid);
ReadOnlySpan<byte> byteSpan = MemoryMarshal.AsBytes(guidSpan);
return new UnityGuid(byteSpan);
if (Unsafe.SizeOf<Guid>() == Unsafe.SizeOf<UnityGuid>())
{
Guid guid = Guid.NewGuid();
return Unsafe.As<Guid, UnityGuid>(ref guid);
}
else
{
return ThrowOrReturnDefault();
}
static UnityGuid ThrowOrReturnDefault()
{
#if DEBUG
throw new InvalidCastException($"{nameof(UnityGuid)} struct size does not match {nameof(Guid)}.");
#else
return default;
#endif
}
}
public static explicit operator UnityGuid(Guid systemGuid) => new UnityGuid(systemGuid);
@@ -57,7 +77,11 @@ public readonly record struct UnityGuid
Span<byte> span = stackalloc byte[16];
unityGuid.Write(span);
ConvertSystemOrUnityBytes(span, span);
#if NET5_0_OR_GREATER
return new Guid(span);
#else
return new Guid(span.ToArray());
#endif
}
private void Write(Span<byte> span)
@@ -79,7 +103,16 @@ public readonly record struct UnityGuid
{
Span<char> span = stackalloc char[32];
ToString(span);
return new string(span);
return CreateString(span);
static string CreateString(Span<char> span)
{
#if NET5_0_OR_GREATER
return new string(span);
#else
return new string(span.ToArray());
#endif
}
}
public void ToString(Span<char> buffer)
@@ -193,15 +226,31 @@ public readonly record struct UnityGuid
/// <returns>A stable guid corresponding to the <paramref name="input"/>.</returns>
public static UnityGuid Md5Hash(scoped ReadOnlySpan<byte> input)
{
byte[] hashBytes = MD5.HashData(input);
ConvertSystemOrUnityBytes(hashBytes, hashBytes);
return new UnityGuid(hashBytes);
Span<byte> buffer = stackalloc byte[16];
HashData(input, buffer);
ConvertSystemOrUnityBytes(buffer, buffer);
return new UnityGuid(buffer);
}
private static void HashData(ReadOnlySpan<byte> data, Span<byte> buffer)
{
#if NET5_0_OR_GREATER
MD5.HashData(data, buffer);
#else
MD5.Create().ComputeHash(data.ToArray()).AsSpan().CopyTo(buffer);
#endif
}
public static UnityGuid Md5Hash(scoped ReadOnlySpan<byte> assemblyName, scoped ReadOnlySpan<byte> @namespace, scoped ReadOnlySpan<byte> className)
{
int length = assemblyName.Length + @namespace.Length + className.Length;
Span<byte> input = length < 1024 ? stackalloc byte[length] : GC.AllocateUninitializedArray<byte>(length);
Span<byte> input = length < 1024
? stackalloc byte[length]
#if NET5_0_OR_GREATER
: GC.AllocateUninitializedArray<byte>(length);
#else
: new byte[length];
#endif
assemblyName.CopyTo(input);
@namespace.CopyTo(input.Slice(assemblyName.Length));
className.CopyTo(input.Slice(assemblyName.Length + @namespace.Length));
@@ -1,4 +1,4 @@
#if NET7_0_OR_GREATER
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
using System.Text.Json;
using System.Text.Json.Serialization;
@@ -27,47 +27,7 @@ public readonly partial struct UnityVersion : IEquatable<UnityVersion>, ICompara
/// </returns>
public int CompareTo(UnityVersion other)
{
//Unity 6 is sequentially equivalent to Unity 2024 because
//they switched to back to the old versioning scheme.
const ushort FirstNewVersion = 6;
if (this == other)
{
return 0;
}
if (UsesYearNumbering(this))
{
if (UsesYearNumbering(other))
{
return CompareByData(this, other);
}
else
{
return other.Major < FirstNewVersion ? 1 : -1;
}
}
else
{
if (UsesYearNumbering(other))
{
return Major < FirstNewVersion ? -1 : 1;
}
else
{
return CompareByData(this, other);
}
}
static int CompareByData(UnityVersion x, UnityVersion y)
{
return x.m_data < y.m_data ? -1 : 1;
}
static bool UsesYearNumbering(UnityVersion version)
{
return version.Major is >= 2017 and <= 2023;
}
return m_data.CompareTo(other.m_data);
}
/// <summary>
@@ -90,6 +50,6 @@ public readonly partial struct UnityVersion : IEquatable<UnityVersion>, ICompara
/// <inheritdoc/>
public override int GetHashCode()
{
return unchecked(827 + 911 * m_data.GetHashCode());
return m_data.GetHashCode();
}
}
@@ -32,7 +32,7 @@ public readonly partial struct UnityVersion
/// <returns></returns>
public static bool operator >(UnityVersion left, UnityVersion right)
{
return left.CompareTo(right) > 0;
return left.m_data > right.m_data;
}
/// <summary>
@@ -43,7 +43,7 @@ public readonly partial struct UnityVersion
/// <returns></returns>
public static bool operator >=(UnityVersion left, UnityVersion right)
{
return left.CompareTo(right) >= 0;
return left.m_data >= right.m_data;
}
/// <summary>
@@ -54,7 +54,7 @@ public readonly partial struct UnityVersion
/// <returns></returns>
public static bool operator <(UnityVersion left, UnityVersion right)
{
return left.CompareTo(right) < 0;
return left.m_data < right.m_data;
}
/// <summary>
@@ -65,6 +65,6 @@ public readonly partial struct UnityVersion
/// <returns></returns>
public static bool operator <=(UnityVersion left, UnityVersion right)
{
return left.CompareTo(right) <= 0;
return left.m_data <= right.m_data;
}
}
+16 -2
View File
@@ -5,6 +5,7 @@ namespace AssetRipper.Primitives;
public readonly partial struct UnityVersion
{
private static readonly Regex majorRegex = new Regex(@"^([0-9]+)$", RegexOptions.Compiled);
private static readonly Regex majorMinorRegex = new Regex(@"^([0-9]+)\.([0-9]+)$", RegexOptions.Compiled);
private static readonly Regex majorMinorBuildRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)$", RegexOptions.Compiled);
private static readonly Regex normalRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?([abcfpx])([0-9]+)((?:.|[\r\n])+)?$", RegexOptions.Compiled);
@@ -125,7 +126,9 @@ public readonly partial struct UnityVersion
int minor = int.Parse(match.Groups[2].Value);
int build = int.Parse(match.Groups[3].Value);
customEngine = null;
version = new UnityVersion((ushort)major, (ushort)minor, (ushort)build, UnityVersionType.Final, 1);
version = major is not 0 || minor is not 0 || build is not 0
? new UnityVersion((ushort)major, (ushort)minor, (ushort)build, UnityVersionType.Final, 1)
: default;
return true;
}
else if (majorMinorRegex.TryMatch(s, out match))
@@ -133,7 +136,18 @@ public readonly partial struct UnityVersion
int major = int.Parse(match.Groups[1].Value);
int minor = int.Parse(match.Groups[2].Value);
customEngine = null;
version = new UnityVersion((ushort)major, (ushort)minor, 0, UnityVersionType.Final, 1);
version = major is not 0 || minor is not 0
? new UnityVersion((ushort)major, (ushort)minor, 0, UnityVersionType.Final, 1)
: default;
return true;
}
else if (majorRegex.TryMatch(s, out match))
{
int major = int.Parse(match.Groups[1].Value);
customEngine = null;
version = major is not 0
? new UnityVersion((ushort)major, 0, 0, UnityVersionType.Final, 1)
: default;
return true;
}
else
+12 -2
View File
@@ -1,4 +1,4 @@
#if NET7_0_OR_GREATER
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
@@ -60,7 +60,14 @@ public sealed class Utf8String : IEquatable<Utf8String>
public override int GetHashCode()
{
HashCode hash = new();
#if NET6_0_OR_GREATER
hash.AddBytes(data);
#else
foreach (byte b in data)
{
hash.Add(b);
}
#endif
return hash.ToHashCode();
}
@@ -198,7 +205,10 @@ public sealed class Utf8String : IEquatable<Utf8String>
public static Utf8String Concat(params Utf8String?[] values)
{
ArgumentNullException.ThrowIfNull(values);
if (values is null)
{
throw new ArgumentNullException(nameof(values));
}
int totalLength = 0;
foreach (Utf8String? value in values)