-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExports.cs
140 lines (117 loc) · 4.23 KB
/
Exports.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace LibreHardwareMonitorAfterburnerPlugin
{
/// <summary>
/// Native DLL Exports
/// Implementing plugin interface according to SDK samples included with MSI Afterburner 4.6.3
/// </summary>
public class Exports
{
private static readonly Lazy<SensorSource> _source = new Lazy<SensorSource>(System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
[DllExport]
public static bool SetupSource(uint dwIndex, IntPtr hWnd)
{
try
{
if (hWnd != IntPtr.Zero)
{
var settingsChanged = false;
// see if settings are changed during dialog
void changeWatcher(object sender, PropertyChangedEventArgs e) => settingsChanged = true;
Properties.Settings.Default.PropertyChanged += changeWatcher;
var dlg = new SetupDialog();
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (settingsChanged)
{
Properties.Settings.Default.Save();
// Reload new settings if SensorSource already exists
if (_source.IsValueCreated)
_source.Value.Reload();
}
Properties.Settings.Default.PropertyChanged -= changeWatcher;
return true;
}
Properties.Settings.Default.PropertyChanged -= changeWatcher;
// Setup was cancelled, reload old settings.
Properties.Settings.Default.Reload();
return false;
}
// only plugin settings, no individual source settings
return dwIndex == 0xFFFFFFFF;
}
catch (Exception e)
{
try
{
Properties.Settings.Default.Reload();
File.AppendAllText(System.Reflection.Assembly.GetExecutingAssembly().Location + ".log", $"{DateTime.Now:s}: {e}\r\n");
}
catch { }
return false;
}
}
[DllExport]
public static uint GetSourcesNum()
{
try
{
return (uint)_source.Value.SensorCount;
}
catch (Exception e)
{
try
{
File.AppendAllText(System.Reflection.Assembly.GetExecutingAssembly().Location + ".log", $"{DateTime.Now:s}: {e}\r\n");
}
catch { }
return 0u;
}
}
[DllExport]
public static bool GetSourceDesc(uint dwIndex, ref MonitoringSourceDesc pDesc)
{
try
{
_source.Value.FillDescription((int)dwIndex, ref pDesc);
return true;
}
catch (Exception e)
{
try
{
File.AppendAllText(System.Reflection.Assembly.GetExecutingAssembly().Location + ".log", $"{DateTime.Now:s}: {e}\r\n");
}
catch { }
return false;
}
}
[DllExport]
public static float GetSourceData(uint dwIndex)
{
try
{
return _source.Value.SensorValue((int)dwIndex);
}
catch (Exception e)
{
try
{
File.AppendAllText(System.Reflection.Assembly.GetExecutingAssembly().Location + ".log", $"{DateTime.Now:s}: {e}\r\n");
}
catch { }
return 0f;
}
}
}
}