optimized_translation.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*************************************************************************/
  2. /* optimized_translation.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "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;
  37. CharString compressed;
  38. int offset;
  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<Map<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. int total_string_size = 0;
  59. for (const StringName &E : keys) {
  60. //hash string
  61. CharString cs = E.operator String().utf8();
  62. uint32_t h = hash(0, cs.get_data());
  63. Pair<int, CharString> p;
  64. p.first = idx;
  65. p.second = cs;
  66. buckets.write[h % size].push_back(p);
  67. //compress string
  68. CharString src_s = p_from->get_message(E).operator String().utf8();
  69. CompressedString ps;
  70. ps.orig_len = src_s.size();
  71. ps.offset = total_compression_size;
  72. if (ps.orig_len != 0) {
  73. CharString dst_s;
  74. dst_s.resize(src_s.size());
  75. int ret = smaz_compress(src_s.get_data(), src_s.size(), dst_s.ptrw(), src_s.size());
  76. if (ret >= src_s.size()) {
  77. //if compressed is larger than original, just use original
  78. ps.orig_len = src_s.size();
  79. ps.compressed = src_s;
  80. } else {
  81. dst_s.resize(ret);
  82. //ps.orig_len=;
  83. ps.compressed = dst_s;
  84. }
  85. } else {
  86. ps.orig_len = 1;
  87. ps.compressed.resize(1);
  88. ps.compressed[0] = 0;
  89. }
  90. compressed.write[idx] = ps;
  91. total_compression_size += ps.compressed.size();
  92. total_string_size += src_s.size();
  93. idx++;
  94. }
  95. int bucket_table_size = 0;
  96. for (int i = 0; i < size; i++) {
  97. const Vector<Pair<int, CharString>> &b = buckets[i];
  98. Map<uint32_t, int> &t = table.write[i];
  99. if (b.size() == 0) {
  100. continue;
  101. }
  102. int d = 1;
  103. int item = 0;
  104. while (item < b.size()) {
  105. uint32_t slot = hash(d, b[item].second.get_data());
  106. if (t.has(slot)) {
  107. item = 0;
  108. d++;
  109. t.clear();
  110. } else {
  111. t[slot] = b[item].first;
  112. item++;
  113. }
  114. }
  115. hfunc_table.write[i] = d;
  116. bucket_table_size += 2 + b.size() * 4;
  117. }
  118. ERR_FAIL_COND(bucket_table_size == 0);
  119. hash_table.resize(size);
  120. bucket_table.resize(bucket_table_size);
  121. int *htwb = hash_table.ptrw();
  122. int *btwb = bucket_table.ptrw();
  123. uint32_t *htw = (uint32_t *)&htwb[0];
  124. uint32_t *btw = (uint32_t *)&btwb[0];
  125. int btindex = 0;
  126. int collisions = 0;
  127. for (int i = 0; i < size; i++) {
  128. const Map<uint32_t, int> &t = table[i];
  129. if (t.size() == 0) {
  130. htw[i] = 0xFFFFFFFF; //nothing
  131. continue;
  132. } else if (t.size() > 1) {
  133. collisions += t.size() - 1;
  134. }
  135. htw[i] = btindex;
  136. btw[btindex++] = t.size();
  137. btw[btindex++] = hfunc_table[i];
  138. for (Map<uint32_t, int>::Element *E = t.front(); E; E = E->next()) {
  139. btw[btindex++] = E->key();
  140. btw[btindex++] = compressed[E->get()].offset;
  141. btw[btindex++] = compressed[E->get()].compressed.size();
  142. btw[btindex++] = compressed[E->get()].orig_len;
  143. }
  144. }
  145. strings.resize(total_compression_size);
  146. uint8_t *cw = strings.ptrw();
  147. for (int i = 0; i < compressed.size(); i++) {
  148. memcpy(&cw[compressed[i].offset], compressed[i].compressed.get_data(), compressed[i].compressed.size());
  149. }
  150. ERR_FAIL_COND(btindex != bucket_table_size);
  151. set_locale(p_from->get_locale());
  152. #endif
  153. }
  154. bool OptimizedTranslation::_set(const StringName &p_name, const Variant &p_value) {
  155. String name = p_name.operator String();
  156. if (name == "hash_table") {
  157. hash_table = p_value;
  158. } else if (name == "bucket_table") {
  159. bucket_table = p_value;
  160. } else if (name == "strings") {
  161. strings = p_value;
  162. } else if (name == "load_from") {
  163. generate(p_value);
  164. } else {
  165. return false;
  166. }
  167. return true;
  168. }
  169. bool OptimizedTranslation::_get(const StringName &p_name, Variant &r_ret) const {
  170. String name = p_name.operator String();
  171. if (name == "hash_table") {
  172. r_ret = hash_table;
  173. } else if (name == "bucket_table") {
  174. r_ret = bucket_table;
  175. } else if (name == "strings") {
  176. r_ret = strings;
  177. } else {
  178. return false;
  179. }
  180. return true;
  181. }
  182. StringName OptimizedTranslation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  183. // p_context passed in is ignore. The use of context is not yet supported in OptimizedTranslation.
  184. int htsize = hash_table.size();
  185. if (htsize == 0) {
  186. return StringName();
  187. }
  188. CharString str = p_src_text.operator String().utf8();
  189. uint32_t h = hash(0, str.get_data());
  190. const int *htr = hash_table.ptr();
  191. const uint32_t *htptr = (const uint32_t *)&htr[0];
  192. const int *btr = bucket_table.ptr();
  193. const uint32_t *btptr = (const uint32_t *)&btr[0];
  194. const uint8_t *sr = strings.ptr();
  195. const char *sptr = (const char *)&sr[0];
  196. uint32_t p = htptr[h % htsize];
  197. if (p == 0xFFFFFFFF) {
  198. return StringName(); //nothing
  199. }
  200. const Bucket &bucket = *(const Bucket *)&btptr[p];
  201. h = hash(bucket.func, str.get_data());
  202. int idx = -1;
  203. for (int i = 0; i < bucket.size; i++) {
  204. if (bucket.elem[i].key == h) {
  205. idx = i;
  206. break;
  207. }
  208. }
  209. if (idx == -1) {
  210. return StringName();
  211. }
  212. if (bucket.elem[idx].comp_size == bucket.elem[idx].uncomp_size) {
  213. String rstr;
  214. rstr.parse_utf8(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].uncomp_size);
  215. return rstr;
  216. } else {
  217. CharString uncomp;
  218. uncomp.resize(bucket.elem[idx].uncomp_size + 1);
  219. smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptrw(), bucket.elem[idx].uncomp_size);
  220. String rstr;
  221. rstr.parse_utf8(uncomp.get_data());
  222. return rstr;
  223. }
  224. }
  225. StringName OptimizedTranslation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  226. // The use of plurals translation is not yet supported in OptimizedTranslation.
  227. return get_message(p_src_text, p_context);
  228. }
  229. void OptimizedTranslation::_get_property_list(List<PropertyInfo> *p_list) const {
  230. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "hash_table"));
  231. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "bucket_table"));
  232. p_list->push_back(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "strings"));
  233. p_list->push_back(PropertyInfo(Variant::OBJECT, "load_from", PROPERTY_HINT_RESOURCE_TYPE, "Translation", PROPERTY_USAGE_EDITOR));
  234. }
  235. void OptimizedTranslation::_bind_methods() {
  236. ClassDB::bind_method(D_METHOD("generate", "from"), &OptimizedTranslation::generate);
  237. }