path_remap.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************/
  2. /* path_remap.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "path_remap.h"
  31. #include "globals.h"
  32. #include "os/os.h"
  33. #include "translation.h"
  34. PathRemap *PathRemap::singleton = NULL;
  35. PathRemap *PathRemap::get_singleton() {
  36. return singleton;
  37. }
  38. void PathRemap::add_remap(const String &p_from, const String &p_to, const String &p_locale) {
  39. if (!remap.has(p_from)) {
  40. remap[p_from] = RemapData();
  41. }
  42. if (p_locale == String())
  43. remap[p_from].always = p_to;
  44. else
  45. remap[p_from].locale[p_locale] = p_to;
  46. }
  47. String PathRemap::get_remap(const String &p_from) const {
  48. const RemapData *ptr = remap.getptr(p_from);
  49. if (!ptr) {
  50. if (OS::get_singleton()->is_stdout_verbose())
  51. print_line("remap failed: " + p_from);
  52. return p_from;
  53. } else {
  54. const RemapData *ptr2 = NULL;
  55. String locale = TranslationServer::get_singleton()->get_locale();
  56. if (ptr->locale.has(locale)) {
  57. if (OS::get_singleton()->is_stdout_verbose())
  58. print_line("remap found: " + p_from + " -> " + ptr->locale[locale]);
  59. ptr2 = remap.getptr(ptr->locale[locale]);
  60. if (ptr2 && ptr2->always != String()) //may have atlas or export remap too
  61. return ptr2->always;
  62. else
  63. return ptr->locale[locale];
  64. }
  65. int p = locale.find("_");
  66. if (p != -1) {
  67. locale = locale.substr(0, p);
  68. if (ptr->locale.has(locale)) {
  69. if (OS::get_singleton()->is_stdout_verbose())
  70. print_line("remap found: " + p_from + " -> " + ptr->locale[locale]);
  71. ptr2 = remap.getptr(ptr->locale[locale]);
  72. if (ptr2 && ptr2->always != String()) //may have atlas or export remap too
  73. return ptr2->always;
  74. else
  75. return ptr->locale[locale];
  76. }
  77. }
  78. if (ptr->always != String()) {
  79. if (OS::get_singleton()->is_stdout_verbose()) {
  80. print_line("remap found: " + p_from + " -> " + ptr->always);
  81. }
  82. return ptr->always;
  83. }
  84. if (OS::get_singleton()->is_stdout_verbose())
  85. print_line("remap failed: " + p_from);
  86. return p_from;
  87. }
  88. }
  89. bool PathRemap::has_remap(const String &p_from) const {
  90. return remap.has(p_from);
  91. }
  92. void PathRemap::erase_remap(const String &p_from) {
  93. ERR_FAIL_COND(!remap.has(p_from));
  94. remap.erase(p_from);
  95. }
  96. void PathRemap::clear_remaps() {
  97. remap.clear();
  98. }
  99. void PathRemap::load_remaps() {
  100. // default remaps first
  101. DVector<String> remaps = Globals::get_singleton()->get("remap/all");
  102. {
  103. int rlen = remaps.size();
  104. ERR_FAIL_COND(rlen % 2);
  105. DVector<String>::Read r = remaps.read();
  106. for (int i = 0; i < rlen / 2; i++) {
  107. String from = r[i * 2 + 0];
  108. String to = r[i * 2 + 1];
  109. add_remap(from, to);
  110. }
  111. }
  112. // platform remaps second, so override
  113. remaps = Globals::get_singleton()->get("remap/" + OS::get_singleton()->get_name());
  114. // remaps = Globals::get_singleton()->get("remap/PSP");
  115. {
  116. int rlen = remaps.size();
  117. ERR_FAIL_COND(rlen % 2);
  118. DVector<String>::Read r = remaps.read();
  119. for (int i = 0; i < rlen / 2; i++) {
  120. String from = r[i * 2 + 0];
  121. String to = r[i * 2 + 1];
  122. // print_line("add remap: "+from+" -> "+to);
  123. add_remap(from, to);
  124. }
  125. }
  126. //locale based remaps
  127. if (Globals::get_singleton()->has("locale/translation_remaps")) {
  128. Dictionary remaps = Globals::get_singleton()->get("locale/translation_remaps");
  129. List<Variant> rk;
  130. remaps.get_key_list(&rk);
  131. for (List<Variant>::Element *E = rk.front(); E; E = E->next()) {
  132. String source = E->get();
  133. StringArray sa = remaps[E->get()];
  134. int sas = sa.size();
  135. StringArray::Read r = sa.read();
  136. for (int i = 0; i < sas; i++) {
  137. String s = r[i];
  138. int qp = s.find_last(":");
  139. if (qp != -1) {
  140. String path = s.substr(0, qp);
  141. String locale = s.substr(qp + 1, s.length());
  142. add_remap(source, path, locale);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. void PathRemap::_bind_methods() {
  149. ObjectTypeDB::bind_method(_MD("add_remap", "from", "to", "locale"), &PathRemap::add_remap, DEFVAL(String()));
  150. ObjectTypeDB::bind_method(_MD("has_remap", "path"), &PathRemap::has_remap);
  151. ObjectTypeDB::bind_method(_MD("get_remap", "path"), &PathRemap::get_remap);
  152. ObjectTypeDB::bind_method(_MD("erase_remap", "path"), &PathRemap::erase_remap);
  153. ObjectTypeDB::bind_method(_MD("clear_remaps"), &PathRemap::clear_remaps);
  154. }
  155. PathRemap::PathRemap() {
  156. singleton = this;
  157. }