compressed_translation.cpp 8.2 KB

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