Package.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "platform/Package.h"
  2. #include "core/StorePackage.h"
  3. #include "platform/Paths.h"
  4. #if JUCE_WINDOWS
  5. #ifndef NOMINMAX
  6. #define NOMINMAX
  7. #endif
  8. #include <windows.h>
  9. #include <shellapi.h>
  10. #endif
  11. namespace lynx::platform
  12. {
  13. namespace
  14. {
  15. struct Identity
  16. {
  17. juce::String family, fullName;
  18. bool simulated = false;
  19. };
  20. const Identity& identity()
  21. {
  22. static const Identity id = [] {
  23. Identity result;
  24. #if JUCE_WINDOWS
  25. // looked up at run time: kernel32 has them since Windows 8, and MinGW's appmodel.h depends on _WIN32_WINNT
  26. using GetName = LONG (WINAPI*) (UINT32*, PWSTR);
  27. if (auto* kernel = GetModuleHandleW (L"kernel32.dll"))
  28. {
  29. const auto query = [kernel] (const char* function) -> juce::String {
  30. auto* get = reinterpret_cast<GetName> (reinterpret_cast<void*> (GetProcAddress (kernel, function)));
  31. if (get == nullptr)
  32. return {};
  33. UINT32 length = 0;
  34. if (get (&length, nullptr) != ERROR_INSUFFICIENT_BUFFER || length == 0 || length > 1024)
  35. return {}; // APPMODEL_ERROR_NO_PACKAGE: a normal, unpackaged process
  36. std::wstring name (length, L'\0');
  37. if (get (&length, name.data()) != ERROR_SUCCESS)
  38. return {};
  39. return juce::String (name.c_str());
  40. };
  41. result.family = query ("GetCurrentPackageFamilyName");
  42. result.fullName = query ("GetCurrentPackageFullName");
  43. }
  44. #endif
  45. if (result.family.isEmpty())
  46. {
  47. const auto simulated = juce::SystemStats::getEnvironmentVariable ("LYNX_SIMULATE_PACKAGE_FAMILY", {}).trim();
  48. if (simulated.isNotEmpty() && ! simulated.containsAnyOf ("\\/:*?\"<>|"))
  49. {
  50. result.family = simulated;
  51. result.simulated = true;
  52. }
  53. }
  54. return result;
  55. }();
  56. return id;
  57. }
  58. // Copies the installer build's preferences once (core/StorePackage.h lists what comes along).
  59. void migrateFromUnpackaged (const juce::File& target)
  60. {
  61. const auto marker = target.getChildFile (store::migrationMarker);
  62. if (marker.exists())
  63. return;
  64. // a packaged process reading %APPDATA%\LYNX sees the real folder merged with its redirected one; the redirected
  65. // one is `target` itself, so whatever is found there that `target` lacks is the real, unpackaged folder's
  66. const auto old = juce::File::getSpecialLocation (juce::File::userApplicationDataDirectory).getChildFile ("LYNX");
  67. juce::StringArray copied;
  68. if (old.isDirectory() && old != target)
  69. {
  70. for (const auto& entry : juce::RangedDirectoryIterator (old, false, "*", juce::File::findFilesAndDirectories))
  71. {
  72. const auto& file = entry.getFile();
  73. const auto name = file.getFileName();
  74. if (! store::migratesFromUnpackaged (name.toStdString(), entry.isDirectory()))
  75. continue;
  76. const auto destination = target.getChildFile (name);
  77. if (destination.exists())
  78. continue; // never overwrite what the packaged LYNX already has
  79. if (entry.isDirectory() ? file.copyDirectoryTo (destination) : file.copyFileTo (destination))
  80. copied.add (name);
  81. }
  82. }
  83. marker.replaceWithText ("Copied from " + old.getFullPathName() + " on first start of the packaged LYNX:\n"
  84. + copied.joinIntoString ("\n") + "\n");
  85. }
  86. } // namespace
  87. bool runningPackaged() { return identity().family.isNotEmpty(); }
  88. juce::String packageFamilyName() { return identity().family; }
  89. juce::String packageFullName() { return identity().fullName; }
  90. juce::File packagedSettingsFolder()
  91. {
  92. static const juce::File folder = [] {
  93. if (! runningPackaged())
  94. return juce::File();
  95. #if JUCE_WINDOWS
  96. const auto local = juce::File::getSpecialLocation (juce::File::windowsLocalAppData).getFullPathName();
  97. #else
  98. const auto local = juce::File::getSpecialLocation (juce::File::userApplicationDataDirectory).getFullPathName();
  99. #endif
  100. const auto path = store::packagedSettingsFolder (local.toStdString(), packageFamilyName().toStdString());
  101. if (path.empty())
  102. return juce::File();
  103. juce::File f (juce::String::fromUTF8 (path.c_str()));
  104. if (! f.createDirectory())
  105. return juce::File();
  106. migrateFromUnpackaged (f);
  107. return f;
  108. }();
  109. return folder;
  110. }
  111. bool relaunchApp()
  112. {
  113. #if JUCE_WINDOWS
  114. if (runningPackaged() && ! identity().simulated)
  115. {
  116. const auto target = juce::String (store::appsFolderTarget (packageFamilyName().toStdString()));
  117. const auto result = (INT_PTR) ShellExecuteW (nullptr, L"open", target.toWideCharPointer(), nullptr, nullptr, SW_SHOWNORMAL);
  118. if (result > 32)
  119. return true;
  120. }
  121. #endif
  122. return juce::File::getSpecialLocation (juce::File::currentExecutableFile).startAsProcess();
  123. }
  124. juce::String packageReport()
  125. {
  126. juce::StringArray lines;
  127. bool ok = true;
  128. const auto& id = identity();
  129. lines.add ("packaged=" + juce::String (runningPackaged() ? (id.simulated ? "simulated" : "yes") : "no"));
  130. lines.add ("family=" + id.family);
  131. lines.add ("fullName=" + id.fullName);
  132. lines.add ("exe=" + juce::File::getSpecialLocation (juce::File::currentExecutableFile).getFullPathName());
  133. const auto settings = settingsFolder();
  134. lines.add ("settings=" + settings.getFullPathName());
  135. lines.add ("cache=" + cacheFolder ("wavecache").getFullPathName());
  136. lines.add ("temp=" + juce::File::getSpecialLocation (juce::File::tempDirectory).getFullPathName());
  137. lines.add ("pluginFolders=" + standardPluginFolders().toString());
  138. if (runningPackaged())
  139. {
  140. // the settings folder must be the package's real folder, so Explorer and other programs find its files
  141. // (LYNX_PROFILE_DIR, a test profile, wins over the package folder on purpose)
  142. const auto expected = packagedSettingsFolder();
  143. const bool profile = profileOverride() != juce::File();
  144. const bool inPackageData = expected != juce::File() && settings == expected;
  145. lines.add ("settingsIsPackageFolder=" + juce::String (inPackageData ? "yes" : profile ? "no (LYNX_PROFILE_DIR)" : "NO"));
  146. ok = ok && (inPackageData || profile);
  147. lines.add ("migration=" + settings.getChildFile (store::migrationMarker).loadFileAsString().replace ("\n", " | ").trim());
  148. }
  149. // a file written to the settings folder can be read back (the CI step checks it from outside the package too)
  150. const auto probe = settings.getChildFile ("package-probe.txt");
  151. const auto stamp = juce::String (juce::Time::currentTimeMillis());
  152. const bool wrote = probe.replaceWithText (stamp) && probe.loadFileAsString() == stamp;
  153. lines.add ("probe=" + probe.getFullPathName() + (wrote ? " written" : " FAILED"));
  154. ok = ok && wrote;
  155. lines.add (juce::String ("result=") + (ok ? "OK" : "FAILED"));
  156. return lines.joinIntoString ("\n") + "\n";
  157. }
  158. } // namespace lynx::platform