optimized_translation.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**************************************************************************/
  2. /* optimized_translation.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "optimized_translation.h"
  31. #include "core/templates/pair.h"
  32. extern "C" {
  33. #include "thirdparty/misc/smaz.h"
  34. }
  35. struct CompressedString {
  36. int orig_len = 0;
  37. CharString compressed;
  38. int offset = 0;
  39. };
  40. void OptimizedTranslation::generate(const Ref<Translation> &p_from) {
  41. // This method compresses a Translation instance.
  42. // Right now, it doesn't handle context or plurals, so Translation subclasses using plurals or context (i.e TranslationPO) shouldn't be compressed.
  43. #ifdef TOOLS_ENABLED
  44. ERR_FAIL_COND(p_from.is_null());
  45. List<StringName> keys;
  46. p_from->get_message_list(&keys);
  47. int size = Math::larger_prime(keys.size());
  48. Vector<Vector<Pair<int, CharString>>> buckets;
  49. Vector<HashMap<uint32_t, int>> table;
  50. Vector<uint32_t> hfunc_table;
  51. Vector<CompressedString> compressed;
  52. table.resize(size);
  53. hfunc_table.resize(size);
  54. buckets.resize(size);
  55. compressed.resize(keys.size());
  56. int idx = 0;
  57. int total_compression_size = 0;
  58. for (const StringName &E : keys) {
  59. //hash string
  60. CharString cs = E.operator String().utf8();
  61. uint32_t h = hash(0, cs.get_data());
  62. Pair<int, CharString> p;
  63. p.first = idx;
  64. p.second = cs;
  65. buckets.write[h % size].push_back(p);
  66. //compress string
  67. CharString src_s = p_from->get_message(E).operator String().utf8();
  68. CompressedString ps;
  69. ps.orig_len = src_s.size();
  70. ps.offset = total_compression_size;
  71. if (ps.orig_len != 0) {
  72. CharString dst_s;
  73. dst_s.resize(src_s.size());
  74. int ret = smaz_compress(src_s.get_data(), src_s.size(), dst_s.ptrw(), src_s.size());
  75. if (ret >= src_s.size()) {
  76. //if compressed is larger than original, just use original
  77. ps.orig_len = src_s.size();
  78. ps.compressed = src_s;
  79. } else {
  80. dst_s.resize(ret);
  81. //ps.orig_len=;
  82. ps.compressed = dst_s;
  83. }
  84. } else {
  85. ps.orig_len = 1;
  86. ps.compressed.resize(1);
  87. ps.compressed[0] = 0;
  88. }
  89. compressed.write[idx] = ps;
  90. total_compression_size += ps.compressed.size();
  91. idx++;
  92. }
  93. int bucket_table_size = 0;
  94. for (int i = 0; i < size; i++) {
  95. const Vector<Pair<int, CharString>> &b = buckets[i];
  96. HashMap<uint32_t, int> &t = table.write[i];
  97. if (b.size() == 0) {
  98. continue;
  99. }
  100. int d = 1;
  101. int item = 0;
  102. while (item < b.size()) {
  103. uint32_t slot = hash(d, b[item].second.get_data());
  104. if (t.has(slot)) {
  105. item = 0;
  106. d++;
  107. t.clear();
  108. } else {
  109. t[slot] = b[item].first;
  110. item++;
  111. }
  112. }
  113. hfunc_table.write[i] = d;
  114. bucket_table_size += 2 + b.size() * 4;
  115. }
  116. ERR_FAIL_COND(bucket_table_size == 0);
  117. hash_table.resize(size);
  118. bucket_table.resize(bucket_table_size);
  119. int *htwb = hash_table.ptrw();
  120. int *btwb = bucket_table.ptrw();
  121. uint32_t *htw = (uint32_t *)&htwb[0];
  122. uint32_t *btw = (uint32_t *)&btwb[0];
  123. int btindex = 0;
  124. for (int i = 0; i < size; i++) {
  125. const HashMap<uint32_t, int> &t = table[i];
  126. if (t.size() == 0) {
  127. htw[i] = 0xFFFFFFFF; //nothing
  128. continue;
  129. }
  130. htw[i] = btindex;
  131. btw[btindex++] = t.size();
  132. btw[btindex++] = hfunc_table[i];
  133. for (const KeyValue<uint32_t, int> &E : t) {
  134. btw[btindex++] = E.key;
  135. btw[btindex++] = compressed[E.value].offset;
  136. btw[btindex++] = compressed[E.value].compressed.size();
  137. btw[btindex++] = compressed[E.value].orig_len;
  138. }
  139. }
  140. strings.resize(total_compression_size);
  141. uint8_t *cw = strings.ptrw();
  142. for (int i = 0; i < compressed.size(); i++) {
  143. memcpy(&cw[compressed[i].offset], compressed[i].compressed.get_data(), compressed[i].compressed.size());
  144. }
  145. ERR_FAIL_COND(btindex != bucket_table_size);
  146. set_locale(p_from->get_locale());
  147. #endif
  148. }
  149. bool OptimizedTranslation::_set(const StringName &p_name, const Variant &p_value) {
  150. String prop_name = p_name.operator String();
  151. if (prop_name == "hash_table") {
  152. hash_table = p_value;
  153. } else if (prop_name == "bucket_table") {
  154. bucket_table = p_value;
  155. } else if (prop_name == "strings") {
  156. strings = p_value;
  157. } else if (prop_name == "load_from") {
  158. generate(p_value);
  159. } else {
  160. return false;
  161. }
  162. return true;
  163. }
  164. bool OptimizedTranslation::_get(const StringName &p_name, Variant &r_ret) const {
  165. String prop_name = p_name.operator String();
  166. if (prop_name == "hash_table") {
  167. r_ret = hash_table;
  168. } else if (prop_name == "bucket_table") {
  169. r_ret = bucket_table;
  170. } else if (prop_name == "strings") {
  171. r_ret = strings;
  172. } else {
  173. return false;
  174. }
  175. return true;
  176. }
  177. StringName OptimizedTranslation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  178. // p_context passed in is ignore. The use of context is not yet supported in OptimizedTranslation.
  179. int htsize = hash_table.size();
  180. if (htsize == 0) {
  181. return StringName();
  182. }
  183. CharString str = p_src_text.operator String().utf8();
  184. uint32_t h = hash(0, str.get_data());
  185. const int *htr = hash_table.ptr();
  186. const uint32_t *htptr = (const uint32_t *)&htr[0];
  187. const int *btr = bucket_table.ptr();
  188. const uint32_t *btptr = (const uint32_t *)&btr[0];
  189. const uint8_t *sr = strings.ptr();
  190. const char *sptr = (const char *)&sr[0];
  191. uint32_t p = htptr[h % htsize];
  192. if (p == 0xFFFFFFFF) {
  193. return StringName(); //nothing
  194. }
  195. const Bucket &bucket = *(const Bucket *)&btptr[p];
  196. h = hash(bucket.func, str.get_data());
  197. int idx = -1;
  198. for (int i = 0; i < bucket.size; i++) {
  199. if (bucket.elem[i].key == h) {
  200. idx = i;
  201. break;
  202. }
  203. }
  204. if (idx == -1) {
  205. return StringName();
  206. }
  207. if (bucket.elem[idx].comp_size == bucket.elem[idx].uncomp_size) {
  208. String rstr;
  209. rstr.parse_utf8(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].uncomp_size);
  210. return rstr;
  211. } else {
  212. CharString uncomp;
  213. uncomp.resize(bucket.elem[idx].uncomp_size + 1);
  214. smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptrw(), bucket.elem[idx].uncomp_size);
  215. String rstr;
  216. rstr.parse_utf8(uncomp.get_data());
  217. return rstr;
  218. }
  219. }
  220. Vector<String> OptimizedTranslation::get_translated_message_list() const {
  221. Vector<String> msgs;
  222. const int *htr = hash_table.ptr();
  223. const uint32_t *htptr = (const uint32_t *)&htr[0];
  224. const int *btr = bucket_table.ptr();
  225. const uint32_t *btptr = (const uint32_t *)&btr[0];
  226. const uint8_t *sr = strings.ptr();
  227. const char *sptr = (const char *)&sr[0];
  228. for (int i = 0; i < hash_table.size(); i++) {
  229. uint32_t p = htptr[i];
  230. if (p != 0xFFFFFFFF) {
  231. const Bucket &bucket = *(const Bucket *)&btptr[p];
  232. for (int j = 0; j < bucket.size; j++) {
  233. if (bucket.elem[j].comp_size == bucket.elem[j].uncomp_size) {
  234. String rstr;
  235. rstr.parse_utf8(&sptr[bucket.elem[j].str_offset], bucket.elem[j].uncomp_size);
  236. msgs.push_back(rstr);
  237. } else {
  238. CharString uncomp;
  239. uncomp.resize(bucket.elem[j].uncomp_size + 1);
  240. smaz_decompress(&sptr[bucket.elem[j].str_offset], bucket.elem[j].comp_size, uncomp.ptrw(), bucket.elem[j].uncomp_size);
  241. String rstr;
  242. rstr.parse_utf8(uncomp.get_data());
  243. msgs.push_back(rstr);
  244. }
  245. }
  246. }
  247. }
  248. return msgs;
  249. }
  250. StringName OptimizedTranslation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  251. // The use of plurals translation is not yet supported in OptimizedTranslation.
  252. return get_message(p_src_text, p_context);
  253. }
  254. void OptimizedTranslation::_get_property_list(List<PropertyInfo> *p_list) const {
  255. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "hash_table"));
  256. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "bucket_table"));
  257. p_list->push_back(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "strings"));
  258. p_list->push_back(PropertyInfo(Variant::OBJECT, "load_from", PROPERTY_HINT_RESOURCE_TYPE, "Translation", PROPERTY_USAGE_EDITOR));
  259. }
  260. void OptimizedTranslation::_bind_methods() {
  261. ClassDB::bind_method(D_METHOD("generate", "from"), &OptimizedTranslation::generate);
  262. }