compressed_translation.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*************************************************************************/
  2. /* compressed_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 "compressed_translation.h"
  31. #include "core/templates/pair.h"
  32. extern "C" {
  33. #include "thirdparty/misc/smaz.h"
  34. }
  35. struct _PHashTranslationCmp {
  36. int orig_len;
  37. CharString compressed;
  38. int offset;
  39. };
  40. void PHashTranslation::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. List<StringName> keys;
  45. p_from->get_message_list(&keys);
  46. int size = Math::larger_prime(keys.size());
  47. Vector<Vector<Pair<int, CharString>>> buckets;
  48. Vector<Map<uint32_t, int>> table;
  49. Vector<uint32_t> hfunc_table;
  50. Vector<_PHashTranslationCmp> compressed;
  51. table.resize(size);
  52. hfunc_table.resize(size);
  53. buckets.resize(size);
  54. compressed.resize(keys.size());
  55. int idx = 0;
  56. int total_compression_size = 0;
  57. int total_string_size = 0;
  58. for (List<StringName>::Element *E = keys.front(); E; E = E->next()) {
  59. //hash string
  60. CharString cs = E->get().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->get()).operator String().utf8();
  68. _PHashTranslationCmp 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. total_string_size += src_s.size();
  92. idx++;
  93. }
  94. int bucket_table_size = 0;
  95. for (int i = 0; i < size; i++) {
  96. const Vector<Pair<int, CharString>> &b = buckets[i];
  97. Map<uint32_t, int> &t = table.write[i];
  98. if (b.size() == 0) {
  99. continue;
  100. }
  101. int d = 1;
  102. int item = 0;
  103. while (item < b.size()) {
  104. uint32_t slot = hash(d, b[item].second.get_data());
  105. if (t.has(slot)) {
  106. item = 0;
  107. d++;
  108. t.clear();
  109. } else {
  110. t[slot] = b[item].first;
  111. item++;
  112. }
  113. }
  114. hfunc_table.write[i] = d;
  115. bucket_table_size += 2 + b.size() * 4;
  116. }
  117. ERR_FAIL_COND(bucket_table_size == 0);
  118. hash_table.resize(size);
  119. bucket_table.resize(bucket_table_size);
  120. int *htwb = hash_table.ptrw();
  121. int *btwb = bucket_table.ptrw();
  122. uint32_t *htw = (uint32_t *)&htwb[0];
  123. uint32_t *btw = (uint32_t *)&btwb[0];
  124. int btindex = 0;
  125. int collisions = 0;
  126. for (int i = 0; i < size; i++) {
  127. const Map<uint32_t, int> &t = table[i];
  128. if (t.size() == 0) {
  129. htw[i] = 0xFFFFFFFF; //nothing
  130. continue;
  131. } else if (t.size() > 1) {
  132. collisions += t.size() - 1;
  133. }
  134. htw[i] = btindex;
  135. btw[btindex++] = t.size();
  136. btw[btindex++] = hfunc_table[i];
  137. for (Map<uint32_t, int>::Element *E = t.front(); E; E = E->next()) {
  138. btw[btindex++] = E->key();
  139. btw[btindex++] = compressed[E->get()].offset;
  140. btw[btindex++] = compressed[E->get()].compressed.size();
  141. btw[btindex++] = compressed[E->get()].orig_len;
  142. }
  143. }
  144. strings.resize(total_compression_size);
  145. uint8_t *cw = strings.ptrw();
  146. for (int i = 0; i < compressed.size(); i++) {
  147. memcpy(&cw[compressed[i].offset], compressed[i].compressed.get_data(), compressed[i].compressed.size());
  148. }
  149. ERR_FAIL_COND(btindex != bucket_table_size);
  150. set_locale(p_from->get_locale());
  151. #endif
  152. }
  153. bool PHashTranslation::_set(const StringName &p_name, const Variant &p_value) {
  154. String name = p_name.operator String();
  155. if (name == "hash_table") {
  156. hash_table = p_value;
  157. } else if (name == "bucket_table") {
  158. bucket_table = p_value;
  159. } else if (name == "strings") {
  160. strings = p_value;
  161. } else if (name == "load_from") {
  162. generate(p_value);
  163. } else {
  164. return false;
  165. }
  166. return true;
  167. }
  168. bool PHashTranslation::_get(const StringName &p_name, Variant &r_ret) const {
  169. String name = p_name.operator String();
  170. if (name == "hash_table") {
  171. r_ret = hash_table;
  172. } else if (name == "bucket_table") {
  173. r_ret = bucket_table;
  174. } else if (name == "strings") {
  175. r_ret = strings;
  176. } else {
  177. return false;
  178. }
  179. return true;
  180. }
  181. StringName PHashTranslation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  182. // p_context passed in is ignore. The use of context is not yet supported in PHashTranslation.
  183. int htsize = hash_table.size();
  184. if (htsize == 0) {
  185. return StringName();
  186. }
  187. CharString str = p_src_text.operator String().utf8();
  188. uint32_t h = hash(0, str.get_data());
  189. const int *htr = hash_table.ptr();
  190. const uint32_t *htptr = (const uint32_t *)&htr[0];
  191. const int *btr = bucket_table.ptr();
  192. const uint32_t *btptr = (const uint32_t *)&btr[0];
  193. const uint8_t *sr = strings.ptr();
  194. const char *sptr = (const char *)&sr[0];
  195. uint32_t p = htptr[h % htsize];
  196. if (p == 0xFFFFFFFF) {
  197. return StringName(); //nothing
  198. }
  199. const Bucket &bucket = *(const Bucket *)&btptr[p];
  200. h = hash(bucket.func, str.get_data());
  201. int idx = -1;
  202. for (int i = 0; i < bucket.size; i++) {
  203. if (bucket.elem[i].key == h) {
  204. idx = i;
  205. break;
  206. }
  207. }
  208. if (idx == -1) {
  209. return StringName();
  210. }
  211. if (bucket.elem[idx].comp_size == bucket.elem[idx].uncomp_size) {
  212. String rstr;
  213. rstr.parse_utf8(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].uncomp_size);
  214. return rstr;
  215. } else {
  216. CharString uncomp;
  217. uncomp.resize(bucket.elem[idx].uncomp_size + 1);
  218. smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptrw(), bucket.elem[idx].uncomp_size);
  219. String rstr;
  220. rstr.parse_utf8(uncomp.get_data());
  221. return rstr;
  222. }
  223. }
  224. StringName PHashTranslation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  225. // The use of plurals translation is not yet supported in PHashTranslation.
  226. return get_message(p_src_text, p_context);
  227. }
  228. void PHashTranslation::_get_property_list(List<PropertyInfo> *p_list) const {
  229. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "hash_table"));
  230. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "bucket_table"));
  231. p_list->push_back(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "strings"));
  232. p_list->push_back(PropertyInfo(Variant::OBJECT, "load_from", PROPERTY_HINT_RESOURCE_TYPE, "Translation", PROPERTY_USAGE_EDITOR));
  233. }
  234. void PHashTranslation::_bind_methods() {
  235. ClassDB::bind_method(D_METHOD("generate", "from"), &PHashTranslation::generate);
  236. }