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.

337 lines
7.6KB

  1. #include <patch.hpp>
  2. #include <asset.hpp>
  3. #include <system.hpp>
  4. #include <engine/Engine.hpp>
  5. #include <app.hpp>
  6. #include <app/common.hpp>
  7. #include <app/Scene.hpp>
  8. #include <app/RackWidget.hpp>
  9. #include <history.hpp>
  10. #include <settings.hpp>
  11. #include <algorithm>
  12. #include <osdialog.h>
  13. namespace rack {
  14. static const char PATCH_FILTERS[] = "VCV Rack patch (.vcv):vcv";
  15. PatchManager::PatchManager() {
  16. path = settings::patchPath;
  17. }
  18. PatchManager::~PatchManager() {
  19. settings::patchPath = path;
  20. }
  21. void PatchManager::init(std::string path) {
  22. if (!path.empty()) {
  23. // Load patch
  24. load(path);
  25. this->path = path;
  26. return;
  27. }
  28. if (!settings::devMode) {
  29. // 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.
  30. bool oldSkipLoadOnLaunch = settings::skipLoadOnLaunch;
  31. settings::skipLoadOnLaunch = true;
  32. settings::save(asset::settingsPath);
  33. settings::skipLoadOnLaunch = false;
  34. 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?")) {
  35. this->path = "";
  36. return;
  37. }
  38. }
  39. // Load autosave
  40. if (load("")) {
  41. return;
  42. }
  43. reset();
  44. }
  45. void PatchManager::reset() {
  46. if (!settings::headless) {
  47. APP->history->clear();
  48. APP->scene->rack->clear();
  49. APP->scene->rackScroll->reset();
  50. }
  51. APP->engine->clear();
  52. path = "";
  53. if (load(asset::templatePath)) {
  54. return;
  55. }
  56. if (load(asset::system("template.vcv"))) {
  57. return;
  58. }
  59. }
  60. static bool promptClear(std::string text) {
  61. if (APP->history->isSaved())
  62. return true;
  63. if (APP->scene->rack->isEmpty())
  64. return true;
  65. return osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, text.c_str());
  66. }
  67. void PatchManager::resetDialog() {
  68. if (!promptClear("The current patch is unsaved. Clear it and start a new patch?"))
  69. return;
  70. reset();
  71. }
  72. void PatchManager::save(std::string path) {
  73. INFO("Saving patch %s", path.c_str());
  74. json_t* rootJ = toJson();
  75. if (!rootJ)
  76. return;
  77. DEFER({
  78. json_decref(rootJ);
  79. });
  80. // Write to temporary path and then rename it to the correct path
  81. std::string tmpPath = path + ".tmp";
  82. FILE* file = std::fopen(tmpPath.c_str(), "w");
  83. if (!file) {
  84. // Fail silently
  85. return;
  86. }
  87. json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(9));
  88. std::fclose(file);
  89. system::moveFile(tmpPath, path);
  90. }
  91. void PatchManager::saveDialog() {
  92. if (!path.empty()) {
  93. save(path);
  94. APP->history->setSaved();
  95. }
  96. else {
  97. saveAsDialog();
  98. }
  99. }
  100. void PatchManager::saveAsDialog() {
  101. std::string dir;
  102. std::string filename;
  103. if (path.empty()) {
  104. dir = asset::user("patches");
  105. system::createDirectory(dir);
  106. }
  107. else {
  108. dir = string::directory(path);
  109. filename = string::filename(path);
  110. }
  111. osdialog_filters* filters = osdialog_filters_parse(PATCH_FILTERS);
  112. DEFER({
  113. osdialog_filters_free(filters);
  114. });
  115. char* pathC = osdialog_file(OSDIALOG_SAVE, dir.c_str(), filename.c_str(), filters);
  116. if (!pathC) {
  117. // Fail silently
  118. return;
  119. }
  120. DEFER({
  121. std::free(pathC);
  122. });
  123. // Append .vcv extension if no extension was given.
  124. std::string pathStr = pathC;
  125. if (string::filenameExtension(string::filename(pathStr)) == "") {
  126. pathStr += ".vcv";
  127. }
  128. save(pathStr);
  129. path = pathStr;
  130. APP->history->setSaved();
  131. }
  132. void PatchManager::saveTemplateDialog() {
  133. // Even if <user>/template.vcv doesn't exist, this message is still valid because it overrides the <system>/template.vcv patch.
  134. if (!osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "Overwrite template patch?"))
  135. return;
  136. save(asset::templatePath);
  137. }
  138. bool PatchManager::load(std::string path) {
  139. std::string actualPath = (path != "") ? path : asset::autosavePath;
  140. INFO("Loading patch %s", actualPath.c_str());
  141. FILE* file = std::fopen(actualPath.c_str(), "r");
  142. if (!file) {
  143. // Exit silently
  144. return false;
  145. }
  146. DEFER({
  147. std::fclose(file);
  148. });
  149. json_error_t error;
  150. json_t* rootJ = json_loadf(file, 0, &error);
  151. if (!rootJ) {
  152. std::string message = string::f("JSON parsing error at %s %d:%d %s", error.source, error.line, error.column, error.text);
  153. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, message.c_str());
  154. return false;
  155. }
  156. DEFER({
  157. json_decref(rootJ);
  158. });
  159. if (!settings::headless) {
  160. APP->history->clear();
  161. APP->scene->rack->clear();
  162. APP->scene->rackScroll->reset();
  163. }
  164. APP->engine->clear();
  165. fromJson(rootJ);
  166. // Update recent patches
  167. if (path != "") {
  168. auto& recent = settings::recentPatchPaths;
  169. // Remove path from recent patches (if exists)
  170. recent.erase(std::remove(recent.begin(), recent.end(), path), recent.end());
  171. // Add path to top of recent patches
  172. recent.push_front(path);
  173. // Limit recent patches size
  174. recent.resize(std::min((int) recent.size(), 10));
  175. }
  176. return true;
  177. }
  178. void PatchManager::loadDialog() {
  179. if (!promptClear("The current patch is unsaved. Clear it and open a new patch?"))
  180. return;
  181. std::string dir;
  182. if (path.empty()) {
  183. dir = asset::user("patches");
  184. system::createDirectory(dir);
  185. }
  186. else {
  187. dir = string::directory(path);
  188. }
  189. osdialog_filters* filters = osdialog_filters_parse(PATCH_FILTERS);
  190. DEFER({
  191. osdialog_filters_free(filters);
  192. });
  193. char* pathC = osdialog_file(OSDIALOG_OPEN, dir.c_str(), NULL, filters);
  194. if (!pathC) {
  195. // Fail silently
  196. return;
  197. }
  198. DEFER({
  199. std::free(pathC);
  200. });
  201. load(pathC);
  202. path = pathC;
  203. APP->history->setSaved();
  204. }
  205. void PatchManager::loadPathDialog(std::string path) {
  206. if (!promptClear("The current patch is unsaved. Clear it and open the new patch?"))
  207. return;
  208. load(path);
  209. this->path = path;
  210. APP->history->setSaved();
  211. }
  212. void PatchManager::revertDialog() {
  213. if (path.empty())
  214. return;
  215. if (!promptClear("Revert patch to the last saved state?"))
  216. return;
  217. load(path);
  218. APP->history->setSaved();
  219. }
  220. void PatchManager::disconnectDialog() {
  221. APP->scene->rack->clearCablesAction();
  222. }
  223. json_t* PatchManager::toJson() {
  224. // root
  225. json_t* rootJ = json_object();
  226. // version
  227. json_t* versionJ = json_string(app::APP_VERSION.c_str());
  228. json_object_set_new(rootJ, "version", versionJ);
  229. json_t* engineJ = APP->engine->toJson();
  230. if (!settings::headless) {
  231. APP->scene->rack->mergeJson(engineJ);
  232. }
  233. // Merge with rootJ
  234. json_object_update(rootJ, engineJ);
  235. json_decref(engineJ);
  236. return rootJ;
  237. }
  238. void PatchManager::fromJson(json_t* rootJ) {
  239. legacy = 0;
  240. // version
  241. std::string version;
  242. json_t* versionJ = json_object_get(rootJ, "version");
  243. if (versionJ)
  244. version = json_string_value(versionJ);
  245. if (version != app::APP_VERSION) {
  246. INFO("Patch was made with Rack v%s, current Rack version is v%s", version.c_str(), app::APP_VERSION.c_str());
  247. }
  248. // Detect old patches with ModuleWidget::params/inputs/outputs indices.
  249. if (string::startsWith(version, "0.3.") || string::startsWith(version, "0.4.") || string::startsWith(version, "0.5.") || version == "" || version == "dev") {
  250. // Use ModuleWidget::params/inputs/outputs indices instead of Module.
  251. legacy = 1;
  252. }
  253. else if (string::startsWith(version, "0.6.")) {
  254. legacy = 2;
  255. }
  256. if (legacy) {
  257. INFO("Loading patch using legacy mode %d", legacy);
  258. }
  259. APP->engine->fromJson(rootJ);
  260. if (!settings::headless) {
  261. APP->scene->rack->fromJson(rootJ);
  262. }
  263. // At this point, ModuleWidgets and CableWidgets should own all Modules and Cables.
  264. // TODO Assert this
  265. // Display a message if we have something to say
  266. if (!warningLog.empty()) {
  267. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, warningLog.c_str());
  268. }
  269. warningLog = "";
  270. }
  271. bool PatchManager::isLegacy(int level) {
  272. return legacy && legacy <= level;
  273. }
  274. void PatchManager::log(std::string msg) {
  275. warningLog += msg;
  276. warningLog += "\n";
  277. }
  278. } // namespace rack