-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdllmain.cpp
More file actions
151 lines (105 loc) · 3.02 KB
/
dllmain.cpp
File metadata and controls
151 lines (105 loc) · 3.02 KB
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
// dllmain.cpp : Defines the entry point for the DLL application.
#include <windows.h>
#include <stdio.h>
#include "fp_cplug.h"
#include "sfltplug.h"
#include <ShlObj.h>
std::string utf8_encode(const std::wstring& wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
std::string CONFIG_PATH = "";
std::string GetConfigPath() {
if (CONFIG_PATH.empty()) {
PWSTR wresult;
HRESULT err = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &wresult);
std::string result = utf8_encode(wresult);
result += "\\SFLT\\";
CONFIG_PATH = result;
}
return CONFIG_PATH;
}
void SaveConfig() {
auto path = GetConfigPath() + ".config";
auto lock = CONFIG.lock_read();
const auto& config = CONFIG.get();
auto ofs = std::ofstream(path);
if (!ofs) {
return; // ?? idk
}
ofs << config.configVersion << "\n";
ofs << config.libraryPath << "\n";
ofs << config.skin << "\n";
ofs << config.renameMode << "\n";
ofs << int(config.threadedLoadingEnabled) << "\n";
}
void InitConfig() {
auto path = GetConfigPath();
if (std::filesystem::exists(path)) {
return;
}
auto sfpath = path + "Soundfonts\\";
std::filesystem::create_directory(path);
std::filesystem::create_directory(sfpath);
{
auto lock = CONFIG.lock_write();
auto& config = CONFIG.get_mut();
config.libraryPath = sfpath;
}
SaveConfig();
}
void LoadConfig() {
auto path = GetConfigPath() + ".config";
auto ifs = std::ifstream(path);
if (!ifs) {
InitConfig();
return;
}
auto lock = CONFIG.lock_write();
auto& config = CONFIG.get_mut();
std::string line;
std::getline(ifs, line);
config.configVersion = std::stoi(line);
std::getline(ifs, config.libraryPath);
std::getline(ifs, line);
config.skin = std::stoi(line);
if (config.configVersion >= 2) {
std::getline(ifs, line);
config.renameMode = std::stoi(line);
}
if (config.configVersion >= 3) {
std::getline(ifs, line);
config.threadedLoadingEnabled = std::stoi(line);
}
}
extern "C" BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
// dll load library
HInstance = (HINSTANCE)hModule; // save global instance handle. HInstance is defined in fp_plugclass.cpp
break;
case DLL_PROCESS_DETACH:
// dll end library
break;
}
return TRUE;
}
extern "C" TFruityPlug * _stdcall CreatePlugInstance(TFruityPlugHost * Host, int Tag)
{
// check FL Studio version
if (Host->HostVersion < 006000000) {
char error_buf[256];
sprintf_s(error_buf, 256, "SFLT|Error: FL Studio 6 or newer is required. (Current Version Id: %d)", Host->HostVersion);
Host->Dispatcher(Tag, FHD_MsgBox, (intptr_t)error_buf, MB_OK | MB_ICONERROR);
return NULL;
}
LoadConfig();
// create the plugin object and return the pointer
return new SFLTPlug(Tag, Host, HInstance);
}