-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceCode.cs
125 lines (109 loc) · 3.89 KB
/
SourceCode.cs
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
using System;
using System.IO;
using System.Runtime.InteropServices;
using Rainmeter;
namespace PluginSkinFound
{
public class Measure
{
private string[] skinNames;
private string skinsFolderPath;
private string outputFormat = "Whole";
public void Reload(API api)
{
string skinNamesSetting = api.ReadString("SkinName", "").Trim();
outputFormat = api.ReadString("Output", "Whole").Trim();
// Retrieve the Skins directory path from the #SKINSPATH# variable
skinsFolderPath = api.ReplaceVariables("#SKINSPATH#").Trim();
if (string.IsNullOrEmpty(skinNamesSetting))
{
api.Log(API.LogType.Error, "SkinFound.dll: No SkinName provided.");
skinNames = Array.Empty<string>();
}
else
{
// Split skin names and remove any extra spaces around them
skinNames = skinNamesSetting.Split('|');
for (int i = 0; i < skinNames.Length; i++)
{
skinNames[i] = skinNames[i].Trim();
}
}
}
public string CheckSkinsExist(API api)
{
if (skinNames == null || skinNames.Length == 0)
{
return "No skins specified.";
}
string result = "";
foreach (string skin in skinNames)
{
string skinPath = Path.Combine(skinsFolderPath, skin);
bool skinExists = Directory.Exists(skinPath);
if (outputFormat.Equals("NameOnly", StringComparison.OrdinalIgnoreCase))
{
if (skinExists)
{
result += $"{skin}\n";
api.Log(API.LogType.Debug, $"SkinFound.dll: {skin} is available.");
}
}
else // Whole format
{
result += $"{skin}: {(skinExists ? 1 : 0)}\n";
api.Log(API.LogType.Debug, $"SkinFound.dll: {skin} is {(skinExists ? "available" : "NOT available")}.");
}
}
return result.TrimEnd('\n');
}
}
public static class Plugin
{
static IntPtr stringBuffer = IntPtr.Zero;
[DllExport]
public static void Initialize(ref IntPtr data, IntPtr rm)
{
data = GCHandle.ToIntPtr(GCHandle.Alloc(new Measure()));
}
[DllExport]
public static void Finalize(IntPtr data)
{
if (data != IntPtr.Zero)
{
GCHandle.FromIntPtr(data).Free();
}
if (stringBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(stringBuffer);
stringBuffer = IntPtr.Zero;
}
}
[DllExport]
public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
{
var measure = (Measure)GCHandle.FromIntPtr(data).Target;
var api = new API(rm);
measure.Reload(api);
}
[DllExport]
public static double Update(IntPtr data)
{
return 0.0;
}
[DllExport]
public static IntPtr GetString(IntPtr data)
{
var measure = (Measure)GCHandle.FromIntPtr(data).Target;
var api = new API(IntPtr.Zero); // Only used for logging purposes
string result = measure.CheckSkinsExist(api);
if (stringBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(stringBuffer);
stringBuffer = IntPtr.Zero;
}
stringBuffer = Marshal.StringToHGlobalUni(result);
return stringBuffer;
}
}
}