mirror of
https://github.com/AssetRipper/AssetRipper.Primitives.git
synced 2026-11-13 04:26:30 -10:00
Compare commits
36
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa6517ba7a | ||
|
|
7091ff7f36 | ||
|
|
3377fa0bea | ||
|
|
d759b3db98 | ||
|
|
55b3294ff5 | ||
|
|
c3ff29e8d7 | ||
|
|
61f47faf04 | ||
|
|
57912c52f1 | ||
|
|
5ab5d744d6 | ||
|
|
cd7f841845 | ||
|
|
9a5f8bb707 | ||
|
|
089c71d944 | ||
|
|
919fa0d5ab | ||
|
|
df8c5e2879 | ||
|
|
13ece60907 | ||
|
|
3a46414f08 | ||
|
|
bc556b7c72 | ||
|
|
7147a5d5b1 | ||
|
|
776ab543fa | ||
|
|
ca701a43dc | ||
|
|
f478cd8015 | ||
|
|
8cd69e8946 | ||
|
|
3cba893ff2 | ||
|
|
9c4a7d8127 | ||
|
|
fd3590ad52 | ||
|
|
3ef5dae9d4 | ||
|
|
78ecfb7de4 | ||
|
|
5080b7eb29 | ||
|
|
2e8940973f | ||
|
|
64f50b46fb | ||
|
|
fbdda777ac | ||
|
|
2de06bfaf4 | ||
|
|
c7a8932863 | ||
|
|
4bcc823371 | ||
|
|
7630da6da0 | ||
|
|
e5802e39ee |
No files matched your search
@@ -7,55 +7,25 @@ on:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
net7:
|
||||
|
||||
build-and-test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
dotnet-version: ["9.0", "8.0", "7.0", "6.0"]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v4.0.0
|
||||
uses: actions/setup-dotnet@v5.3.0
|
||||
with:
|
||||
dotnet-version: 7.0.x
|
||||
dotnet-version: |
|
||||
6.0.x
|
||||
7.0.x
|
||||
8.0.x
|
||||
9.0.x
|
||||
- name: Restore dependencies
|
||||
run: dotnet restore
|
||||
- name: Build
|
||||
run: dotnet build --framework net7.0 --no-restore
|
||||
run: dotnet build --framework net${{ matrix.dotnet-version }} --no-restore
|
||||
- name: Test
|
||||
run: dotnet test --framework net7.0 --no-build --verbosity normal
|
||||
|
||||
net6:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup .NET
|
||||
uses: actions/[email protected]
|
||||
with:
|
||||
dotnet-version: 6.0.x
|
||||
- name: Restore dependencies
|
||||
run: dotnet restore
|
||||
- name: Build
|
||||
run: dotnet build --framework net6.0 --no-restore
|
||||
- name: Test
|
||||
run: dotnet test --framework net6.0 --no-build --verbosity normal
|
||||
|
||||
net5:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup .NET
|
||||
uses: actions/[email protected]
|
||||
with:
|
||||
dotnet-version: |
|
||||
5.0.x
|
||||
6.0.x
|
||||
- name: Restore dependencies
|
||||
run: dotnet restore
|
||||
- name: Build
|
||||
run: dotnet build --framework net5.0 --no-restore
|
||||
- name: Test
|
||||
run: dotnet test --framework net5.0 --no-build --verbosity normal
|
||||
run: dotnet test --framework net${{ matrix.dotnet-version }} --no-build --verbosity normal
|
||||
@@ -1,15 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<LangVersion>Latest</LangVersion>
|
||||
<TargetFrameworks>net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,11 +0,0 @@
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
namespace AssetRipper.Primitives.Benckmarks;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
BenchmarkRunner.Run(typeof(Program).Assembly);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace AssetRipper.Primitives.Benckmarks;
|
||||
|
||||
[SimpleJob(RuntimeMoniker.Net80)]
|
||||
[RPlotExporter]
|
||||
public class YearVersionCheck
|
||||
{
|
||||
[Params(4, 5, 6, 7, 2016, 2017, 2018, 2022, 2023, 2024)]
|
||||
public ushort Major;
|
||||
|
||||
[Benchmark]
|
||||
public bool Simple() => Major is >= 2017 and <= 2023;
|
||||
|
||||
[Benchmark]
|
||||
public bool BitsWith2016() => BitComparison(Major);
|
||||
|
||||
[Benchmark]
|
||||
public bool BitsWithout2016() => BitComparison(Major) && Major is not 2016;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
private static bool BitComparison(uint value)
|
||||
{
|
||||
//This function determines if an unsigned integer
|
||||
//is between 2016 and 2023, inclusive. It does this
|
||||
//by comparing bits of the value with know constants.
|
||||
|
||||
const uint VariableBitMask = 0x7;
|
||||
const uint FixedBitMask = ~VariableBitMask;
|
||||
const uint ExpectedFixedBits = 0x7E0;//2016
|
||||
return (value & FixedBitMask) == ExpectedFixedBits;
|
||||
}
|
||||
|
||||
/* Results:
|
||||
* Due to the simplicity of the methods being profiled,
|
||||
* benchmarking was inconclusive. The costs of calling
|
||||
* these methods are almost neglible.
|
||||
*
|
||||
* Decision:
|
||||
* For the time being, the library will continue to use
|
||||
* the simple bounds check. If another option is shown
|
||||
* to be significantly better, I am open to switching.
|
||||
*/
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netframework3.5;netframework4.0;netframework4.8;net5.0;net6.0;net7.0</TargetFrameworks>
|
||||
<TargetFrameworks>net35;net40;net48;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
<LangVersion>Latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -68,6 +73,22 @@ public class ParsingTests
|
||||
});
|
||||
}
|
||||
|
||||
[TestCase("g2019")]
|
||||
[TestCase("g2019.4")]
|
||||
[TestCase("g2019.4.3")]
|
||||
[TestCase("g2019.4.3f")]
|
||||
[TestCase("g2019.4.3f1")]
|
||||
[TestCase("g2019.4.3f1c1")]
|
||||
[TestCase("2019g")]
|
||||
[TestCase("2019.4g")]
|
||||
[TestCase("2019.4.3g")]
|
||||
[TestCase("2019.4.3fg")]
|
||||
// "2019.4.3f1g" and "2019.4.3f1c1g" are technically valid because custom engines often append some text to the version string.
|
||||
public void InvalidVersionStringFailsToParse(string versionString)
|
||||
{
|
||||
Assert.That(UnityVersion.TryParse(versionString, out _, out _), Is.False);
|
||||
}
|
||||
|
||||
private static IEnumerable<UnityVersion> GenerateRandomVersions(int count)
|
||||
{
|
||||
Randomizer random = TestContext.CurrentContext.Random;
|
||||
|
||||
@@ -33,7 +33,7 @@ public class Utf8StringTests
|
||||
[Test]
|
||||
public void Utf8StringMadeFromUtf8LiteralIsTheSameAsMadeFromConstantString()
|
||||
{
|
||||
Utf8String dataString = "AssetRipperTestString"u8;
|
||||
Utf8String dataString = (Utf8String)"AssetRipperTestString"u8;
|
||||
Utf8String systemString = "AssetRipperTestString";
|
||||
Assert.That(dataString, Is.EqualTo(systemString));
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class Utf8StringTests
|
||||
[Test]
|
||||
public void ConcatenationBetweenTwoUtf8StringsWorks()
|
||||
{
|
||||
Utf8String dataString = "AssetRipperTestString"u8;
|
||||
Utf8String dataString = (Utf8String)"AssetRipperTestString"u8;
|
||||
Utf8String systemString = "AssetRipperTestString";
|
||||
Utf8String concatenated = dataString + systemString;
|
||||
Assert.That(concatenated, Is.EqualTo((Utf8String)"AssetRipperTestStringAssetRipperTestString"u8));
|
||||
@@ -58,10 +58,10 @@ public class Utf8StringTests
|
||||
[Test]
|
||||
public void ConcatenationBetweenFourUtf8StringsWorks()
|
||||
{
|
||||
Utf8String string1 = "hello"u8;
|
||||
Utf8String string2 = " world"u8;
|
||||
Utf8String string3 = "!"u8;
|
||||
Utf8String string4 = ", how are you?"u8;
|
||||
Utf8String string1 = (Utf8String)"hello"u8;
|
||||
Utf8String string2 = (Utf8String)" world"u8;
|
||||
Utf8String string3 = (Utf8String)"!"u8;
|
||||
Utf8String string4 = (Utf8String)", how are you?"u8;
|
||||
Utf8String concatenated = Utf8String.Concat(string1, string2, string3, string4);
|
||||
Assert.That(concatenated, Is.EqualTo((Utf8String)"hello world!, how are you?"u8));
|
||||
}
|
||||
@@ -81,7 +81,7 @@ public class Utf8StringTests
|
||||
[Test]
|
||||
public void IsNullOrEmptyReturnsFalseForNonEmptyString()
|
||||
{
|
||||
Assert.That(Utf8String.IsNullOrEmpty("AssetRipperTestString"u8), Is.False);
|
||||
Assert.That(Utf8String.IsNullOrEmpty((Utf8String)"AssetRipperTestString"u8), Is.False);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -24,8 +24,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssetRipper.Primitives", "A
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssetRipper.Primitives.Tests", "AssetRipper.Primitives.Tests\AssetRipper.Primitives.Tests.csproj", "{545E670D-141B-4F05-9AFD-2F184A59EADC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetRipper.Primitives.Benckmarks", "AssetRipper.Primitives.Benckmarks\AssetRipper.Primitives.Benckmarks.csproj", "{E3DEB571-4EE4-4C89-9005-DB10D9FB561B}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -40,10 +38,6 @@ Global
|
||||
{545E670D-141B-4F05-9AFD-2F184A59EADC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{545E670D-141B-4F05-9AFD-2F184A59EADC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{545E670D-141B-4F05-9AFD-2F184A59EADC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E3DEB571-4EE4-4C89-9005-DB10D9FB561B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E3DEB571-4EE4-4C89-9005-DB10D9FB561B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E3DEB571-4EE4-4C89-9005-DB10D9FB561B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E3DEB571-4EE4-4C89-9005-DB10D9FB561B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netframework3.5;netframework4.0;netstandard2.0;net5.0;net6.0;net7.0</TargetFrameworks>
|
||||
<TargetFrameworks>net35;net40;net461;netstandard2.0;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
<LangVersion>Latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<Authors>ds5678</Authors>
|
||||
<Company>AssetRipper</Company>
|
||||
<Version>3.0.0</Version>
|
||||
<Version>3.2.0</Version>
|
||||
<AssemblyVersion>$(Version)</AssemblyVersion>
|
||||
<PackageId>AssetRipper.Primitives</PackageId>
|
||||
<PackageTags>C# assetripper unity unity3d</PackageTags>
|
||||
@@ -14,17 +14,21 @@
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageProjectUrl>https://github.com/AssetRipper/AssetRipper.Primitives</PackageProjectUrl>
|
||||
<Copyright>Copyright (c) 2022-2023 ds5678</Copyright>
|
||||
<Copyright>Copyright (c) 2022-2025 ds5678</Copyright>
|
||||
<Description>Managed library for primitive types used in AssetRipper.</Description>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(TargetFramework)' != 'netframework3.5' and '$(TargetFramework)' != 'netframework4.0' ">
|
||||
|
||||
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
|
||||
<IsTrimmable>true</IsTrimmable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
|
||||
<IsAotCompatible>true</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
@@ -36,33 +40,18 @@
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net461'">
|
||||
<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>
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
|
||||
<PackageReference Include="System.ValueTuple">
|
||||
<Version>4.5.0</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
|
||||
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER
|
||||
using System.Buffers.Binary;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -6,12 +6,16 @@ using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetRipper.Primitives;
|
||||
|
||||
[JsonConverter(typeof(UnityGuidJsonConverter))]
|
||||
#if NET5_0_OR_GREATER
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(UnityGuidJsonConverter))]
|
||||
#endif
|
||||
public readonly record struct UnityGuid
|
||||
#if NET7_0_OR_GREATER
|
||||
: System.Numerics.IEqualityOperators<UnityGuid, UnityGuid, bool>
|
||||
#endif
|
||||
{
|
||||
public UnityGuid(Guid guid)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
|
||||
#if NET5_0_OR_GREATER
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
|
||||
@@ -2,10 +2,24 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace AssetRipper.Primitives;
|
||||
partial struct UnityVersion : IComparisonOperators<UnityVersion, UnityVersion, bool>, IEqualityOperators<UnityVersion, UnityVersion, bool>, IMinMaxValue<UnityVersion>
|
||||
partial struct UnityVersion :
|
||||
IComparisonOperators<UnityVersion, UnityVersion, bool>,
|
||||
IEqualityOperators<UnityVersion, UnityVersion, bool>,
|
||||
IMinMaxValue<UnityVersion>,
|
||||
IParsable<UnityVersion>
|
||||
{
|
||||
static UnityVersion IMinMaxValue<UnityVersion>.MaxValue => MaxVersion;
|
||||
|
||||
static UnityVersion IMinMaxValue<UnityVersion>.MinValue => MinVersion;
|
||||
|
||||
static UnityVersion IParsable<UnityVersion>.Parse(string s, IFormatProvider? provider)
|
||||
{
|
||||
return Parse(s);
|
||||
}
|
||||
|
||||
static bool IParsable<UnityVersion>.TryParse(string? s, IFormatProvider? provider, out UnityVersion result)
|
||||
{
|
||||
return TryParse(s, out result, out _);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,10 @@
|
||||
#if NET5_0_OR_GREATER
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetRipper.Primitives;
|
||||
|
||||
[JsonConverter(typeof(UnityVersionJsonConverter))]
|
||||
partial struct UnityVersion
|
||||
{
|
||||
}
|
||||
#endif
|
||||
@@ -2,14 +2,8 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AssetRipper.Primitives;
|
||||
|
||||
public readonly partial struct UnityVersion
|
||||
{
|
||||
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);
|
||||
private static readonly Regex chinaRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?f1c([0-9]+)((?:.|[\r\n])+)?$", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Serialize the version as a string using <see cref="UnityVersionFormatFlags.Default"/>.
|
||||
/// </summary>
|
||||
@@ -90,15 +84,15 @@ public readonly partial struct UnityVersion
|
||||
/// <param name="version">The parsed Unity version</param>
|
||||
/// <param name="customEngine">Not null if this version was generated by a custom Unity Engine.</param>
|
||||
/// <returns>True if parsing was successful</returns>
|
||||
public static bool TryParse(string s, out UnityVersion version, out string? customEngine)
|
||||
public static bool TryParse(string? s, out UnityVersion version, out string? customEngine)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s))
|
||||
if (s is null || s.Length is 0)
|
||||
{
|
||||
customEngine = null;
|
||||
version = default;
|
||||
return false;
|
||||
}
|
||||
else if (chinaRegex.TryMatch(s, out Match? match))
|
||||
else if (UnityVersionRegexes.GetChinaRegex().TryMatch(s, out Match? match))
|
||||
{
|
||||
int major = int.Parse(match.Groups[1].Value);
|
||||
int minor = int.Parse(match.Groups[2].Value);
|
||||
@@ -108,7 +102,7 @@ public readonly partial struct UnityVersion
|
||||
version = new UnityVersion((ushort)major, (ushort)minor, (ushort)build, UnityVersionType.China, (byte)typeNumber);
|
||||
return true;
|
||||
}
|
||||
else if(normalRegex.TryMatch(s, out match))
|
||||
else if(UnityVersionRegexes.GetNormalRegex().TryMatch(s, out match))
|
||||
{
|
||||
int major = int.Parse(match.Groups[1].Value);
|
||||
int minor = int.Parse(match.Groups[2].Value);
|
||||
@@ -119,21 +113,34 @@ public readonly partial struct UnityVersion
|
||||
version = new UnityVersion((ushort)major, (ushort)minor, (ushort)build, type.ToUnityVersionType(), (byte)typeNumber);
|
||||
return true;
|
||||
}
|
||||
else if (majorMinorBuildRegex.TryMatch(s, out match))
|
||||
else if (UnityVersionRegexes.GetMajorMinorBuildRegex().TryMatch(s, out match))
|
||||
{
|
||||
int major = int.Parse(match.Groups[1].Value);
|
||||
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))
|
||||
else if (UnityVersionRegexes.GetMajorMinorRegex().TryMatch(s, out match))
|
||||
{
|
||||
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 (UnityVersionRegexes.GetMajorRegex().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
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#if NET5_0_OR_GREATER
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetRipper.Primitives;
|
||||
|
||||
public sealed class UnityVersionJsonConverter : JsonConverter<UnityVersion>
|
||||
{
|
||||
public override UnityVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return UnityVersion.Parse(reader.GetString() ?? throw new JsonException("String was read as null"));
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, UnityVersion value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AssetRipper.Primitives;
|
||||
|
||||
internal static partial class UnityVersionRegexes
|
||||
{
|
||||
#if NET35
|
||||
private const RegexOptions Options = RegexOptions.None;
|
||||
#else
|
||||
private const RegexOptions Options = RegexOptions.Compiled;
|
||||
#endif
|
||||
|
||||
[StringSyntax("regex")]
|
||||
private const string Major = @"^([0-9]+)$";
|
||||
|
||||
[StringSyntax("regex")]
|
||||
private const string MajorMinor = @"^([0-9]+)\.([0-9]+)$";
|
||||
|
||||
[StringSyntax("regex")]
|
||||
private const string MajorMinorBuild = @"^([0-9]+)\.([0-9]+)\.([0-9]+)$";
|
||||
|
||||
[StringSyntax("regex")]
|
||||
private const string Normal = @"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?([abcfpx])([0-9]+)((?:.|[\r\n])+)?$";
|
||||
|
||||
[StringSyntax("regex")]
|
||||
private const string China = @"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?f1c([0-9]+)((?:.|[\r\n])+)?$";
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
[GeneratedRegex(Major, Options)]
|
||||
public static partial Regex GetMajorRegex();
|
||||
#else
|
||||
private static readonly Regex majorRegex = new Regex(Major, Options);
|
||||
public static Regex GetMajorRegex() => majorRegex;
|
||||
#endif
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
[GeneratedRegex(MajorMinor, Options)]
|
||||
public static partial Regex GetMajorMinorRegex();
|
||||
#else
|
||||
private static readonly Regex majorMinorRegex = new Regex(MajorMinor, Options);
|
||||
public static Regex GetMajorMinorRegex() => majorMinorRegex;
|
||||
#endif
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
[GeneratedRegex(MajorMinorBuild, Options)]
|
||||
public static partial Regex GetMajorMinorBuildRegex();
|
||||
#else
|
||||
private static readonly Regex majorMinorBuildRegex = new Regex(MajorMinorBuild, Options);
|
||||
public static Regex GetMajorMinorBuildRegex() => majorMinorBuildRegex;
|
||||
#endif
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
[GeneratedRegex(Normal, Options)]
|
||||
public static partial Regex GetNormalRegex();
|
||||
#else
|
||||
private static readonly Regex normalRegex = new Regex(Normal, Options);
|
||||
public static Regex GetNormalRegex() => normalRegex;
|
||||
#endif
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
[GeneratedRegex(China, Options)]
|
||||
public static partial Regex GetChinaRegex();
|
||||
#else
|
||||
private static readonly Regex chinaRegex = new Regex(China, Options);
|
||||
public static Regex GetChinaRegex() => chinaRegex;
|
||||
#endif
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
|
||||
#if NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
@@ -11,7 +11,15 @@ namespace AssetRipper.Primitives;
|
||||
/// <remarks>
|
||||
/// Conversions to and from <see cref="string"/> are handled by <see cref="Encoding.UTF8"/>.
|
||||
/// </remarks>
|
||||
public sealed class Utf8String : IEquatable<Utf8String>
|
||||
#if NET5_0_OR_GREATER
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(Utf8StringJsonConverter))]
|
||||
#endif
|
||||
public sealed class Utf8String :
|
||||
#if NET7_0_OR_GREATER
|
||||
System.Numerics.IEqualityOperators<Utf8String?, Utf8String?, bool>,
|
||||
System.Numerics.IEqualityOperators<Utf8String?, string?, bool>,
|
||||
#endif
|
||||
IEquatable<Utf8String>
|
||||
{
|
||||
private readonly byte[] data;
|
||||
private string? cachedString;
|
||||
@@ -23,7 +31,7 @@ public sealed class Utf8String : IEquatable<Utf8String>
|
||||
/// </summary>
|
||||
public Utf8String()
|
||||
{
|
||||
data = Array.Empty<byte>();
|
||||
data = [];
|
||||
cachedString = "";
|
||||
}
|
||||
|
||||
@@ -103,8 +111,10 @@ public sealed class Utf8String : IEquatable<Utf8String>
|
||||
return utf8String?.String;
|
||||
}
|
||||
|
||||
public static implicit operator Utf8String(ReadOnlySpan<byte> data)
|
||||
public static explicit operator Utf8String(ReadOnlySpan<byte> data)
|
||||
{
|
||||
// Explicit conversion because of first class spans
|
||||
// https://github.com/dotnet/roslyn/issues/78314
|
||||
return data.Length == 0 ? Empty : new Utf8String(data);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#if NET5_0_OR_GREATER
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetRipper.Primitives;
|
||||
|
||||
public sealed class Utf8StringJsonConverter : JsonConverter<Utf8String>
|
||||
{
|
||||
public override Utf8String Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return reader.GetString() ?? throw new JsonException("String was read as null");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Utf8String value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user