optimized_translation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.
  43. #ifdef TOOLS_ENABLED
  44. ERR_FAIL_COND(p_from.is_null());
  45. List<StringName> keys;
  46. {
  47. List<StringName> raw_keys;
  48. p_from->get_message_list(&raw_keys);
  49. for (const StringName &key : raw_keys) {
  50. const String key_str = key.operator String();
  51. int p = key_str.find_char(0x04);
  52. if (p == -1) {
  53. keys.push_back(key);
  54. } else {
  55. const String &msgctxt = key_str.substr(0, p);
  56. const String &msgid = key_str.substr(p + 1);
  57. WARN_PRINT(vformat("OptimizedTranslation does not support context, ignoring message '%s' with context '%s'.", msgid, msgctxt));
  58. }
  59. }
  60. }
  61. int size = Math::larger_prime(keys.size());
  62. Vector<Vector<Pair<int, CharString>>> buckets;
  63. Vector<HashMap<uint32_t, int>> table;
  64. Vector<uint32_t> hfunc_table;
  65. Vector<CompressedString> compressed;
  66. table.resize(size);
  67. hfunc_table.resize(size);
  68. buckets.resize(size);
  69. compressed.resize(keys.size());
  70. int idx = 0;
  71. int total_compression_size = 0;
  72. for (const StringName &E : keys) {
  73. //hash string
  74. CharString cs = E.operator String().utf8();
  75. uint32_t h = hash(0, cs.get_data());
  76. Pair<int, CharString> p;
  77. p.first = idx;
  78. p.second = cs;
  79. buckets.write[h % size].push_back(p);
  80. //compress string
  81. CharString src_s = p_from->get_message(E).operator String().utf8();
  82. CompressedString ps;
  83. ps.orig_len = src_s.size();
  84. ps.offset = total_compression_size;
  85. if (ps.orig_len != 0) {
  86. CharString dst_s;
  87. dst_s.resize_uninitialized(src_s.size());
  88. int ret = smaz_compress(src_s.get_data(), src_s.size(), dst_s.ptrw(), src_s.size());
  89. if (ret >= src_s.size()) {
  90. //if compressed is larger than original, just use original
  91. ps.orig_len = src_s.size();
  92. ps.compressed = src_s;
  93. } else {
  94. dst_s.resize_uninitialized(ret);
  95. //ps.orig_len=;
  96. ps.compressed = dst_s;
  97. }
  98. } else {
  99. ps.orig_len = 1;
  100. ps.compressed.resize_uninitialized(1);
  101. ps.compressed[0] = 0;
  102. }
  103. compressed.write[idx] = ps;
  104. total_compression_size += ps.compressed.size();
  105. idx++;
  106. }
  107. int bucket_table_size = 0;
  108. for (int i = 0; i < size; i++) {
  109. const Vector<Pair<int, CharString>> &b = buckets[i];
  110. HashMap<uint32_t, int> &t = table.write[i];
  111. if (b.is_empty()) {
  112. continue;
  113. }
  114. int d = 1;
  115. int item = 0;
  116. while (item < b.size()) {
  117. uint32_t slot = hash(d, b[item].second.get_data());
  118. if (t.has(slot)) {
  119. item = 0;
  120. d++;
  121. t.clear();
  122. } else {
  123. t[slot] = b[item].first;
  124. item++;
  125. }
  126. }
  127. hfunc_table.write[i] = d;
  128. bucket_table_size += 2 + b.size() * 4;
  129. }
  130. ERR_FAIL_COND(bucket_table_size == 0);
  131. hash_table.resize(size);
  132. bucket_table.resize(bucket_table_size);
  133. int *htwb = hash_table.ptrw();
  134. int *btwb = bucket_table.ptrw();
  135. uint32_t *htw = (uint32_t *)&htwb[0];
  136. uint32_t *btw = (uint32_t *)&btwb[0];
  137. int btindex = 0;
  138. for (int i = 0; i < size; i++) {
  139. const HashMap<uint32_t, int> &t = table[i];
  140. if (t.is_empty()) {
  141. htw[i] = 0xFFFFFFFF; //nothing
  142. continue;
  143. }
  144. htw[i] = btindex;
  145. btw[btindex++] = t.size();
  146. btw[btindex++] = hfunc_table[i];
  147. for (const KeyValue<uint32_t, int> &E : t) {
  148. btw[btindex++] = E.key;
  149. btw[btindex++] = compressed[E.value].offset;
  150. btw[btindex++] = compressed[E.value].compressed.size();
  151. btw[btindex++] = compressed[E.value].orig_len;
  152. }
  153. }
  154. strings.resize(total_compression_size);
  155. uint8_t *cw = strings.ptrw();
  156. for (int i = 0; i < compressed.size(); i++) {
  157. memcpy(&cw[compressed[i].offset], compressed[i].compressed.get_data(), compressed[i].compressed.size());
  158. }
  159. ERR_FAIL_COND(btindex != bucket_table_size);
  160. set_locale(p_from->get_locale());
  161. #endif
  162. }
  163. bool OptimizedTranslation::_set(const StringName &p_name, const Variant &p_value) {
  164. String prop_name = p_name.operator String();
  165. if (prop_name == "hash_table") {
  166. hash_table = p_value;
  167. } else if (prop_name == "bucket_table") {
  168. bucket_table = p_value;
  169. } else if (prop_name == "strings") {
  170. strings = p_value;
  171. } else if (prop_name == "load_from") {
  172. generate(p_value);
  173. } else {
  174. return false;
  175. }
  176. return true;
  177. }
  178. bool OptimizedTranslation::_get(const StringName &p_name, Variant &r_ret) const {
  179. String prop_name = p_name.operator String();
  180. if (prop_name == "hash_table") {
  181. r_ret = hash_table;
  182. } else if (prop_name == "bucket_table") {
  183. r_ret = bucket_table;
  184. } else if (prop_name == "strings") {
  185. r_ret = strings;
  186. } else {
  187. return false;
  188. }
  189. return true;
  190. }
  191. StringName OptimizedTranslation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  192. // p_context passed in is ignore. The use of context is not yet supported in OptimizedTranslation.
  193. int htsize = hash_table.size();
  194. if (htsize == 0) {
  195. return StringName();
  196. }
  197. CharString str = p_src_text.operator String().utf8();
  198. uint32_t h = hash(0, str.get_data());
  199. const int *htr = hash_table.ptr();
  200. const uint32_t *htptr = (const uint32_t *)&htr[0];
  201. const int *btr = bucket_table.ptr();
  202. const uint32_t *btptr = (const uint32_t *)&btr[0];
  203. const uint8_t *sr = strings.ptr();
  204. const char *sptr = (const char *)&sr[0];
  205. uint32_t p = htptr[h % htsize];
  206. if (p == 0xFFFFFFFF) {
  207. return StringName(); //nothing
  208. }
  209. const Bucket &bucket = *(const Bucket *)&btptr[p];
  210. h = hash(bucket.func, str.get_data());
  211. int idx = -1;
  212. for (int i = 0; i < bucket.size; i++) {
  213. if (bucket.elem[i].key == h) {
  214. idx = i;
  215. break;
  216. }
  217. }
  218. if (idx == -1) {
  219. return StringName();
  220. }
  221. if (bucket.elem[idx].comp_size == bucket.elem[idx].uncomp_size) {
  222. return String::utf8(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].uncomp_size);
  223. } else {
  224. CharString uncomp;
  225. uncomp.resize_uninitialized(bucket.elem[idx].uncomp_size + 1);
  226. smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptrw(), bucket.elem[idx].uncomp_size);
  227. return String::utf8(uncomp.get_data());
  228. }
  229. }
  230. Vector<String> OptimizedTranslation::get_translated_message_list() const {
  231. Vector<String> msgs;
  232. const int *htr = hash_table.ptr();
  233. const uint32_t *htptr = (const uint32_t *)&htr[0];
  234. const int *btr = bucket_table.ptr();
  235. const uint32_t *btptr = (const uint32_t *)&btr[0];
  236. const uint8_t *sr = strings.ptr();
  237. const char *sptr = (const char *)&sr[0];
  238. for (int i = 0; i < hash_table.size(); i++) {
  239. uint32_t p = htptr[i];
  240. if (p != 0xFFFFFFFF) {
  241. const Bucket &bucket = *(const Bucket *)&btptr[p];
  242. for (int j = 0; j < bucket.size; j++) {
  243. if (bucket.elem[j].comp_size == bucket.elem[j].uncomp_size) {
  244. String rstr = String::utf8(&sptr[bucket.elem[j].str_offset], bucket.elem[j].uncomp_size);
  245. msgs.push_back(rstr);
  246. } else {
  247. CharString uncomp;
  248. uncomp.resize_uninitialized(bucket.elem[j].uncomp_size + 1);
  249. smaz_decompress(&sptr[bucket.elem[j].str_offset], bucket.elem[j].comp_size, uncomp.ptrw(), bucket.elem[j].uncomp_size);
  250. String rstr = String::utf8(uncomp.get_data());
  251. msgs.push_back(rstr);
  252. }
  253. }
  254. }
  255. }
  256. return msgs;
  257. }
  258. StringName OptimizedTranslation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  259. // The use of plurals translation is not yet supported in OptimizedTranslation.
  260. return get_message(p_src_text, p_context);
  261. }
  262. Vector<String> OptimizedTranslation::_get_message_list() const {
  263. WARN_PRINT_ONCE("OptimizedTranslation does not store the message texts to be translated.");
  264. return {};
  265. }
  266. void OptimizedTranslation::get_message_list(List<StringName> *r_messages) const {
  267. WARN_PRINT_ONCE("OptimizedTranslation does not store the message texts to be translated.");
  268. }
  269. int OptimizedTranslation::get_message_count() const {
  270. WARN_PRINT_ONCE("OptimizedTranslation does not store the message texts to be translated.");
  271. return 0;
  272. }
  273. void OptimizedTranslation::_get_property_list(List<PropertyInfo> *p_list) const {
  274. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "hash_table", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  275. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "bucket_table", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  276. p_list->push_back(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "strings", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  277. p_list->push_back(PropertyInfo(Variant::OBJECT, "load_from", PROPERTY_HINT_RESOURCE_TYPE, "Translation", PROPERTY_USAGE_EDITOR));
  278. }
  279. void OptimizedTranslation::_bind_methods() {
  280. ClassDB::bind_method(D_METHOD("generate", "from"), &OptimizedTranslation::generate);
  281. }