ToolPrefs.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/IO/File.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Resource/JSONFile.h>
  26. #include "ToolPrefs.h"
  27. namespace ToolCore
  28. {
  29. ToolPrefs::ToolPrefs(Context* context) : Object(context),
  30. androidSDKPath_(),
  31. jdkRootPath_(),
  32. antPath_(),
  33. releasePath_(),
  34. releaseCheck_(0),
  35. ndkPath_()
  36. {
  37. }
  38. ToolPrefs::~ToolPrefs()
  39. {
  40. }
  41. const String& ToolPrefs::GetAntPath()
  42. {
  43. #ifdef ATOMIC_PLATFORM_OSX
  44. static String defaultAntPath("/usr/local/bin/ant");
  45. if (!antPath_.Length())
  46. return defaultAntPath;
  47. return antPath_;
  48. #else
  49. return antPath_;
  50. #endif
  51. }
  52. String ToolPrefs::GetPrefsPath()
  53. {
  54. FileSystem* fs = GetSubsystem<FileSystem>();
  55. String path = fs->GetAppPreferencesDir("AtomicGameEngine", "ToolCore");
  56. path += "ToolPrefs.json";
  57. return path;
  58. }
  59. void ToolPrefs::Load()
  60. {
  61. String path = GetPrefsPath();
  62. // Check that the tool prefs file exists
  63. FileSystem* fs = GetSubsystem<FileSystem>();
  64. if (!fs->FileExists(path))
  65. return;
  66. SharedPtr<File> file(new File(context_, path));
  67. if (!file->IsOpen())
  68. return;
  69. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  70. bool result = jsonFile->Load(*file);
  71. file->Close();
  72. if (!result)
  73. return;
  74. JSONValue& root = jsonFile->GetRoot();
  75. if (!root.IsObject())
  76. return;
  77. JSONValue androidRoot = root.Get("android");
  78. if (androidRoot.IsObject()) {
  79. androidSDKPath_ = androidRoot.Get("androidSDKPath").GetString();
  80. jdkRootPath_ = androidRoot.Get("jdkRootPath").GetString();
  81. antPath_ = androidRoot.Get("antPath").GetString();
  82. releasePath_ = androidRoot.Get("releasePath").GetString();
  83. releaseCheck_ = androidRoot.Get("releaseCheck").GetInt();
  84. ndkPath_ = androidRoot.Get("ndkPath").GetString();
  85. }
  86. }
  87. void ToolPrefs::Save()
  88. {
  89. String path = GetPrefsPath();
  90. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  91. JSONValue& root = jsonFile->GetRoot();
  92. root.Clear();
  93. JSONValue androidRoot;
  94. androidRoot["androidSDKPath"] = androidSDKPath_;
  95. androidRoot["jdkRootPath"] = jdkRootPath_;
  96. androidRoot["antPath"] = antPath_;
  97. androidRoot["releasePath"] = releasePath_;
  98. androidRoot["releaseCheck"] = releaseCheck_;
  99. androidRoot["ndkPath"] = ndkPath_;
  100. root["android"] = androidRoot;
  101. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  102. jsonFile->Save(*file, " ");
  103. file->Close();
  104. }
  105. }