Skip to content

Commit baae79f

Browse files
committedSep 25, 2017
Updated C# API
Mostly just summary cleanup. Fixes LogF and replaces it to use C# string formating instead of C++ style. Also adds some log overloads.
1 parent 25c3e74 commit baae79f

File tree

1 file changed

+169
-38
lines changed

1 file changed

+169
-38
lines changed
 

‎API/RainmeterAPI.cs

+169-38
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public API(IntPtr rm)
2222
m_Rm = rm;
2323
}
2424

25+
static public implicit operator API(IntPtr rm)
26+
{
27+
return new Rainmeter.API(rm);
28+
}
29+
2530
[DllImport("Rainmeter.dll", CharSet = CharSet.Unicode)]
2631
private extern static IntPtr RmReadString(IntPtr rm, string option, string defValue, bool replaceMeasures);
2732

@@ -34,21 +39,35 @@ public API(IntPtr rm)
3439
[DllImport("Rainmeter.dll", CharSet = CharSet.Unicode)]
3540
private extern static IntPtr RmPathToAbsolute(IntPtr rm, string relativePath);
3641

42+
/// <summary>
43+
/// Executes a command
44+
/// </summary>
45+
/// <param name="skin">Pointer to current skin (See API.GetSkin)</param>
46+
/// <param name="command">Bang to execute</param>
47+
/// <returns>No return type</returns>
48+
/// <example>
49+
/// <code>
50+
/// [DllExport]
51+
/// internal double Update(IntPtr data)
52+
/// {
53+
/// Measure measure = (Measure)data;
54+
/// Rainmeter.API.Execute(measure->skin, "!SetVariable SomeVar 10"); // 'measure->skin' stored previously in the Initialize function
55+
/// return 0.0;
56+
/// }
57+
/// </code>
58+
/// </example>
3759
[DllImport("Rainmeter.dll", EntryPoint = "RmExecute", CharSet = CharSet.Unicode)]
3860
public extern static void Execute(IntPtr skin, string command);
3961

4062
[DllImport("Rainmeter.dll")]
4163
private extern static IntPtr RmGet(IntPtr rm, RmGetType type);
4264

