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.

277 lines
6.0KB

  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. this->path = "";
  39. if (load(asset::user("template.vcv"))) {
  40. return;
  41. }
  42. if (load(asset::system("template.vcv"))) {
  43. return;
  44. }
  45. }
  46. void PatchManager::reset() {
  47. APP->history->clear();
  48. APP->scene->rackWidget->clear();
  49. APP->scene->scrollWidget->offset = math::Vec(0, 0);
  50. // Fails silently if file does not exist
  51. load(asset::user("template.vcv"));
  52. legacy = 0;
  53. path = "";
  54. }
  55. void PatchManager::resetDialog() {
  56. if (osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "Clear patch and start over?")) {
  57. reset();
  58. }
  59. }
  60. void PatchManager::save(std::string path) {
  61. INFO("Saving patch %s", path.c_str());
  62. json_t *rootJ = toJson();
  63. if (!rootJ)
  64. return;
  65. DEFER({
  66. json_decref(rootJ);
  67. });
  68. FILE *file = std::fopen(path.c_str(), "w");
  69. if (!file) {
  70. // Fail silently
  71. return;
  72. }
  73. DEFER({
  74. std::fclose(file);
  75. });
  76. json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(9));
  77. }
  78. void PatchManager::saveDialog() {
  79. if (!path.empty()) {
  80. save(path);
  81. }
  82. else {
  83. saveAsDialog();
  84. }
  85. }
  86. void PatchManager::saveAsDialog() {
  87. std::string dir;
  88. std::string filename;
  89. if (path.empty()) {
  90. dir = asset::user("patches");
  91. system::createDirectory(dir);
  92. }
  93. else {
  94. dir = string::directory(path);
  95. filename = string::filename(path);
  96. }
  97. osdialog_filters *filters = osdialog_filters_parse(PATCH_FILTERS);
  98. DEFER({
  99. osdialog_filters_free(filters);
  100. });
  101. char *pathC = osdialog_file(OSDIALOG_SAVE, dir.c_str(), filename.c_str(), filters);
  102. if (!pathC) {
  103. // Fail silently
  104. return;
  105. }
  106. DEFER({
  107. free(pathC);
  108. });
  109. std::string pathStr = pathC;
  110. if (string::extension(pathStr).empty()) {
  111. pathStr += ".vcv";
  112. }
  113. save(pathStr);
  114. path = pathStr;
  115. }
  116. void PatchManager::saveTemplateDialog() {
  117. if (osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "Overwrite template patch?")) {
  118. save(asset::user("template.vcv"));
  119. }
  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->rackWidget->clear();
  143. APP->scene->scrollWidget->offset = math::Vec(0, 0);
  144. fromJson(rootJ);
  145. return true;
  146. }
  147. void PatchManager::loadDialog() {
  148. std::string dir;
  149. if (path.empty()) {
  150. dir = asset::user("patches");
  151. system::createDirectory(dir);
  152. }
  153. else {
  154. dir = string::directory(path);
  155. }
  156. osdialog_filters *filters = osdialog_filters_parse(PATCH_FILTERS);
  157. DEFER({
  158. osdialog_filters_free(filters);
  159. });
  160. char *pathC = osdialog_file(OSDIALOG_OPEN, dir.c_str(), NULL, filters);
  161. if (!pathC) {
  162. // Fail silently
  163. return;
  164. }
  165. DEFER({
  166. free(pathC);
  167. });
  168. load(pathC);
  169. path = pathC;
  170. }
  171. void PatchManager::revertDialog() {
  172. if (path.empty())
  173. return;
  174. if (osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "Revert patch to the last saved state?")) {
  175. load(path);
  176. }
  177. }
  178. void PatchManager::disconnectDialog() {
  179. // Since we have undo history, no need for a warning.
  180. // if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, "Remove all patch cables?"))
  181. // return;
  182. APP->scene->rackWidget->clearCablesAction();
  183. }
  184. json_t *PatchManager::toJson() {
  185. // root
  186. json_t *rootJ = json_object();
  187. // version
  188. json_t *versionJ = json_string(app::APP_VERSION);
  189. json_object_set_new(rootJ, "version", versionJ);
  190. // Merge with RackWidget JSON
  191. json_t *rackJ = APP->scene->rackWidget->toJson();
  192. // Merge with rootJ
  193. json_object_update(rootJ, rackJ);
  194. json_decref(rackJ);
  195. return rootJ;
  196. }
  197. void PatchManager::fromJson(json_t *rootJ) {
  198. legacy = 0;
  199. // version
  200. std::string version;
  201. json_t *versionJ = json_object_get(rootJ, "version");
  202. if (versionJ)
  203. version = json_string_value(versionJ);
  204. if (version != app::APP_VERSION) {
  205. INFO("Patch was made with Rack v%s, current Rack version is v%s", version.c_str(), app::APP_VERSION);
  206. }
  207. // Detect old patches with ModuleWidget::params/inputs/outputs indices.
  208. // (We now use Module::params/inputs/outputs indices.)
  209. if (string::startsWith(version, "0.3.") || string::startsWith(version, "0.4.") || string::startsWith(version, "0.5.") || version == "" || version == "dev") {
  210. legacy = 1;
  211. }
  212. else if (string::startsWith(version, "0.6.")) {
  213. legacy = 2;
  214. }
  215. if (legacy) {
  216. INFO("Loading patch using legacy mode %d", legacy);
  217. }
  218. APP->scene->rackWidget->fromJson(rootJ);
  219. // Display a message if we have something to say
  220. if (!warningLog.empty()) {
  221. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, warningLog.c_str());
  222. }
  223. warningLog = "";
  224. }
  225. bool PatchManager::isLegacy(int level) {
  226. return legacy && legacy <= level;
  227. }
  228. } // namespace rack