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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Events;
using StardewModdingAPI.Framework.Reflection;
using StardewValley;
using StardewValley.Menus;
namespace StardewModdingAPI.Framework
{
/// <summary>Provides extension methods for SMAPI's internal use.</summary>
internal static class InternalExtensions
{
/****
** IMonitor
****/
/// <summary>Log a message for the player or developer the first time it occurs.</summary>
/// <param name="monitor">The monitor through which to log the message.</param>
/// <param name="hash">The hash of logged messages.</param>
/// <param name="message">The message to log.</param>
/// <param name="level">The log severity level.</param>
public static void LogOnce(this IMonitor monitor, HashSet<string> hash, string message, LogLevel level = LogLevel.Trace)
{
if (!hash.Contains(message))
{
monitor.Log(message, level);
hash.Add(message);
}
}
/****
** IModMetadata
****/
/// <summary>Log a message using the mod's monitor.</summary>
/// <param name="metadata">The mod whose monitor to use.</param>
/// <param name="message">The message to log.</param>
/// <param name="level">The log severity level.</param>
public static void LogAsMod(this IModMetadata metadata, string message, LogLevel level = LogLevel.Trace)
{
metadata.Monitor.Log(message, level);
}
/****
** ManagedEvent
****/
/// <summary>Raise the event using the default event args and notify all handlers.</summary>
/// <typeparam name="TEventArgs">The event args type to construct.</typeparam>
/// <param name="event">The event to raise.</param>
public static void RaiseEmpty<TEventArgs>(this ManagedEvent<TEventArgs> @event) where TEventArgs : new()
{
@event.Raise(Singleton<TEventArgs>.Instance);
}
/****
** Exceptions
****/
/// <summary>Get a string representation of an exception suitable for writing to the error log.</summary>
/// <param name="exception">The error to summarize.</param>
public static string GetLogSummary(this Exception exception)
{
switch (exception)
{
case TypeLoadException ex:
return $"Failed loading type '{ex.TypeName}': {exception}";
case ReflectionTypeLoadException ex:
string summary = exception.ToString();
foreach (Exception childEx in ex.LoaderExceptions)
summary += $"\n\n{childEx.GetLogSummary()}";
return summary;
default:
return exception.ToString();
}
}
/// <summary>Get the lowest exception in an exception stack.</summary>
/// <param name="exception">The exception from which to search.</param>
public static Exception GetInnermostException(this Exception exception)
{
while (exception.InnerException != null)
exception = exception.InnerException;
return exception;
}
/****
** ReaderWriterLockSlim
****/
/// <summary>Run code within a read lock.</summary>
/// <param name="lock">The lock to set.</param>
/// <param name="action">The action to perform.</param>
public static void InReadLock(this ReaderWriterLockSlim @lock, Action action)
{
@lock.EnterReadLock();
try
{
action();
}
finally
{
@lock.ExitReadLock();
}
}
/// <summary>Run code within a read lock.</summary>
/// <typeparam name="TReturn">The action's return value.</typeparam>
/// <param name="lock">The lock to set.</param>
/// <param name="action">The action to perform.</param>
public static TReturn InReadLock<TReturn>(this ReaderWriterLockSlim @lock, Func<TReturn> action)
{
@lock.EnterReadLock();
try
{
return action();
}
finally
{
@lock.ExitReadLock();
}
}
/// <summary>Run code within a write lock.</summary>
/// <param name="lock">The lock to set.</param>
/// <param name="action">The action to perform.</param>
public static void InWriteLock(this ReaderWriterLockSlim @lock, Action action)
{
@lock.EnterWriteLock();
try
{
action();
}
finally
{
@lock.ExitWriteLock();
}
}
/// <summary>Run code within a write lock.</summary>
/// <typeparam name="TReturn">The action's return value.</typeparam>
/// <param name="lock">The lock to set.</param>
/// <param name="action">The action to perform.</param>
public static TReturn InWriteLock<TReturn>(this ReaderWriterLockSlim @lock, Func<TReturn> action)
{
@lock.EnterWriteLock();
try
{
return action();
}
finally
{
@lock.ExitWriteLock();
}
}
/****
** IActiveClickableMenu
****/
/// <summary>Get a string representation of the menu chain to the given menu (including the specified menu), in parent to child order.</summary>
/// <param name="menu">The menu whose chain to get.</param>
public static string GetMenuChainLabel(this IClickableMenu menu)
{
static IEnumerable<IClickableMenu> GetAncestors(IClickableMenu menu)
{
for (; menu != null; menu = menu.GetParentMenu())
yield return menu;
}
return string.Join(" > ", GetAncestors(menu).Reverse().Select(p => p.GetType().FullName));
}
/****
** Sprite batch
****/
/// <summary>Get whether the sprite batch is between a begin and end pair.</summary>
/// <param name="spriteBatch">The sprite batch to check.</param>
/// <param name="reflection">The reflection helper with which to access private fields.</param>
public static bool IsOpen(this SpriteBatch spriteBatch, Reflector reflection)
{
// get field name
const string fieldName =
#if SMAPI_FOR_WINDOWS
"inBeginEndPair";
#else
"_beginCalled";
#endif
// get result
return reflection.GetField<bool>(Game1.spriteBatch, fieldName).GetValue();
}
}
}
|