mirror of
https://github.com/AssetRipper/AssetRipper.Primitives.git
synced 2026-11-13 04:26:30 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d2c656ff24 | ||
|
|
d254482fb7 | ||
|
|
d1b5be899a | ||
|
|
ec5b531448 | ||
|
|
ae69a49b4d | ||
|
|
4c164435b2 | ||
|
|
4cf22f6b44 | ||
|
|
1f49a61abe | ||
|
|
2bd7bb0914 | ||
|
|
27bb084d72 | ||
|
|
a03114987b | ||
|
|
54b5d01872 | ||
|
|
27583c9e44 | ||
|
|
7906d263b0 | ||
|
|
4e70cc8c58 | ||
|
|
8f58ce7a23 | ||
|
|
a7061ae190 | ||
|
|
a78136fcd6 | ||
|
|
5cd8f940e8 | ||
|
|
a3b6f7fdae | ||
|
|
01a05f9494 | ||
|
|
9fe6f0f7df | ||
|
|
683cee8da8 | ||
|
|
eae2f9438b | ||
|
|
b8b8bb94b0 | ||
|
|
9b60458f1d | ||
|
|
b80421fb6c | ||
|
|
ade88858b4 | ||
|
|
79940dc376 | ||
|
|
dddb3f98e1 | ||
|
|
616b101747 | ||
|
|
dba487c1e2 | ||
|
|
c506979787 | ||
|
|
e712430913 | ||
|
|
c48b62e600 | ||
|
|
6caf19db67 | ||
|
|
07c4f74cb3 | ||
|
|
6467eaf99f |
No files matched your search
@@ -0,0 +1,18 @@
|
||||
# To get started with Dependabot version updates, you'll need to specify which
|
||||
# package ecosystems to update and where the package manifests are located.
|
||||
# Please see the documentation for all configuration options:
|
||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "nuget" # See documentation for possible values
|
||||
directory: "/" # Location of package manifests
|
||||
schedule:
|
||||
interval: "daily"
|
||||
|
||||
- package-ecosystem: "github-actions"
|
||||
# Workflow files stored in the
|
||||
# default location of `.github/workflows`
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
||||
@@ -0,0 +1,44 @@
|
||||
name: .NET
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
net6:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v2
|
||||
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@v3
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v2
|
||||
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
|
||||
@@ -0,0 +1,53 @@
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace AssetRipper.VersionUtilities.Tests
|
||||
{
|
||||
public class ComparisonTests
|
||||
{
|
||||
private static readonly UnityVersion lowValue = new UnityVersion(2017, 3, 0, UnityVersionType.Patch, 1);
|
||||
private static readonly UnityVersion mediumValue = new UnityVersion(2019, 4, 3, UnityVersionType.Final, 1);
|
||||
private static readonly UnityVersion highValue = new UnityVersion(2020, 2, 0, UnityVersionType.Final, 1);
|
||||
|
||||
[Test]
|
||||
public void LessThanIsTransitive()
|
||||
{
|
||||
Assert.IsTrue(lowValue < mediumValue);
|
||||
Assert.IsTrue(mediumValue < highValue);
|
||||
Assert.IsTrue(lowValue < highValue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GreaterThanIsTransitive()
|
||||
{
|
||||
Assert.IsTrue(mediumValue > lowValue);
|
||||
Assert.IsTrue(highValue > mediumValue);
|
||||
Assert.IsTrue(highValue > lowValue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WeakInequalityHolds()
|
||||
{
|
||||
UnityVersion low = lowValue;
|
||||
Assert.IsTrue(lowValue <= low);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MinimumValueDefinition()
|
||||
{
|
||||
Assert.IsTrue(UnityVersion.MinVersion == new UnityVersion(ushort.MinValue, ushort.MinValue, ushort.MinValue, UnityVersionType.Alpha, byte.MinValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MaximumValueDefinition()
|
||||
{
|
||||
Assert.IsTrue(UnityVersion.MaxVersion == new UnityVersion(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, (UnityVersionType)byte.MaxValue, byte.MaxValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnIncreaseOfTypeNumberMakesInequal()
|
||||
{
|
||||
Assert.IsTrue(UnityVersion.MinVersion < new UnityVersion(ushort.MinValue, ushort.MinValue, ushort.MinValue, UnityVersionType.Alpha, 1));
|
||||
Assert.IsTrue(lowValue < new UnityVersion(lowValue.Major, lowValue.Minor, lowValue.Build, lowValue.Type, (byte)(lowValue.TypeNumber + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace AssetRipper.VersionUtilities.Tests
|
||||
{
|
||||
public class DistanceTests
|
||||
{
|
||||
private static readonly UnityVersion version3 = new UnityVersion(3, 0, 0, UnityVersionType.Final, 1);
|
||||
private static readonly UnityVersion version4 = new UnityVersion(4, 4, 0, UnityVersionType.Alpha, 1);
|
||||
private static readonly UnityVersion version5 = new UnityVersion(5, 6, 1, UnityVersionType.Final, 11);
|
||||
private static readonly UnityVersion version6 = new UnityVersion(6, 0, 0, UnityVersionType.Final, 1);
|
||||
private static readonly UnityVersion version7 = new UnityVersion(7, 0, 0, UnityVersionType.Final, 1);
|
||||
private static readonly UnityVersion version8 = new UnityVersion(8, 0, 0, UnityVersionType.Final, 1);
|
||||
private static readonly UnityVersion version9 = new UnityVersion(9, 0, 0, UnityVersionType.Final, 1);
|
||||
private static readonly UnityVersion[] versionArray = new UnityVersion[] { version3, version4, version5, version6, version7, version8, version9 };
|
||||
|
||||
[Test]
|
||||
public void DistanceFunctionIsSymmetric()
|
||||
{
|
||||
ulong distance1 = UnityVersion.Distance(version4, version5);
|
||||
ulong distance2 = UnityVersion.Distance(version5, version4);
|
||||
Assert.IsTrue(distance1 == distance2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DistanceIsZeroForEquivalentVersions()
|
||||
{
|
||||
ulong distance = UnityVersion.Distance(version4, version4);
|
||||
Assert.IsTrue(distance == 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CorrectlyReturnsVersion6()
|
||||
{
|
||||
UnityVersion version = new UnityVersion(6, 1);
|
||||
UnityVersion closest = version.GetClosestVersion(versionArray);
|
||||
Assert.IsTrue(closest == version6);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CorrectlyReturnsVersion5()
|
||||
{
|
||||
UnityVersion version = new UnityVersion(5, 4, 9);
|
||||
UnityVersion closest = version.GetClosestVersion(versionArray);
|
||||
Assert.IsTrue(closest == version5);
|
||||
}
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -11,10 +11,10 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||
<PackageReference Include="NUnit" Version="3.13.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -19,6 +19,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Licenses", "Licenses", "{4B
|
||||
Licenses\uTinyRipper.md = Licenses\uTinyRipper.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{B63714DC-6258-4032-9930-DFF0667624BC}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -39,6 +44,7 @@ Global
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{4BE414B8-5B2D-40C1-BA3E-7DBBD6F7D854} = {36D291AB-E468-4CE7-8DB3-2C46639FE5AE}
|
||||
{B63714DC-6258-4032-9930-DFF0667624BC} = {36D291AB-E468-4CE7-8DB3-2C46639FE5AE}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {370218F4-B218-4C62-B7F7-D03B799F6349}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
internal static class CharacterExtensions
|
||||
{
|
||||
public static int ParseDigit(this char _this)
|
||||
{
|
||||
return _this - '0';
|
||||
}
|
||||
|
||||
public static UnityVersionType ToUnityVersionType(this char _this)
|
||||
{
|
||||
return _this switch
|
||||
{
|
||||
'a' => UnityVersionType.Alpha,
|
||||
'b' => UnityVersionType.Beta,
|
||||
'c' => UnityVersionType.China,
|
||||
'f' => UnityVersionType.Final,
|
||||
'p' => UnityVersionType.Patch,
|
||||
'x' => UnityVersionType.Experimental,
|
||||
_ => throw new Exception($"Unsupported version type {_this}"),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
public partial struct CompactUnityVersion24 : IEquatable<CompactUnityVersion24>, IComparable, IComparable<CompactUnityVersion24>
|
||||
{
|
||||
/// <summary>
|
||||
/// Compare to another object
|
||||
/// </summary>
|
||||
/// <param name="obj">Another object</param>
|
||||
/// <returns>
|
||||
/// 1 if this is larger or the other is not a Unity version<br/>
|
||||
/// -1 if this is smaller<br/>
|
||||
/// 0 if equal
|
||||
/// </returns>
|
||||
public int CompareTo(object? obj)
|
||||
{
|
||||
return obj is CompactUnityVersion24 version ? CompareTo(version) : 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compare two Unity versions
|
||||
/// </summary>
|
||||
/// <param name="other">Another Unity version</param>
|
||||
/// <returns>
|
||||
/// 1 if this is larger<br/>
|
||||
/// -1 if this is smaller<br/>
|
||||
/// 0 if equal
|
||||
/// </returns>
|
||||
public int CompareTo(CompactUnityVersion24 other)
|
||||
{
|
||||
if (this > other)
|
||||
return 1;
|
||||
else if (this < other)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check equality with another object
|
||||
/// </summary>
|
||||
/// <param name="obj">Another object</param>
|
||||
/// <returns>True if they're equal; false otherwise</returns>
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is CompactUnityVersion24 version && this == version;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check equality with another Unity version
|
||||
/// </summary>
|
||||
/// <param name="other">Another Unity version</param>
|
||||
/// <returns>True if they're equal; false otherwise</returns>
|
||||
public bool Equals(CompactUnityVersion24 other) => this == other;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return m_MajorMinorByte << 16 | m_BuildTypeShort;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
public partial struct CompactUnityVersion24
|
||||
{
|
||||
/// <summary>
|
||||
/// Expand to 64 bits
|
||||
/// </summary>
|
||||
/// <param name="version">The version to convert</param>
|
||||
public static implicit operator UnityVersion(CompactUnityVersion24 version)
|
||||
{
|
||||
return new UnityVersion(version.Major, version.Minor, version.Build, version.Type, version.TypeNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Expand to 32 bits
|
||||
/// </summary>
|
||||
/// <param name="version">The version to convert</param>
|
||||
public static implicit operator CompactUnityVersion32(CompactUnityVersion24 version)
|
||||
{
|
||||
return new CompactUnityVersion32(version.Major, version.Minor, version.Build, version.Type, version.TypeNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compact to 24 bits
|
||||
/// </summary>
|
||||
/// <param name="version"></param>
|
||||
public static explicit operator CompactUnityVersion24(UnityVersion version)
|
||||
{
|
||||
return new CompactUnityVersion24(version.Major, (byte)version.Minor, (byte)version.Build, version.Type, version.TypeNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compact to 24 bits
|
||||
/// </summary>
|
||||
/// <param name="version"></param>
|
||||
public static explicit operator CompactUnityVersion24(CompactUnityVersion32 version)
|
||||
{
|
||||
return new CompactUnityVersion24(version.Major, version.Minor, version.Build, version.Type, version.TypeNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Equality operator
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator ==(CompactUnityVersion24 left, CompactUnityVersion24 right)
|
||||
{
|
||||
return left.m_MajorMinorByte == right.m_MajorMinorByte && left.m_BuildTypeShort == right.m_BuildTypeShort;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inequality operator
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator !=(CompactUnityVersion24 left, CompactUnityVersion24 right)
|
||||
{
|
||||
return left.m_MajorMinorByte != right.m_MajorMinorByte || left.m_BuildTypeShort != right.m_BuildTypeShort;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Greater than
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator >(CompactUnityVersion24 left, CompactUnityVersion24 right)
|
||||
{
|
||||
return (left.m_MajorMinorByte > right.m_MajorMinorByte) ||
|
||||
(left.m_MajorMinorByte == right.m_MajorMinorByte && left.m_BuildTypeShort > right.m_BuildTypeShort);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Greater than or equal to
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator >=(CompactUnityVersion24 left, CompactUnityVersion24 right)
|
||||
{
|
||||
return (left.m_MajorMinorByte > right.m_MajorMinorByte) ||
|
||||
(left.m_MajorMinorByte == right.m_MajorMinorByte && left.m_BuildTypeShort >= right.m_BuildTypeShort);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Less than
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator <(CompactUnityVersion24 left, CompactUnityVersion24 right)
|
||||
{
|
||||
return (left.m_MajorMinorByte < right.m_MajorMinorByte) ||
|
||||
(left.m_MajorMinorByte == right.m_MajorMinorByte && left.m_BuildTypeShort < right.m_BuildTypeShort);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Less than or equal to
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator <=(CompactUnityVersion24 left, CompactUnityVersion24 right)
|
||||
{
|
||||
return (left.m_MajorMinorByte < right.m_MajorMinorByte) ||
|
||||
(left.m_MajorMinorByte == right.m_MajorMinorByte && left.m_BuildTypeShort <= right.m_BuildTypeShort);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// A compact value type for efficiently storing Unity versions inside 24 bits
|
||||
/// </summary>
|
||||
public partial struct CompactUnityVersion24
|
||||
{
|
||||
private const int majorOffset = 3;
|
||||
private const int buildOffset = 9;
|
||||
private const int typeOffset = 6;
|
||||
|
||||
private const uint bitMask3 = 0x0007U;
|
||||
private const uint bitMask5 = 0x001FU;
|
||||
private const uint bitMask6 = 0x003FU;
|
||||
private const uint bitMask7 = 0x007FU;
|
||||
|
||||
/// <summary>
|
||||
/// 5 bits for the major,
|
||||
/// 3 bits for the minor
|
||||
/// </summary>
|
||||
private readonly byte m_MajorMinorByte;
|
||||
|
||||
/// <summary>
|
||||
/// 7 bits for the build,
|
||||
/// 3 bits for the type,
|
||||
/// 6 bits for the type number
|
||||
/// </summary>
|
||||
private readonly ushort m_BuildTypeShort;
|
||||
|
||||
/// <summary>
|
||||
/// 2042
|
||||
/// </summary>
|
||||
public const ushort MajorMaxValue = (ushort)(31U + 2011U);
|
||||
|
||||
/// <summary>
|
||||
/// 5 bits
|
||||
/// </summary>
|
||||
private byte MajorRaw => unchecked((byte)((m_MajorMinorByte >> majorOffset) & bitMask5));
|
||||
|
||||
/// <summary>
|
||||
/// The first number in a Unity version string. 5 bits.
|
||||
/// Valid values inclusive: 0 - 5, 2017 - 2042
|
||||
/// </summary>
|
||||
public ushort Major => ConvertMajorRawToNormal(MajorRaw);
|
||||
|
||||
/// <summary>
|
||||
/// The second number in a Unity version string. 3 bits
|
||||
/// </summary>
|
||||
public byte Minor => unchecked((byte)(m_MajorMinorByte & bitMask3));
|
||||
|
||||
/// <summary>
|
||||
/// The third number in a Unity version string. 7 bits
|
||||
/// </summary>
|
||||
public byte Build => unchecked((byte)((m_BuildTypeShort >> buildOffset) & bitMask7));
|
||||
|
||||
/// <summary>
|
||||
/// The letter in a Unity version string. 3 bits
|
||||
/// </summary>
|
||||
public UnityVersionType Type => (UnityVersionType)unchecked((byte)((m_BuildTypeShort >> typeOffset) & bitMask3));
|
||||
|
||||
/// <summary>
|
||||
/// The last number in a Unity version string. 6 bits
|
||||
/// </summary>
|
||||
public byte TypeNumber => unchecked((byte)(m_BuildTypeShort & bitMask6));
|
||||
|
||||
/// <summary>
|
||||
/// The minimum value this type can have
|
||||
/// </summary>
|
||||
public static CompactUnityVersion24 MinVersion { get; } = new CompactUnityVersion24((byte)0, (ushort)0);
|
||||
|
||||
/// <summary>
|
||||
/// The maximum value this type can have
|
||||
/// </summary>
|
||||
public static CompactUnityVersion24 MaxVersion { get; } = new CompactUnityVersion24(byte.MaxValue, ushort.MaxValue);
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion24(ushort major)
|
||||
{
|
||||
m_MajorMinorByte = unchecked((byte)(ConvertMajorRawToNormal(major) << majorOffset));
|
||||
m_BuildTypeShort = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion24(ushort major, byte minor)
|
||||
{
|
||||
m_MajorMinorByte = unchecked((byte)((ConvertMajorRawToNormal(major) << majorOffset) | CastToThreeBits(minor)));
|
||||
m_BuildTypeShort = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion24(ushort major, byte minor, byte build)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
m_MajorMinorByte = (byte)((ConvertMajorRawToNormal(major) << majorOffset) | CastToThreeBits(minor));
|
||||
m_BuildTypeShort = (ushort)(CastToSevenBits(build) << buildOffset);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion24(ushort major, byte minor, byte build, UnityVersionType type)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
m_MajorMinorByte = (byte)((ConvertMajorRawToNormal(major) << majorOffset) | CastToThreeBits(minor));
|
||||
m_BuildTypeShort = (ushort)(
|
||||
(CastToSevenBits(build) << buildOffset) |
|
||||
(CastToThreeBits((byte)type) << typeOffset));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion24(ushort major, byte minor, byte build, UnityVersionType type, byte typeNumber)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
m_MajorMinorByte = (byte)((ConvertMajorRawToNormal(major) << majorOffset) | CastToThreeBits(minor));
|
||||
m_BuildTypeShort = (ushort)(
|
||||
(CastToSevenBits(build) << buildOffset) |
|
||||
(CastToThreeBits((byte)type) << typeOffset) |
|
||||
CastToSixBits(typeNumber));
|
||||
}
|
||||
}
|
||||
|
||||
private CompactUnityVersion24(byte majorMinorByte, ushort buildTypeShort)
|
||||
{
|
||||
m_MajorMinorByte = majorMinorByte;
|
||||
m_BuildTypeShort = buildTypeShort;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts this to its binary representation
|
||||
/// </summary>
|
||||
public void GetBits(out byte majorMinorByte, out ushort buildTypeShort)
|
||||
{
|
||||
majorMinorByte = m_MajorMinorByte;
|
||||
buildTypeShort = m_BuildTypeShort;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a binary representation into its respective version
|
||||
/// </summary>
|
||||
/// <param name="majorMinorByte"></param>
|
||||
/// <param name="buildTypeShort"></param>
|
||||
/// <returns>A new Unity version with the cooresponding bits</returns>
|
||||
public static CompactUnityVersion24 FromBits(byte majorMinorByte, ushort buildTypeShort) => new CompactUnityVersion24(majorMinorByte, buildTypeShort);
|
||||
|
||||
private static ushort ConvertMajorRawToNormal(byte raw)
|
||||
{
|
||||
return raw < 6 ? raw : unchecked((ushort)(raw + 2011U));
|
||||
}
|
||||
|
||||
private static byte ConvertMajorRawToNormal(ushort major)
|
||||
{
|
||||
if (major < 6)
|
||||
{
|
||||
return unchecked((byte)major);
|
||||
}
|
||||
else if (major >= 2017 && major <= MajorMaxValue)
|
||||
{
|
||||
return unchecked((byte)(major - 2011U));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(major));
|
||||
}
|
||||
}
|
||||
|
||||
private static byte CastToThreeBits(byte b)
|
||||
{
|
||||
return b <= bitMask3 ? b : throw new ArgumentOutOfRangeException(nameof(b));
|
||||
}
|
||||
|
||||
private static byte CastToSixBits(byte b)
|
||||
{
|
||||
return b <= bitMask6 ? b : throw new ArgumentOutOfRangeException(nameof(b));
|
||||
}
|
||||
|
||||
private static byte CastToSevenBits(byte b)
|
||||
{
|
||||
return b <= bitMask7 ? b : throw new ArgumentOutOfRangeException(nameof(b));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize the version as a string
|
||||
/// </summary>
|
||||
/// <returns>A new string like 2019.4.3f1</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Major}.{Minor}.{Build}{Type.ToCharacter()}{TypeNumber}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
public partial struct CompactUnityVersion32 : IEquatable<CompactUnityVersion32>, IComparable, IComparable<CompactUnityVersion32>
|
||||
{
|
||||
/// <summary>
|
||||
/// Compare to another object
|
||||
/// </summary>
|
||||
/// <param name="obj">Another object</param>
|
||||
/// <returns>
|
||||
/// 1 if this is larger or the other is not a Unity version<br/>
|
||||
/// -1 if this is smaller<br/>
|
||||
/// 0 if equal
|
||||
/// </returns>
|
||||
public int CompareTo(object? obj)
|
||||
{
|
||||
return obj is CompactUnityVersion32 version ? CompareTo(version) : 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compare two Unity versions
|
||||
/// </summary>
|
||||
/// <param name="other">Another Unity version</param>
|
||||
/// <returns>
|
||||
/// 1 if this is larger<br/>
|
||||
/// -1 if this is smaller<br/>
|
||||
/// 0 if equal
|
||||
/// </returns>
|
||||
public int CompareTo(CompactUnityVersion32 other)
|
||||
{
|
||||
if (this > other)
|
||||
return 1;
|
||||
else if (this < other)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check equality with another object
|
||||
/// </summary>
|
||||
/// <param name="obj">Another object</param>
|
||||
/// <returns>True if they're equal; false otherwise</returns>
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is CompactUnityVersion32 version && this == version;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check equality with another Unity version
|
||||
/// </summary>
|
||||
/// <param name="other">Another Unity version</param>
|
||||
/// <returns>True if they're equal; false otherwise</returns>
|
||||
public bool Equals(CompactUnityVersion32 other) => this == other;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return m_data.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
public partial struct CompactUnityVersion32
|
||||
{
|
||||
/// <summary>
|
||||
/// Expand to 64 bits
|
||||
/// </summary>
|
||||
/// <param name="version">The version to convert</param>
|
||||
public static implicit operator UnityVersion(CompactUnityVersion32 version)
|
||||
{
|
||||
return new UnityVersion(version.Major, version.Minor, version.Build, version.Type, version.TypeNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compact to 32 bits
|
||||
/// </summary>
|
||||
/// <param name="version"></param>
|
||||
public static explicit operator CompactUnityVersion32(UnityVersion version)
|
||||
{
|
||||
return new CompactUnityVersion32(version.Major, (byte)version.Minor, (byte)version.Build, version.Type, version.TypeNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Equality operator
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator ==(CompactUnityVersion32 left, CompactUnityVersion32 right)
|
||||
{
|
||||
return left.m_data == right.m_data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inequality operator
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator !=(CompactUnityVersion32 left, CompactUnityVersion32 right)
|
||||
{
|
||||
return left.m_data != right.m_data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Greater than
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator >(CompactUnityVersion32 left, CompactUnityVersion32 right)
|
||||
{
|
||||
return left.m_data > right.m_data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Greater than or equal to
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator >=(CompactUnityVersion32 left, CompactUnityVersion32 right)
|
||||
{
|
||||
return left.m_data >= right.m_data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Less than
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator <(CompactUnityVersion32 left, CompactUnityVersion32 right)
|
||||
{
|
||||
return left.m_data < right.m_data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Less than or equal to
|
||||
/// </summary>
|
||||
/// <param name="left">The left Unity version</param>
|
||||
/// <param name="right">The right Unity version</param>
|
||||
/// <returns></returns>
|
||||
public static bool operator <=(CompactUnityVersion32 left, CompactUnityVersion32 right)
|
||||
{
|
||||
return left.m_data <= right.m_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// A compact value type for efficiently storing Unity versions inside 32 bits
|
||||
/// </summary>
|
||||
public partial struct CompactUnityVersion32
|
||||
{
|
||||
private const int majorOffset = 24;
|
||||
private const int minorOffset = 20;
|
||||
private const int buildOffset = 12;
|
||||
private const int typeOffset = 8;
|
||||
|
||||
private const uint byteMask = 0x00FFU;
|
||||
private const uint bitMask4 = 0x000FU;
|
||||
|
||||
private readonly uint m_data;
|
||||
|
||||
/// <summary>
|
||||
/// 2266
|
||||
/// </summary>
|
||||
public const ushort MajorMaxValue = (ushort)(byte.MaxValue + 2011U);
|
||||
|
||||
/// <summary>
|
||||
/// 8 bits
|
||||
/// </summary>
|
||||
private byte MajorRaw => unchecked((byte)((m_data >> majorOffset) & byteMask));
|
||||
|
||||
/// <summary>
|
||||
/// The first number in a Unity version string. 8 bits.
|
||||
/// Valid values inclusive: 0 - 5, 2017 - 2266
|
||||
/// </summary>
|
||||
public ushort Major => ConvertMajorRawToNormal(MajorRaw);
|
||||
|
||||
/// <summary>
|
||||
/// The second number in a Unity version string. 4 bits
|
||||
/// </summary>
|
||||
public byte Minor => unchecked((byte)((m_data >> minorOffset) & bitMask4));
|
||||
|
||||
/// <summary>
|
||||
/// The third number in a Unity version string. 8 bits
|
||||
/// </summary>
|
||||
public byte Build => unchecked((byte)((m_data >> buildOffset) & byteMask));
|
||||
|
||||
/// <summary>
|
||||
/// The letter in a Unity version string. 4 bits
|
||||
/// </summary>
|
||||
public UnityVersionType Type => (UnityVersionType)unchecked((byte)((m_data >> typeOffset) & bitMask4));
|
||||
|
||||
/// <summary>
|
||||
/// The last number in a Unity version string. 8 bits
|
||||
/// </summary>
|
||||
public byte TypeNumber => unchecked((byte)(m_data & byteMask));
|
||||
|
||||
/// <summary>
|
||||
/// The minimum value this type can have
|
||||
/// </summary>
|
||||
public static CompactUnityVersion32 MinVersion { get; } = new CompactUnityVersion32(0U);
|
||||
|
||||
/// <summary>
|
||||
/// The maximum value this type can have
|
||||
/// </summary>
|
||||
public static CompactUnityVersion32 MaxVersion { get; } = new CompactUnityVersion32(uint.MaxValue);
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion32(ushort major)
|
||||
{
|
||||
m_data = (uint)ConvertMajorRawToNormal(major) << majorOffset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion32(ushort major, byte minor)
|
||||
{
|
||||
m_data = ((uint)ConvertMajorRawToNormal(major) << majorOffset) | ((uint)CastToFourBits(minor) << minorOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion32(ushort major, byte minor, byte build)
|
||||
{
|
||||
m_data = ((uint)ConvertMajorRawToNormal(major) << majorOffset) | ((uint)CastToFourBits(minor) << minorOffset) | ((uint)build << buildOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion32(ushort major, byte minor, byte build, UnityVersionType type)
|
||||
{
|
||||
m_data = ((uint)ConvertMajorRawToNormal(major) << majorOffset) | ((uint)CastToFourBits(minor) << minorOffset) | ((uint)build << buildOffset)
|
||||
| ((uint)CastToFourBits(type) << typeOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public CompactUnityVersion32(ushort major, byte minor, byte build, UnityVersionType type, byte typeNumber)
|
||||
{
|
||||
m_data = ((uint)ConvertMajorRawToNormal(major) << majorOffset) | ((uint)CastToFourBits(minor) << minorOffset) | ((uint)build << buildOffset)
|
||||
| ((uint)CastToFourBits(type) << typeOffset) | typeNumber;
|
||||
}
|
||||
|
||||
private CompactUnityVersion32(uint data)
|
||||
{
|
||||
m_data = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts this to its binary representation
|
||||
/// </summary>
|
||||
/// <returns>An unsigned integer having the same bits as this</returns>
|
||||
public uint GetBits() => m_data;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a binary representation into its respective version
|
||||
/// </summary>
|
||||
/// <param name="bits">An unsigned integer having the relevant bits</param>
|
||||
/// <returns>A new Unity version with the cooresponding bits</returns>
|
||||
public static CompactUnityVersion32 FromBits(uint bits) => new CompactUnityVersion32(bits);
|
||||
|
||||
private static ushort ConvertMajorRawToNormal(byte raw)
|
||||
{
|
||||
return raw < 6 ? raw : unchecked((ushort)(raw + 2011U));
|
||||
}
|
||||
|
||||
private static byte ConvertMajorRawToNormal(ushort major)
|
||||
{
|
||||
if (major < 6)
|
||||
{
|
||||
return unchecked((byte)major);
|
||||
}
|
||||
else if (major >= 2017 && major <= MajorMaxValue)
|
||||
{
|
||||
return unchecked((byte)(major - 2011U));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(major));
|
||||
}
|
||||
}
|
||||
|
||||
private static byte CastToFourBits(byte b)
|
||||
{
|
||||
return b <= bitMask4 ? b : throw new ArgumentOutOfRangeException(nameof(b));
|
||||
}
|
||||
|
||||
private static UnityVersionType CastToFourBits(UnityVersionType type)
|
||||
{
|
||||
return (byte)type <= bitMask4 ? type : throw new ArgumentOutOfRangeException(nameof(type));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize the version as a string
|
||||
/// </summary>
|
||||
/// <returns>A new string like 2019.4.3f1</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Major}.{Minor}.{Build}{Type.ToCharacter()}{TypeNumber}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.IO;
|
||||
|
||||
namespace AssetRipper.VersionUtilities.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Unity version extension methods for <see cref="BinaryReader"/>
|
||||
/// </summary>
|
||||
public static class BinaryReaderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads a Unity version from the stream
|
||||
/// </summary>
|
||||
/// <param name="reader">A binary reader</param>
|
||||
/// <returns>The read Unity version</returns>
|
||||
public static UnityVersion ReadUnityVersion(this BinaryReader reader)
|
||||
{
|
||||
return UnityVersion.FromBits(reader.ReadUInt64());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a Unity version from the stream
|
||||
/// </summary>
|
||||
/// <param name="reader">A binary reader</param>
|
||||
/// <returns>The read Unity version</returns>
|
||||
public static CompactUnityVersion32 ReadCompactUnityVersion32(this BinaryReader reader)
|
||||
{
|
||||
return CompactUnityVersion32.FromBits(reader.ReadUInt32());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a Unity version from the stream
|
||||
/// </summary>
|
||||
/// <param name="reader">A binary reader</param>
|
||||
/// <returns>The read Unity version</returns>
|
||||
public static CompactUnityVersion24 ReadCompactUnityVersion24(this BinaryReader reader)
|
||||
{
|
||||
byte b = reader.ReadByte();
|
||||
ushort s = reader.ReadUInt16();
|
||||
return CompactUnityVersion24.FromBits(b, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.IO;
|
||||
|
||||
namespace AssetRipper.VersionUtilities.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Unity version extension methods for <see cref="BinaryWriter"/>
|
||||
/// </summary>
|
||||
public static class BinaryWriterExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Write a Unity version to the stream
|
||||
/// </summary>
|
||||
/// <param name="writer">A binary writer</param>
|
||||
/// <param name="version">A Unity version</param>
|
||||
public static void Write(this BinaryWriter writer, UnityVersion version)
|
||||
{
|
||||
writer.Write(version.GetBits());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a Unity version to the stream
|
||||
/// </summary>
|
||||
/// <param name="writer">A binary writer</param>
|
||||
/// <param name="version">A Unity version</param>
|
||||
public static void Write(this BinaryWriter writer, CompactUnityVersion32 version)
|
||||
{
|
||||
writer.Write(version.GetBits());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a Unity version to the stream
|
||||
/// </summary>
|
||||
/// <param name="writer">A binary writer</param>
|
||||
/// <param name="version">A Unity version</param>
|
||||
public static void Write(this BinaryWriter writer, CompactUnityVersion24 version)
|
||||
{
|
||||
version.GetBits(out byte b, out ushort s);
|
||||
writer.Write(b);
|
||||
writer.Write(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace AssetRipper.VersionUtilities.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="char"/>
|
||||
/// </summary>
|
||||
public static class CharacterExtensions
|
||||
{
|
||||
internal static int ParseDigit(this char _this)
|
||||
{
|
||||
return _this - '0';
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a character into a Unity Version Type
|
||||
/// </summary>
|
||||
/// <param name="c">The character</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException">No version type for character</exception>
|
||||
public static UnityVersionType ToUnityVersionType(this char c)
|
||||
{
|
||||
return c switch
|
||||
{
|
||||
'a' => UnityVersionType.Alpha,
|
||||
'b' => UnityVersionType.Beta,
|
||||
'c' => UnityVersionType.China,
|
||||
'f' => UnityVersionType.Final,
|
||||
'p' => UnityVersionType.Patch,
|
||||
'x' => UnityVersionType.Experimental,
|
||||
_ => throw new ArgumentException($"There is no version type {c}", nameof(c)),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
global using System;
|
||||
@@ -0,0 +1,3 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("VersionUtilities.Tests")]
|
||||
@@ -2,68 +2,66 @@
|
||||
{
|
||||
public readonly partial struct UnityVersion
|
||||
{
|
||||
private const ulong subMajorMask = 0x0000FFFFFFFFFFFFUL;
|
||||
private const ulong subMinorMask = 0x00000000FFFFFFFFUL;
|
||||
private const ulong subBuildMask = 0x000000000000FFFFUL;
|
||||
private const ulong subTypeMask = 0x00000000000000FFUL;
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
public bool IsEqual(int major) => this == From(major);
|
||||
public bool IsEqual(int major, int minor) => this == From(major, minor);
|
||||
public bool IsEqual(int major, int minor, int build) => this == From(major, minor, build);
|
||||
public bool IsEqual(int major, int minor, int build, UnityVersionType type) => this == From(major, minor, build, type);
|
||||
public bool IsEqual(int major, int minor, int build, UnityVersionType type, int typeNumber) => this == new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsEqual(ushort major) => this == From(major);
|
||||
public bool IsEqual(ushort major, ushort minor) => this == From(major, minor);
|
||||
public bool IsEqual(ushort major, ushort minor, ushort build) => this == From(major, minor, build);
|
||||
public bool IsEqual(ushort major, ushort minor, ushort build, UnityVersionType type) => this == From(major, minor, build, type);
|
||||
public bool IsEqual(ushort major, ushort minor, ushort build, UnityVersionType type, byte typeNumber) => this == new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsEqual(string version) => this == Parse(version);
|
||||
|
||||
public bool IsLess(int major) => this < From(major);
|
||||
public bool IsLess(int major, int minor) => this < From(major, minor);
|
||||
public bool IsLess(int major, int minor, int build) => this < From(major, minor, build);
|
||||
public bool IsLess(int major, int minor, int build, UnityVersionType type) => this < From(major, minor, build, type);
|
||||
public bool IsLess(int major, int minor, int build, UnityVersionType type, int typeNumber) => this < new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsLess(ushort major) => this < From(major);
|
||||
public bool IsLess(ushort major, ushort minor) => this < From(major, minor);
|
||||
public bool IsLess(ushort major, ushort minor, ushort build) => this < From(major, minor, build);
|
||||
public bool IsLess(ushort major, ushort minor, ushort build, UnityVersionType type) => this < From(major, minor, build, type);
|
||||
public bool IsLess(ushort major, ushort minor, ushort build, UnityVersionType type, byte typeNumber) => this < new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsLess(string version) => this < Parse(version);
|
||||
|
||||
public bool IsLessEqual(int major) => this <= From(major);
|
||||
public bool IsLessEqual(int major, int minor) => this <= From(major, minor);
|
||||
public bool IsLessEqual(int major, int minor, int build) => this <= From(major, minor, build);
|
||||
public bool IsLessEqual(int major, int minor, int build, UnityVersionType type) => this <= From(major, minor, build, type);
|
||||
public bool IsLessEqual(int major, int minor, int build, UnityVersionType type, int typeNumber) => this <= new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsLessEqual(ushort major) => this <= From(major);
|
||||
public bool IsLessEqual(ushort major, ushort minor) => this <= From(major, minor);
|
||||
public bool IsLessEqual(ushort major, ushort minor, ushort build) => this <= From(major, minor, build);
|
||||
public bool IsLessEqual(ushort major, ushort minor, ushort build, UnityVersionType type) => this <= From(major, minor, build, type);
|
||||
public bool IsLessEqual(ushort major, ushort minor, ushort build, UnityVersionType type, byte typeNumber) => this <= new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsLessEqual(string version) => this <= Parse(version);
|
||||
|
||||
public bool IsGreater(int major) => this > From(major);
|
||||
public bool IsGreater(int major, int minor) => this > From(major, minor);
|
||||
public bool IsGreater(int major, int minor, int build) => this > From(major, minor, build);
|
||||
public bool IsGreater(int major, int minor, int build, UnityVersionType type) => this > From(major, minor, build, type);
|
||||
public bool IsGreater(int major, int minor, int build, UnityVersionType type, int typeNumber) => this > new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsGreater(ushort major) => this > From(major);
|
||||
public bool IsGreater(ushort major, ushort minor) => this > From(major, minor);
|
||||
public bool IsGreater(ushort major, ushort minor, ushort build) => this > From(major, minor, build);
|
||||
public bool IsGreater(ushort major, ushort minor, ushort build, UnityVersionType type) => this > From(major, minor, build, type);
|
||||
public bool IsGreater(ushort major, ushort minor, ushort build, UnityVersionType type, byte typeNumber) => this > new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsGreater(string version) => this > Parse(version);
|
||||
|
||||
public bool IsGreaterEqual(int major) => this >= From(major);
|
||||
public bool IsGreaterEqual(int major, int minor) => this >= From(major, minor);
|
||||
public bool IsGreaterEqual(int major, int minor, int build) => this >= From(major, minor, build);
|
||||
public bool IsGreaterEqual(int major, int minor, int build, UnityVersionType type) => this >= From(major, minor, build, type);
|
||||
public bool IsGreaterEqual(int major, int minor, int build, UnityVersionType type, int typeNumber) => this >= new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsGreaterEqual(ushort major) => this >= From(major);
|
||||
public bool IsGreaterEqual(ushort major, ushort minor) => this >= From(major, minor);
|
||||
public bool IsGreaterEqual(ushort major, ushort minor, ushort build) => this >= From(major, minor, build);
|
||||
public bool IsGreaterEqual(ushort major, ushort minor, ushort build, UnityVersionType type) => this >= From(major, minor, build, type);
|
||||
public bool IsGreaterEqual(ushort major, ushort minor, ushort build, UnityVersionType type, byte typeNumber) => this >= new UnityVersion(major, minor, build, type, typeNumber);
|
||||
public bool IsGreaterEqual(string version) => this >= Parse(version);
|
||||
|
||||
private UnityVersion From(int major)
|
||||
private UnityVersion From(ushort major)
|
||||
{
|
||||
ulong data = ((ulong)(major & 0xFFFF) << 48) | (0x0000FFFFFFFFFFFFUL & m_data);
|
||||
ulong data = ((ulong)major << majorOffset) | subMajorMask & m_data;
|
||||
return new UnityVersion(data);
|
||||
}
|
||||
private UnityVersion From(int major, int minor)
|
||||
private UnityVersion From(ushort major, ushort minor)
|
||||
{
|
||||
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | (0x000000FFFFFFFFFFUL & m_data);
|
||||
ulong data = ((ulong)major << majorOffset) | ((ulong)minor << minorOffset) | subMinorMask & m_data;
|
||||
return new UnityVersion(data);
|
||||
}
|
||||
private UnityVersion From(int major, int minor, int build)
|
||||
private UnityVersion From(ushort major, ushort minor, ushort build)
|
||||
{
|
||||
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32) |
|
||||
(0x00000000FFFFFFFFUL & m_data);
|
||||
ulong data = ((ulong)major << majorOffset) | ((ulong)minor << minorOffset) | ((ulong)build << buildOffset) |
|
||||
subBuildMask & m_data;
|
||||
return new UnityVersion(data);
|
||||
}
|
||||
private UnityVersion From(int major, int minor, int build, UnityVersionType type)
|
||||
private UnityVersion From(ushort major, ushort minor, ushort build, UnityVersionType type)
|
||||
{
|
||||
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32) |
|
||||
((ulong)((int)type & 0xFF) << 24) | (0x0000000000FFFFFFUL & m_data);
|
||||
return new UnityVersion(data);
|
||||
}
|
||||
private UnityVersion From(int major, int minor, int build, UnityVersionType type, int typeNumber)
|
||||
{
|
||||
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32)
|
||||
| ((ulong)((int)type & 0xFF) << 24) | ((ulong)(typeNumber & 0xFF) << 16) | (0x000000000000FFFFUL & m_data);
|
||||
ulong data = ((ulong)major << majorOffset) | ((ulong)minor << minorOffset) | ((ulong)build << buildOffset) |
|
||||
((ulong)(ushort)type << typeOffset) | subTypeMask & m_data;
|
||||
return new UnityVersion(data);
|
||||
}
|
||||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
|
||||
namespace AssetRipper.VersionUtilities
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
public readonly partial struct UnityVersion : IEquatable<UnityVersion>, IComparable, IComparable<UnityVersion>
|
||||
{
|
||||
|
||||
@@ -43,5 +43,39 @@
|
||||
? right.m_data - left.m_data
|
||||
: left.m_data - right.m_data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the closest Unity version in an array of versions using <see cref="Distance(UnityVersion, UnityVersion)"/>
|
||||
/// </summary>
|
||||
/// <param name="versions">The Unity version array</param>
|
||||
/// <returns>The closest Unity version</returns>
|
||||
/// <exception cref="ArgumentNullException">The array is null</exception>
|
||||
/// <exception cref="ArgumentException">The array is empty</exception>
|
||||
public UnityVersion GetClosestVersion(UnityVersion[] versions)
|
||||
{
|
||||
if (versions is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(versions));
|
||||
}
|
||||
|
||||
if (versions.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("Length cannot be zero", nameof(versions));
|
||||
}
|
||||
|
||||
UnityVersion result = versions[0];
|
||||
ulong currentDistance = Distance(this, result);
|
||||
for(int i = 1; i < versions.Length; i++)
|
||||
{
|
||||
ulong newDistance = Distance(this, versions[i]);
|
||||
if (newDistance < currentDistance)
|
||||
{
|
||||
currentDistance = newDistance;
|
||||
result = versions[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using AssetRipper.VersionUtilities.Extensions;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace AssetRipper.VersionUtilities
|
||||
/// <returns>A new string like 2019.4.3f1</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Major}.{Minor}.{Build}{Type.ToLiteral()}{TypeNumber}";
|
||||
return $"{Major}.{Minor}.{Build}{Type.ToCharacter()}{TypeNumber}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,7 +37,7 @@ namespace AssetRipper.VersionUtilities
|
||||
sb.Append(separator);
|
||||
sb.Append(Build);
|
||||
sb.Append(separator);
|
||||
sb.Append(Type.ToLiteral());
|
||||
sb.Append(Type.ToCharacter());
|
||||
sb.Append(TypeNumber);
|
||||
|
||||
if(hasExtension)
|
||||
@@ -161,7 +161,7 @@ namespace AssetRipper.VersionUtilities
|
||||
typeNumber = typeNumber * 10 + c.ParseDigit();
|
||||
}
|
||||
|
||||
return new UnityVersion(major, minor, build, versionType, typeNumber);
|
||||
return new UnityVersion((ushort)major, (ushort)minor, (ushort)build, versionType, (byte)typeNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,32 +5,40 @@
|
||||
/// </summary>
|
||||
public readonly partial struct UnityVersion
|
||||
{
|
||||
private const int majorOffset = 48;
|
||||
private const int minorOffset = 32;
|
||||
private const int buildOffset = 16;
|
||||
private const int typeOffset = 8;
|
||||
|
||||
private const ulong byteMask = 0xFFUL;
|
||||
private const ulong ushortMask = 0xFFFFUL;
|
||||
|
||||
private readonly ulong m_data;
|
||||
|
||||
/// <summary>
|
||||
/// The first number in a Unity version string
|
||||
/// </summary>
|
||||
public int Major => unchecked((int)((m_data >> 48) & 0xFFFFUL));
|
||||
public ushort Major => unchecked((ushort)((m_data >> majorOffset) & ushortMask));
|
||||
|
||||
/// <summary>
|
||||
/// The second number in a Unity version string
|
||||
/// </summary>
|
||||
public int Minor => unchecked((int)((m_data >> 40) & 0xFFUL));
|
||||
public ushort Minor => unchecked((ushort)((m_data >> minorOffset) & ushortMask));
|
||||
|
||||
/// <summary>
|
||||
/// The third number in a Unity version string
|
||||
/// </summary>
|
||||
public int Build => unchecked((int)((m_data >> 32) & 0xFFUL));
|
||||
public ushort Build => unchecked((ushort)((m_data >> buildOffset) & ushortMask));
|
||||
|
||||
/// <summary>
|
||||
/// The letter in a Unity version string
|
||||
/// </summary>
|
||||
public UnityVersionType Type => (UnityVersionType)unchecked((int)((m_data >> 24) & 0xFFUL));
|
||||
public UnityVersionType Type => (UnityVersionType)unchecked((byte)((m_data >> typeOffset) & byteMask));
|
||||
|
||||
/// <summary>
|
||||
/// The last number in a Unity version string
|
||||
/// </summary>
|
||||
public int TypeNumber => unchecked((int)((m_data >> 16) & 0xFFUL));
|
||||
public byte TypeNumber => unchecked((byte)(m_data & byteMask));
|
||||
|
||||
/// <summary>
|
||||
/// The minimum value this type can have
|
||||
@@ -45,48 +53,61 @@
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public UnityVersion(int major)
|
||||
public UnityVersion(ushort major)
|
||||
{
|
||||
m_data = (ulong)(major & 0xFFFF) << 48;
|
||||
m_data = (ulong)major << majorOffset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public UnityVersion(int major, int minor)
|
||||
public UnityVersion(ushort major, ushort minor)
|
||||
{
|
||||
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40);
|
||||
m_data = ((ulong)major << majorOffset) | ((ulong)minor << minorOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public UnityVersion(int major, int minor, int build)
|
||||
public UnityVersion(ushort major, ushort minor, ushort build)
|
||||
{
|
||||
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32);
|
||||
m_data = ((ulong)major << majorOffset) | ((ulong)minor << minorOffset) | ((ulong)build << buildOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public UnityVersion(int major, int minor, int build, UnityVersionType type)
|
||||
public UnityVersion(ushort major, ushort minor, ushort build, UnityVersionType type)
|
||||
{
|
||||
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32)
|
||||
| ((ulong)((int)type & 0xFF) << 24);
|
||||
m_data = ((ulong)major << majorOffset) | ((ulong)minor << minorOffset) | ((ulong)build << buildOffset)
|
||||
| ((ulong)type << typeOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new Unity version
|
||||
/// </summary>
|
||||
public UnityVersion(int major, int minor, int build, UnityVersionType type, int typeNumber)
|
||||
public UnityVersion(ushort major, ushort minor, ushort build, UnityVersionType type, byte typeNumber)
|
||||
{
|
||||
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32)
|
||||
| ((ulong)((int)type & 0xFF) << 24) | ((ulong)(typeNumber & 0xFF) << 16);
|
||||
m_data = ((ulong)major << majorOffset) | ((ulong)minor << minorOffset) | ((ulong)build << buildOffset)
|
||||
| ((ulong)type << typeOffset) | typeNumber;
|
||||
}
|
||||
|
||||
private UnityVersion(ulong data)
|
||||
{
|
||||
m_data = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts this to its binary representation
|
||||
/// </summary>
|
||||
/// <returns>An unsigned long integer having the same bits as this</returns>
|
||||
public ulong GetBits() => m_data;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a binary representation into its respective version
|
||||
/// </summary>
|
||||
/// <param name="bits">An unsigned long integer having the relevant bits</param>
|
||||
/// <returns>A new Unity version with the cooresponding bits</returns>
|
||||
public static UnityVersion FromBits(ulong bits) => new UnityVersion(bits);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace AssetRipper.VersionUtilities
|
||||
namespace AssetRipper.VersionUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// An enumeration representing the letter in a Unity Version string
|
||||
/// </summary>
|
||||
public enum UnityVersionType
|
||||
public enum UnityVersionType : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// a
|
||||
@@ -53,9 +51,18 @@ namespace AssetRipper.VersionUtilities
|
||||
/// <param name="_this">A Unity version type</param>
|
||||
/// <returns>The character this value represents</returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The type is not a valid value</exception>
|
||||
public static char ToLiteral(this UnityVersionType _this)
|
||||
[Obsolete("Changed to ToCharacter", true)]
|
||||
public static char ToLiteral(this UnityVersionType _this) => _this.ToCharacter();
|
||||
|
||||
/// <summary>
|
||||
/// Convert to the relevant character
|
||||
/// </summary>
|
||||
/// <param name="type">A Unity version type</param>
|
||||
/// <returns>The character this value represents</returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The type is not a valid value</exception>
|
||||
public static char ToCharacter(this UnityVersionType type)
|
||||
{
|
||||
return _this switch
|
||||
return type switch
|
||||
{
|
||||
UnityVersionType.Alpha => 'a',
|
||||
UnityVersionType.Beta => 'b',
|
||||
@@ -63,7 +70,7 @@ namespace AssetRipper.VersionUtilities
|
||||
UnityVersionType.Final => 'f',
|
||||
UnityVersionType.Patch => 'p',
|
||||
UnityVersionType.Experimental => 'x',
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(_this), $"Unsupported version type {_this}"),
|
||||
_ => 'u',//unknown
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<RootNamespace>AssetRipper.VersionUtilities</RootNamespace>
|
||||
<AssemblyName>AssetRipper.VersionUtilities</AssemblyName>
|
||||
<Authors>ds5678</Authors>
|
||||
<Company>AssetRipper</Company>
|
||||
<Version>1.0.1.0</Version>
|
||||
<AssemblyVersion>1.0.1.0</AssemblyVersion>
|
||||
<Version>1.2.1.0</Version>
|
||||
<AssemblyVersion>1.2.1.0</AssemblyVersion>
|
||||
<PackageId>AssetRipper.VersionUtilities</PackageId>
|
||||
<PackageTags>C# version unity unity3d</PackageTags>
|
||||
<RepositoryUrl>https://github.com/AssetRipper/VersionUtilities</RepositoryUrl>
|
||||
@@ -22,5 +23,9 @@
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<DocumentationFile>bin\VersionUtilities.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(TargetFramework)' != 'netframework3.5' and '$(TargetFramework)' != 'netframework4.0' ">
|
||||
<IsTrimmable>true</IsTrimmable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in new issue
Block a user