path_remap.cpp 5.8 KB

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