The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

1234 lines
43KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_Project.h"
  19. #include "jucer_ProjectType.h"
  20. #include "../Project Saving/jucer_ProjectExporter.h"
  21. #include "../Project Saving/jucer_ProjectSaver.h"
  22. #include "../Application/jucer_OpenDocumentManager.h"
  23. #include "../Application/jucer_Application.h"
  24. namespace
  25. {
  26. String makeValid4CC (const String& seed)
  27. {
  28. String s (CodeHelpers::makeValidIdentifier (seed, false, true, false) + "xxxx");
  29. return s.substring (0, 1).toUpperCase()
  30. + s.substring (1, 4).toLowerCase();
  31. }
  32. }
  33. //==============================================================================
  34. Project::Project (const File& f)
  35. : FileBasedDocument (projectFileExtension,
  36. String ("*") + projectFileExtension,
  37. "Choose a Jucer project to load",
  38. "Save Jucer project"),
  39. projectRoot (Ids::JUCERPROJECT),
  40. isSaving (false)
  41. {
  42. Logger::writeToLog ("Loading project: " + f.getFullPathName());
  43. setFile (f);
  44. removeDefunctExporters();
  45. updateOldModulePaths();
  46. setMissingDefaultValues();
  47. setChangedFlag (false);
  48. projectRoot.addListener (this);
  49. }
  50. Project::~Project()
  51. {
  52. projectRoot.removeListener (this);
  53. ProjucerApplication::getApp().openDocumentManager.closeAllDocumentsUsingProject (*this, false);
  54. }
  55. const char* Project::projectFileExtension = ".jucer";
  56. //==============================================================================
  57. void Project::setTitle (const String& newTitle)
  58. {
  59. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  60. getMainGroup().getNameValue() = newTitle;
  61. }
  62. String Project::getTitle() const
  63. {
  64. return projectRoot.getChildWithName (Ids::MAINGROUP) [Ids::name];
  65. }
  66. String Project::getDocumentTitle()
  67. {
  68. return getTitle();
  69. }
  70. void Project::updateProjectSettings()
  71. {
  72. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, nullptr);
  73. projectRoot.setProperty (Ids::name, getDocumentTitle(), nullptr);
  74. }
  75. void Project::setMissingDefaultValues()
  76. {
  77. if (! projectRoot.hasProperty (Ids::ID))
  78. projectRoot.setProperty (Ids::ID, createAlphaNumericUID(), nullptr);
  79. // Create main file group if missing
  80. if (! projectRoot.getChildWithName (Ids::MAINGROUP).isValid())
  81. {
  82. Item mainGroup (*this, ValueTree (Ids::MAINGROUP), false);
  83. projectRoot.addChild (mainGroup.state, 0, 0);
  84. }
  85. getMainGroup().initialiseMissingProperties();
  86. if (getDocumentTitle().isEmpty())
  87. setTitle ("JUCE Project");
  88. if (! projectRoot.hasProperty (Ids::projectType))
  89. getProjectTypeValue() = ProjectType_GUIApp::getTypeName();
  90. if (! projectRoot.hasProperty (Ids::version))
  91. getVersionValue() = "1.0.0";
  92. updateOldStyleConfigList();
  93. moveOldPropertyFromProjectToAllExporters (Ids::bigIcon);
  94. moveOldPropertyFromProjectToAllExporters (Ids::smallIcon);
  95. if (getProjectType().isAudioPlugin())
  96. setMissingAudioPluginDefaultValues();
  97. getModules().sortAlphabetically();
  98. if (getBundleIdentifier().toString().isEmpty())
  99. getBundleIdentifier() = getDefaultBundleIdentifier();
  100. if (shouldIncludeBinaryInAppConfig() == var::null)
  101. shouldIncludeBinaryInAppConfig() = true;
  102. ProjucerApplication::getApp().updateNewlyOpenedProject (*this);
  103. }
  104. void Project::setMissingAudioPluginDefaultValues()
  105. {
  106. const String sanitisedProjectName (CodeHelpers::makeValidIdentifier (getTitle(), false, true, false));
  107. setValueIfVoid (shouldBuildVST(), true);
  108. setValueIfVoid (shouldBuildVST3(), false);
  109. setValueIfVoid (shouldBuildAU(), true);
  110. setValueIfVoid (shouldBuildAUv3(), false);
  111. setValueIfVoid (shouldBuildRTAS(), false);
  112. setValueIfVoid (shouldBuildAAX(), false);
  113. setValueIfVoid (shouldBuildStandalone(), false);
  114. setValueIfVoid (getPluginName(), getTitle());
  115. setValueIfVoid (getPluginDesc(), getTitle());
  116. setValueIfVoid (getPluginManufacturer(), "yourcompany");
  117. setValueIfVoid (getPluginManufacturerCode(), "Manu");
  118. setValueIfVoid (getPluginCode(), makeValid4CC (getProjectUID() + getProjectUID()));
  119. setValueIfVoid (getPluginChannelConfigs(), "");
  120. setValueIfVoid (getPluginIsSynth(), false);
  121. setValueIfVoid (getPluginWantsMidiInput(), false);
  122. setValueIfVoid (getPluginProducesMidiOut(), false);
  123. setValueIfVoid (getPluginIsMidiEffectPlugin(), false);
  124. setValueIfVoid (getPluginEditorNeedsKeyFocus(), false);
  125. setValueIfVoid (getPluginAUExportPrefix(), sanitisedProjectName + "AU");
  126. setValueIfVoid (getPluginRTASCategory(), String::empty);
  127. setValueIfVoid (getBundleIdentifier(), getDefaultBundleIdentifier());
  128. setValueIfVoid (getAAXIdentifier(), getDefaultAAXIdentifier());
  129. setValueIfVoid (getPluginAAXCategory(), "AAX_ePlugInCategory_Dynamics");
  130. }
  131. void Project::updateOldStyleConfigList()
  132. {
  133. ValueTree deprecatedConfigsList (projectRoot.getChildWithName (Ids::CONFIGURATIONS));
  134. if (deprecatedConfigsList.isValid())
  135. {
  136. projectRoot.removeChild (deprecatedConfigsList, nullptr);
  137. for (Project::ExporterIterator exporter (*this); exporter.next();)
  138. {
  139. if (exporter->getNumConfigurations() == 0)
  140. {
  141. ValueTree newConfigs (deprecatedConfigsList.createCopy());
  142. if (! exporter->isXcode())
  143. {
  144. for (int j = newConfigs.getNumChildren(); --j >= 0;)
  145. {
  146. ValueTree config (newConfigs.getChild(j));
  147. config.removeProperty (Ids::osxSDK, nullptr);
  148. config.removeProperty (Ids::osxCompatibility, nullptr);
  149. config.removeProperty (Ids::osxArchitecture, nullptr);
  150. }
  151. }
  152. exporter->settings.addChild (newConfigs, 0, nullptr);
  153. }
  154. }
  155. }
  156. }
  157. void Project::moveOldPropertyFromProjectToAllExporters (Identifier name)
  158. {
  159. if (projectRoot.hasProperty (name))
  160. {
  161. for (Project::ExporterIterator exporter (*this); exporter.next();)
  162. exporter->settings.setProperty (name, projectRoot [name], nullptr);
  163. projectRoot.removeProperty (name, nullptr);
  164. }
  165. }
  166. void Project::removeDefunctExporters()
  167. {
  168. ValueTree exporters (projectRoot.getChildWithName (Ids::EXPORTFORMATS));
  169. for (;;)
  170. {
  171. ValueTree oldVC6Exporter (exporters.getChildWithName ("MSVC6"));
  172. if (oldVC6Exporter.isValid())
  173. exporters.removeChild (oldVC6Exporter, nullptr);
  174. else
  175. break;
  176. }
  177. }
  178. void Project::updateOldModulePaths()
  179. {
  180. for (Project::ExporterIterator exporter (*this); exporter.next();)
  181. exporter->updateOldModulePaths();
  182. }
  183. //==============================================================================
  184. static int getVersionElement (StringRef v, int index)
  185. {
  186. StringArray parts;
  187. parts.addTokens (v, "., ", StringRef());
  188. return parts [parts.size() - index - 1].getIntValue();
  189. }
  190. static int getJuceVersion (const String& v)
  191. {
  192. return getVersionElement (v, 2) * 100000
  193. + getVersionElement (v, 1) * 1000
  194. + getVersionElement (v, 0);
  195. }
  196. static int getBuiltJuceVersion()
  197. {
  198. return JUCE_MAJOR_VERSION * 100000
  199. + JUCE_MINOR_VERSION * 1000
  200. + JUCE_BUILDNUMBER;
  201. }
  202. static bool isAnyModuleNewerThanProjucer (const OwnedArray<ModuleDescription>& modules)
  203. {
  204. for (int i = modules.size(); --i >= 0;)
  205. {
  206. const ModuleDescription* m = modules.getUnchecked(i);
  207. if (m->getID().startsWith ("juce_")
  208. && getJuceVersion (m->getVersion()) > getBuiltJuceVersion())
  209. return true;
  210. }
  211. return false;
  212. }
  213. void Project::warnAboutOldProjucerVersion()
  214. {
  215. ModuleList available;
  216. available.scanAllKnownFolders (*this);
  217. if (isAnyModuleNewerThanProjucer (available.modules))
  218. {
  219. if (ProjucerApplication::getApp().isRunningCommandLine)
  220. std::cout << "WARNING! This version of the Projucer is out-of-date!" << std::endl;
  221. else
  222. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  223. "Projucer",
  224. "This version of the Projucer is out-of-date!"
  225. "\n\n"
  226. "Always make sure that you're running the very latest version, "
  227. "preferably compiled directly from the JUCE repository that you're working with!");
  228. }
  229. }
  230. //==============================================================================
  231. static File lastDocumentOpened;
  232. File Project::getLastDocumentOpened() { return lastDocumentOpened; }
  233. void Project::setLastDocumentOpened (const File& file) { lastDocumentOpened = file; }
  234. static void registerRecentFile (const File& file)
  235. {
  236. RecentlyOpenedFilesList::registerRecentFileNatively (file);
  237. getAppSettings().recentFiles.addFile (file);
  238. getAppSettings().flush();
  239. }
  240. //==============================================================================
  241. Result Project::loadDocument (const File& file)
  242. {
  243. ScopedPointer<XmlElement> xml (XmlDocument::parse (file));
  244. if (xml == nullptr || ! xml->hasTagName (Ids::JUCERPROJECT.toString()))
  245. return Result::fail ("Not a valid Jucer project!");
  246. ValueTree newTree (ValueTree::fromXml (*xml));
  247. if (! newTree.hasType (Ids::JUCERPROJECT))
  248. return Result::fail ("The document contains errors and couldn't be parsed!");
  249. registerRecentFile (file);
  250. enabledModulesList = nullptr;
  251. projectRoot = newTree;
  252. removeDefunctExporters();
  253. setMissingDefaultValues();
  254. updateOldModulePaths();
  255. setChangedFlag (false);
  256. if (! ProjucerApplication::getApp().isRunningCommandLine)
  257. warnAboutOldProjucerVersion();
  258. return Result::ok();
  259. }
  260. Result Project::saveDocument (const File& file)
  261. {
  262. return saveProject (file, false);
  263. }
  264. Result Project::saveProject (const File& file, bool isCommandLineApp)
  265. {
  266. if (isSaving)
  267. return Result::ok();
  268. updateProjectSettings();
  269. sanitiseConfigFlags();
  270. if (! isCommandLineApp)
  271. registerRecentFile (file);
  272. const ScopedValueSetter<bool> vs (isSaving, true, false);
  273. ProjectSaver saver (*this, file);
  274. return saver.save (! isCommandLineApp);
  275. }
  276. Result Project::saveResourcesOnly (const File& file)
  277. {
  278. ProjectSaver saver (*this, file);
  279. return saver.saveResourcesOnly();
  280. }
  281. //==============================================================================
  282. void Project::valueTreePropertyChanged (ValueTree&, const Identifier& property)
  283. {
  284. if (property == Ids::projectType)
  285. setMissingDefaultValues();
  286. changed();
  287. }
  288. void Project::valueTreeChildAdded (ValueTree&, ValueTree&) { changed(); }
  289. void Project::valueTreeChildRemoved (ValueTree&, ValueTree&, int) { changed(); }
  290. void Project::valueTreeChildOrderChanged (ValueTree&, int, int) { changed(); }
  291. void Project::valueTreeParentChanged (ValueTree&) {}
  292. //==============================================================================
  293. File Project::resolveFilename (String filename) const
  294. {
  295. if (filename.isEmpty())
  296. return File::nonexistent;
  297. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename);
  298. if (FileHelpers::isAbsolutePath (filename))
  299. return File::createFileWithoutCheckingPath (FileHelpers::currentOSStylePath (filename)); // (avoid assertions for windows-style paths)
  300. return getFile().getSiblingFile (FileHelpers::currentOSStylePath (filename));
  301. }
  302. String Project::getRelativePathForFile (const File& file) const
  303. {
  304. String filename (file.getFullPathName());
  305. File relativePathBase (getFile().getParentDirectory());
  306. String p1 (relativePathBase.getFullPathName());
  307. String p2 (file.getFullPathName());
  308. while (p1.startsWithChar (File::separator))
  309. p1 = p1.substring (1);
  310. while (p2.startsWithChar (File::separator))
  311. p2 = p2.substring (1);
  312. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  313. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  314. {
  315. filename = FileHelpers::getRelativePathFrom (file, relativePathBase);
  316. }
  317. return filename;
  318. }
  319. //==============================================================================
  320. const ProjectType& Project::getProjectType() const
  321. {
  322. if (const ProjectType* type = ProjectType::findType (getProjectTypeString()))
  323. return *type;
  324. const ProjectType* guiType = ProjectType::findType (ProjectType_GUIApp::getTypeName());
  325. jassert (guiType != nullptr);
  326. return *guiType;
  327. }
  328. //==============================================================================
  329. void Project::createPropertyEditors (PropertyListBuilder& props)
  330. {
  331. props.add (new TextPropertyComponent (getProjectNameValue(), "Project Name", 256, false),
  332. "The name of the project.");
  333. props.add (new TextPropertyComponent (getVersionValue(), "Project Version", 16, false),
  334. "The project's version number, This should be in the format major.minor.point[.point]");
  335. props.add (new TextPropertyComponent (getCompanyName(), "Company Name", 256, false),
  336. "Your company name, which will be added to the properties of the binary where possible");
  337. props.add (new TextPropertyComponent (getCompanyWebsite(), "Company Website", 256, false),
  338. "Your company website, which will be added to the properties of the binary where possible");
  339. props.add (new TextPropertyComponent (getCompanyEmail(), "Company E-mail", 256, false),
  340. "Your company e-mail, which will be added to the properties of the binary where possible");
  341. {
  342. StringArray projectTypeNames;
  343. Array<var> projectTypeCodes;
  344. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  345. for (int i = 0; i < types.size(); ++i)
  346. {
  347. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  348. projectTypeCodes.add (types.getUnchecked(i)->getType());
  349. }
  350. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  351. }
  352. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false),
  353. "A unique identifier for this product, mainly for use in OSX/iOS builds. It should be something like 'com.yourcompanyname.yourproductname'");
  354. if (getProjectType().isAudioPlugin())
  355. createAudioPluginPropertyEditors (props);
  356. {
  357. const int maxSizes[] = { 20480, 10240, 6144, 2048, 1024, 512, 256, 128, 64 };
  358. StringArray maxSizeNames;
  359. Array<var> maxSizeCodes;
  360. maxSizeNames.add (TRANS("Default"));
  361. maxSizeCodes.add (var::null);
  362. maxSizeNames.add (String::empty);
  363. maxSizeCodes.add (var::null);
  364. for (int i = 0; i < numElementsInArray (maxSizes); ++i)
  365. {
  366. const int sizeInBytes = maxSizes[i] * 1024;
  367. maxSizeNames.add (File::descriptionOfSizeInBytes (sizeInBytes));
  368. maxSizeCodes.add (sizeInBytes);
  369. }
  370. props.add (new ChoicePropertyComponent (getMaxBinaryFileSize(), "BinaryData.cpp size limit", maxSizeNames, maxSizeCodes),
  371. "When splitting binary data into multiple cpp files, the Projucer attempts to keep the file sizes below this threshold. "
  372. "(Note that individual resource files which are larger than this size cannot be split across multiple cpp files).");
  373. }
  374. props.add (new BooleanPropertyComponent (shouldIncludeBinaryInAppConfig(), "Include Binary",
  375. "Include BinaryData.h in the AppConfig.h file"));
  376. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, true),
  377. "Global preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, or "
  378. "new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  379. props.add (new TextPropertyComponent (getProjectUserNotes(), "Notes", 32768, true),
  380. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  381. }
  382. void Project::createAudioPluginPropertyEditors (PropertyListBuilder& props)
  383. {
  384. props.add (new BooleanPropertyComponent (shouldBuildVST(), "Build VST", "Enabled"),
  385. "Whether the project should produce a VST plugin.");
  386. props.add (new BooleanPropertyComponent (shouldBuildVST3(), "Build VST3", "Enabled"),
  387. "Whether the project should produce a VST3 plugin.");
  388. props.add (new BooleanPropertyComponent (shouldBuildAU(), "Build AudioUnit", "Enabled"),
  389. "Whether the project should produce an AudioUnit plugin.");
  390. props.add (new BooleanPropertyComponent (shouldBuildAUv3(), "Build AudioUnit v3", "Enabled"),
  391. "Whether the project should produce an AudioUnit version 3 plugin.");
  392. props.add (new BooleanPropertyComponent (shouldBuildRTAS(), "Build RTAS", "Enabled"),
  393. "Whether the project should produce an RTAS plugin.");
  394. props.add (new BooleanPropertyComponent (shouldBuildAAX(), "Build AAX", "Enabled"),
  395. "Whether the project should produce an AAX plugin.");
  396. /* TODO: this property editor is temporarily disabled because right now we build standalone if and only if
  397. we also build AUv3. However as soon as targets are supported on non-Xcode exporters as well, we should
  398. re-enable this option and allow to build the standalone plug-in independently from AUv3.
  399. */
  400. // props.add (new BooleanPropertyComponent (shouldBuildStandalone(), "Build Standalone", "Enabled"),
  401. // "Whether the project should produce a standalone version of the plugin. Required for AUv3.");
  402. props.add (new TextPropertyComponent (getPluginName(), "Plugin Name", 128, false),
  403. "The name of your plugin (keep it short!)");
  404. props.add (new TextPropertyComponent (getPluginDesc(), "Plugin Description", 256, false),
  405. "A short description of your plugin.");
  406. props.add (new TextPropertyComponent (getPluginManufacturer(), "Plugin Manufacturer", 256, false),
  407. "The name of your company (cannot be blank).");
  408. props.add (new TextPropertyComponent (getPluginManufacturerCode(), "Plugin Manufacturer Code", 4, false),
  409. "A four-character unique ID for your company. Note that for AU compatibility, this must contain at least one upper-case letter!");
  410. props.add (new TextPropertyComponent (getPluginCode(), "Plugin Code", 4, false),
  411. "A four-character unique ID for your plugin. Note that for AU compatibility, this must contain at least one upper-case letter!");
  412. props.add (new TextPropertyComponent (getPluginChannelConfigs(), "Plugin Channel Configurations", 1024, false),
  413. "This list is a comma-separated set list in the form {numIns, numOuts} and each pair indicates a valid plug-in "
  414. "configuration. For example {1, 1}, {2, 2} means that the plugin can be used either with 1 input and 1 output, "
  415. "or with 2 inputs and 2 outputs. If your plug-in requires side-chains, aux output buses etc., then you must leave "
  416. "this field empty and override the setPreferredBusArrangement method in your AudioProcessor.");
  417. props.add (new BooleanPropertyComponent (getPluginIsSynth(), "Plugin is a Synth", "Is a Synth"),
  418. "Enable this if you want your plugin to be treated as a synth or generator. It doesn't make much difference to the plugin itself, but some hosts treat synths differently to other plugins.");
  419. props.add (new BooleanPropertyComponent (getPluginWantsMidiInput(), "Plugin Midi Input", "Plugin wants midi input"),
  420. "Enable this if you want your plugin to accept midi messages.");
  421. props.add (new BooleanPropertyComponent (getPluginProducesMidiOut(), "Plugin Midi Output", "Plugin produces midi output"),
  422. "Enable this if your plugin is going to produce midi messages.");
  423. props.add (new BooleanPropertyComponent (getPluginIsMidiEffectPlugin(), "Midi Effect Plugin", "Plugin is a midi effect plugin"),
  424. "Enable this if your plugin only processes midi and no audio.");
  425. props.add (new BooleanPropertyComponent (getPluginEditorNeedsKeyFocus(), "Key Focus", "Plugin editor requires keyboard focus"),
  426. "Enable this if your plugin needs keyboard input - some hosts can be a bit funny about keyboard focus..");
  427. props.add (new TextPropertyComponent (getPluginAUExportPrefix(), "Plugin AU Export Prefix", 64, false),
  428. "A prefix for the names of exported entry-point functions that the component exposes - typically this will be a version of your plugin's name that can be used as part of a C++ token.");
  429. props.add (new TextPropertyComponent (getPluginAUMainType(), "Plugin AU Main Type", 128, false),
  430. "In an AU, this is the value that is set as JucePlugin_AUMainType. Leave it blank unless you want to use a custom value.");
  431. props.add (new TextPropertyComponent (getPluginVSTCategory(), "VST Category", 64, false),
  432. "In a VST, this is the value that is set as JucePlugin_VSTCategory. Leave it blank unless you want to use a custom value.");
  433. props.add (new TextPropertyComponent (getPluginRTASCategory(), "Plugin RTAS Category", 64, false),
  434. "(Leave this blank if your plugin is a synth). This is one of the RTAS categories from FicPluginEnums.h, such as: ePlugInCategory_None, ePlugInCategory_EQ, ePlugInCategory_Dynamics, "
  435. "ePlugInCategory_PitchShift, ePlugInCategory_Reverb, ePlugInCategory_Delay, "
  436. "ePlugInCategory_Modulation, ePlugInCategory_Harmonic, ePlugInCategory_NoiseReduction, "
  437. "ePlugInCategory_Dither, ePlugInCategory_SoundField");
  438. props.add (new TextPropertyComponent (getPluginAAXCategory(), "Plugin AAX Category", 64, false),
  439. "This is one of the categories from the AAX_EPlugInCategory enum");
  440. props.add (new TextPropertyComponent (getAAXIdentifier(), "Plugin AAX Identifier", 256, false),
  441. "The value to use for the JucePlugin_AAXIdentifier setting");
  442. }
  443. //==============================================================================
  444. static StringArray getVersionSegments (const Project& p)
  445. {
  446. StringArray segments;
  447. segments.addTokens (p.getVersionString(), ",.", "");
  448. segments.trim();
  449. segments.removeEmptyStrings();
  450. return segments;
  451. }
  452. int Project::getVersionAsHexInteger() const
  453. {
  454. const StringArray segments (getVersionSegments (*this));
  455. int value = (segments[0].getIntValue() << 16)
  456. + (segments[1].getIntValue() << 8)
  457. + segments[2].getIntValue();
  458. if (segments.size() >= 4)
  459. value = (value << 8) + segments[3].getIntValue();
  460. return value;
  461. }
  462. String Project::getVersionAsHex() const
  463. {
  464. return "0x" + String::toHexString (getVersionAsHexInteger());
  465. }
  466. StringPairArray Project::getPreprocessorDefs() const
  467. {
  468. return parsePreprocessorDefs (projectRoot [Ids::defines]);
  469. }
  470. File Project::getBinaryDataCppFile (int index) const
  471. {
  472. const File cpp (getGeneratedCodeFolder().getChildFile ("BinaryData.cpp"));
  473. if (index > 0)
  474. return cpp.getSiblingFile (cpp.getFileNameWithoutExtension() + String (index + 1))
  475. .withFileExtension (cpp.getFileExtension());
  476. return cpp;
  477. }
  478. Project::Item Project::getMainGroup()
  479. {
  480. return Item (*this, projectRoot.getChildWithName (Ids::MAINGROUP), false);
  481. }
  482. PropertiesFile& Project::getStoredProperties() const
  483. {
  484. return getAppSettings().getProjectProperties (getProjectUID());
  485. }
  486. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  487. {
  488. if (item.isImageFile())
  489. {
  490. found.add (new Project::Item (item));
  491. }
  492. else if (item.isGroup())
  493. {
  494. for (int i = 0; i < item.getNumChildren(); ++i)
  495. findImages (item.getChild (i), found);
  496. }
  497. }
  498. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  499. {
  500. findImages (getMainGroup(), items);
  501. }
  502. //==============================================================================
  503. Project::Item::Item (Project& p, const ValueTree& s, bool isModuleCode)
  504. : project (p), state (s), belongsToModule (isModuleCode)
  505. {
  506. }
  507. Project::Item::Item (const Item& other)
  508. : project (other.project), state (other.state), belongsToModule (other.belongsToModule)
  509. {
  510. }
  511. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  512. String Project::Item::getID() const { return state [Ids::ID]; }
  513. void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); }
  514. Drawable* Project::Item::loadAsImageFile() const
  515. {
  516. return isValid() ? Drawable::createFromImageFile (getFile())
  517. : nullptr;
  518. }
  519. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid, bool isModuleCode)
  520. {
  521. Item group (project, ValueTree (Ids::GROUP), isModuleCode);
  522. group.setID (uid);
  523. group.initialiseMissingProperties();
  524. group.getNameValue() = name;
  525. return group;
  526. }
  527. bool Project::Item::isFile() const { return state.hasType (Ids::FILE); }
  528. bool Project::Item::isGroup() const { return state.hasType (Ids::GROUP) || isMainGroup(); }
  529. bool Project::Item::isMainGroup() const { return state.hasType (Ids::MAINGROUP); }
  530. bool Project::Item::isImageFile() const
  531. {
  532. return isFile() && (ImageFileFormat::findImageFormatForFileExtension (getFile()) != nullptr
  533. || getFile().hasFileExtension ("svg"));
  534. }
  535. Project::Item Project::Item::findItemWithID (const String& targetId) const
  536. {
  537. if (state [Ids::ID] == targetId)
  538. return *this;
  539. if (isGroup())
  540. {
  541. for (int i = getNumChildren(); --i >= 0;)
  542. {
  543. Item found (getChild(i).findItemWithID (targetId));
  544. if (found.isValid())
  545. return found;
  546. }
  547. }
  548. return Item (project, ValueTree(), false);
  549. }
  550. bool Project::Item::canContain (const Item& child) const
  551. {
  552. if (isFile())
  553. return false;
  554. if (isGroup())
  555. return child.isFile() || child.isGroup();
  556. jassertfalse;
  557. return false;
  558. }
  559. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  560. Value Project::Item::getShouldCompileValue() { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  561. bool Project::Item::shouldBeCompiled() const { return state [Ids::compile]; }
  562. Value Project::Item::getShouldAddToBinaryResourcesValue() { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  563. bool Project::Item::shouldBeAddedToBinaryResources() const { return state [Ids::resource]; }
  564. Value Project::Item::getShouldAddToXcodeResourcesValue() { return state.getPropertyAsValue (Ids::xcodeResource, getUndoManager()); }
  565. bool Project::Item::shouldBeAddedToXcodeResources() const { return state [Ids::xcodeResource]; }
  566. Value Project::Item::getShouldInhibitWarningsValue() { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  567. bool Project::Item::shouldInhibitWarnings() const { return state [Ids::noWarnings]; }
  568. bool Project::Item::isModuleCode() const { return belongsToModule; }
  569. String Project::Item::getFilePath() const
  570. {
  571. if (isFile())
  572. return state [Ids::file].toString();
  573. return String();
  574. }
  575. File Project::Item::getFile() const
  576. {
  577. if (isFile())
  578. return project.resolveFilename (state [Ids::file].toString());
  579. return File::nonexistent;
  580. }
  581. void Project::Item::setFile (const File& file)
  582. {
  583. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  584. jassert (getFile() == file);
  585. }
  586. void Project::Item::setFile (const RelativePath& file)
  587. {
  588. jassert (isFile());
  589. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  590. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  591. }
  592. bool Project::Item::renameFile (const File& newFile)
  593. {
  594. const File oldFile (getFile());
  595. if (oldFile.moveFileTo (newFile)
  596. || (newFile.exists() && ! oldFile.exists()))
  597. {
  598. setFile (newFile);
  599. ProjucerApplication::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  600. return true;
  601. }
  602. return false;
  603. }
  604. bool Project::Item::containsChildForFile (const RelativePath& file) const
  605. {
  606. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  607. }
  608. Project::Item Project::Item::findItemForFile (const File& file) const
  609. {
  610. if (getFile() == file)
  611. return *this;
  612. if (isGroup())
  613. {
  614. for (int i = getNumChildren(); --i >= 0;)
  615. {
  616. Item found (getChild(i).findItemForFile (file));
  617. if (found.isValid())
  618. return found;
  619. }
  620. }
  621. return Item (project, ValueTree(), false);
  622. }
  623. File Project::Item::determineGroupFolder() const
  624. {
  625. jassert (isGroup());
  626. File f;
  627. for (int i = 0; i < getNumChildren(); ++i)
  628. {
  629. f = getChild(i).getFile();
  630. if (f.exists())
  631. return f.getParentDirectory();
  632. }
  633. Item parent (getParent());
  634. if (parent != *this)
  635. {
  636. f = parent.determineGroupFolder();
  637. if (f.getChildFile (getName()).isDirectory())
  638. f = f.getChildFile (getName());
  639. }
  640. else
  641. {
  642. f = project.getProjectFolder();
  643. if (f.getChildFile ("Source").isDirectory())
  644. f = f.getChildFile ("Source");
  645. }
  646. return f;
  647. }
  648. void Project::Item::initialiseMissingProperties()
  649. {
  650. if (! state.hasProperty (Ids::ID))
  651. setID (createAlphaNumericUID());
  652. if (isFile())
  653. {
  654. state.setProperty (Ids::name, getFile().getFileName(), nullptr);
  655. }
  656. else if (isGroup())
  657. {
  658. for (int i = getNumChildren(); --i >= 0;)
  659. getChild(i).initialiseMissingProperties();
  660. }
  661. }
  662. Value Project::Item::getNameValue()
  663. {
  664. return state.getPropertyAsValue (Ids::name, getUndoManager());
  665. }
  666. String Project::Item::getName() const
  667. {
  668. return state [Ids::name];
  669. }
  670. void Project::Item::addChild (const Item& newChild, int insertIndex)
  671. {
  672. state.addChild (newChild.state, insertIndex, getUndoManager());
  673. }
  674. void Project::Item::removeItemFromProject()
  675. {
  676. state.getParent().removeChild (state, getUndoManager());
  677. }
  678. Project::Item Project::Item::getParent() const
  679. {
  680. if (isMainGroup() || ! isGroup())
  681. return *this;
  682. return Item (project, state.getParent(), belongsToModule);
  683. }
  684. struct ItemSorter
  685. {
  686. static int compareElements (const ValueTree& first, const ValueTree& second)
  687. {
  688. return first [Ids::name].toString().compareNatural (second [Ids::name].toString());
  689. }
  690. };
  691. struct ItemSorterWithGroupsAtStart
  692. {
  693. static int compareElements (const ValueTree& first, const ValueTree& second)
  694. {
  695. const bool firstIsGroup = first.hasType (Ids::GROUP);
  696. const bool secondIsGroup = second.hasType (Ids::GROUP);
  697. if (firstIsGroup == secondIsGroup)
  698. return first [Ids::name].toString().compareNatural (second [Ids::name].toString());
  699. return firstIsGroup ? -1 : 1;
  700. }
  701. };
  702. static void sortGroup (ValueTree& state, bool keepGroupsAtStart, UndoManager* undoManager)
  703. {
  704. if (keepGroupsAtStart)
  705. {
  706. ItemSorterWithGroupsAtStart sorter;
  707. state.sort (sorter, undoManager, true);
  708. }
  709. else
  710. {
  711. ItemSorter sorter;
  712. state.sort (sorter, undoManager, true);
  713. }
  714. }
  715. static bool isGroupSorted (const ValueTree& state, bool keepGroupsAtStart)
  716. {
  717. if (state.getNumChildren() == 0)
  718. return false;
  719. if (state.getNumChildren() == 1)
  720. return true;
  721. ValueTree stateCopy (state.createCopy());
  722. sortGroup (stateCopy, keepGroupsAtStart, nullptr);
  723. return stateCopy.isEquivalentTo (state);
  724. }
  725. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  726. {
  727. sortGroup (state, keepGroupsAtStart, getUndoManager());
  728. }
  729. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  730. {
  731. for (int i = state.getNumChildren(); --i >= 0;)
  732. {
  733. const ValueTree child (state.getChild (i));
  734. if (child.getProperty (Ids::name) == name && child.hasType (Ids::GROUP))
  735. return Item (project, child, belongsToModule);
  736. }
  737. return addNewSubGroup (name, -1);
  738. }
  739. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  740. {
  741. String newID (createGUID (getID() + name + String (getNumChildren())));
  742. int n = 0;
  743. while (project.getMainGroup().findItemWithID (newID).isValid())
  744. newID = createGUID (newID + String (++n));
  745. Item group (createGroup (project, name, newID, belongsToModule));
  746. jassert (canContain (group));
  747. addChild (group, insertIndex);
  748. return group;
  749. }
  750. bool Project::Item::addFileAtIndex (const File& file, int insertIndex, const bool shouldCompile)
  751. {
  752. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  753. return false;
  754. if (file.isDirectory())
  755. {
  756. Item group (addNewSubGroup (file.getFileName(), insertIndex));
  757. for (DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories); iter.next();)
  758. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  759. group.addFileRetainingSortOrder (iter.getFile(), shouldCompile);
  760. }
  761. else if (file.existsAsFile())
  762. {
  763. if (! project.getMainGroup().findItemForFile (file).isValid())
  764. addFileUnchecked (file, insertIndex, shouldCompile);
  765. }
  766. else
  767. {
  768. jassertfalse;
  769. }
  770. return true;
  771. }
  772. bool Project::Item::addFileRetainingSortOrder (const File& file, bool shouldCompile)
  773. {
  774. const bool wasSortedGroupsNotFirst = isGroupSorted (state, false);
  775. const bool wasSortedGroupsFirst = isGroupSorted (state, true);
  776. if (! addFileAtIndex (file, 0, shouldCompile))
  777. return false;
  778. if (wasSortedGroupsNotFirst || wasSortedGroupsFirst)
  779. sortAlphabetically (wasSortedGroupsFirst);
  780. return true;
  781. }
  782. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  783. {
  784. Item item (project, ValueTree (Ids::FILE), belongsToModule);
  785. item.initialiseMissingProperties();
  786. item.getNameValue() = file.getFileName();
  787. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension (fileTypesToCompileByDefault);
  788. item.getShouldAddToBinaryResourcesValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  789. if (canContain (item))
  790. {
  791. item.setFile (file);
  792. addChild (item, insertIndex);
  793. }
  794. }
  795. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  796. {
  797. Item item (project, ValueTree (Ids::FILE), belongsToModule);
  798. item.initialiseMissingProperties();
  799. item.getNameValue() = file.getFileName();
  800. item.getShouldCompileValue() = shouldCompile;
  801. item.getShouldAddToBinaryResourcesValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  802. if (canContain (item))
  803. {
  804. item.setFile (file);
  805. addChild (item, insertIndex);
  806. return true;
  807. }
  808. return false;
  809. }
  810. Icon Project::Item::getIcon() const
  811. {
  812. const Icons& icons = getIcons();
  813. if (isFile())
  814. {
  815. if (isImageFile())
  816. return Icon (icons.imageDoc, Colours::blue);
  817. return Icon (icons.document, Colours::yellow);
  818. }
  819. if (isMainGroup())
  820. return Icon (icons.juceLogo, Colours::orange);
  821. return Icon (icons.folder, Colours::darkgrey);
  822. }
  823. bool Project::Item::isIconCrossedOut() const
  824. {
  825. return isFile()
  826. && ! (shouldBeCompiled()
  827. || shouldBeAddedToBinaryResources()
  828. || getFile().hasFileExtension (headerFileExtensions));
  829. }
  830. //==============================================================================
  831. ValueTree Project::getConfigNode()
  832. {
  833. return projectRoot.getOrCreateChildWithName (Ids::JUCEOPTIONS, nullptr);
  834. }
  835. const char* const Project::configFlagDefault = "default";
  836. const char* const Project::configFlagEnabled = "enabled";
  837. const char* const Project::configFlagDisabled = "disabled";
  838. Value Project::getConfigFlag (const String& name)
  839. {
  840. ValueTree configNode (getConfigNode());
  841. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  842. if (v.getValue().toString().isEmpty())
  843. v = configFlagDefault;
  844. return v;
  845. }
  846. bool Project::isConfigFlagEnabled (const String& name) const
  847. {
  848. return projectRoot.getChildWithName (Ids::JUCEOPTIONS).getProperty (name) == configFlagEnabled;
  849. }
  850. void Project::sanitiseConfigFlags()
  851. {
  852. ValueTree configNode (getConfigNode());
  853. for (int i = configNode.getNumProperties(); --i >= 0;)
  854. {
  855. const var value (configNode [configNode.getPropertyName(i)]);
  856. if (value != configFlagEnabled && value != configFlagDisabled)
  857. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  858. }
  859. }
  860. //==============================================================================
  861. String Project::getPluginRTASCategoryCode()
  862. {
  863. if (static_cast<bool> (getPluginIsSynth().getValue()))
  864. return "ePlugInCategory_SWGenerators";
  865. String s (getPluginRTASCategory().toString());
  866. if (s.isEmpty())
  867. s = "ePlugInCategory_None";
  868. return s;
  869. }
  870. String Project::getAUMainTypeString()
  871. {
  872. String s (getPluginAUMainType().toString());
  873. if (s.isEmpty())
  874. {
  875. if (getPluginIsSynth().getValue()) s = "kAudioUnitType_MusicDevice";
  876. else if (getPluginWantsMidiInput().getValue()) s = "kAudioUnitType_MusicEffect";
  877. else s = "kAudioUnitType_Effect";
  878. }
  879. return s;
  880. }
  881. String Project::getAUMainTypeCode()
  882. {
  883. String s (getPluginAUMainType().toString());
  884. if (s.isEmpty())
  885. {
  886. if (getPluginIsMidiEffectPlugin().getValue()) s = "aumi";
  887. else if (getPluginIsSynth().getValue()) s = "aumu";
  888. else if (getPluginWantsMidiInput().getValue()) s = "aumf";
  889. else s = "aufx";
  890. }
  891. return s;
  892. }
  893. String Project::getPluginVSTCategoryString()
  894. {
  895. String s (getPluginVSTCategory().toString().trim());
  896. if (s.isEmpty())
  897. s = static_cast<bool> (getPluginIsSynth().getValue()) ? "kPlugCategSynth"
  898. : "kPlugCategEffect";
  899. return s;
  900. }
  901. bool Project::isAUPluginHost()
  902. {
  903. return getModules().isModuleEnabled ("juce_audio_processors") && isConfigFlagEnabled ("JUCE_PLUGINHOST_AU");
  904. }
  905. bool Project::isVSTPluginHost()
  906. {
  907. return getModules().isModuleEnabled ("juce_audio_processors") && isConfigFlagEnabled ("JUCE_PLUGINHOST_VST");
  908. }
  909. bool Project::isVST3PluginHost()
  910. {
  911. return getModules().isModuleEnabled ("juce_audio_processors") && isConfigFlagEnabled ("JUCE_PLUGINHOST_VST3");
  912. }
  913. //==============================================================================
  914. EnabledModuleList& Project::getModules()
  915. {
  916. if (enabledModulesList == nullptr)
  917. enabledModulesList = new EnabledModuleList (*this, projectRoot.getOrCreateChildWithName (Ids::MODULES, nullptr));
  918. return *enabledModulesList;
  919. }
  920. //==============================================================================
  921. ValueTree Project::getExporters()
  922. {
  923. return projectRoot.getOrCreateChildWithName (Ids::EXPORTFORMATS, nullptr);
  924. }
  925. int Project::getNumExporters()
  926. {
  927. return getExporters().getNumChildren();
  928. }
  929. ProjectExporter* Project::createExporter (int index)
  930. {
  931. jassert (index >= 0 && index < getNumExporters());
  932. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  933. }
  934. void Project::addNewExporter (const String& exporterName)
  935. {
  936. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  937. ValueTree exporters (getExporters());
  938. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  939. }
  940. void Project::createExporterForCurrentPlatform()
  941. {
  942. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  943. }
  944. //==============================================================================
  945. String Project::getFileTemplate (const String& templateName)
  946. {
  947. int dataSize;
  948. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  949. if (data == nullptr)
  950. {
  951. jassertfalse;
  952. return String::empty;
  953. }
  954. return String::fromUTF8 (data, dataSize);
  955. }
  956. //==============================================================================
  957. Project::ExporterIterator::ExporterIterator (Project& p) : index (-1), project (p) {}
  958. Project::ExporterIterator::~ExporterIterator() {}
  959. bool Project::ExporterIterator::next()
  960. {
  961. if (++index >= project.getNumExporters())
  962. return false;
  963. exporter = project.createExporter (index);
  964. if (exporter == nullptr)
  965. {
  966. jassertfalse; // corrupted project file?
  967. return next();
  968. }
  969. return true;
  970. }