4365
[DllImport("Rainmeter.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
44-
private extern static int LSLog(LogType type, string unused, string message);
66+
private extern static int LSLog(int type, string unused, string message);
4567

4668
[DllImport("Rainmeter.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
4769
private extern static int RmLog(IntPtr rm, LogType type, string message);
4870

49-
[DllImport("Rainmeter.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
50-
private extern static int RmLogF(IntPtr rm, LogType type, string format, params string[] message);
51-
5271
private enum RmGetType
5372
{
5473
MeasureName = 0,
@@ -75,10 +94,13 @@ public enum LogType
7594
/// <returns>Returns the option value as a string</returns>
7695
/// <example>
7796
/// <code>
78-
/// internal void Reload(Rainmeter.API rm, ref double maxValue)
97+
/// [DllExport]
98+
/// public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
7999
/// {
80-
/// string value = rm.ReadString("Value", "DefaultValue");
81-
/// string action = rm.ReadString("Action", "", false); // [MeasureNames] will be parsed/replaced when the action is executed with RmExecute
100+
/// Measure measure = (Measure)data;
101+
/// Rainmeter.API api = (Rainmeter.API)rm;
102+
/// string value = api.ReadString("Value", "DefaultValue");
103+
/// string action = api.ReadString("Action", "", false); // [MeasureNames] will be parsed/replaced when the action is executed with RmExecute
82104
/// }
83105
/// </code>
84106
/// </example>
@@ -95,9 +117,12 @@ public string ReadString(string option, string defValue, bool replaceMeasures =
95117
/// <returns>Returns the absolute path of the option value as a string</returns>
96118
/// <example>
97119
/// <code>
98-
/// internal void Reload(Rainmeter.API rm, ref double maxValue)
120+
/// [DllExport]
121+
/// public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
99122
/// {
100-
/// string path = rm.ReadPath("MyPath", "C:\\");
123+
/// Measure measure = (Measure)data;
124+
/// Rainmeter.API api = (Rainmeter.API)rm;
125+
/// string path = api.ReadPath("MyPath", "C:\\");
101126
/// }
102127
/// </code>
103128
/// </example>
@@ -115,9 +140,12 @@ public string ReadPath(string option, string defValue)
115140
/// <returns>Returns the option value as a double</returns>
116141
/// <example>
117142
/// <code>
118-
/// internal void Reload(Rainmeter.API rm, ref double maxValue)
143+
/// [DllExport]
144+
/// public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
119145
/// {
120-
/// double value = rm.ReadDouble("Value", 20.0);
146+
/// Measure measure = (Measure)data;
147+
/// Rainmeter.API api = (Rainmeter.API)rm;
148+
/// double value = api.ReadDouble("Value", 20.0);
121149
/// }
122150
/// </code>
123151
/// </example>
@@ -135,9 +163,12 @@ public double ReadDouble(string option, double defValue)
135163
/// <returns>Returns the option value as an integer</returns>
136164
/// <example>
137165
/// <code>
138-
/// internal void Reload(Rainmeter.API rm, ref double maxValue)
166+
/// [DllExport]
167+
/// public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
139168
/// {
140-
/// int value = rm.ReadInt("Value", 20);
169+
/// Measure measure = (Measure)data;
170+
/// Rainmeter.API api = (Rainmeter.API)rm;
171+
/// int value = api.ReadInt("Value", 20);
141172
/// }
142173
/// </code>
143174
/// </example>
@@ -153,9 +184,11 @@ public int ReadInt(string option, int defValue)
153184
/// <returns>Returns a string replacing any variables in the 'str'</returns>
154185
/// <example>
155186
/// <code>
156-
/// internal double Update()
187+
/// [DllExport]
188+
/// public static double Update(IntPtr data)
157189
/// {
158-
/// string myVar = ReplaceVariables("#MyVar#").ToUpperInvariant();
190+
/// Measure measure = (Measure)data;
191+
/// string myVar = measure.api.ReplaceVariables("#MyVar#").ToUpperInvariant(); // 'measure.api' stored previously in the Initialize function
159192
/// if (myVar == "SOMETHING") { return 1.0; }
160193
/// return 0.0;
161194
/// }
@@ -173,9 +206,13 @@ public string ReplaceVariables(string str)
173206
/// <returns>Returns the current measure name as a string</returns>
174207
/// <example>
175208
/// <code>
176-
/// internal void Initialize(Rainmeter.API rm)
209+
/// [DllExport]
210+
/// public static void Initialize(ref IntPtr data, IntPtr rm)
177211
/// {
178-
/// myName = GetMeasureName(); // declare 'myName' as a string in class scope
212+
/// Measure measure = new Measure();
213+
/// Rainmeter.API api = (Rainmeter.API)rm;
214+
/// measure.myName = api.GetMeasureName(); // declare 'myName' as a string in measure class
215+
/// data = GCHandle.ToIntPtr(GCHandle.Alloc(measure));
179216
/// }
180217
/// </code>
181218
/// </example>
@@ -191,9 +228,13 @@ public string GetMeasureName()
191228
/// <returns>Returns an IntPtr to the current skin</returns>
192229
/// <example>
193230
/// <code>
194-
/// internal void Initialize(Rainmeter.API rm)
231+
/// [DllExport]
232+
/// public static void Initialize(ref IntPtr data, IntPtr rm)
195233
/// {
196-
/// mySkin = GetSkin(); // declare 'mySkin' as a IntPtr in class scope
234+
/// Measure measure = new Measure();
235+
/// Rainmeter.API api = (Rainmeter.API)rm;
236+
/// measure.mySkin = api.GetSkin(); // declare 'mySkin' as a IntPtr in measure class
237+
/// data = GCHandle.ToIntPtr(GCHandle.Alloc(measure));
197238
/// }
198239
/// </code>
199240
/// </example>
@@ -203,21 +244,23 @@ public IntPtr GetSkin()
203244
}
204245

205246
/// <summary>
206-
/// Retrieves a path to the Rainmeter data file (Rainmeter.data).
247+
/// Retrieves a path to the Rainmeter data file (Rainmeter.data)
207248
/// </summary>
208249
/// <remarks>Call GetSettingsFile() in the Initialize function and store the results for later use</remarks>
209250
/// <returns>Returns the path and filename of the Rainmeter data file as a string</returns>
210251
/// <example>
211252
/// <code>
212-
/// internal void Initialize(Rainmeter.API rm)
253+
/// public static void Initialize(ref IntPtr data, IntPtr rm)
213254
/// {
214-
/// if (rmDataFile == null) { rmDataFile = GetSettingsFile(); } // declare 'rmDataFile' as a string in global scope
255+
/// data = GCHandle.ToIntPtr(GCHandle.Alloc(new Measure()));
256+
/// Rainmeter.API api = (Rainmeter.API)rm;
257+
/// if (rmDataFile == null) { rmDataFile = API.GetSettingsFile(); } // declare 'rmDataFile' as a string in global scope
215258
/// }
216259
/// </code>
217260
/// </example>
218-
public string GetSettingsFile()
261+
public static string GetSettingsFile()
219262
{
220-
return Marshal.PtrToStringUni(RmGet(m_Rm, RmGetType.SettingsFile));
263+
return Marshal.PtrToStringUni(RmGet(IntPtr.Zero, RmGetType.SettingsFile));
221264
}
222265

223266
/// <summary>
@@ -227,9 +270,13 @@ public string GetSettingsFile()
227270
/// <returns>Returns the path and filename of the skin as a string</returns>
228271
/// <example>
229272
/// <code>
230-
/// internal void Initialize(Rainmeter.API rm)
273+
/// [DllExport]
274+
/// public static void Initialize(ref IntPtr data, IntPtr rm)
231275
/// {
232-
/// skinName = GetSkinName(); } // declare 'skinName' as a string in class scope
276+
/// Measure measure = new Measure();
277+
/// Rainmeter.API api = (Rainmeter.API)rm;
278+
/// measure.skinName = api.GetSkinName(); } // declare 'skinName' as a string in measure class
279+
/// data = GCHandle.ToIntPtr(GCHandle.Alloc(measure));
233280
/// }
234281
/// </code>
235282
/// </example>
@@ -238,16 +285,41 @@ public string GetSkinName()
238285
return Marshal.PtrToStringUni(RmGet(m_Rm, RmGetType.SkinName));
239286
}
240287

288+
/// <summary>
289+
/// Executes a command auto getting the skin reference
290+
/// </summary>
291+
/// <param name="command">Bang to execute</param>
292+
/// <returns>No return type</returns>
293+
/// <example>
294+
/// <code>
295+
/// [DllExport]
296+
/// public static double Update(IntPtr data)
297+
/// {
298+
/// Measure measure = (Measure)data;
299+
/// measure.api.Execute("!SetVariable SomeVar 10"); // 'measure.api' stored previously in the Initialize function
300+
/// return 0.0;
301+
/// }
302+
/// </code>
303+
/// </example>
304+
public void Execute(string command)
305+
{
306+
Execute(this.GetSkin(), command);
307+
}
308+
241309
/// <summary>
242310
/// Returns a pointer to the handle of the skin window (HWND)
243311
/// </summary>
244312
/// <remarks>Call GetSkinWindow() in the Initialize function and store the results for later use</remarks>
245313
/// <returns>Returns a handle to the skin window as a IntPtr</returns>
246314
/// <example>
247315
/// <code>
316+
/// [DllExport]
248317
/// internal void Initialize(Rainmeter.API rm)
249318
/// {
250-
/// skinWindow = GetSkinWindow(); } // declare 'skinWindow' as a IntPtr in class scope
319+
/// Measure measure = new Measure();
320+
/// Rainmeter.API api = (Rainmeter.API)rm;
321+
/// measure.skinWindow = api.GetSkinWindow(); } // declare 'skinWindow' as a IntPtr in measure class
322+
/// data = GCHandle.ToIntPtr(GCHandle.Alloc(measure));
251323
/// }
252324
/// </code>
253325
/// </example>
@@ -257,9 +329,9 @@ public IntPtr GetSkinWindow()
257329
}
258330

259331
/// <summary>
260-
/// DEPRECATED: Use Log(rm, type, message). Sends a message to the Rainmeter log.
332+
/// DEPRECATED: Save your rm or api reference and use Log(rm, type, message). Sends a message to the Rainmeter log with no source.
261333
/// </summary>
262-
public static void Log(LogType type, string message)
334+
public static void Log(int type, string message)
263335
{
264336
LSLog(type, null, message);
265337
}
@@ -268,40 +340,98 @@ public static void Log(LogType type, string message)
268340
/// Sends a message to the Rainmeter log with source
269341
/// </summary>
270342
/// <remarks>LOG_DEBUG messages are logged only when Rainmeter is in debug mode</remarks>
271-
/// <param name="type">Log type (LOG_ERROR, LOG_WARNING, LOG_NOTICE, or LOG_DEBUG)</param>
343+
/// <param name="rm">Pointer to the plugin measure</param>
344+
/// <param name="type">Log type, use API.LogType enum (Error, Warning, Notice, or Debug)</param>
272345
/// <param name="message">Message to be logged</param>
273346
/// <returns>No return type</returns>
274347
/// <example>
275348
/// <code>
276-
/// Log(rm, LOG_NOTICE, "I am a 'notice' log message with a source");
349+
/// Rainmeter.API.Log(rm, API.LogType.Notice, "I am a 'notice' log message with a source");
277350
/// </code>
278351
/// </example>
279352
public static void Log(IntPtr rm, LogType type, string message)
280353
{
281354
RmLog(rm, type, message);
282355
}
283356

357+
/// <summary>
284358
/// <summary>
285359
/// Sends a formatted message to the Rainmeter log
286360
/// </summary>
287361
/// <remarks>LOG_DEBUG messages are logged only when Rainmeter is in debug mode</remarks>
288362
/// <param name="rm">Pointer to the plugin measure</param>
289-
/// <param name="level">Log level (LOG_ERROR, LOG_WARNING, LOG_NOTICE, or LOG_DEBUG)</param>
290-
/// <param name="format">Formatted message to be logged, follows printf syntax</param>
363+
/// <param name="type">Log type, use API.LogType enum (Error, Warning, Notice, or Debug)</param>
364+
/// <param name="format">Formatted message to be logged, follows string.Format syntax</param>
291365
/// <param name="args">Comma separated list of args referenced in the formatted message</param>
292366
/// <returns>No return type</returns>
293367
/// <example>
294368
/// <code>
295-
/// string notice = "notice";
296-
/// LogF(rm, LOG_NOTICE, "I am a '%s' log message with a source", notice);
369+
/// [DllExport]
370+
/// public static double Update(IntPtr data)
371+
/// {
372+
/// Measure measure = (Measure)data;
373+
/// string notice = "notice";
374+
/// measure.api.LogF(measure.rm, API.LogType.Notice, "I am a '{0}' log message with a source", notice); // 'measure.rm' stored previously in the Initialize function
375+
///
376+
/// return 0.0;
377+
/// }
297378
/// </code>
298379
/// </example>
299-
public static void LogF(IntPtr rm, LogType type, string format, params string[] args)
380+
public static void LogF(IntPtr rm, LogType type, string format, params Object[] args)
300381
{
301-
RmLogF(rm, type, format, args);
382+
RmLog(rm, type, string.Format(format, args));
383+
}
384+
385+
/// <summary>
386+
/// Sends a message to the Rainmeter log with source
387+
/// </summary>
388+
/// <remarks>LOG_DEBUG messages are logged only when Rainmeter is in debug mode</remarks>
389+
/// <param name="type">Log type, use API.LogType enum (Error, Warning, Notice, or Debug)</param>
390+
/// <param name="message">Message to be logged</param>
391+
/// <returns>No return type</returns>
392+
/// <example>
393+
/// <code>
394+
/// [DllExport]
395+
/// public static double Update(IntPtr data)
396+
/// {
397+
/// Measure measure = (Measure)data;
398+
/// measure.api.Log(api, API.LogType.Notice, "I am a 'notice' log message with a source"); // 'measure.api' stored previously in the Initialize function
399+
///
400+
/// return 0.0;
401+
/// }
402+
/// </code>
403+
/// </example>
404+
public void Log(LogType type, string message)
405+
{
406+
RmLog(this.m_Rm, type, message);
302407
}
303-
}
304408

409+
/// <summary>
410+
/// Sends a formatted message to the Rainmeter log
411+
/// </summary>
412+
/// <remarks>LOG_DEBUG messages are logged only when Rainmeter is in debug mode</remarks>
413+
/// <param name="type">Log type, use API.LogType enum (Error, Warning, Notice, or Debug)</param>
414+
/// <param name="format">Formatted message to be logged, follows string.Format syntax</param>
415+
/// <param name="args">Comma separated list of args referenced in the formatted message</param>
416+
/// <returns>No return type</returns>
417+
/// <example>
418+
/// <code>
419+
/// [DllExport]
420+
/// public static double Update(IntPtr data)
421+
/// {
422+
/// Measure measure = (Measure)data;
423+
/// string notice = "notice";
424+
/// measure.api.LogF(API.LogType.Notice, "I am a '{0}' log message with a source", notice); // 'measure.api' stored previously in the Initialize function
425+
///
426+
/// return 0.0;
427+
/// }
428+
/// </code>
429+
/// </example>
430+
public void LogF(LogType type, string format, params Object[] args)
431+
{
432+
RmLog(this.m_Rm, type, string.Format(format, args));
433+
}
434+
}
305435
/// <summary>
306436
/// Dummy attribute to mark method as exported for DllExporter.exe.
307437
/// </summary>
@@ -310,6 +440,7 @@ public class DllExport : Attribute
310440
{
311441
public DllExport()
312442
{
443+
313444
}
314445
}
315446
}

0 commit comments

Comments
 (0)
Please sign in to comment.