Skip to content

Commit ce94587

Browse files
Add Unity project and enable netstandard2.1 support
Introduces Maple2.File.Unity project targeting netstandard2.1 with global usings and references to IO and Flat projects. Updates Maple2.File.IO and Maple2.File.Flat to support both net8.0 and netstandard2.1, adds compatibility shims for missing APIs, and updates code to use these shims conditionally. Also refactors array initializations for compatibility.
1 parent 41477e7 commit ce94587

14 files changed

Lines changed: 163 additions & 14 deletions

File tree

Maple2.File.Flat/Maple2.File.Flat.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
55
<LangVersion>12</LangVersion>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>

Maple2.File.IO/IsExternalInit.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ReSharper disable once CheckNamespace
2+
namespace System.Runtime.CompilerServices;
3+
4+
#if NETSTANDARD2_1
5+
internal static class IsExternalInit { }
6+
#endif

Maple2.File.IO/Maple2.File.IO.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
55
<LangVersion>12</LangVersion>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
77
<ImplicitUsings>enable</ImplicitUsings>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Maple2.File.IO;
5+
6+
#if NETSTANDARD2_1
7+
public static class NetStandardExtensions {
8+
// EnsureCapacity for netstandard2.1 (added in .NET 5)
9+
public static void EnsureCapacityCompat<T>(this List<T> list, int capacity) {
10+
if (list.Capacity < capacity) {
11+
list.Capacity = capacity;
12+
}
13+
}
14+
15+
// Matrix4x4 indexer for netstandard2.1
16+
public static float GetElement(this Matrix4x4 matrix, int row, int column) {
17+
return column switch {
18+
0 => row switch {
19+
0 => matrix.M11,
20+
1 => matrix.M21,
21+
2 => matrix.M31,
22+
3 => matrix.M41,
23+
_ => 0f
24+
},
25+
1 => row switch {
26+
0 => matrix.M12,
27+
1 => matrix.M22,
28+
2 => matrix.M32,
29+
3 => matrix.M42,
30+
_ => 0f
31+
},
32+
2 => row switch {
33+
0 => matrix.M13,
34+
1 => matrix.M23,
35+
2 => matrix.M33,
36+
3 => matrix.M43,
37+
_ => 0f
38+
},
39+
3 => row switch {
40+
0 => matrix.M14,
41+
1 => matrix.M24,
42+
2 => matrix.M34,
43+
3 => matrix.M44,
44+
_ => 0f
45+
},
46+
_ => 0f
47+
};
48+
}
49+
50+
public static Matrix4x4 SetMatrixElement(Matrix4x4 matrix, int row, int column, float value) {
51+
if (column == 0 && row == 0) matrix.M11 = value;
52+
else if (column == 0 && row == 1) matrix.M21 = value;
53+
else if (column == 0 && row == 2) matrix.M31 = value;
54+
else if (column == 0 && row == 3) matrix.M41 = value;
55+
else if (column == 1 && row == 0) matrix.M12 = value;
56+
else if (column == 1 && row == 1) matrix.M22 = value;
57+
else if (column == 1 && row == 2) matrix.M32 = value;
58+
else if (column == 1 && row == 3) matrix.M42 = value;
59+
else if (column == 2 && row == 0) matrix.M13 = value;
60+
else if (column == 2 && row == 1) matrix.M23 = value;
61+
else if (column == 2 && row == 2) matrix.M33 = value;
62+
else if (column == 2 && row == 3) matrix.M43 = value;
63+
else if (column == 3 && row == 0) matrix.M14 = value;
64+
else if (column == 3 && row == 1) matrix.M24 = value;
65+
else if (column == 3 && row == 2) matrix.M34 = value;
66+
else if (column == 3 && row == 3) matrix.M44 = value;
67+
return matrix;
68+
}
69+
}
70+
#endif

