2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* path_remap.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2015-04-18 17:38:54 +00:00
|
|
|
#include "path_remap.h"
|
|
|
|
#include "globals.h"
|
|
|
|
#include "os/os.h"
|
|
|
|
#include "translation.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
PathRemap *PathRemap::singleton = NULL;
|
2015-04-18 17:38:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
PathRemap *PathRemap::get_singleton() {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void PathRemap::add_remap(const String &p_from, const String &p_to, const String &p_locale) {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
|
|
|
if (!remap.has(p_from)) {
|
2017-03-18 23:36:26 +00:00
|
|
|
remap[p_from] = RemapData();
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_locale == String())
|
|
|
|
remap[p_from].always = p_to;
|
2015-04-18 17:38:54 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
remap[p_from].locale[p_locale] = p_to;
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String PathRemap::get_remap(const String &p_from) const {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const RemapData *ptr = remap.getptr(p_from);
|
2015-04-18 17:38:54 +00:00
|
|
|
if (!ptr) {
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose())
|
2017-03-18 23:36:26 +00:00
|
|
|
print_line("remap failed: " + p_from);
|
2015-04-18 17:38:54 +00:00
|
|
|
return p_from;
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const RemapData *ptr2 = NULL;
|
2016-06-29 13:17:53 +00:00
|
|
|
|
2015-04-18 17:38:54 +00:00
|
|
|
String locale = TranslationServer::get_singleton()->get_locale();
|
|
|
|
|
|
|
|
if (ptr->locale.has(locale)) {
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose())
|
2017-03-18 23:36:26 +00:00
|
|
|
print_line("remap found: " + p_from + " -> " + ptr->locale[locale]);
|
2016-06-29 13:17:53 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ptr2 = remap.getptr(ptr->locale[locale]);
|
2016-06-29 13:17:53 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ptr2 && ptr2->always != String()) //may have atlas or export remap too
|
2016-06-29 13:17:53 +00:00
|
|
|
return ptr2->always;
|
|
|
|
else
|
|
|
|
return ptr->locale[locale];
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int p = locale.find("_");
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p != -1) {
|
|
|
|
locale = locale.substr(0, p);
|
2015-04-18 17:38:54 +00:00
|
|
|
if (ptr->locale.has(locale)) {
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose())
|
2017-03-18 23:36:26 +00:00
|
|
|
print_line("remap found: " + p_from + " -> " + ptr->locale[locale]);
|
2016-06-29 13:17:53 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ptr2 = remap.getptr(ptr->locale[locale]);
|
2016-06-29 13:17:53 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ptr2 && ptr2->always != String()) //may have atlas or export remap too
|
2016-06-29 13:17:53 +00:00
|
|
|
return ptr2->always;
|
|
|
|
else
|
|
|
|
return ptr->locale[locale];
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ptr->always != String()) {
|
2015-04-18 17:38:54 +00:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
print_line("remap found: " + p_from + " -> " + ptr->always);
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
return ptr->always;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose())
|
2017-03-18 23:36:26 +00:00
|
|
|
print_line("remap failed: " + p_from);
|
2015-04-18 17:38:54 +00:00
|
|
|
|
|
|
|
return p_from;
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool PathRemap::has_remap(const String &p_from) const {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
|
|
|
return remap.has(p_from);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void PathRemap::erase_remap(const String &p_from) {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!remap.has(p_from));
|
|
|
|
remap.erase(p_from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathRemap::clear_remaps() {
|
|
|
|
|
|
|
|
remap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathRemap::load_remaps() {
|
|
|
|
|
|
|
|
// default remaps first
|
|
|
|
DVector<String> remaps = Globals::get_singleton()->get("remap/all");
|
|
|
|
|
|
|
|
{
|
|
|
|
int rlen = remaps.size();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(rlen % 2);
|
2015-04-18 17:38:54 +00:00
|
|
|
DVector<String>::Read r = remaps.read();
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < rlen / 2; i++) {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String from = r[i * 2 + 0];
|
|
|
|
String to = r[i * 2 + 1];
|
|
|
|
add_remap(from, to);
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// platform remaps second, so override
|
2017-03-18 23:36:26 +00:00
|
|
|
remaps = Globals::get_singleton()->get("remap/" + OS::get_singleton()->get_name());
|
|
|
|
// remaps = Globals::get_singleton()->get("remap/PSP");
|
2015-04-18 17:38:54 +00:00
|
|
|
{
|
|
|
|
int rlen = remaps.size();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(rlen % 2);
|
2015-04-18 17:38:54 +00:00
|
|
|
DVector<String>::Read r = remaps.read();
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < rlen / 2; i++) {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String from = r[i * 2 + 0];
|
|
|
|
String to = r[i * 2 + 1];
|
|
|
|
// print_line("add remap: "+from+" -> "+to);
|
|
|
|
add_remap(from, to);
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//locale based remaps
|
|
|
|
|
|
|
|
if (Globals::get_singleton()->has("locale/translation_remaps")) {
|
|
|
|
|
|
|
|
Dictionary remaps = Globals::get_singleton()->get("locale/translation_remaps");
|
|
|
|
List<Variant> rk;
|
|
|
|
remaps.get_key_list(&rk);
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Variant>::Element *E = rk.front(); E; E = E->next()) {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
|
|
|
String source = E->get();
|
|
|
|
StringArray sa = remaps[E->get()];
|
|
|
|
int sas = sa.size();
|
|
|
|
StringArray::Read r = sa.read();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < sas; i++) {
|
2015-04-18 17:38:54 +00:00
|
|
|
|
|
|
|
String s = r[i];
|
|
|
|
int qp = s.find_last(":");
|
2017-03-18 23:36:26 +00:00
|
|
|
if (qp != -1) {
|
|
|
|
String path = s.substr(0, qp);
|
|
|
|
String locale = s.substr(qp + 1, s.length());
|
|
|
|
add_remap(source, path, locale);
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathRemap::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_remap", "from", "to", "locale"), &PathRemap::add_remap, DEFVAL(String()));
|
|
|
|
ObjectTypeDB::bind_method(_MD("has_remap", "path"), &PathRemap::has_remap);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_remap", "path"), &PathRemap::get_remap);
|
|
|
|
ObjectTypeDB::bind_method(_MD("erase_remap", "path"), &PathRemap::erase_remap);
|
|
|
|
ObjectTypeDB::bind_method(_MD("clear_remaps"), &PathRemap::clear_remaps);
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PathRemap::PathRemap() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
singleton = this;
|
2015-04-18 17:38:54 +00:00
|
|
|
}
|