Clean up server-side translations, remove global variable (#10075)

This commit is contained in:
rubenwardy 2020-09-16 14:51:11 +01:00 committed by GitHub
parent c8303f790c
commit 9ec75d7765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 50 additions and 65 deletions

@ -238,18 +238,13 @@ void Client::scanModSubfolder(const std::string &mod_name, const std::string &mo
infostream << "Client::scanModSubfolder(): Loading \"" << real_path infostream << "Client::scanModSubfolder(): Loading \"" << real_path
<< "\" as \"" << vfs_path << "\"." << std::endl; << "\" as \"" << vfs_path << "\"." << std::endl;
std::ifstream is(real_path, std::ios::binary | std::ios::ate); std::string contents;
if(!is.good()) { if (!fs::ReadFile(real_path, contents)) {
errorstream << "Client::scanModSubfolder(): Can't read file \"" errorstream << "Client::scanModSubfolder(): Can't read file \""
<< real_path << "\"." << std::endl; << real_path << "\"." << std::endl;
continue; continue;
} }
auto size = is.tellg();
std::string contents(size, '\0');
is.seekg(0);
is.read(&contents[0], size);
infostream << " size: " << size << " bytes" << std::endl;
m_mod_vfs.emplace(vfs_path, contents); m_mod_vfs.emplace(vfs_path, contents);
} }
} }

@ -750,6 +750,21 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
return true; return true;
} }
bool ReadFile(const std::string &path, std::string &out)
{
std::ifstream is(path, std::ios::binary | std::ios::ate);
if (!is.good()) {
return false;
}
auto size = is.tellg();
out.resize(size);
is.seekg(0);
is.read(&out[0], size);
return true;
}
bool Rename(const std::string &from, const std::string &to) bool Rename(const std::string &from, const std::string &to)
{ {
return rename(from.c_str(), to.c_str()) == 0; return rename(from.c_str(), to.c_str()) == 0;

@ -128,6 +128,8 @@ const char *GetFilenameFromPath(const char *path);
bool safeWriteToFile(const std::string &path, const std::string &content); bool safeWriteToFile(const std::string &path, const std::string &content);
bool ReadFile(const std::string &path, std::string &out);
bool Rename(const std::string &from, const std::string &to); bool Rename(const std::string &from, const std::string &to);
} // namespace fs } // namespace fs

@ -1340,9 +1340,9 @@ int ModApiEnvMod::l_get_translated_string(lua_State * L)
GET_ENV_PTR; GET_ENV_PTR;
std::string lang_code = luaL_checkstring(L, 1); std::string lang_code = luaL_checkstring(L, 1);
std::string string = luaL_checkstring(L, 2); std::string string = luaL_checkstring(L, 2);
getServer(L)->loadTranslationLanguage(lang_code);
string = wide_to_utf8(translate_string(utf8_to_wide(string), auto *translations = getServer(L)->getTranslationLanguage(lang_code);
&(*g_server_translations)[lang_code])); string = wide_to_utf8(translate_string(utf8_to_wide(string), translations));
lua_pushstring(L, string.c_str()); lua_pushstring(L, string.c_str());
return 1; return 1;
} }

