Krotos Modules 3
Loading...
Searching...
No Matches
PresetManager.cpp
Go to the documentation of this file.
1namespace krotos
2{
4 "0.0.17"; // oldest preset version allowed to be loaded, version for new template system
5 const String PresetManager::m_factoryPresetFolderName = "Factory";
6 const String PresetManager::m_userPresetFolderName = "User";
7 const String PresetManager::m_presetFileExtension = "ksp";
8 const String PresetManager::m_initFileName = "init";
9
10 bool PresetManager::checkPresetVersion(const File& presetFile, const String& pluginVersion,
11 const bool showAlertWindow)
12 {
13 jassert(presetFile.hasFileExtension(m_presetFileExtension)); // is valid preset
14
15 auto presetVersion = PresetManager::getPresetVersion(presetFile);
16
17 // remove decimal points for comparison, ie 0.0.15 becomes 15
18 auto presetVersionAsInt = presetVersion.removeCharacters(".").getIntValue();
19 auto versionCutoffAsInt = m_cutoffVersion.removeCharacters(".").getIntValue();
20
21 if (presetVersion.equalsIgnoreCase("") || presetVersionAsInt < versionCutoffAsInt)
22 {
23 if (showAlertWindow)
24 {
25 PresetManagerUI::alertPresetVersion(pluginVersion, presetVersion, m_cutoffVersion);
26 }
27
28 return false;
29 }
30
31 return true;
32 }
33
34 String PresetManager::getPresetVersion(const File& presetFile)
35 {
36 jassert(presetFile.hasFileExtension(m_presetFileExtension)); // is valid preset
37
38 auto incomingPreset = PresetManager::getPresetFromFile(presetFile);
39 auto valueTree = PresetManager::getValueTreeFromFile(incomingPreset.path);
40
41 auto presetVersion = valueTree.getChildWithProperty(XmlType::Property::id, "PluginVersion");
42 auto versionString = presetVersion.getProperty(XmlType::Property::value);
43
44 return versionString;
45 }
46
47 String PresetManager::getPresetVersion(const ValueTree& state)
48 {
49 jassert(state.isValid());
50
51 auto presetVersion = state.getChildWithProperty(XmlType::Property::id, "PluginVersion");
52 auto versionString = presetVersion.getProperty(XmlType::Property::value);
53
54 return versionString;
55 }
56
57 void PresetManager::writePresetVersion(ValueTree& state, const String& versionNumber)
58 {
59 jassert(state.isValid());
60
61 ValueTree pluginData{XmlType::Tag::pluginData,
62 {{XmlType::Property::id, "PluginVersion"}, // preset saved with this version of the plugin
63 {XmlType::Property::value, versionNumber}}};
64
65 state.appendChild(pluginData, nullptr);
66 }
67
69 {
70 jassert(state.isValid());
71
72 // iterate through the child trees of "state" and find the "KWIDGETDATA" tree with "id" equal to
73 // "ReformerVersion"
74 ValueTree kwidgetData;
75 for (int i = 0; i < state.getNumChildren(); ++i)
76 {
77 ValueTree child = state.getChild(i);
78 if (child.getType() == XmlType::Tag::kwidgetData &&
79 child.getProperty(XmlType::Property::id) == "ReformerVersion")
80 {
81 kwidgetData = child;
82 break; // found the "KWIDGETDATA" tree with the "ReformerVersion" ID, no need to continue searching
83 }
84 }
85
86 if (kwidgetData.isValid())
87 {
88 // the "KWIDGETDATA" tree with "ReformerVersion" exists, so update its value
89 kwidgetData.setProperty(XmlType::Property::value, Kwidget_Reformer::Version, nullptr);
90 }
91 else
92 {
93 // create a new "KWIDGETDATA" tree and add it to the state tree
94 kwidgetData = ValueTree(XmlType::Tag::kwidgetData, {{XmlType::Property::id, "ReformerVersion"},
96
97 state.appendChild(kwidgetData, nullptr);
98 }
99 }
100
102
104 {
105 auto state = getValueTreeFromFile(preset);
108 }
109
111 {
112 bool hasReformerKwidget = false;
113
114 auto kwidgets = state.getChildWithName(XmlType::Tag::kwidgets);
115
116 for (const auto& kwidget : kwidgets)
117 {
118 if (kwidget.getProperty(XmlType::Property::type).equals("Reformer"))
119 {
120 hasReformerKwidget = true;
121 break;
122 }
123 }
124
125 if (!hasReformerKwidget)
126 {
127 return;
128 }
129
130 auto incomingReformerVersion = PresetManager::getReformerVersion(state);
131
132 // remove decimal points for comparison, i.e., 0.15 becomes 15
133 auto incomingReformerVersionAsInt = incomingReformerVersion.removeCharacters(".").getIntValue();
134 auto currentReformerVersionAsInt = Kwidget_Reformer::Version.removeCharacters(".").getIntValue();
135
136 if ((incomingReformerVersion.equalsIgnoreCase("") ||
137 incomingReformerVersionAsInt < currentReformerVersionAsInt) &&
138 currentReformerVersionAsInt < 2)
139 {
141
142 // TODO: any future serialization step(s) required can be called here!
143 }
144 }
145
146 String PresetManager::getReformerVersion(ValueTree& state)
147 {
148 auto reformerVersion = state.getChildWithProperty(XmlType::Property::id, "ReformerVersion");
149 auto versionString = reformerVersion.getProperty(XmlType::Property::value);
150
151 return versionString;
152 }
153
154 ValueTree PresetManager::getValueTreeFromFile(const File& presetFile)
155 {
156 jassert(presetFile.hasFileExtension(m_presetFileExtension));
157
158 XmlDocument doc(presetFile);
159 auto xml = doc.getDocumentElement();
160 return ValueTree::fromXml(*xml);
161 }
162
163 void PresetManager::writeValueTreeToFile(const ValueTree& valueTree, const File& outputFile)
164 {
165 jassert(outputFile.hasFileExtension(m_presetFileExtension));
166
167 auto xml = valueTree.createXml();
168 xml->writeTo(outputFile);
169 }
170
172 {
173 jassert(file.hasFileExtension(m_presetFileExtension));
174
175 auto name = file.getFileNameWithoutExtension();
176 return {name, file};
177 }
178
180 {
181#ifdef JUCE_MAC
182 return File::getSpecialLocation(File::commonApplicationDataDirectory).getFullPathName() +
183 "/Application Support/Krotos/" + JucePlugin_Name + "/UITemplates/";
184#else
185 return File("C:\\ProgramData\\Krotos\\" JucePlugin_Name "\\UITemplates\\");
186#endif
187 }
188
190 {
191#ifdef JUCE_MAC
192 return File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() +
193 "/Application Support/Krotos/" + JucePlugin_Name + "/UITemplates/";
194#else
195 return File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + "\\Krotos\\" +
196 JucePlugin_Name + "\\UITemplates\\";
197#endif
198 }
199
201 {
202#ifdef JUCE_MAC
203 return File::getSpecialLocation(File::commonApplicationDataDirectory).getFullPathName() +
204 "/Application Support/Krotos/" + JucePlugin_Name + "/Presets/main/";
205#else
206 return File::getSpecialLocation(File::commonApplicationDataDirectory).getFullPathName() + "\\Krotos\\" +
207 JucePlugin_Name + "\\Presets\\main";
208#endif
209 }
210
212 {
213#ifdef JUCE_MAC
214 return File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() +
215 "/Application Support/Krotos/" + JucePlugin_Name + "/Presets/main/";
216#else
217 return File::getSpecialLocation(File::userApplicationDataDirectory).getFullPathName() + "\\Krotos\\" +
218 JucePlugin_Name + "\\Presets\\main\\";
219#endif
220 }
221
223 {
224 File initFile = File(getFactoryPresetDirectory().getFullPathName() + File::getSeparatorString() +
226
227 if (!initFile.exists())
228 {
229 throw PresetException(/*what*/ "Error: Cannot find init.ksp",
230 /*when*/ "On load of init preset");
231 }
232
233 if (!initFile.hasFileExtension(m_presetFileExtension))
234 {
235 throw PresetException(/*what*/ "Error: Invalid init file",
236 /*when*/ "On load of init preset");
237 }
238 return initFile;
239 }
240
242 {
243// do not try to init resources/directories when running unit tests!
244#ifndef UNIT_TEST
245 PluginHostType host;
246
247 if (!host.isPluginval()) // pluginval currently not compatible with state loading
248 {
249 try
250 {
252 }
253 catch (PresetException& ex)
254 {
255 std::cout << ex.what() << std::endl;
256 }
257 }
258#endif
259 }
260
262
263 void PresetManager::setCurrentPreset(const File& presetFile)
264 {
265 jassert(presetFile.hasFileExtension(m_presetFileExtension));
267
268 if (presetChanged)
269 MessageManager::callAsync([=] { presetChanged(getValueTreeFromFile(m_currentPreset.path)); });
270 }
271
273 {
274 PopupMenu menu;
275
278
279 return menu;
280 }
281
283 {
284 auto userPresetPath = getUserPresetDirectory();
285 auto factoryPresetPath = getFactoryPresetDirectory();
286
287 if (userPresetPath.isDirectory() != true)
288 {
289 if (userPresetPath.createDirectory() != Result::ok())
290 {
291 throw PresetException(/*what*/ "Error: Cannot find or create user preset directory",
292 /*when*/ "On init of user preset dir");
293 }
294 }
295
296 if (factoryPresetPath.isDirectory() != true)
297 {
298 throw PresetException(/*what*/ "Error: Factory preset folder does not exist",
299 /*when*/ "On init of factory preset dir");
300 }
301
302 auto userTemplatePath = getUserTemplateDirectory();
303 auto factoryTemplatePath = getFactoryTemplateDirectory();
304
305 if (userTemplatePath.isDirectory() != true)
306 {
307 if (userTemplatePath.createDirectory() != Result::ok())
308 {
309 throw PresetException(/*what*/ "Error: Cannot find or create user template directory",
310 /*when*/ "On init of user template dir");
311 }
312 }
313
314 if (factoryTemplatePath.isDirectory() != true)
315 {
316 throw PresetException(/*what*/ "Error: Factory template folder does not exist",
317 /*when*/ "On init of factory template dir");
318 }
319 }
320
321 PopupMenu PresetManager::getPopupMenuFromDirectory(const File& presetDirectory)
322 {
323 PopupMenu menu;
324
325 auto files = presetDirectory.findChildFiles(File::findFilesAndDirectories, false, "*");
326 for (auto& file : files)
327 {
328 auto name = file.getFileNameWithoutExtension();
329
330 if (file.hasFileExtension(m_presetFileExtension))
331 menu.addItem(name, [=]() { setCurrentPreset(file); });
332 else if (file.isDirectory())
333 menu.addSubMenu(name, getPopupMenuFromDirectory(file));
334 }
335
336 return menu;
337 }
338
339 char const* PresetException::what() const noexcept { return runtime_error::what(); }
340
341 String PresetException::when() const { return m_whenDidItHappen; }
342} // namespace krotos
static const String Version
Definition Kwidget_Reformer.h:19
An exception for preset manager.
Definition PresetManager.h:148
String m_whenDidItHappen
Definition PresetManager.h:159
char const * what() const noexcept override
Definition PresetManager.cpp:339
String when() const
Definition PresetManager.cpp:341
const Preset & getCurrentPreset() const
Definition PresetManager.cpp:261
static void writeReformerVersion(ValueTree &state)
Writes the Reformer version to a ValueTree.
Definition PresetManager.cpp:68
static const String m_cutoffVersion
Definition PresetManager.h:130
PopupMenu getCurrentPopupMenu()
Definition PresetManager.cpp:272
void setCurrentPreset(const File &presetFile)
Definition PresetManager.cpp:263
PresetManager()
Definition PresetManager.cpp:241
static void checkReformerVersion(ValueTree &state)
Checks the Reformer version within a ValueTree.
Definition PresetManager.cpp:101
static File getFactoryPresetDirectory()
Definition PresetManager.cpp:200
static const String m_presetFileExtension
Definition PresetManager.h:133
static File getInitPresetFile()
Definition PresetManager.cpp:222
static void checkReformerVersionInternal(ValueTree &state)
Definition PresetManager.cpp:110
static File getUserPresetDirectory()
Definition PresetManager.cpp:211
Preset m_currentPreset
Definition PresetManager.h:136
static const String m_initFileName
Definition PresetManager.h:134
static const String m_factoryPresetFolderName
Definition PresetManager.h:131
static ValueTree getValueTreeFromFile(const File &presetFile)
Definition PresetManager.cpp:154
static void writeValueTreeToFile(const ValueTree &valueTree, const File &outputFile)
Definition PresetManager.cpp:163
static Preset getPresetFromFile(const File &file)
Definition PresetManager.cpp:171
void initializePresetDirectories()
Definition PresetManager.cpp:282
static String getPresetVersion(const ValueTree &state)
Definition PresetManager.cpp:47
PopupMenu getPopupMenuFromDirectory(const File &presetDirectory)
Definition PresetManager.cpp:321
static File getUserTemplateDirectory()
Definition PresetManager.cpp:189
static bool checkPresetVersion(const File &presetFile, const String &pluginVersion, const bool showAlertWindow)
Definition PresetManager.cpp:10
std::function< void(const ValueTree &)> presetChanged
Definition PresetManager.h:26
static const String m_userPresetFolderName
Definition PresetManager.h:132
static void writePresetVersion(ValueTree &state, const String &versionNumber)
Definition PresetManager.cpp:57
static String getReformerVersion(ValueTree &state)
Retrieves the Reformer version from a ValueTree.
Definition PresetManager.cpp:146
static File getFactoryTemplateDirectory()
Definition PresetManager.cpp:179
static void alertPresetVersion(const String &pluginVersion, const String &presetVersion, const String &cutoffVersion)
Definition PresetManagerUI.cpp:3
Definition AirAbsorptionFilter.cpp:2
Definition PresetManager.h:20
File path
Definition PresetManager.h:22
static const Identifier id
Definition XmlType.h:41
static const Identifier value
Definition XmlType.h:42
static const Identifier type
Definition XmlType.h:43
static const Identifier kwidgets
Definition XmlType.h:22
static const Identifier pluginData
Definition XmlType.h:17
static const Identifier kwidgetData
Definition XmlType.h:30