Path: blob/master/core/string/optimized_translation.cpp
9903 views
/**************************************************************************/1/* optimized_translation.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "optimized_translation.h"3132#include "core/templates/pair.h"3334extern "C" {35#include "thirdparty/misc/smaz.h"36}3738struct CompressedString {39int orig_len = 0;40CharString compressed;41int offset = 0;42};4344void OptimizedTranslation::generate(const Ref<Translation> &p_from) {45// This method compresses a Translation instance.46// Right now, it doesn't handle context or plurals, so Translation subclasses using plurals or context (i.e TranslationPO) shouldn't be compressed.47#ifdef TOOLS_ENABLED48ERR_FAIL_COND(p_from.is_null());49List<StringName> keys;50p_from->get_message_list(&keys);5152int size = Math::larger_prime(keys.size());5354Vector<Vector<Pair<int, CharString>>> buckets;55Vector<HashMap<uint32_t, int>> table;56Vector<uint32_t> hfunc_table;57Vector<CompressedString> compressed;5859table.resize(size);60hfunc_table.resize(size);61buckets.resize(size);62compressed.resize(keys.size());6364int idx = 0;65int total_compression_size = 0;6667for (const StringName &E : keys) {68//hash string69CharString cs = E.operator String().utf8();70uint32_t h = hash(0, cs.get_data());71Pair<int, CharString> p;72p.first = idx;73p.second = cs;74buckets.write[h % size].push_back(p);7576//compress string77CharString src_s = p_from->get_message(E).operator String().utf8();78CompressedString ps;79ps.orig_len = src_s.size();80ps.offset = total_compression_size;8182if (ps.orig_len != 0) {83CharString dst_s;84dst_s.resize_uninitialized(src_s.size());85int ret = smaz_compress(src_s.get_data(), src_s.size(), dst_s.ptrw(), src_s.size());86if (ret >= src_s.size()) {87//if compressed is larger than original, just use original88ps.orig_len = src_s.size();89ps.compressed = src_s;90} else {91dst_s.resize_uninitialized(ret);92//ps.orig_len=;93ps.compressed = dst_s;94}95} else {96ps.orig_len = 1;97ps.compressed.resize_uninitialized(1);98ps.compressed[0] = 0;99}100101compressed.write[idx] = ps;102total_compression_size += ps.compressed.size();103idx++;104}105106int bucket_table_size = 0;107108for (int i = 0; i < size; i++) {109const Vector<Pair<int, CharString>> &b = buckets[i];110HashMap<uint32_t, int> &t = table.write[i];111112if (b.is_empty()) {113continue;114}115116int d = 1;117int item = 0;118119while (item < b.size()) {120uint32_t slot = hash(d, b[item].second.get_data());121if (t.has(slot)) {122item = 0;123d++;124t.clear();125} else {126t[slot] = b[item].first;127item++;128}129}130131hfunc_table.write[i] = d;132bucket_table_size += 2 + b.size() * 4;133}134135ERR_FAIL_COND(bucket_table_size == 0);136137hash_table.resize(size);138bucket_table.resize(bucket_table_size);139140int *htwb = hash_table.ptrw();141int *btwb = bucket_table.ptrw();142143uint32_t *htw = (uint32_t *)&htwb[0];144uint32_t *btw = (uint32_t *)&btwb[0];145146int btindex = 0;147148for (int i = 0; i < size; i++) {149const HashMap<uint32_t, int> &t = table[i];150if (t.is_empty()) {151htw[i] = 0xFFFFFFFF; //nothing152continue;153}154155htw[i] = btindex;156btw[btindex++] = t.size();157btw[btindex++] = hfunc_table[i];158159for (const KeyValue<uint32_t, int> &E : t) {160btw[btindex++] = E.key;161btw[btindex++] = compressed[E.value].offset;162btw[btindex++] = compressed[E.value].compressed.size();163btw[btindex++] = compressed[E.value].orig_len;164}165}166167strings.resize(total_compression_size);168uint8_t *cw = strings.ptrw();169170for (int i = 0; i < compressed.size(); i++) {171memcpy(&cw[compressed[i].offset], compressed[i].compressed.get_data(), compressed[i].compressed.size());172}173174ERR_FAIL_COND(btindex != bucket_table_size);175set_locale(p_from->get_locale());176177#endif178}179180bool OptimizedTranslation::_set(const StringName &p_name, const Variant &p_value) {181String prop_name = p_name.operator String();182if (prop_name == "hash_table") {183hash_table = p_value;184} else if (prop_name == "bucket_table") {185bucket_table = p_value;186} else if (prop_name == "strings") {187strings = p_value;188} else if (prop_name == "load_from") {189generate(p_value);190} else {191return false;192}193194return true;195}196197bool OptimizedTranslation::_get(const StringName &p_name, Variant &r_ret) const {198String prop_name = p_name.operator String();199if (prop_name == "hash_table") {200r_ret = hash_table;201} else if (prop_name == "bucket_table") {202r_ret = bucket_table;203} else if (prop_name == "strings") {204r_ret = strings;205} else {206return false;207}208209return true;210}211212StringName OptimizedTranslation::get_message(const StringName &p_src_text, const StringName &p_context) const {213// p_context passed in is ignore. The use of context is not yet supported in OptimizedTranslation.214215int htsize = hash_table.size();216217if (htsize == 0) {218return StringName();219}220221CharString str = p_src_text.operator String().utf8();222uint32_t h = hash(0, str.get_data());223224const int *htr = hash_table.ptr();225const uint32_t *htptr = (const uint32_t *)&htr[0];226const int *btr = bucket_table.ptr();227const uint32_t *btptr = (const uint32_t *)&btr[0];228const uint8_t *sr = strings.ptr();229const char *sptr = (const char *)&sr[0];230231uint32_t p = htptr[h % htsize];232233if (p == 0xFFFFFFFF) {234return StringName(); //nothing235}236237const Bucket &bucket = *(const Bucket *)&btptr[p];238239h = hash(bucket.func, str.get_data());240241int idx = -1;242243for (int i = 0; i < bucket.size; i++) {244if (bucket.elem[i].key == h) {245idx = i;246break;247}248}249250if (idx == -1) {251return StringName();252}253254if (bucket.elem[idx].comp_size == bucket.elem[idx].uncomp_size) {255return String::utf8(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].uncomp_size);256} else {257CharString uncomp;258uncomp.resize_uninitialized(bucket.elem[idx].uncomp_size + 1);259smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptrw(), bucket.elem[idx].uncomp_size);260return String::utf8(uncomp.get_data());261}262}263264Vector<String> OptimizedTranslation::get_translated_message_list() const {265Vector<String> msgs;266267const int *htr = hash_table.ptr();268const uint32_t *htptr = (const uint32_t *)&htr[0];269const int *btr = bucket_table.ptr();270const uint32_t *btptr = (const uint32_t *)&btr[0];271const uint8_t *sr = strings.ptr();272const char *sptr = (const char *)&sr[0];273274for (int i = 0; i < hash_table.size(); i++) {275uint32_t p = htptr[i];276if (p != 0xFFFFFFFF) {277const Bucket &bucket = *(const Bucket *)&btptr[p];278for (int j = 0; j < bucket.size; j++) {279if (bucket.elem[j].comp_size == bucket.elem[j].uncomp_size) {280String rstr = String::utf8(&sptr[bucket.elem[j].str_offset], bucket.elem[j].uncomp_size);281msgs.push_back(rstr);282} else {283CharString uncomp;284uncomp.resize_uninitialized(bucket.elem[j].uncomp_size + 1);285smaz_decompress(&sptr[bucket.elem[j].str_offset], bucket.elem[j].comp_size, uncomp.ptrw(), bucket.elem[j].uncomp_size);286String rstr = String::utf8(uncomp.get_data());287msgs.push_back(rstr);288}289}290}291}292return msgs;293}294295StringName OptimizedTranslation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {296// The use of plurals translation is not yet supported in OptimizedTranslation.297return get_message(p_src_text, p_context);298}299300void OptimizedTranslation::_get_property_list(List<PropertyInfo> *p_list) const {301p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "hash_table"));302p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "bucket_table"));303p_list->push_back(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "strings"));304p_list->push_back(PropertyInfo(Variant::OBJECT, "load_from", PROPERTY_HINT_RESOURCE_TYPE, "Translation", PROPERTY_USAGE_EDITOR));305}306307void OptimizedTranslation::_bind_methods() {308ClassDB::bind_method(D_METHOD("generate", "from"), &OptimizedTranslation::generate);309}310311312