Repository navigation
Expand file tree
/
Copy pathSDL.cs
More file actions
479 lines (422 loc) · 20.8 KB
/
Copy pathSDL.cs
File metadata and controls
479 lines (422 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#region License
/* Copyright (c) 2024-2026 Eduard Gushchin.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#endregion
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
namespace SDL3;
public static partial class SDL
{
private const string SDLLibrary = "SDL3";
/// <summary>
/// Converts a pointer to a structure of type <typeparamref name="T"/>.
/// </summary>
/// <param name="pointer">
/// The <see cref="IntPtr"/> representing the memory address of the structure.
/// </param>
/// <typeparam name="T">
/// The type of the structure to which the pointer will be converted.
/// Must be a value type (struct).
/// </typeparam>
/// <returns>
/// An instance of type <typeparamref name="T"/> containing the data from the memory location pointed to by <paramref name="pointer"/>,
/// or <see langword="null"/> if <paramref name="pointer"/> is <see cref="IntPtr.Zero"/>.
/// </returns>
/// <remarks>
/// This method is typically used when interop with unmanaged code is required, such as reading data from
/// unmanaged memory into a managed structure.
/// </remarks>
/// <seealso cref="StructureToPointer{T}"/>
/// <seealso cref="StructureArrayToPointer{T}"/>
/// <seealso cref="PointerToPointerArray"/>
/// <seealso cref="PointerToStringArray(nint)"/>
/// <seealso cref="PointerToStringArray(nint, int)"/>
/// <seealso cref="PointerToStructureArray{T}"/>
public static T? PointerToStructure<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(IntPtr pointer) where T : struct
{
return pointer == IntPtr.Zero ? null : Marshal.PtrToStructure<T>(pointer);
}
/// <summary>
/// Allocates unmanaged memory and copies the data of a structure of type <typeparamref name="T"/> into the allocated memory.
/// </summary>
/// <param name="structure">
/// The instance of the structure to copy into unmanaged memory.
/// If <paramref name="structure"/> is <see langword="null"/>, the method returns <see cref="IntPtr.Zero"/>.
/// </param>
/// <typeparam name="T">
/// The type of the structure to be converted. Must be a value type (struct).
/// </typeparam>
/// <returns>
/// An <see cref="IntPtr"/> pointing to the allocated unmanaged memory containing the structure data,
/// or <see cref="IntPtr.Zero"/> if <paramref name="structure"/> is <see langword="null"/>.
/// </returns>
/// <remarks>
/// <para>
/// This method allocates unmanaged memory using <see cref="Marshal.AllocHGlobal(int)"/> and copies the structure data
/// into the allocated memory using <see cref="Marshal.StructureToPtr(object, IntPtr, bool)"/>.
/// </para>
/// <para>
/// The caller is responsible for releasing the allocated memory by calling <see cref="Marshal.FreeHGlobal(IntPtr)"/>
/// when the memory is no longer needed to prevent memory leaks.
/// </para>
/// <para>
/// Be cautious when working with unmanaged memory, as improper memory management can lead to resource leaks
/// or application instability.
/// </para>
/// </remarks>
/// &l
10000
t;seealso cref="PointerToStructure{T}"/>
/// <seealso cref="StructureArrayToPointer{T}"/>
/// <seealso cref="PointerToPointerArray"/>
/// <seealso cref="PointerToStringArray(nint)"/>
/// <seealso cref="PointerToStringArray(nint, int)"/>
/// <seealso cref="PointerToStructureArray{T}"/>
public static IntPtr StructureToPointer<T>(T? structure) where T : struct
{
if (!structure.HasValue) return IntPtr.Zero;
var ptr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
Marshal.StructureToPtr(structure, ptr, false);
return ptr;
}
/// <summary>
/// Allocates unmanaged memory and copies the data of an array of structures of type <typeparamref name="T"/>
/// into the allocated memory.
/// </summary>
/// <param name="array">
/// The array of structures to copy into unmanaged memory.
/// If <paramref name="array"/> is <see langword="null"/> or empty, the method returns <see cref="IntPtr.Zero"/>.
/// </param>
/// <typeparam name="T">
/// The type of the structures in the array. Must be a value type (struct).
/// </typeparam>
/// <returns>
/// An <see cref="IntPtr"/> pointing to the allocated unmanaged memory containing the data of the array,
/// or <see cref="IntPtr.Zero"/> if <paramref name="array"/> is <see langword="null"/> or empty.
/// </returns>
/// <remarks>
/// <para>
/// This method allocates unmanaged memory using <see cref="Marshal.AllocHGlobal(int)"/> and copies each element
/// of the array into the allocated memory using <see cref="Marshal.StructureToPtr(object, IntPtr, bool)"/>.
/// The structures are stored contiguously in memory.
/// </para>
/// <para>
/// The caller is responsible for releasing the allocated memory by calling <see cref="Marshal.FreeHGlobal(IntPtr)"/>
/// when the memory is no longer needed to prevent memory leaks.
/// </para>
/// <para>
/// Be cautious when working with unmanaged memory, as improper memory management can lead to resource leaks
/// or application instability.
/// </para>
/// </remarks>
/// <seealso cref="PointerToStructure{T}"/>
/// <seealso cref="StructureToPointer{T}"/>
/// <seealso cref="PointerToPointerArray"/>
/// <seealso cref="PointerToStringArray(nint)"/>
/// <seealso cref="PointerToStringArray(nint, int)"/>
/// <seealso cref="PointerToStructureArray{T}"/>
public static IntPtr StructureArrayToPointer<T>(T[] array) where T : struct
{
if (array.Length == 0) return IntPtr.Zero;
var sizeOfT = Marshal.SizeOf<T>();
var unmanagedPointer = Marshal.AllocHGlobal(sizeOfT * array.Length);
for (var i = 0; i < array.Length; i++)
{
var offset = IntPtr.Add(unmanagedPointer, i * sizeOfT);
Marshal.StructureToPtr(array[i], offset, false);
}
return unmanagedPointer;
}
/// <summary>
/// Converts a block of unmanaged memory pointed to by an <see cref="IntPtr"/> into an array of <see cref="IntPtr"/> instances.
/// </summary>
/// <param name="pointer">
/// A pointer to the start of the unmanaged memory block containing the array of pointers.
/// If <paramref name="pointer"/> is <see cref="IntPtr.Zero"/>, the method returns <see langword="null"/>.
/// </param>
/// <param name="size">
/// The number of <see cref="IntPtr"/> elements in the array. If <paramref name="size"/> is 0,
/// the method returns an empty array.
/// </param>
/// <returns>
/// An array of <see cref="IntPtr"/> instances representing the pointers stored in the unmanaged memory block,
/// or <see langword="null"/> if <paramref name="pointer"/> is <see cref="IntPtr.Zero"/>.
/// </returns>
/// <remarks>
/// <para>
/// This method uses <see cref="Marshal.Copy(IntPtr, IntPtr[], int, int)"/> to copy the contents of the unmanaged
/// memory block into a managed array of <see cref="IntPtr"/>.
/// </para>
/// <para>
/// The caller is responsible for ensuring that the memory block pointed to by <paramref name="pointer"/>
/// is valid and that its size matches the specified <paramref name="size"/>.
/// </para>
/// <para>
/// Be cautious when working with unmanaged memory, as accessing invalid memory can cause application crashes or undefined behavior.
/// </para>
/// </remarks>
/// <seealso cref="PointerToStructure{T}"/>
/// <seealso cref="StructureToPointer{T}"/>
/// <seealso cref="StructureArrayToPointer{T}"/>
/// <seealso cref="PointerToStringArray(nint)"/>
/// <seealso cref="PointerToStringArray(nint, int)"/>
/// <seealso cref="PointerToStructureArray{T}"/>
public static IntPtr[]? PointerToPointerArray(IntPtr pointer, int size)
{
if (pointer == IntPtr.Zero) return null;
if (size == 0) return [];
var pointers = new IntPtr[size];
Marshal.Copy(pointer, pointers, 0, pointers.Length);
return pointers;
}
/// <summary>
/// Converts an unmanaged array of <c>null</c>-terminated UTF-8 strings, represented as an <see cref="IntPtr"/>,
/// into a managed array of <see cref="string"/>.
/// </summary>
/// <param name="pointer">
/// A pointer to the start of an unmanaged array of pointers, where each pointer refers to a <c>null</c>-terminated UTF-8 string.
/// The array is terminated by a <see cref="IntPtr.Zero"/>.
/// If <paramref name="pointer"/> is <see cref="IntPtr.Zero"/>, the method returns <see langword="null"/>.
/// </param>
/// <returns>
/// A managed array of <see cref="string"/> containing the strings from the unmanaged array,
/// or <see langword="null"/> if <paramref name="pointer"/> is <see cref="IntPtr.Zero"/> or the array is empty.
/// </returns>
/// <remarks>
/// <para>
/// This method reads an unmanaged array of pointers from the memory location specified by <paramref name="pointer"/>.
/// Each pointer is assumed to reference a <c>null</c>-terminated UTF-8 string in unmanaged memory.
/// The array of pointers is terminated by a <see cref="IntPtr.Zero"/> entry, indicating the end of the array.
/// </para>
/// <para>
/// The method uses <see cref="Marshal.ReadIntPtr(IntPtr)"/> to read pointers from the unmanaged array and
/// <see cref="Marshal.PtrToStringUTF8(IntPtr)"/> to convert each pointer to a managed string.
/// </para>
/// <para>
/// The caller is responsible for ensuring that the memory block pointed to by <paramref name="pointer"/>
/// is valid and correctly structured (i.e., an array of pointers to <c>null</c>-terminated UTF-8 strings followed by <see cref="IntPtr.Zero"/>).
/// </para>
/// <para>
/// If the unmanaged array contains only a terminating <see cref="IntPtr.Zero"/>, the method returns <see langword="null"/>.
/// </para>
/// </remarks>
/// <seealso cref="PointerToStructure{T}"/>
/// <seealso cref="StructureToPointer{T}"/>
/// <seealso cref="StructureArrayToPointer{T}"/>
/// <seealso cref="PointerToPointerArray"/>
/// <seealso cref="PointerToStringArray(nint, int)"/>
/// <seealso cref="PointerToStructureArray{T}"/>
public static string[]? PointerToStringArray(IntPtr pointer)
{
if (pointer == IntPtr.Zero) return null;
var result = new List<string>();
while (true)
{
var currentPtr = Marshal.ReadIntPtr(pointer);
if (currentPtr == IntPtr.Zero)
break;
var str = Marshal.PtrToStringUTF8(currentPtr);
if (str != null)
result.Add(str);
pointer += IntPtr.Size;
}
return result.Count > 0 ? result.ToArray() : null;
}
/// <summary>
/// Converts an unmanaged array of pointers to <c>null</c>-terminated UTF-8 strings, represented by an <see cref="IntPtr"/>,
/// into a managed array of <see cref="string"/> of a specified size.
/// </summary>
/// <param name="pointer">
/// A pointer to the start of an unmanaged array of pointers, where each pointer refers to a <c>null</c>-terminated UTF-8 string.
/// The size of the array is determined by the <paramref name="size"/> parameter.
/// If <paramref name="pointer"/> is <see cref="IntPtr.Zero"/>, the method returns <see langword="null"/>.
/// </param>
/// <param name="size">
/// The number of strings in the unmanaged array. If <paramref name="size"/> is zero, the method returns an empty array.
/// </param>
/// <returns>
/// A managed array of <see cref="string"/> containing the strings from the unmanaged array,
/// or <see langword="null"/> if <paramref name="pointer"/> is <see cref="IntPtr.Zero"/>.
/// </returns>
/// <remarks>
/// <para>
/// This method reads an unmanaged array of pointers from the memory location specified by <paramref name="pointer"/>.
/// Each pointer is assumed to reference a <c>null</c>-terminated UTF-8 string in unmanaged memory.
/// The number of elements in the array is specified by the <paramref name="size"/> parameter.
/// </para>
/// <para>
/// The method uses <see cref="Marshal.Copy(IntPtr, IntPtr[], int, int)"/> to copy the pointers from unmanaged memory
/// into a managed array of <see cref="IntPtr"/>. Then, <see cref="Marshal.PtrToStringUTF8(IntPtr)"/> is used to convert
/// each pointer to a managed string.
/// </para>
/// <para>
/// If the array contains any invalid pointers or strings that are not properly <c>null</c>-terminated, the behavior is undefined.
/// </para>
/// <para>
/// The caller is responsible for ensuring that the memory block pointed to by <paramref name="pointer"/>
/// is valid and correctly structured (i.e., an array of pointers to <c>null</c>-terminated UTF-8 strings).
/// </para>
/// </remarks>
/// <seealso cref="PointerToStructure{T}"/>
/// <seealso cref="StructureToPointer{T}"/>
/// <seealso cref="StructureArrayToPointer{T}"/>
/// <seealso cref="PointerToPointerArray"/>
/// <seealso cref="PointerToStringArray(nint)"/>
/// <seealso cref="PointerToStructureArray{T}"/>
public static string[]? PointerToStringArray(IntPtr pointer, int size)
{
if (pointer == IntPtr.Zero) return null;
if (size == 0) return [];
var result = new string[size];
var ptrArray = new IntPtr[size];
Marshal.Copy(pointer, ptrArray, 0, size);
for (var i = 0; i < size; i++)
{
result[i] = Marshal.PtrToStringUTF8(ptrArray[i])!;
}
CB47
return result;
}
/// <summary>
/// Converts a managed UTF-16 string to an unmanaged, <c>null</c>-terminated UTF-8 string pointer.
/// </summary>
/// <param name="str">The managed string to convert. Can be <c>null</c>.</param>
/// <returns>
/// A pointer to unmanaged memory containing the <c>null</c>-terminated UTF-8 encoded version of the input string,
/// or <see cref="IntPtr.Zero"/> if <paramref name="str"/> is <c>null</c>.
/// </returns>
/// <remarks>
/// The returned pointer must be freed manually using <see cref="Marshal.FreeHGlobal(IntPtr)"/>
/// to avoid memory leaks. If the returned pointer is <see cref="IntPtr.Zero"/>, no deallocation is needed.
/// </remarks>
public static IntPtr StringToPointer(string? str)
{
if (str == null) return IntPtr.Zero;
var utf8Bytes = System.Text.Encoding.UTF8.GetBytes(str + '\0');
var unmanagedPointer = Marshal.AllocHGlobal(utf8Bytes.Length);
Marshal.Copy(utf8Bytes, 0, unmanagedPointer, utf8Bytes.Length);
return unmanagedPointer;
}
/// <summary>
/// Converts a unmanaged pointer to an UTF-8 string
/// </summary>
public static string? PointerToString(IntPtr pointer)
{
return Marshal.PtrToStringUTF8(pointer);
}
/// <summary>
/// Converts an unmanaged array of pointers (or raw memory) to a managed array of structures.
/// </summary>
/// <param name="pointer">
/// A pointer to the start of an unmanaged block of memory. This block should contain a sequence of structures
/// or a series of pointers to structures. If <paramref name="pointer"/> is <see cref="IntPtr.Zero"/> or invalid,
/// the method will return <see langword="null"/>.
/// </param>
/// <param name="count">
/// The number of structures in the unmanaged memory block. If <paramref name="count"/> is zero, the method
/// will return an empty array. If <paramref name="count"/> is negative, the method will return <see langword="null"/>.
/// </param>
/// <typeparam name="T">
/// The type of the structure. This type must be a value type (i.e., a struct).
/// </typeparam>
/// <returns>
/// A managed array of <typeparamref name="T"/> structures. Returns <see langword="null"/> if the <paramref name="pointer"/>
/// is invalid or if <paramref name="count"/> is less than 0.
/// </returns>
/// <remarks>
/// This method assumes that the unmanaged memory block pointed to by <paramref name="pointer"/> contains either:
/// 1. A raw memory block representing a contiguous array of structures.
/// 2. A set of pointers (as an array of <see cref="IntPtr"/>) to the individual structures.
///
/// If the structures are of a primitive type (e.g., <see cref="int"/>, <see cref="float"/>), the method uses a more efficient
/// memory copy operation. For non-primitive types, the method will iterate over the array and use <see cref="Marshal.PtrToStructure{T}(IntPtr)"/>
/// to convert each pointer into its corresponding structure.
/// </remarks>
/// <seealso cref="PointerToStructure{T}"/>
/// <seealso cref="StructureToPointer{T}"/>
/// <seealso cref="StructureArrayToPointer{T}"/>
/// <seealso cref="PointerToPointerArray"/>
/// <seealso cref="PointerToStringArray(nint)"/>
/// <seealso cref="PointerToStringArray(nint, int)"/>
public static unsafe T[]? PointerToStructureArray<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(IntPtr pointer, int count) where T : struct
{
if (pointer == IntPtr.Zero || count < 0) return null;
if (count == 0) return [];
var array = new T[count];
if (typeof(T).IsPrimitive)
{
new Span<T>((void*)pointer, count).CopyTo(new Span<T>(array, 0, count));
}
else
{
var sizePtr = IntPtr.Size;
for (var i = 0; i < count; i++)
{
var elementPtr = Marshal.ReadIntPtr(pointer, i * sizePtr);
array[i] = Marshal.PtrToStructure<T>(elementPtr);
}
}
return array;
}
/// <summary>
/// Allocates unmanaged memory for an array of UTF-8 string pointers (char*).
/// Each string is converted via <see cref="StringToPointer(string?)"/>.
/// — The array is terminated with a trailing <see cref="IntPtr.Zero"/>.
/// </summary>
/// <param name="array">
/// — The managed array of strings. Can be <c>null</c> or empty.
/// </param>
/// <returns>
/// — A pointer to unmanaged memory containing an array of <see cref="IntPtr"/> pointers,
/// — each pointing to a UTF-8 <c>null</c>-terminated string.
/// — Returns <see cref="IntPtr.Zero"/> for <c>null</c> or empty arrays.
/// </returns>
/// <remarks>
/// The caller is responsible for freeing all allocated memory:
/// 1. Free each individual string pointer using <see cref="Marshal.FreeHGlobal"/>.
/// 2. Free the returned array pointer using <see cref="Marshal.FreeHGlobal"/>.
/// Failure to do so will cause memory leaks.
/// </remarks>
public static IntPtr StringArrayToPointer(string[]? array)
{
unsafe
{
if (array == null || array.Length == 0)
return IntPtr.Zero;
var total = array.Length + 1;
var size = IntPtr.Size * total;
var blockPtr = Marshal.AllocHGlobal(size);
var zeroInit = new Span<byte>((void*)blockPtr, size);
zeroInit.Clear();
for (var i = 0; i < array.Length; i++)
{
var strPtr = StringToPointer(array[i]);
Marshal.WriteIntPtr(blockPtr, i * IntPtr.Size, strPtr);
}
Marshal.WriteIntPtr(blockPtr, array.Length * IntPtr.Size, IntPtr.Zero);
return blockPtr;
}
}
/// <summary>
/// Indicates that a method is a <c>#define</c> macro.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class MacroAttribute : Attribute;
}