Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Helpers.cs
Go to the documentation of this file.
1using System;
3using System.IO;
5using Microsoft.Xna.Framework.Audio;
6
8
9internal static class Helpers
10{
11 public const uint InvalidHandle = uint.MaxValue;
12
13 public const int MaximumStringLength = 260;
14
15 public const int Guide_MessageBox_MaxButtons = 3;
16
17 public const int MaxNumberOfSignedInPlayers = 1;
18
19 internal const int PlayerAnyIndex = 255;
20
21 private static object KeepKernelReturnCode = typeof(KernelReturnCode);
22
23 public static bool Succeeded(int error)
24 {
25 return error >= 0;
26 }
27
28 public static bool Succeeded(ErrorCodes error)
29 {
30 return (int)error >= 0;
31 }
32
33 public static bool Failed(int error)
34 {
35 return error < 0;
36 }
37
38 public static bool Failed(ErrorCodes error)
39 {
40 return (int)error < 0;
41 }
42
43 public unsafe static int SmartGetHashCode(object obj)
44 {
45 GCHandle gCHandle = GCHandle.Alloc(obj, GCHandleType.Pinned);
46 try
47 {
48 int num = Marshal.SizeOf(obj);
49 int num2 = 0;
50 int num3 = 0;
51 int* ptr = (int*)gCHandle.AddrOfPinnedObject().ToPointer();
52 while (num2 + 4 <= num)
53 {
54 num3 ^= *ptr;
55 num2 += 4;
56 ptr++;
57 }
58 return (num3 == 0) ? int.MaxValue : num3;
59 }
60 finally
61 {
62 gCHandle.Free();
63 }
64 }
65
66 public static void ValidateCopyParameters(int dataLength, int dataIndex, int elementCount)
67 {
68 if (dataIndex < 0 || dataIndex > dataLength)
69 {
71 }
72 if (elementCount + dataIndex > dataLength)
73 {
75 }
76 if (elementCount <= 0)
77 {
79 }
80 }
81
82 public static uint GetSizeOf<T>() where T : struct
83 {
84 return (uint)Marshal.SizeOf(typeof(T));
85 }
86
87 public static void ThrowExceptionFromErrorCode(ErrorCodes error)
88 {
89 if (Failed(error))
90 {
91 throw GetExceptionFromResult((uint)error);
92 }
93 }
94
95 public static void ThrowExceptionFromErrorCode(int error)
96 {
97 if (Failed(error))
98 {
99 throw GetExceptionFromResult((uint)error);
100 }
101 }
102
103 public static void ThrowExceptionFromResult(uint result)
104 {
105 if (result == 0)
106 {
107 return;
108 }
109 throw GetExceptionFromResult(result);
110 }
111
112 [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
113 [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly")]
114 [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
115 public static Exception GetExceptionFromResult(uint result)
116 {
117 if (result == 0)
118 {
119 return null;
120 }
121 switch (result)
122 {
123 case 2289436696u:
125 case 2289436774u:
127 case 2289436775u:
129 case 2289436701u:
131 case 2289436786u:
133 case 2328297475u:
135 case 2328297478u:
137 case 2328297494u:
139 case 2328297482u:
141 case 2328297485u:
143 case 2328297486u:
145 case 2328297487u:
147 case 2328297488u:
149 case 2328297479u:
151 case 2328297483u:
153 case 2328297484u:
155 case 2328297490u:
157 case 2328297491u:
159 case 2328297492u:
161 case 2328297495u:
162 return new NoAudioHardwareException();
163 case 2328297496u:
165 case 2328297480u:
166 case 2343370753u:
167 return new InstancePlayLimitException(FrameworkResources.InstancePlayFailedDueToLimit);
168 case 2147746390u:
170 case 2364407809u:
171 return new NoMicrophoneConnectedException();
172 case 2147500033u:
173 return new NotImplementedException();
174 case 2147942487u:
175 return new ArgumentException();
176 case 2147942405u:
177 return new UnauthorizedAccessException();
178 case 2147942414u:
179 throw new OutOfMemoryException();
180 case 2147500036u:
182 default:
184 }
185 }
186
187 [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
188 public static void CheckDisposed(object obj, IntPtr pComPtr)
189 {
190 if (pComPtr == IntPtr.Zero)
191 {
192 throw new ObjectDisposedException(obj.GetType().Name);
193 }
194 }
195
196 public static void ValidateOrientation(DisplayOrientation orientation)
197 {
198 if (orientation != 0 && orientation != DisplayOrientation.LandscapeLeft && orientation != DisplayOrientation.LandscapeRight && orientation != DisplayOrientation.Portrait)
199 {
201 }
202 }
203
204 public static bool IsLandscape(DisplayOrientation orientation)
205 {
206 return (orientation & (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight)) != 0;
207 }
208
209 public static DisplayOrientation ChooseOrientation(DisplayOrientation orientation, int width, int height, bool allowLandscapeLeftAndRight)
210 {
211 if ((orientation & (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.Portrait)) != 0)
212 {
213 return orientation;
214 }
215 if (width > height)
216 {
217 if (allowLandscapeLeftAndRight)
218 {
219 return DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
220 }
221 return DisplayOrientation.LandscapeLeft;
222 }
223 return DisplayOrientation.Portrait;
224 }
225}
static bool Failed(ErrorCodes error)
Definition Helpers.cs:38
static unsafe int SmartGetHashCode(object obj)
Definition Helpers.cs:43
static bool Succeeded(ErrorCodes error)
Definition Helpers.cs:28
static object KeepKernelReturnCode
Definition Helpers.cs:21
static bool Succeeded(int error)
Definition Helpers.cs:23
static void CheckDisposed(object obj, IntPtr pComPtr)
Definition Helpers.cs:188
static void ThrowExceptionFromErrorCode(int error)
Definition Helpers.cs:95
static Exception GetExceptionFromResult(uint result)
Definition Helpers.cs:115
static void ThrowExceptionFromErrorCode(ErrorCodes error)
Definition Helpers.cs:87
static bool IsLandscape(DisplayOrientation orientation)
Definition Helpers.cs:204
static void ValidateCopyParameters(int dataLength, int dataIndex, int elementCount)
Definition Helpers.cs:66
const int Guide_MessageBox_MaxButtons
Definition Helpers.cs:15
static void ValidateOrientation(DisplayOrientation orientation)
Definition Helpers.cs:196
static bool Failed(int error)
Definition Helpers.cs:33
static void ThrowExceptionFromResult(uint result)
Definition Helpers.cs:103
static DisplayOrientation ChooseOrientation(DisplayOrientation orientation, int width, int height, bool allowLandscapeLeftAndRight)
Definition Helpers.cs:209
static int SizeOf(object structure)
Definition Marshal.cs:697
static readonly IntPtr Zero
Definition IntPtr.cs:18
unsafe void * ToPointer()
Definition IntPtr.cs:210
static GCHandle Alloc(object? value)
Definition GCHandle.cs:81