@ -2451,31 +2451,14 @@ bool Server::addMediaFile(const std::string &filename,
// Ok, attempt to load the file and add to cache // Ok, attempt to load the file and add to cache
// Read data // Read data
std::ifstream fis(filepath.c_str(), std::ios_base::binary);
if (!fis.good()) {
errorstream << "Server::addMediaFile(): Could not open \""
<< filename << "\" for reading" << std::endl;
return false;
}
std::string filedata; std::string filedata;
bool bad = false; if (!fs::ReadFile(filepath, filedata)) {
for (;;) { errorstream << "Server::addMediaFile(): Failed to open \""
char buf[1024]; << filename << "\" for reading" << std::endl;
fis.read(buf, sizeof(buf));
std::streamsize len = fis.gcount();
filedata.append(buf, len);
if (fis.eof())
break;
if (!fis.good()) {
bad = true;
break;
}
}
if (bad) {
errorstream << "Server::addMediaFile(): Failed to read \""
<< filename << "\"" << std::endl;
return false; return false;
} else if (filedata.empty()) { }
if (filedata.empty()) {
errorstream << "Server::addMediaFile(): Empty file \"" errorstream << "Server::addMediaFile(): Empty file \""
<< filepath << "\"" << std::endl; << filepath << "\"" << std::endl;
return false; return false;
@ -3890,19 +3873,27 @@ void Server::broadcastModChannelMessage(const std::string &channel,
} }
} }
void Server::loadTranslationLanguage(const std::string &lang_code) Translations *Server::getTranslationLanguage(const std::string &lang_code)
{ {
if (g_server_translations->count(lang_code)) if (lang_code.empty())
return; // Already loaded return nullptr;
auto it = server_translations.find(lang_code);
if (it != server_translations.end())
return &it->second; // Already loaded
// [] will create an entry
auto *translations = &server_translations[lang_code];
std::string suffix = "." + lang_code + ".tr"; std::string suffix = "." + lang_code + ".tr";
for (const auto &i : m_media) { for (const auto &i : m_media) {
if (str_ends_with(i.first, suffix)) { if (str_ends_with(i.first, suffix)) {
std::ifstream t(i.second.path); std::string data;
std::string data((std::istreambuf_iterator<char>(t)), if (fs::ReadFile(i.second.path, data)) {
std::istreambuf_iterator<char>()); translations->loadTranslation(data);
}
(*g_server_translations)[lang_code].loadTranslation(data);
} }
} }
return translations;
} }

@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serverenvironment.h" #include "serverenvironment.h"
#include "clientiface.h" #include "clientiface.h"
#include "chatmessage.h" #include "chatmessage.h"
#include "translation.h"
#include <string> #include <string>
#include <list> #include <list>
#include <map> #include <map>
@ -343,8 +344,8 @@ public:
// Send block to specific player only // Send block to specific player only
bool SendBlock(session_t peer_id, const v3s16 &blockpos); bool SendBlock(session_t peer_id, const v3s16 &blockpos);
// Load translations for a language // Get or load translations for a language
void loadTranslationLanguage(const std::string &lang_code); Translations *getTranslationLanguage(const std::string &lang_code);
// Bind address // Bind address
Address m_bind_addr; Address m_bind_addr;
@ -557,6 +558,8 @@ private:
// Mods // Mods
std::unique_ptr<ServerModManager> m_modmgr; std::unique_ptr<ServerModManager> m_modmgr;
std::unordered_map<std::string, Translations> server_translations;
/* /*
Threads Threads
*/ */

@ -52,15 +52,7 @@ std::vector<ServerListSpec> getLocal()
{ {
std::string path = ServerList::getFilePath(); std::string path = ServerList::getFilePath();
std::string liststring; std::string liststring;
if (fs::PathExists(path)) { fs::ReadFile(path, liststring);
std::ifstream istream(path.c_str());
if (istream.is_open()) {
std::ostringstream ostream;
ostream << istream.rdbuf();
liststring = ostream.str();
istream.close();
}
}
return deSerialize(liststring); return deSerialize(liststring);
} }

@ -29,14 +29,6 @@ Translations client_translations;
Translations *g_client_translations = &client_translations; Translations *g_client_translations = &client_translations;
#endif #endif
// Per language server translations
std::unordered_map<std::string,Translations> server_translations;
std::unordered_map<std::string,Translations> *g_server_translations = &server_translations;
Translations::~Translations()
{
clear();
}
void Translations::clear() void Translations::clear()
{ {

@ -23,7 +23,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string> #include <string>
class Translations; class Translations;
extern std::unordered_map<std::string, Translations> *g_server_translations;
#ifndef SERVER #ifndef SERVER
extern Translations *g_client_translations; extern Translations *g_client_translations;
#endif #endif
@ -31,10 +30,6 @@ extern Translations *g_client_translations;
class Translations class Translations
{ {
public: public:
Translations() = default;
~Translations();
void loadTranslation(const std::string &data); void loadTranslation(const std::string &data);
void clear(); void clear();
const std::wstring &getTranslation( const std::wstring &getTranslation(