Skip to content

Commit 8f0f1bb

Browse files
committed
fix: mods
1 parent a5dcd6c commit 8f0f1bb

File tree

6 files changed

+42
-30
lines changed

6 files changed

+42
-30
lines changed

src/OTAPI.UnifiedServerProcess/ModAssemblyMerger.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ static void SetModTypePlaceholder(ModuleDefinition module, Dictionary<string, Ty
283283
}
284284
foreach (var method in modType.Methods) {
285285
PrepareMethod(target, method, null);
286+
SetMemberReplace(module, method.CustomAttributes, false);
286287
}
287288
}
288289
else {
@@ -292,6 +293,19 @@ static void SetModTypePlaceholder(ModuleDefinition module, Dictionary<string, Ty
292293
existingMethod = null;
293294
}
294295
PrepareMethod(target, method, existingMethod);
296+
SetMemberReplace(module, method.CustomAttributes, false);
297+
}
298+
if (modType.IsEnum) {
299+
SetMemberReplace(module, modType.CustomAttributes, true);
300+
}
301+
foreach (var field in modType.Fields) {
302+
SetMemberReplace(module, field.CustomAttributes, modType.IsEnum);
303+
}
304+
foreach (var prop in modType.Properties) {
305+
SetMemberReplace(module, prop.CustomAttributes, false);
306+
}
307+
foreach (var ev in modType.Events) {
308+
SetMemberReplace(module, ev.CustomAttributes, false);
295309
}
296310
}
297311
foreach (var nested in modType.NestedTypes) {
@@ -300,9 +314,17 @@ static void SetModTypePlaceholder(ModuleDefinition module, Dictionary<string, Ty
300314
}
301315
static void PrepareMethod(TypeDefinition targetType, MethodDefinition modMethod, MethodDefinition? originalMethod) {
302316
if (modMethod.IsConstructor && !modMethod.IsStatic) {
303-
var attType = modMethod.Module.ImportReference(typeof(MonoMod.MonoModConstructor));
304-
modMethod.CustomAttributes.Add(new CustomAttribute(new MethodReference(".ctor", modMethod.Module.TypeSystem.Void, attType) { HasThis = true }));
317+
var attType_ctor = modMethod.Module.ImportReference(typeof(MonoMod.MonoModConstructor));
318+
modMethod.CustomAttributes.Add(new CustomAttribute(new MethodReference(".ctor", modMethod.Module.TypeSystem.Void, attType_ctor) { HasThis = true }));
319+
}
320+
}
321+
static void SetMemberReplace(ModuleDefinition module, Collection<CustomAttribute> attributes, bool isEnum) {
322+
var type = isEnum ? typeof(MonoMod.MonoModEnumReplace) : typeof(MonoMod.MonoModReplace);
323+
if (attributes.Any(a => a.AttributeType.Name == type.Name)) {
324+
return;
305325
}
326+
var attType_replace = module.ImportReference(type);
327+
attributes.Add(new CustomAttribute(new MethodReference(".ctor", module.TypeSystem.Void, attType_replace) { HasThis = true }));
306328
}
307329

308330
static void AdjustInterfaces(ModuleDefinition target, ModuleDefinition mod, TypeDefinition type, TypeDefinition mappedType) {

src/OTAPI.UnifiedServerProcess/Mods/SocketMod.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Terraria.Net.Sockets
1919
{
2020
public interface ISocket
2121
{
22+
[MonoMod.MonoModIgnore]
2223
void Close();
2324
void AsyncSendNoCopy(byte[] data, int offset, int size, SocketSendCallback callback, object state = null);
2425
void AsyncSend(ReadOnlyMemory<byte> data, SocketSendCallback callback, object state = null);
@@ -28,7 +29,7 @@ public class TcpSocket : ISocket
2829
[MonoMod.MonoModIgnore]
2930
void ISocket.Close() { }
3031
public TcpClient _connection;
31-
void ISocket.AsyncSendNoCopy(byte[] data, int offset, int size, SocketSendCallback callback, object state = null) {
32+
void ISocket.AsyncSendNoCopy(byte[] data, int offset, int size, SocketSendCallback callback, object state) {
3233
_connection.GetStream().BeginWrite(data, offset, size, static result => {
3334
var tuple = (Tuple<TcpSocket, SocketSendCallback, object>)result.AsyncState;
3435
try {
@@ -40,7 +41,7 @@ void ISocket.AsyncSendNoCopy(byte[] data, int offset, int size, SocketSendCallba
4041
}
4142
}, new Tuple<TcpSocket, SocketSendCallback, object>(this, callback, state));
4243
}
43-
void ISocket.AsyncSend(ReadOnlyMemory<byte> data, SocketSendCallback callback, object state = null) {
44+
void ISocket.AsyncSend(ReadOnlyMemory<byte> data, SocketSendCallback callback, object state) {
4445
var array = ArrayPool<byte>.Shared.Rent(data.Length);
4546
data.CopyTo(array);
4647
_connection.GetStream().BeginWrite(array, 0, data.Length, static result => {
@@ -63,7 +64,7 @@ public class SocialSocket : ISocket
6364
[MonoMod.MonoModIgnore]
6465
void ISocket.Close() { }
6566
public RemoteAddress _remoteAddress;
66-
void ISocket.AsyncSendNoCopy(byte[] data, int offset, int size, SocketSendCallback callback, object state = null) {
67+
void ISocket.AsyncSendNoCopy(byte[] data, int offset, int size, SocketSendCallback callback, object state) {
6768
if (offset is not 0) {
6869
var copy = ArrayPool<byte>.Shared.Rent(size);
6970
Buffer.BlockCopy(data, offset, copy, 0, size);
@@ -76,7 +77,7 @@ void ISocket.AsyncSendNoCopy(byte[] data, int offset, int size, SocketSendCallba
7677
callback.BeginInvoke(state, null, null);
7778
}
7879
}
79-
void ISocket.AsyncSend(ReadOnlyMemory<byte> data, SocketSendCallback callback, object state = null) {
80+
void ISocket.AsyncSend(ReadOnlyMemory<byte> data, SocketSendCallback callback, object state) {
8081
var copy = ArrayPool<byte>.Shared.Rent(data.Length);
8182
data.CopyTo(copy);
8283
SocialAPI.Network.Send(_remoteAddress, copy, data.Length);

src/TrProtocol/ExportedModels/Microsoft.Xna.Framework/Point.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ namespace Microsoft.Xna.Framework
66
[StructLayout(LayoutKind.Explicit, Size = 8)]
77
public struct Point(int x, int y) : IPackedSerializable, IEquatable<Point>
88
{
9-
[FieldOffset(0)]
10-
public int X = x;
11-
[FieldOffset(4)]
12-
public int Y = y;
13-
[FieldOffset(0)]
14-
private ulong packedValue;
9+
[FieldOffset(0)] public int X = x;
10+
[FieldOffset(4)] public int Y = y;
11+
[FieldOffset(0)] private ulong packedValue;
1512

1613
public static bool operator ==(Point a, Point b) => a.Equals(b);
1714
public static bool operator !=(Point a, Point b) => !a.Equals(b);

src/TrProtocol/ExportedModels/Terraria/DataStructures/Point16.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@ namespace Terraria.DataStructures
77
[StructLayout(LayoutKind.Explicit)]
88
public struct Point16 : IPackedSerializable, IEquatable<Point16>
99
{
10-
[FieldOffset(0)]
11-
public short X;
12-
[FieldOffset(1)]
13-
public short Y;
14-
[FieldOffset(0)]
15-
uint packedValue;
16-
17-
public readonly static Point16 Zero = new(0, 0);
18-
public readonly static Point16 NegativeOne = new(-1, -1);
19-
10+
[FieldOffset(0)] public short X;
11+
[FieldOffset(2)] public short Y;
12+
[FieldOffset(0)] uint packedValue;
2013
public Point16(Point point) {
2114
X = (short)point.X;
2215
Y = (short)point.Y;
@@ -31,7 +24,6 @@ public Point16(short X, short Y) {
3124
this.X = X;
3225
this.Y = Y;
3326
}
34-
3527
public static bool operator ==(Point16 first, Point16 second) => first.Equals(second);
3628
public static bool operator !=(Point16 first, Point16 second) => !first.Equals(second);
3729
public readonly override bool Equals(object? obj) => obj is Point16 point && Equals(point);

src/TrProtocol/TrProtocol.csproj

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

3-
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
7-
<LangVersion>12.0</LangVersion>
8-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<LangVersion>12.0</LangVersion>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
</PropertyGroup>
1010

1111
<ItemGroup>
1212
<ProjectReference Include="..\TrProtocol.SerializerGenerator\TrProtocol.SerializerGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="All" />

0 commit comments

Comments
 (0)