prepare.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * prepare.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "prepare.h"
  12. #include "../vcmiqt/launcherdirs.h"
  13. #include <QDir>
  14. #include <QFile>
  15. #include <QFileInfo>
  16. #ifdef VCMI_ANDROID
  17. #include "../lib/CAndroidVMHelper.h"
  18. #include <QAndroidJniEnvironment>
  19. #include <QAndroidJniObject>
  20. #include <QtAndroid>
  21. namespace
  22. {
  23. // https://gist.github.com/ssendeavour/7324701
  24. bool copyRecursively(const QString &srcFilePath, const QString &tgtFilePath)
  25. {
  26. QFileInfo srcFileInfo{srcFilePath};
  27. if(srcFileInfo.isDir()) {
  28. QDir targetDir{tgtFilePath};
  29. targetDir.cdUp();
  30. if(!targetDir.mkpath(QFileInfo{tgtFilePath}.fileName()))
  31. return false;
  32. targetDir.setPath(tgtFilePath);
  33. QDir sourceDir{srcFilePath};
  34. const auto fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
  35. for(const auto & fileName : fileNames) {
  36. const auto newSrcFilePath = sourceDir.filePath(fileName);
  37. const auto newTgtFilePath = targetDir.filePath(fileName);
  38. if(!copyRecursively(newSrcFilePath, newTgtFilePath))
  39. return false;
  40. }
  41. } else {
  42. if(!QFile::copy(srcFilePath, tgtFilePath))
  43. return false;
  44. }
  45. return true;
  46. }
  47. void prepareAndroid()
  48. {
  49. QAndroidJniEnvironment jniEnv;
  50. CAndroidVMHelper::initClassloader(static_cast<JNIEnv *>(jniEnv));
  51. const bool justLaunched = QtAndroid::androidActivity().getField<jboolean>("justLaunched") == JNI_TRUE;
  52. if(!justLaunched)
  53. return;
  54. // copy core data to internal directory
  55. const auto vcmiDir = QAndroidJniObject::callStaticObjectMethod<jstring>("eu/vcmi/vcmi/NativeMethods", "internalDataRoot").toString();
  56. for(auto vcmiFilesResource : {QLatin1String{"config"}, QLatin1String{"Mods"}})
  57. {
  58. QDir destDir = QString{"%1/%2"}.arg(vcmiDir, vcmiFilesResource);
  59. destDir.removeRecursively();
  60. copyRecursively(QString{":/%1"}.arg(vcmiFilesResource), destDir.absolutePath());
  61. }
  62. }
  63. }
  64. #endif
  65. namespace launcher
  66. {
  67. void prepare()
  68. {
  69. #ifdef VCMI_ANDROID
  70. prepareAndroid();
  71. #endif
  72. CLauncherDirs::prepare();
  73. }
  74. }