| 46 | }; |
| 47 | |
| 48 | void OptimizedTranslationExtractor::generate(const Ref<Translation> &p_from) { |
| 49 | // This method compresses a Translation instance. |
| 50 | // Right now, it doesn't handle context or plurals, so Translation subclasses using plurals or context (i.e TranslationPO) shouldn't be compressed. |
| 51 | #ifdef TOOLS_ENABLED |
| 52 | ERR_FAIL_COND(p_from.is_null()); |
| 53 | List<StringName> keys; |
| 54 | p_from->get_message_list(&keys); |
| 55 | |
| 56 | int size = Math::larger_prime(keys.size()); |
| 57 | |
| 58 | Vector<Vector<Pair<int, CharString>>> buckets; |
| 59 | Vector<HashMap<uint32_t, int>> table; |
| 60 | Vector<uint32_t> hfunc_table; |
| 61 | Vector<CompressedString> compressed; |
| 62 | |
| 63 | table.resize(size); |
| 64 | hfunc_table.resize(size); |
| 65 | buckets.resize(size); |
| 66 | compressed.resize(keys.size()); |
| 67 | |
| 68 | int idx = 0; |
| 69 | int total_compression_size = 0; |
| 70 | |
| 71 | for (const StringName &E : keys) { |
| 72 | //hash string |
| 73 | CharString cs = E.operator String().utf8(); |
| 74 | uint32_t h = hash(0, cs.get_data()); |
| 75 | Pair<int, CharString> p; |
| 76 | p.first = idx; |
| 77 | p.second = cs; |
| 78 | buckets.write[h % size].push_back(p); |
| 79 | |
| 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 | |
| 86 | if (ps.orig_len != 0) { |
| 87 | CharString dst_s; |
| 88 | dst_s.resize_uninitialized(src_s.size()); |
| 89 | int ret = smaz_compress(src_s.get_data(), src_s.size(), dst_s.ptrw(), src_s.size()); |
| 90 | if (ret >= src_s.size()) { |
| 91 | //if compressed is larger than original, just use original |
| 92 | ps.orig_len = src_s.size(); |
| 93 | ps.compressed = src_s; |
| 94 | } else { |
| 95 | dst_s.resize_uninitialized(ret); |
| 96 | //ps.orig_len=; |
| 97 | ps.compressed = dst_s; |
| 98 | } |
| 99 | } else { |
| 100 | ps.orig_len = 1; |
| 101 | ps.compressed.resize_uninitialized(1); |
| 102 | ps.compressed[0] = 0; |
| 103 | } |
| 104 | |
| 105 | compressed.write[idx] = ps; |