From a502bf6ff6f464c3e36b4834f3527e6be9b225a4 Mon Sep 17 00:00:00 2001 From: Thomas Altenburger Date: Thu, 20 Mar 2025 10:09:04 +0100 Subject: [PATCH] Code change for PublishAot compliance --- CodeGen/src/interfaces.py | 4 +-- .../SteamNetworkingMessage_t.cs | 2 +- .../Runtime/CallbackDispatcher.cs | 30 +++++++++++-------- .../Runtime/ISteamMatchmakingResponses.cs | 8 ++--- .../Runtime/InteropHelp.cs | 8 ++--- .../Runtime/Packsize.cs | 4 +-- .../Runtime/autogen/isteammatchmaking.cs | 2 +- .../SteamNetworkingMessage_t.cs | 2 +- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/CodeGen/src/interfaces.py b/CodeGen/src/interfaces.py index 6bf6b6c4..1f6157ef 100644 --- a/CodeGen/src/interfaces.py +++ b/CodeGen/src/interfaces.py @@ -718,8 +718,8 @@ def parse_func(f, interface, func): argnames += ")" elif func.returntype == "gameserveritem_t *": wrapperreturntype = "gameserveritem_t" - strReturnable += "(gameserveritem_t)Marshal.PtrToStructure(" - argnames += "), typeof(gameserveritem_t)" + strReturnable += "Marshal.PtrToStructure(" + argnames += "))" elif func.returntype == "CSteamID": wrapperreturntype = "CSteamID" strReturnable += "(CSteamID)" diff --git a/CodeGen/templates/custom_types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs b/CodeGen/templates/custom_types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs index e10f7d2c..97639c7d 100644 --- a/CodeGen/templates/custom_types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs +++ b/CodeGen/templates/custom_types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs @@ -100,7 +100,7 @@ public static void Release(IntPtr pointer) { /// Convert an IntPtr received from ISteamNetworkingSockets.ReceiveMessagesOnPollGroup into our structure. /// This is a Steamworks.NET extension. public static SteamNetworkingMessage_t FromIntPtr(IntPtr pointer) { - return (SteamNetworkingMessage_t)Marshal.PtrToStructure(pointer, typeof(SteamNetworkingMessage_t)); + return Marshal.PtrToStructure(pointer); } } } diff --git a/com.rlabrecque.steamworks.net/Runtime/CallbackDispatcher.cs b/com.rlabrecque.steamworks.net/Runtime/CallbackDispatcher.cs index 47bcaecf..900d09f7 100644 --- a/com.rlabrecque.steamworks.net/Runtime/CallbackDispatcher.cs +++ b/com.rlabrecque.steamworks.net/Runtime/CallbackDispatcher.cs @@ -57,7 +57,7 @@ internal static void Initialize() { lock (m_sync) { if (m_initCount == 0) { NativeMethods.SteamAPI_ManualDispatch_Init(); - m_pCallbackMsg = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CallbackMsg_t))); + m_pCallbackMsg = Marshal.AllocHGlobal(Marshal.SizeOf()); } ++m_initCount; } @@ -160,11 +160,11 @@ internal static void RunFrame(bool isGameServer) { NativeMethods.SteamAPI_ManualDispatch_RunFrame(hSteamPipe); var callbacksRegistry = isGameServer ? m_registeredGameServerCallbacks : m_registeredCallbacks; while (NativeMethods.SteamAPI_ManualDispatch_GetNextCallback(hSteamPipe, m_pCallbackMsg)) { - CallbackMsg_t callbackMsg = (CallbackMsg_t)Marshal.PtrToStructure(m_pCallbackMsg, typeof(CallbackMsg_t)); + CallbackMsg_t callbackMsg = Marshal.PtrToStructure(m_pCallbackMsg); try { // Check for dispatching API call results if (callbackMsg.m_iCallback == SteamAPICallCompleted_t.k_iCallback) { - SteamAPICallCompleted_t callCompletedCb = (SteamAPICallCompleted_t)Marshal.PtrToStructure(callbackMsg.m_pubParam, typeof(SteamAPICallCompleted_t)); + SteamAPICallCompleted_t callCompletedCb = Marshal.PtrToStructure(callbackMsg.m_pubParam); IntPtr pTmpCallResult = Marshal.AllocHGlobal((int)callCompletedCb.m_cubParam); bool bFailed; if (NativeMethods.SteamAPI_ManualDispatch_GetAPICallResult(hSteamPipe, callCompletedCb.m_hAsyncCall, pTmpCallResult, (int)callCompletedCb.m_cubParam, callCompletedCb.m_iCallback, out bFailed)) { @@ -181,14 +181,12 @@ internal static void RunFrame(bool isGameServer) { } Marshal.FreeHGlobal(pTmpCallResult); } else { - List callbacksCopy = null; - lock (m_sync) { - List callbacks = null; - if (callbacksRegistry.TryGetValue(callbackMsg.m_iCallback, out callbacks)) { + List callbacks; + if (callbacksRegistry.TryGetValue(callbackMsg.m_iCallback, out callbacks)) { + List callbacksCopy; + lock (m_sync) { callbacksCopy = new List(callbacks); } - } - if (callbacksCopy != null) { foreach (var callback in callbacksCopy) { callback.OnRunCallback(callbackMsg.m_pubParam); } @@ -210,7 +208,10 @@ public abstract class Callback { internal abstract void SetUnregistered(); } - public sealed class Callback : Callback, IDisposable { + public sealed class Callback< + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] + T + > : Callback, IDisposable { public delegate void DispatchDelegate(T param); private event DispatchDelegate m_Func; @@ -290,7 +291,7 @@ internal override Type GetCallbackType() { internal override void OnRunCallback(IntPtr pvParam) { try { - m_Func((T)Marshal.PtrToStructure(pvParam, typeof(T))); + m_Func(Marshal.PtrToStructure(pvParam)); } catch (Exception e) { CallbackDispatcher.ExceptionHandler(e); @@ -308,7 +309,10 @@ public abstract class CallResult { internal abstract void SetUnregistered(); } - public sealed class CallResult : CallResult, IDisposable { + public sealed class CallResult< + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] + T + > : CallResult, IDisposable { public delegate void APIDispatchDelegate(T param, bool bIOFailure); private event APIDispatchDelegate m_Func; @@ -385,7 +389,7 @@ internal override void OnRunCallResult(IntPtr pvParam, bool bFailed, ulong hStea SteamAPICall_t hSteamAPICall = (SteamAPICall_t)hSteamAPICall_; if (hSteamAPICall == m_hAPICall) { try { - m_Func((T)Marshal.PtrToStructure(pvParam, typeof(T)), bFailed); + m_Func(Marshal.PtrToStructure(pvParam), bFailed); } catch (Exception e) { CallbackDispatcher.ExceptionHandler(e); diff --git a/com.rlabrecque.steamworks.net/Runtime/ISteamMatchmakingResponses.cs b/com.rlabrecque.steamworks.net/Runtime/ISteamMatchmakingResponses.cs index 63b5b2b4..7fcf78c4 100644 --- a/com.rlabrecque.steamworks.net/Runtime/ISteamMatchmakingResponses.cs +++ b/com.rlabrecque.steamworks.net/Runtime/ISteamMatchmakingResponses.cs @@ -58,7 +58,7 @@ public ISteamMatchmakingServerListResponse(ServerResponded onServerResponded, Se m_VTServerFailedToRespond = InternalOnServerFailedToRespond, m_VTRefreshComplete = InternalOnRefreshComplete }; - m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VTable))); + m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(m_VTable, m_pVTable, false); m_pGCHandle = GCHandle.Alloc(m_pVTable, GCHandleType.Pinned); @@ -204,7 +204,7 @@ public ISteamMatchmakingPingResponse(ServerResponded onServerResponded, ServerFa m_VTServerResponded = InternalOnServerResponded, m_VTServerFailedToRespond = InternalOnServerFailedToRespond, }; - m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VTable))); + m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(m_VTable, m_pVTable, false); m_pGCHandle = GCHandle.Alloc(m_pVTable, GCHandleType.Pinned); @@ -303,7 +303,7 @@ public ISteamMatchmakingPlayersResponse(AddPlayerToList onAddPlayerToList, Playe m_VTPlayersFailedToRespond = InternalOnPlayersFailedToRespond, m_VTPlayersRefreshComplete = InternalOnPlayersRefreshComplete }; - m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VTable))); + m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(m_VTable, m_pVTable, false); m_pGCHandle = GCHandle.Alloc(m_pVTable, GCHandleType.Pinned); @@ -416,7 +416,7 @@ public ISteamMatchmakingRulesResponse(RulesResponded onRulesResponded, RulesFail m_VTRulesFailedToRespond = InternalOnRulesFailedToRespond, m_VTRulesRefreshComplete = InternalOnRulesRefreshComplete }; - m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VTable))); + m_pVTable = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(m_VTable, m_pVTable, false); m_pGCHandle = GCHandle.Alloc(m_pVTable, GCHandleType.Pinned); diff --git a/com.rlabrecque.steamworks.net/Runtime/InteropHelp.cs b/com.rlabrecque.steamworks.net/Runtime/InteropHelp.cs index a9ae57a1..13ce4564 100644 --- a/com.rlabrecque.steamworks.net/Runtime/InteropHelp.cs +++ b/com.rlabrecque.steamworks.net/Runtime/InteropHelp.cs @@ -137,14 +137,14 @@ public SteamParamStringArray(System.Collections.Generic.IList strings) { Marshal.Copy(strbuf, 0, m_Strings[i], strbuf.Length); } - m_ptrStrings = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * m_Strings.Length); + m_ptrStrings = Marshal.AllocHGlobal(Marshal.SizeOf() * m_Strings.Length); SteamParamStringArray_t stringArray = new SteamParamStringArray_t() { m_ppStrings = m_ptrStrings, m_nNumStrings = m_Strings.Length }; Marshal.Copy(m_Strings, 0, stringArray.m_ppStrings, m_Strings.Length); - m_pSteamParamStringArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SteamParamStringArray_t))); + m_pSteamParamStringArray = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(stringArray, m_pSteamParamStringArray, false); } @@ -181,9 +181,9 @@ public MMKVPMarshaller(MatchMakingKeyValuePair_t[] filters) { return; } - int sizeOfMMKVP = Marshal.SizeOf(typeof(MatchMakingKeyValuePair_t)); + int sizeOfMMKVP = Marshal.SizeOf(); - m_pNativeArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * filters.Length); + m_pNativeArray = Marshal.AllocHGlobal(Marshal.SizeOf() * filters.Length); m_pArrayEntries = Marshal.AllocHGlobal(sizeOfMMKVP * filters.Length); for (int i = 0; i < filters.Length; ++i) { Marshal.StructureToPtr(filters[i], new IntPtr(m_pArrayEntries.ToInt64() + (i * sizeOfMMKVP)), false); diff --git a/com.rlabrecque.steamworks.net/Runtime/Packsize.cs b/com.rlabrecque.steamworks.net/Runtime/Packsize.cs index 3b2efb09..2203854d 100644 --- a/com.rlabrecque.steamworks.net/Runtime/Packsize.cs +++ b/com.rlabrecque.steamworks.net/Runtime/Packsize.cs @@ -46,8 +46,8 @@ public static class Packsize { #endif public static bool Test() { - int sentinelSize = Marshal.SizeOf(typeof(ValvePackingSentinel_t)); - int subscribedFilesSize = Marshal.SizeOf(typeof(RemoteStorageEnumerateUserSubscribedFilesResult_t)); + int sentinelSize = Marshal.SizeOf(); + int subscribedFilesSize = Marshal.SizeOf(); #if VALVE_CALLBACK_PACK_LARGE if (sentinelSize != 32 || subscribedFilesSize != (1 + 1 + 1 + 50 + 100) * 4 + 4) return false; diff --git a/com.rlabrecque.steamworks.net/Runtime/autogen/isteammatchmaking.cs b/com.rlabrecque.steamworks.net/Runtime/autogen/isteammatchmaking.cs index 5945345f..3f954a61 100644 --- a/com.rlabrecque.steamworks.net/Runtime/autogen/isteammatchmaking.cs +++ b/com.rlabrecque.steamworks.net/Runtime/autogen/isteammatchmaking.cs @@ -547,7 +547,7 @@ public static void ReleaseRequest(HServerListRequest hServerListRequest) { /// public static gameserveritem_t GetServerDetails(HServerListRequest hRequest, int iServer) { InteropHelp.TestIfAvailableClient(); - return (gameserveritem_t)Marshal.PtrToStructure(NativeMethods.ISteamMatchmakingServers_GetServerDetails(CSteamAPIContext.GetSteamMatchmakingServers(), hRequest, iServer), typeof(gameserveritem_t)); + return Marshal.PtrToStructure(NativeMethods.ISteamMatchmakingServers_GetServerDetails(CSteamAPIContext.GetSteamMatchmakingServers(), hRequest, iServer)); } /// diff --git a/com.rlabrecque.steamworks.net/Runtime/types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs b/com.rlabrecque.steamworks.net/Runtime/types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs index ca2dc3b5..a1226d42 100644 --- a/com.rlabrecque.steamworks.net/Runtime/types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs +++ b/com.rlabrecque.steamworks.net/Runtime/types/SteamNetworkingTypes/SteamNetworkingMessage_t.cs @@ -116,7 +116,7 @@ public static void Release(IntPtr pointer) { /// Convert an IntPtr received from ISteamNetworkingSockets.ReceiveMessagesOnPollGroup into our structure. /// This is a Steamworks.NET extension. public static SteamNetworkingMessage_t FromIntPtr(IntPtr pointer) { - return (SteamNetworkingMessage_t)Marshal.PtrToStructure(pointer, typeof(SteamNetworkingMessage_t)); + return Marshal.PtrToStructure(pointer); } } }