ToolPrefs.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_(false)
  35. {
  36. }
  37. ToolPrefs::~ToolPrefs()
  38. {
  39. }
  40. const String& ToolPrefs::GetAntPath()
  41. {
  42. #ifdef ATOMIC_PLATFORM_OSX
  43. static String defaultAntPath("/usr/local/bin/ant");
  44. if (!antPath_.Length())
  45. return defaultAntPath;
  46. return antPath_;
  47. #else
  48. return antPath_;
  49. #endif
  50. }
  51. String ToolPrefs::GetPrefsPath()
  52. {
  53. FileSystem* fs = GetSubsystem<FileSystem>();
  54. String path = fs->GetAppPreferencesDir("AtomicGameEngine", "ToolCore");
  55. path += "ToolPrefs.json";
  56. return path;
  57. }
  58. void ToolPrefs::Load()
  59. {
  60. String path = GetPrefsPath();
  61. // Check that the tool prefs file exists
  62. FileSystem* fs = GetSubsystem<FileSystem>();
  63. if (!fs->FileExists(path))
  64. return;
  65. SharedPtr<File> file(new File(context_, path));
  66. if (!file->IsOpen())
  67. return;
  68. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  69. bool result = jsonFile->Load(*file);
  70. file->Close();
  71. if (!result)
  72. return;
  73. JSONValue root = jsonFile->GetRoot();
  74. if (!root.IsObject())
  75. return;
  76. JSONValue androidRoot = root.Get("android");
  77. if (androidRoot.IsObject()) {
  78. androidSDKPath_ = androidRoot.Get("androidSDKPath").GetString();
  79. jdkRootPath_ = androidRoot.Get("jdkRootPath").GetString();
  80. antPath_ = androidRoot.Get("antPath").GetString();
  81. releasePath_ = androidRoot.Get("releasePath").GetString();
  82. releaseCheck_ = androidRoot.Get("releaseCheck").GetInt();
  83. }
  84. }
  85. void ToolPrefs::Save()
  86. {
  87. String path = GetPrefsPath();
  88. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  89. JSONValue& root = jsonFile->GetRoot();
  90. root.Clear();
  91. JSONValue androidRoot;
  92. androidRoot["androidSDKPath"] = androidSDKPath_;
  93. androidRoot["jdkRootPath"] = jdkRootPath_;
  94. androidRoot["antPath"] = antPath_;
  95. androidRoot["releasePath"] = releasePath_;
  96. androidRoot["releaseCheck"] = releaseCheck_;
  97. root["android"] = androidRoot;
  98. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  99. jsonFile->Save(*file, " ");
  100. file->Close();
  101. }
  102. }