You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
6.2KB

  1. #include "patch.hpp"
  2. #include "asset.hpp"
  3. #include "system.hpp"
  4. #include "app.hpp"
  5. #include "app/Scene.hpp"
  6. #include "app/RackWidget.hpp"
  7. #include "history.hpp"
  8. #include "settings.hpp"
  9. #include "osdialog.h"
  10. namespace rack {
  11. static const char PATCH_FILTERS[] = "VCV Rack patch (.vcv):vcv";
  12. PatchManager::PatchManager() {
  13. path = settings::patchPath;
  14. }
  15. PatchManager::~PatchManager() {
  16. settings::patchPath = path;
  17. }
  18. void PatchManager::init(std::string path) {
  19. if (!path.empty()) {
  20. // Load patch
  21. load(path);
  22. this->path = path;
  23. return;
  24. }
  25. // To prevent launch crashes, if Rack crashes between now and 15 seconds from now, the "skipAutosaveOnLaunch" property will remain in settings.json, so that in the next launch, the broken autosave will not be loaded.
  26. bool oldSkipLoadOnLaunch = settings::skipLoadOnLaunch;
  27. settings::skipLoadOnLaunch = true;
  28. settings::save(asset::user("settings.json"));
  29. settings::skipLoadOnLaunch = false;
  30. if (oldSkipLoadOnLaunch && osdialog_message(OSDIALOG_INFO, OSDIALOG_YES_NO, "Rack has recovered from a crash, possibly caused by a faulty module in your patch. Clear your patch and start over?")) {
  31. this->path = "";
  32. return;
  33. }
  34. // Load autosave
  35. if (load(asset::user("autosave.vcv"))) {
  36. return;
  37. }
  38. reset();
  39. }
  40. void PatchManager::reset() {
  41. APP->history->clear();
  42. APP->scene->rack->clear();
  43. APP->scene->rackScroll->reset();
  44. path = "";
  45. if (load(asset::user("template.vcv"))) {
  46. return;
  47. }
  48. if (load(asset::system("template.vcv"))) {
  49. return;
  50. }
  51. }
  52. void PatchManager::resetDialog() {
  53. if (!(APP->history->isSaved() || osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "The current patch is unsaved. Clear it and start a new patch?")))
  54. return;
  55. reset();
  56. }
  57. void PatchManager::save(std::string path) {
  58. INFO("Saving patch %s", path.c_str());
  59. json_t *rootJ = toJson();
  60. if (!rootJ)
  61. return;
  62. DEFER({
  63. json_decref(rootJ);
  64. });
  65. FILE *file = std::fopen(path.c_str(), "w");
  66. if (!file) {
  67. // Fail silently
  68. return;
  69. }
  70. DEFER({
  71. std::fclose(file);
  72. });
  73. json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(9));
  74. }
  75. void PatchManager::saveDialog() {
  76. if (!path.empty()) {
  77. save(path);
  78. APP->history->setSaved();
  79. }
  80. else {
  81. saveAsDialog();
  82. }
  83. }
  84. void PatchManager::saveAsDialog() {
  85. std::string dir;
  86. std::string filename;
  87. if (path.empty()) {
  88. dir = asset::user("patches");
  89. system::createDirectory(dir);
  90. }
  91. else {
  92. dir = string::directory(path);
  93. filename = string::filename(path);
  94. }
  95. osdialog_filters *filters = osdialog_filters_parse(PATCH_FILTERS);
  96. DEFER({
  97. osdialog_filters_free(filters);
  98. });
  99. char *pathC = osdialog_file(OSDIALOG_SAVE, dir.c_str(), filename.c_str(), filters);
  100. if (!pathC) {
  101. // Fail silently
  102. return;
  103. }
  104. DEFER({
  105. std::free(pathC);
  106. });
  107. std::string pathStr = pathC;
  108. if (string::extension(pathStr).empty()) {
  109. pathStr += ".vcv";
  110. }
  111. save(pathStr);
  112. path = pathStr;
  113. APP->history->setSaved();
  114. }
  115. void PatchManager::saveTemplateDialog() {
  116. // Even if <user>/template.vcv doesn't exist, this message is still valid because it overrides the <system>/template.vcv patch.
  117. if (!osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "Overwrite template patch?"))
  118. return;
  119. save(asset::user("template.vcv"));
  120. }
  121. bool PatchManager::load(std::string path) {
  122. INFO("Loading patch %s", path.c_str());
  123. FILE *file = std::fopen(path.c_str(), "r");
  124. if (!file) {
  125. // Exit silently
  126. return false;
  127. }
  128. DEFER({
  129. std::fclose(file);
  130. });
  131. json_error_t error;
  132. json_t *rootJ = json_loadf(file, 0, &error);
  133. if (!rootJ) {
  134. std::string message = string::f("JSON parsing error at %s %d:%d %s", error.source, error.line, error.column, error.text);
  135. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, message.c_str());
  136. return false;
  137. }
  138. DEFER({
  139. json_decref(rootJ);
  140. });
  141. APP->history->clear();
  142. APP->scene->rack->clear();
  143. APP->scene->rackScroll->reset();
  144. legacy = 0;
  145. fromJson(rootJ);
  146. return true;
  147. }
  148. void PatchManager::loadDialog() {
  149. if (!(APP->history->isSaved() || osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "The current patch is unsaved. Clear it and open a new patch?")))
  150. return;
  151. std::string dir;
  152. if (path.empty()) {
  153. dir = asset::user("patches");
  154. system::createDirectory(dir);
  155. }
  156. else {
  157. dir = string::directory(path);
  158. }
  159. osdialog_filters *filters = osdialog_filters_parse(PATCH_FILTERS);
  160. DEFER({
  161. osdialog_filters_free(filters);
  162. });
  163. char *pathC = osdialog_file(OSDIALOG_OPEN, dir.c_str(), NULL, filters);
  164. if (!pathC) {
  165. // Fail silently
  166. return;
  167. }
  168. DEFER({
  169. std::free(pathC);
  170. });
  171. load(pathC);
  172. path = pathC;
  173. APP->history->setSaved();
  174. }
  175. void PatchManager::revertDialog() {
  176. if (path.empty())
  177. return;
  178. if (!(APP->history->isSaved() || osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "Revert patch to the last saved state?")))
  179. return;
  180. load(path);
  181. APP->history->setSaved();
  182. }
  183. void PatchManager::disconnectDialog() {
  184. APP->scene->rack->clearCablesAction();
  185. }
  186. json_t *PatchManager::toJson() {
  187. // root
  188. json_t *rootJ = json_object();
  189. // version
  190. json_t *versionJ = json_string(app::APP_VERSION);
  191. json_object_set_new(rootJ, "version", versionJ);
  192. // Merge with RackWidget JSON
  193. json_t *rackJ = APP->scene->rack->toJson();
  194. // Merge with rootJ
  195. json_object_update(rootJ, rackJ);
  196. json_decref(rackJ);
  197. return rootJ;
  198. }
  199. void PatchManager::fromJson(json_t *rootJ) {
  200. legacy = 0;
  201. // version
  202. std::string version;
  203. json_t *versionJ = json_object_get(rootJ, "version");
  204. if (versionJ)
  205. version = json_string_value(versionJ);
  206. if (version != app::APP_VERSION) {
  207. INFO("Patch was made with Rack v%s, current Rack version is v%s", version.c_str(), app::APP_VERSION);
  208. }
  209. // Detect old patches with ModuleWidget::params/inputs/outputs indices.
  210. // (We now use Module::params/inputs/outputs indices.)
  211. if (string::startsWith(version, "0.3.") || string::startsWith(version, "0.4.") || string::startsWith(version, "0.5.") || version == "" || version == "dev") {
  212. legacy = 1;
  213. }
  214. else if (string::startsWith(version, "0.6.")) {
  215. legacy = 2;
  216. }
  217. if (legacy) {
  218. INFO("Loading patch using legacy mode %d", legacy);
  219. }
  220. APP->scene->rack->fromJson(rootJ);
  221. // Display a message if we have something to say
  222. if (!warningLog.empty()) {
  223. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, warningLog.c_str());
  224. }
  225. warningLog = "";
  226. }
  227. bool PatchManager::isLegacy(int level) {
  228. return legacy && legacy <= level;
  229. }
  230. } // namespace rack