-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathr4xh4x.cpp
More file actions
160 lines (145 loc) · 5.19 KB
/
r4xh4x.cpp
File metadata and controls
160 lines (145 loc) · 5.19 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
152
153
154
155
156
157
158
159
160
/*
main.cpp - VCV Rack src/main.cpp replacemet for JSON catalog h4x.
howto: clone rack, replace src/main.cpp, make as usual, then rename Rack to r4xh4x
todo: clean up/simplify as time allows
*/
#include "util/common.hpp"
#include "app.hpp"
#include "plugin.hpp"
#include <unistd.h>
#include <iostream>
#include <sys/stat.h>
#include <dirent.h>
#if ARCH_WIN
#include <windows.h>
#include <direct.h>
#define mkdir(_dir, _perms) _mkdir(_dir)
#else
#include <dlfcn.h>
#endif
using namespace rack;
std::string gLog = "";
static bool loadPlugin(std::string path) {
std::string libraryFilename;
#if ARCH_LIN
libraryFilename = path + "/" + "plugin.so";
#elif ARCH_WIN
libraryFilename = path + "/" + "plugin.dll";
#elif ARCH_MAC
libraryFilename = path + "/" + "plugin.dylib";
#endif
if (!systemIsFile(libraryFilename)) {
// std::cout << "Plugin file " << libraryFilename << " does not exist" << std::endl;
gLog += "Plugin file " + libraryFilename + " does not exist";
return 1;
}
#if ARCH_WIN
SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
HINSTANCE handle = LoadLibrary(libraryFilename.c_str());
SetErrorMode(0);
if (!handle) {
int error = GetLastError();
// std::cout << "Failed to load library " << libraryFilename << ": code " << error << std::endl;
gLog += "Failed to load library " + libraryFilename + ": code " + std::to_string(error);
return 1;
}
#else
void *handle = dlopen(libraryFilename.c_str(), RTLD_NOW);
if (!handle) {
// std::cout << "Failed to load library " << libraryFilename << ": code " << dlerror() << std::endl;
gLog += "Failed to load library " + libraryFilename + ": code " + dlerror();
return 1;
}
#endif
typedef void (*InitCallback)(Plugin *);
InitCallback initCallback;
#if ARCH_WIN
initCallback = (InitCallback) GetProcAddress(handle, "init");
#else
initCallback = (InitCallback) dlsym(handle, "init");
#endif
if (!initCallback) {
// std::cout << "Failed to read init() symbol in " << libraryFilename << std::endl;
gLog += "Failed to read init() symbol in " + libraryFilename;
return false;
}
Plugin *plugin = new Plugin();
plugin->path = path;
plugin->handle = handle;
initCallback(plugin);
gPlugins.push_back(plugin);
return true;
}
static void loadPlugins(std::string path) {
int plugins = 0;
std::string message;
Plugin *corePlugin = new Plugin();
init(corePlugin);
gPlugins.push_back(corePlugin);
for (std::string pluginPath : systemListEntries(path)) {
if (!systemIsDirectory(pluginPath)) {
message += pluginPath + " is an invalid plugin path";
continue;
}
if (!loadPlugin(pluginPath)) {
message += stringf("Could not load plugin %s\n", pluginPath.c_str());
}
plugins++;
}
if (!message.empty()) {
message += "See log for details.";
}
else if (!plugins) message += "No pugins in " + path;
gLog += message;
}
int main(int argc, char* argv[]) {
if (argc == 1) {
std::cout << "usage: " << argv[0] << " PLUGINS_DIR" << std::endl;
return 1;
}
tagsInit();
loadPlugins(argv[1]);
if (gPlugins.size() == 0) {
std::cout << "{\"log\": \"" << gLog << "\"}" << std::endl;
return 1;
}
json_t *catalogJ = json_object(); // meta as json, plugins as json, etc
json_t *pluginsJ = json_array();
json_object_set_new(catalogJ, "applicationName", json_string(gApplicationName.c_str()));
json_object_set_new(catalogJ, "applicationVersion", json_string(gApplicationVersion.c_str()));
json_object_set_new(catalogJ, "apiHost", json_string(gApiHost.c_str()));
json_object_set_new(catalogJ, "token", json_string(gToken.c_str()));
json_object_set_new(catalogJ, "path", json_string(getcwd(NULL, 0)));
json_object_set_new(catalogJ, "pluginCount", json_integer(gPlugins.size()));
for (Plugin *plugin : gPlugins) {
json_t *pluginJ = json_object();
json_object_set_new(pluginJ, "slug", json_string(plugin->slug.c_str()));
json_object_set_new(pluginJ, "path", json_string(plugin->path.c_str()));
json_object_set_new(pluginJ, "version", json_string(plugin->version.c_str()));
json_object_set_new(pluginJ, "modelCount", json_integer(plugin->models.size()));
json_t *modelsJ = json_array();
json_object_set_new(pluginJ, "models", modelsJ);
for (Model *model : plugin->models) {
json_t *modelJ = json_object();
json_object_set_new(modelJ, "slug", json_string(model->slug.c_str()));
json_object_set_new(modelJ, "name", json_string(model->name.c_str()));
json_object_set_new(modelJ, "author", json_string(model->author.c_str()));
json_t *tagsJ = json_array();
for (ModelTag tag : model->tags) {
json_array_append(tagsJ, json_string(gTagNames[tag].c_str()));
}
json_object_set_new(modelJ, "tags", tagsJ);
// instantiate model for geometry -- someday!?!?
// ModuleWidget *moduleWidget = model->createModuleWidget();
// if (!moduleWidget) return 1;
// json_object_set_new(modelJ, "width", json_integer(int(moduleWidget->box.size.x)));
// json_object_set_new(modelJ, "height", json_integer(int(moduleWidget->box.size.y)));
json_array_append(modelsJ, modelJ);
}
json_array_append(pluginsJ, pluginJ);
}
json_object_set_new(catalogJ, "plugins", pluginsJ);
json_object_set_new(catalogJ, "log", json_string(gLog.c_str()));
std::cout << json_dumps(catalogJ, JSON_INDENT(2) | JSON_REAL_PRECISION(9)) << std::endl;
return 0;
}