compressed_translation.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*************************************************************************/
  2. /* compressed_translation.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "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. #ifdef TOOLS_ENABLED
  42. List<StringName> keys;
  43. p_from->get_message_list(&keys);
  44. int size = Math::larger_prime(keys.size());
  45. print_line("compressing keys: " + itos(keys.size()));
  46. Vector<Vector<Pair<int, CharString> > > buckets;
  47. Vector<Map<uint32_t, int> > table;
  48. Vector<uint32_t> hfunc_table;
  49. Vector<_PHashTranslationCmp> compressed;
  50. table.resize(size);
  51. hfunc_table.resize(size);
  52. buckets.resize(size);
  53. compressed.resize(keys.size());
  54. int idx = 0;
  55. int total_compression_size = 0;
  56. int total_string_size = 0;
  57. for (List<StringName>::Element *E = keys.front(); E; E = E->next()) {
  58. //hash string
  59. CharString cs = E->get().operator String().utf8();
  60. uint32_t h = hash(0, cs.get_data());
  61. Pair<int, CharString> p;
  62. p.first = idx;
  63. p.second = cs;
  64. buckets[h % size].push_back(p);
  65. //compress string
  66. CharString src_s = p_from->get_message(E->get()).operator String().utf8();
  67. _PHashTranslationCmp ps;
  68. ps.orig_len = src_s.size();
  69. ps.offset = total_compression_size;
  70. if (ps.orig_len != 0) {
  71. CharString dst_s;
  72. dst_s.resize(src_s.size());
  73. int ret = smaz_compress(src_s.get_data(), src_s.size(), &dst_s[0], src_s.size());
  74. if (ret >= src_s.size()) {
  75. //if compressed is larger than original, just use original
  76. ps.orig_len = src_s.size();
  77. ps.compressed = src_s;
  78. } else {
  79. dst_s.resize(ret);
  80. //ps.orig_len=;
  81. ps.compressed = dst_s;
  82. }
  83. } else {
  84. ps.orig_len = 1;
  85. ps.compressed.resize(1);
  86. ps.compressed[0] = 0;
  87. }
  88. compressed[idx] = ps;
  89. total_compression_size += ps.compressed.size();
  90. total_string_size += src_s.size();
  91. idx++;
  92. }
  93. int bucket_table_size = 0;
  94. print_line("total compressed string size: " + itos(total_compression_size) + " (" + itos(total_string_size) + " uncompressed).");
  95. for (int i = 0; i < size; i++) {
  96. Vector<Pair<int, CharString> > &b = buckets[i];
  97. Map<uint32_t, int> &t = table[i];
  98. if (b.size() == 0)
  99. continue;
  100. //print_line("bucket: "+itos(i)+" - elements: "+itos(b.size()));
  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[i] = d;
  115. bucket_table_size += 2 + b.size() * 4;
  116. }
  117. print_line("bucket table size: " + itos(bucket_table_size * 4));
  118. print_line("hash table size: " + itos(size * 4));
  119. hash_table.resize(size);
  120. bucket_table.resize(bucket_table_size);
  121. DVector<int>::Write htwb = hash_table.write();
  122. DVector<int>::Write btwb = bucket_table.write();
  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. 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. print_line("total collisions: " + itos(collisions));
  146. strings.resize(total_compression_size);
  147. DVector<uint8_t>::Write cw = strings.write();
  148. for (int i = 0; i < compressed.size(); i++) {
  149. memcpy(&cw[compressed[i].offset], compressed[i].compressed.get_data(), compressed[i].compressed.size());
  150. }
  151. ERR_FAIL_COND(btindex != bucket_table_size);
  152. set_locale(p_from->get_locale());
  153. #endif
  154. }
  155. bool PHashTranslation::_set(const StringName &p_name, const Variant &p_value) {
  156. String name = p_name.operator String();
  157. if (name == "hash_table") {
  158. hash_table = p_value;
  159. //print_line("translation: loaded hash table of size: "+itos(hash_table.size()));
  160. } else if (name == "bucket_table") {
  161. bucket_table = p_value;
  162. //print_line("translation: loaded bucket table of size: "+itos(bucket_table.size()));
  163. } else if (name == "strings") {
  164. strings = p_value;
  165. //print_line("translation: loaded string table of size: "+itos(strings.size()));
  166. } else if (name == "load_from") {
  167. //print_line("generating");
  168. generate(p_value);
  169. } else
  170. return false;
  171. return true;
  172. }
  173. bool PHashTranslation::_get(const StringName &p_name, Variant &r_ret) const {
  174. String name = p_name.operator String();
  175. if (name == "hash_table")
  176. r_ret = hash_table;
  177. else if (name == "bucket_table")
  178. r_ret = bucket_table;
  179. else if (name == "strings")
  180. r_ret = strings;
  181. else
  182. return false;
  183. return true;
  184. }
  185. StringName PHashTranslation::get_message(const StringName &p_src_text) const {
  186. int htsize = hash_table.size();
  187. if (htsize == 0)
  188. return StringName();
  189. CharString str = p_src_text.operator String().utf8();
  190. uint32_t h = hash(0, str.get_data());
  191. DVector<int>::Read htr = hash_table.read();
  192. const uint32_t *htptr = (const uint32_t *)&htr[0];
  193. DVector<int>::Read btr = bucket_table.read();
  194. const uint32_t *btptr = (const uint32_t *)&btr[0];
  195. DVector<uint8_t>::Read sr = strings.read();
  196. const char *sptr = (const char *)&sr[0];
  197. uint32_t p = htptr[h % htsize];
  198. //print_line("String: "+p_src_text.operator String());
  199. //print_line("Hash: "+itos(p));
  200. if (p == 0xFFFFFFFF) {
  201. // print_line("GETMSG: Nothing!");
  202. return StringName(); //nothing
  203. }
  204. const Bucket &bucket = *(const Bucket *)&btptr[p];
  205. h = hash(bucket.func, str.get_data());
  206. int idx = -1;
  207. for (int i = 0; i < bucket.size; i++) {
  208. if (bucket.elem[i].key == h) {
  209. idx = i;
  210. break;
  211. }
  212. }
  213. //print_line("bucket pos: "+itos(idx));
  214. if (idx == -1) {
  215. // print_line("GETMSG: Not in Bucket!");
  216. return StringName();
  217. }
  218. if (bucket.elem[idx].comp_size == bucket.elem[idx].uncomp_size) {
  219. String rstr;
  220. rstr.parse_utf8(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].uncomp_size);
  221. // print_line("Uncompressed, size: "+itos(bucket.elem[idx].comp_size));
  222. // print_line("Return: "+rstr);
  223. return rstr;
  224. } else {
  225. CharString uncomp;
  226. uncomp.resize(bucket.elem[idx].uncomp_size + 1);
  227. smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptr(), bucket.elem[idx].uncomp_size);
  228. String rstr;
  229. rstr.parse_utf8(uncomp.get_data());
  230. // print_line("Compressed, size: "+itos(bucket.elem[idx].comp_size));
  231. // print_line("Return: "+rstr);
  232. return rstr;
  233. }
  234. }
  235. void PHashTranslation::_get_property_list(List<PropertyInfo> *p_list) const {
  236. p_list->push_back(PropertyInfo(Variant::INT_ARRAY, "hash_table"));
  237. p_list->push_back(PropertyInfo(Variant::INT_ARRAY, "bucket_table"));
  238. p_list->push_back(PropertyInfo(Variant::RAW_ARRAY, "strings"));
  239. p_list->push_back(PropertyInfo(Variant::OBJECT, "load_from", PROPERTY_HINT_RESOURCE_TYPE, "Translation", PROPERTY_USAGE_EDITOR));
  240. }
  241. void PHashTranslation::_bind_methods() {
  242. ObjectTypeDB::bind_method(_MD("generate", "from:Translation"), &PHashTranslation::generate);
  243. }
  244. PHashTranslation::PHashTranslation() {
  245. }