ToolPrefs.cpp 3.1 KB

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