Maple2.File.IO/Nif/Endian.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ public Matrix4x4 ReadAdjustedMatrix4x3() {
6565

6666
for (int column = 0; column < 3; ++column) {
6767
for (int row = 0; row < 3; ++row) {
68+
#if NETSTANDARD2_1
69+
float value = ReadAdjustedFloat32();
70+
matrix = NetStandardExtensions.SetMatrixElement(matrix, row, column, value);
71+
#else
6872
matrix[row, column] = ReadAdjustedFloat32();
73+
#endif
6974
}
7075
}
7176

Maple2.File.IO/Nif/NiPhysXActorDesc.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public override void Parse(NifDocument document) {
3232
int numPoses = document.Reader.ReadAdjustedInt32();
3333

3434
Poses = new List<Matrix4x4>();
35+
#if NETSTANDARD2_1
36+
Poses.EnsureCapacityCompat(numPoses);
37+
#else
3538
Poses.EnsureCapacity(numPoses);
39+
#endif
3640

3741
for (int i = 0; i < numPoses; ++i) {
3842
Poses.Add(document.Reader.ReadAdjustedMatrix4x3());

Maple2.File.IO/Nif/NiPhysXPropDesc.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@ public override void Parse(NifDocument document) {
4040

4141
uint numStates = document.Reader.ReadAdjustedUInt32();
4242

43+
#if NETSTANDARD2_1
44+
StateNames.EnsureCapacityCompat((int) numStates);
45+
#else
4346
StateNames.EnsureCapacity((int) numStates);
47+
#endif
4448

4549
for (uint i = 0; i < numStates; i++) {
4650
State state = new State();
4751

4852
int numStrings = document.Reader.ReadAdjustedInt32();
4953

54+
#if NETSTANDARD2_1
55+
state.EnsureCapacityCompat((int) numStrings);
56+
#else
5057
state.EnsureCapacity((int) numStrings);
58+
#endif
5159

5260
for (int j = 0; j < numStrings; j++) {
5361
string key = document.ReadString();

Maple2.File.IO/Nif/NifDocument.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public List<T> ReadBlockRefList<T>() {
8383

8484
int count = Reader.ReadAdjustedInt32();
8585

86+
#if NETSTANDARD2_1
87+
blocks.EnsureCapacityCompat(count);
88+
#else
8689
blocks.EnsureCapacity(count);
90+
#endif
8791

8892
for (int i = 0; i < count; ++i) {
8993
T? block = ReadBlockRef<T>();

Maple2.File.IO/Nif/PhysXMesh.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,13 @@ private NxsMeshType ParseConvexMesh(EndianReader reader) {
9292
uint unk8 = reader.ReadAdjustedUInt32();
9393
uint unk9 = reader.ReadAdjustedUInt32();
9494

95+
#if NETSTANDARD2_1
96+
Vertices.EnsureCapacityCompat(vertexCount);
97+
Faces.EnsureCapacityCompat(faceCount);
98+
#else
9599
Vertices.EnsureCapacity(vertexCount);
96100
Faces.EnsureCapacity(faceCount);
101+
#endif
97102

98103
for (int i = 0; i < vertexCount; ++i) {
99104
Vertices.Add(reader.ReadAdjustedVector3());
@@ -149,8 +154,13 @@ private NxsMeshType ParseTriangleMesh(EndianReader reader) {
149154
int vertexCount = reader.ReadAdjustedInt32();
150155
int faceCount = reader.ReadAdjustedInt32();
151156

157+
#if NETSTANDARD2_1
158+
Vertices.EnsureCapacityCompat(vertexCount);
159+
Faces.EnsureCapacityCompat(faceCount);
160+
#else
152161
Vertices.EnsureCapacity(vertexCount);
153162
Faces.EnsureCapacity(faceCount);
163+
#endif
154164

155165
for (int i = 0; i < vertexCount; ++i) {
156166
Vertices.Add(reader.ReadAdjustedVector3());

Maple2.File.Parser/MapXBlock/Generator/PresetGenerator.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ public string GenerateClass(string @namespace, FlatType type, Type parent) {
6969
builder.Replace("\t", " ");
7070

7171
var imports = new StringBuilder();
72-
string[] replaces = [
73-
"System.Collections.Generic",
74-
"System.Drawing",
75-
"System.Numerics",
76-
];
72+
string[] replaces = new[] { "System.Collections.Generic", "System.Drawing", "System.Numerics" };
7773
foreach (string replace in replaces) {
7874
int before = builder.Length;
7975
builder.Replace($"{replace}.", "");

0 commit comments

Comments
 (0)