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.

1237 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. Value Project::Item::getShouldUseStdCallValue() { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  569. bool Project::Item::shouldUseStdCall() const { return state [Ids::useStdCall]; }
  570. bool Project::Item::isModuleCode() const { return belongsToModule; }
  571. String Project::Item::getFilePath() const
  572. {
  573. if (isFile())
  574. return state [Ids::file].toString();
  575. return String();
  576. }
  577. File Project::Item::getFile() const
  578. {
  579. if (isFile())
  580. return project.resolveFilename (state [Ids::file].toString());
  581. return File::nonexistent;
  582. }
  583. void Project::Item::setFile (const File& file)
  584. {
  585. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  586. jassert (getFile() == file);
  587. }
  588. void Project::Item::setFile (const RelativePath& file)
  589. {
  590. jassert (isFile());
  591. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  592. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  593. }
  594. bool Project::Item::renameFile (const File& newFile)
  595. {
  596. const File oldFile (getFile());
  597. if (oldFile.moveFileTo (newFile)
  598. || (newFile.exists() && ! oldFile.exists()))
  599. {
  600. setFile (newFile);
  601. ProjucerApplication::getApp().openDocumentManager.fileHasBeenRenamed (oldFile, newFile);
  602. return true;
  603. }
  604. return false;
  605. }
  606. bool Project::Item::containsChildForFile (const RelativePath& file) const
  607. {
  608. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  609. }
  610. Project::Item Project::Item::findItemForFile (const File& file) const
  611. {
  612. if (getFile() == file)
  613. return *this;
  614. if (isGroup())
  615. {
  616. for (int i = getNumChildren(); --i >= 0;)
  617. {
  618. Item found (getChild(i).findItemForFile (file));
  619. if (found.isValid())
  620. return found;
  621. }
  622. }
  623. return Item (project, ValueTree(), false);
  624. }
  625. File Project::Item::determineGroupFolder() const
  626. {
  627. jassert (isGroup());
  628. File f;
  629. for (int i = 0; i < getNumChildren(); ++i)
  630. {
  631. f = getChild(i).getFile();
  632. if (f.exists())
  633. return f.getParentDirectory();
  634. }
  635. Item parent (getParent());
  636. if (parent != *this)
  637. {
  638. f = parent.determineGroupFolder();
  639. if (f.getChildFile (getName()).isDirectory())
  640. f = f.getChildFile (getName());
  641. }
  642. else
  643. {
  644. f = project.getProjectFolder();
  645. if (f.getChildFile ("Source").isDirectory())
  646. f = f.getChildFile ("Source");
  647. }
  648. return f;
  649. }
  650. void Project::Item::initialiseMissingProperties()
  651. {
  652. if (! state.hasProperty (Ids::ID))
  653. setID (createAlphaNumericUID());
  654. if (isFile())
  655. {
  656. state.setProperty (Ids::name, getFile().getFileName(), nullptr);
  657. }
  658. else if (isGroup())
  659. {
  660. for (int i = getNumChildren(); --i >= 0;)
  661. getChild(i).initialiseMissingProperties();
  662. }
  663. }
  664. Value Project::Item::getNameValue()
  665. {
  666. return state.getPropertyAsValue (Ids::name, getUndoManager());
  667. }
  668. String Project::Item::getName() const
  669. {
  670. return state [Ids::name];
  671. }
  672. void Project::Item::addChild (const Item& newChild, int insertIndex)
  673. {
  674. state.addChild (newChild.state, insertIndex, getUndoManager());
  675. }
  676. void Project::Item::removeItemFromProject()
  677. {
  678. state.getParent().removeChild (state, getUndoManager());
  679. }
  680. Project::Item Project::Item::getParent() const
  681. {
  682. if (isMainGroup() || ! isGroup())
  683. return *this;
  684. return Item (project, state.getParent(), belongsToModule);
  685. }
  686. struct ItemSorter
  687. {
  688. static int compareElements (const ValueTree& first, const ValueTree& second)
  689. {
  690. return first [Ids::name].toString().compareNatural (second [Ids::name].toString());
  691. }
  692. };
  693. struct ItemSorterWithGroupsAtStart
  694. {
  695. static int compareElements (const ValueTree& first, const ValueTree& second)
  696. {
  697. const bool firstIsGroup = first.hasType (Ids::GROUP);
  698. const bool secondIsGroup = second.hasType (Ids::GROUP);
  699. if (firstIsGroup == secondIsGroup)
  700. return first [Ids::name].toString().compareNatural (second [Ids::name].toString());
  701. return firstIsGroup ? -1 : 1;
  702. }
  703. };
  704. static void sortGroup (ValueTree& state, bool keepGroupsAtStart, UndoManager* undoManager)
  705. {
  706. if (keepGroupsAtStart)
  707. {
  708. ItemSorterWithGroupsAtStart sorter;
  709. state.sort (sorter, undoManager, true);
  710. }
  711. else
  712. {
  713. ItemSorter sorter;
  714. state.sort (sorter, undoManager, true);
  715. }
  716. }
  717. static bool isGroupSorted (const ValueTree& state, bool keepGroupsAtStart)
  718. {
  719. if (state.getNumChildren() == 0)
  720. return false;
  721. if (state.getNumChildren() == 1)
  722. return true;
  723. ValueTree stateCopy (state.createCopy());
  724. sortGroup (stateCopy, keepGroupsAtStart, nullptr);
  725. return stateCopy.isEquivalentTo (state);
  726. }
  727. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  728. {
  729. sortGroup (state, keepGroupsAtStart, getUndoManager());
  730. }
  731. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  732. {
  733. for (int i = state.getNumChildren(); --i >= 0;)
  734. {
  735. const ValueTree child (state.getChild (i));
  736. if (child.getProperty (Ids::name) == name && child.hasType (Ids::GROUP))
  737. return Item (project, child, belongsToModule);
  738. }
  739. return addNewSubGroup (name, -1);
  740. }
  741. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  742. {
  743. String newID (createGUID (getID() + name + String (getNumChildren())));
  744. int n = 0;
  745. while (project.getMainGroup().findItemWithID (newID).isValid())
  746. newID = createGUID (newID + String (++n));
  747. Item group (createGroup (project, name, newID, belongsToModule));
  748. jassert (canContain (group));
  749. addChild (group, insertIndex);
  750. return group;
  751. }
  752. bool Project::Item::addFileAtIndex (const File& file, int insertIndex, const bool shouldCompile)
  753. {
  754. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  755. return false;
  756. if (file.isDirectory())
  757. {
  758. Item group (addNewSubGroup (file.getFileName(), insertIndex));
  759. for (DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories); iter.next();)
  760. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  761. group.addFileRetainingSortOrder (iter.getFile(), shouldCompile);
  762. }
  763. else if (file.existsAsFile())
  764. {
  765. if (! project.getMainGroup().findItemForFile (file).isValid())
  766. addFileUnchecked (file, insertIndex, shouldCompile);
  767. }
  768. else
  769. {
  770. jassertfalse;
  771. }
  772. return true;
  773. }
  774. bool Project::Item::addFileRetainingSortOrder (const File& file, bool shouldCompile)
  775. {
  776. const bool wasSortedGroupsNotFirst = isGroupSorted (state, false);
  777. const bool wasSortedGroupsFirst = isGroupSorted (state, true);
  778. if (! addFileAtIndex (file, 0, shouldCompile))
  779. return false;
  780. if (wasSortedGroupsNotFirst || wasSortedGroupsFirst)
  781. sortAlphabetically (wasSortedGroupsFirst);
  782. return true;
  783. }
  784. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  785. {
  786. Item item (project, ValueTree (Ids::FILE), belongsToModule);
  787. item.initialiseMissingProperties();
  788. item.getNameValue() = file.getFileName();
  789. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension (fileTypesToCompileByDefault);
  790. item.getShouldAddToBinaryResourcesValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  791. if (canContain (item))
  792. {
  793. item.setFile (file);
  794. addChild (item, insertIndex);
  795. }
  796. }
  797. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  798. {
  799. Item item (project, ValueTree (Ids::FILE), belongsToModule);
  800. item.initialiseMissingProperties();
  801. item.getNameValue() = file.getFileName();
  802. item.getShouldCompileValue() = shouldCompile;
  803. item.getShouldAddToBinaryResourcesValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  804. if (canContain (item))
  805. {
  806. item.setFile (file);
  807. addChild (item, insertIndex);
  808. return true;
  809. }
  810. return false;
  811. }
  812. Icon Project::Item::getIcon() const
  813. {
  814. const Icons& icons = getIcons();
  815. if (isFile())
  816. {
  817. if (isImageFile())
  818. return Icon (icons.imageDoc, Colours::blue);
  819. return Icon (icons.document, Colours::yellow);
  820. }
  821. if (isMainGroup())
  822. return Icon (icons.juceLogo, Colours::orange);
  823. return Icon (icons.folder, Colours::darkgrey);
  824. }
  825. bool Project::Item::isIconCrossedOut() const
  826. {
  827. return isFile()
  828. && ! (shouldBeCompiled()
  829. || shouldBeAddedToBinaryResources()
  830. || getFile().hasFileExtension (headerFileExtensions));
  831. }
  832. //==============================================================================
  833. ValueTree Project::getConfigNode()
  834. {
  835. return projectRoot.getOrCreateChildWithName (Ids::JUCEOPTIONS, nullptr);
  836. }
  837. const char* const Project::configFlagDefault = "default";
  838. const char* const Project::configFlagEnabled = "enabled";
  839. const char* const Project::configFlagDisabled = "disabled";
  840. Value Project::getConfigFlag (const String& name)
  841. {
  842. ValueTree configNode (getConfigNode());
  843. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  844. if (v.getValue().toString().isEmpty())
  845. v = configFlagDefault;
  846. return v;
  847. }
  848. bool Project::isConfigFlagEnabled (const String& name) const
  849. {
  850. return projectRoot.getChildWithName (Ids::JUCEOPTIONS).getProperty (name) == configFlagEnabled;
  851. }
  852. void Project::sanitiseConfigFlags()
  853. {
  854. ValueTree configNode (getConfigNode());
  855. for (int i = configNode.getNumProperties(); --i >= 0;)
  856. {
  857. const var value (configNode [configNode.getPropertyName(i)]);
  858. if (value != configFlagEnabled && value != configFlagDisabled)
  859. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  860. }
  861. }
  862. //==============================================================================
  863. String Project::getPluginRTASCategoryCode()
  864. {
  865. if (static_cast<bool> (getPluginIsSynth().getValue()))
  866. return "ePlugInCategory_SWGenerators";
  867. String s (getPluginRTASCategory().toString());
  868. if (s.isEmpty())
  869. s = "ePlugInCategory_None";
  870. return s;
  871. }
  872. String Project::getAUMainTypeString()
  873. {
  874. String s (getPluginAUMainType().toString());
  875. if (s.isEmpty())
  876. {
  877. if (getPluginIsSynth().getValue()) s = "kAudioUnitType_MusicDevice";
  878. else if (getPluginWantsMidiInput().getValue()) s = "kAudioUnitType_MusicEffect";
  879. else s = "kAudioUnitType_Effect";
  880. }
  881. return s;
  882. }
  883. String Project::getAUMainTypeCode()
  884. {
  885. String s (getPluginAUMainType().toString());
  886. if (s.isEmpty())
  887. {
  888. if (getPluginIsMidiEffectPlugin().getValue()) s = "aumi";
  889. else if (getPluginIsSynth().getValue()) s = "aumu";
  890. else if (getPluginWantsMidiInput().getValue()) s = "aumf";
  891. else s = "aufx";
  892. }
  893. return s;
  894. }
  895. String Project::getPluginVSTCategoryString()
  896. {
  897. String s (getPluginVSTCategory().toString().trim());
  898. if (s.isEmpty())
  899. s = static_cast<bool> (getPluginIsSynth().getValue()) ? "kPlugCategSynth"
  900. : "kPlugCategEffect";
  901. return s;
  902. }
  903. bool Project::isAUPluginHost()
  904. {
  905. return getModules().isModuleEnabled ("juce_audio_processors") && isConfigFlagEnabled ("JUCE_PLUGINHOST_AU");
  906. }
  907. bool Project::isVSTPluginHost()
  908. {
  909. return getModules().isModuleEnabled ("juce_audio_processors") && isConfigFlagEnabled ("JUCE_PLUGINHOST_VST");
  910. }
  911. bool Project::isVST3PluginHost()
  912. {
  913. return getModules().isModuleEnabled ("juce_audio_processors") && isConfigFlagEnabled ("JUCE_PLUGINHOST_VST3");
  914. }
  915. //==============================================================================
  916. EnabledModuleList& Project::getModules()
  917. {
  918. if (enabledModulesList == nullptr)
  919. enabledModulesList = new EnabledModuleList (*this, projectRoot.getOrCreateChildWithName (Ids::MODULES, nullptr));
  920. return *enabledModulesList;
  921. }
  922. //==============================================================================
  923. ValueTree Project::getExporters()
  924. {
  925. return projectRoot.getOrCreateChildWithName (Ids::EXPORTFORMATS, nullptr);
  926. }
  927. int Project::getNumExporters()
  928. {
  929. return getExporters().getNumChildren();
  930. }
  931. ProjectExporter* Project::createExporter (int index)
  932. {
  933. jassert (index >= 0 && index < getNumExporters());
  934. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  935. }
  936. void Project::addNewExporter (const String& exporterName)
  937. {
  938. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  939. ValueTree exporters (getExporters());
  940. exporters.addChild (exp->settings, -1, getUndoManagerFor (exporters));
  941. }
  942. void Project::createExporterForCurrentPlatform()
  943. {
  944. addNewExporter (ProjectExporter::getCurrentPlatformExporterName());
  945. }
  946. //==============================================================================
  947. String Project::getFileTemplate (const String& templateName)
  948. {
  949. int dataSize;
  950. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  951. if (data == nullptr)
  952. {
  953. jassertfalse;
  954. return String::empty;
  955. }
  956. return String::fromUTF8 (data, dataSize);
  957. }
  958. //==============================================================================
  959. Project::ExporterIterator::ExporterIterator (Project& p) : index (-1), project (p) {}
  960. Project::ExporterIterator::~ExporterIterator() {}
  961. bool Project::ExporterIterator::next()
  962. {
  963. if (++index >= project.getNumExporters())
  964. return false;
  965. exporter = project.createExporter (index);
  966. if (exporter == nullptr)
  967. {
  968. jassertfalse; // corrupted project file?
  969. return next();
  970. }
  971. return true;
  972. }