Skip to content

Commit 9ec75d7

Browse files
authoredSep 16, 2020
Clean up server-side translations, remove global variable (#10075)
1 parent c8303f7 commit 9ec75d7

File tree

9 files changed

+50
-65
lines changed

9 files changed

+50
-65
lines changed
 

Diff for: ‎src/client/client.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,13 @@ void Client::scanModSubfolder(const std::string &mod_name, const std::string &mo
238238
infostream << "Client::scanModSubfolder(): Loading \"" << real_path
239239
<< "\" as \"" << vfs_path << "\"." << std::endl;
240240

241-
std::ifstream is(real_path, std::ios::binary | std::ios::ate);
242-
if(!is.good()) {
241+
std::string contents;
242+
if (!fs::ReadFile(real_path, contents)) {
243243
errorstream << "Client::scanModSubfolder(): Can't read file \""
244244
<< real_path << "\"." << std::endl;
245245
continue;
246246
}
247-
auto size = is.tellg();
248-
std::string contents(size, '\0');
249-
is.seekg(0);
250-
is.read(&contents[0], size);
251247

252-
infostream << " size: " << size << " bytes" << std::endl;
253248
m_mod_vfs.emplace(vfs_path, contents);
254249
}
255250
}

Diff for: ‎src/filesys.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,21 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
750750
return true;
751751
}
752752

753+
bool ReadFile(const std::string &path, std::string &out)
754+
{
755+
std::ifstream is(path, std::ios::binary | std::ios::ate);
756+
if (!is.good()) {
757+
return false;
758+
}
759+
760+
auto size = is.tellg();
761+
out.resize(size);
762+
is.seekg(0);
763+
is.read(&out[0], size);
764+
765+
return true;
766+
}
767+
753768
bool Rename(const std::string &from, const std::string &to)
754769
{
755770
return rename(from.c_str(), to.c_str()) == 0;

Diff for: ‎src/filesys.h

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ const char *GetFilenameFromPath(const char *path);
128128

129129
bool safeWriteToFile(const std::string &path, const std::string &content);
130130

131+
bool ReadFile(const std::string &path, std::string &out);
132+
131133
bool Rename(const std::string &from, const std::string &to);
132134

133135
} // namespace fs

Diff for: ‎src/script/lua_api/l_env.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1340,9 +1340,9 @@ int ModApiEnvMod::l_get_translated_string(lua_State * L)
13401340
GET_ENV_PTR;
13411341
std::string lang_code = luaL_checkstring(L, 1);
13421342
std::string string = luaL_checkstring(L, 2);
1343-
getServer(L)->loadTranslationLanguage(lang_code);
1344-
string = wide_to_utf8(translate_string(utf8_to_wide(string),
1345-
&(*g_server_translations)[lang_code]));
1343+
1344+
auto *translations = getServer(L)->getTranslationLanguage(lang_code);
1345+
string = wide_to_utf8(translate_string(utf8_to_wide(string), translations));
13461346
lua_pushstring(L, string.c_str());
13471347
return 1;
13481348
}

Diff for: ‎src/server.cpp

+22-31
Original file line numberDiff line numberDiff line change
@@ -2451,31 +2451,14 @@ bool Server::addMediaFile(const std::string &filename,
24512451
// Ok, attempt to load the file and add to cache
24522452

24532453
// Read data
2454-
std::ifstream fis(filepath.c_str(), std::ios_base::binary);
2455-
if (!fis.good()) {
2456-
errorstream << "Server::addMediaFile(): Could not open \""
2457-
<< filename << "\" for reading" << std::endl;
2458-
return false;
2459-
}
24602454
std::string filedata;
2461-
bool bad = false;
2462-
for (;;) {
2463-
char buf[1024];
2464-
fis.read(buf, sizeof(buf));
2465-
std::streamsize len = fis.gcount();
2466-
filedata.append(buf, len);
2467-
if (fis.eof())
2468-
break;
2469-
if (!fis.good()) {
2470-
bad = true;
2471-
break;
2472-
}
2473-
}
2474-
if (bad) {
2475-
errorstream << "Server::addMediaFile(): Failed to read \""
2476-
<< filename << "\"" << std::endl;
2455+
if (!fs::ReadFile(filepath, filedata)) {
2456+
errorstream << "Server::addMediaFile(): Failed to open \""
2457+
<< filename << "\" for reading" << std::endl;
24772458
return false;
2478-
} else if (filedata.empty()) {
2459+
}
2460+
2461+
if (filedata.empty()) {
24792462
errorstream << "Server::addMediaFile(): Empty file \""
24802463
<< filepath << "\"" << std::endl;
24812464
return false;
@@ -3890,19 +3873,27 @@ void Server::broadcastModChannelMessage(const std::string &channel,
38903873
}
38913874
}
38923875

3893-
void Server::loadTranslationLanguage(const std::string &lang_code)
3876+
Translations *Server::getTranslationLanguage(const std::string &lang_code)
38943877
{
3895-
if (g_server_translations->count(lang_code))
3896-
return; // Already loaded
3878+
if (lang_code.empty())
3879+
return nullptr;
3880+
3881+
auto it = server_translations.find(lang_code);
3882+
if (it != server_translations.end())
3883+
return &it->second; // Already loaded
3884+
3885+
// [] will create an entry
3886+
auto *translations = &server_translations[lang_code];
38973887

38983888
std::string suffix = "." + lang_code + ".tr";
38993889
for (const auto &i : m_media) {
39003890
if (str_ends_with(i.first, suffix)) {
3901-
std::ifstream t(i.second.path);
3902-
std::string data((std::istreambuf_iterator<char>(t)),
3903-
std::istreambuf_iterator<char>());
3904-
3905-
(*g_server_translations)[lang_code].loadTranslation(data);
3891+
std::string data;
3892+
if (fs::ReadFile(i.second.path, data)) {
3893+
translations->loadTranslation(data);
3894+
}
39063895
}
39073896
}
3897+
3898+
return translations;
39083899
}

Diff for: ‎src/server.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3838
#include "serverenvironment.h"
3939
#include "clientiface.h"
4040
#include "chatmessage.h"
41+
#include "translation.h"
4142
#include <string>
4243
#include <list>
4344
#include <map>
@@ -343,8 +344,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
343344
// Send block to specific player only
344345
bool SendBlock(session_t peer_id, const v3s16 &blockpos);
345346

346-
// Load translations for a language
347-
void loadTranslationLanguage(const std::string &lang_code);
347+
// Get or load translations for a language
348+
Translations *getTranslationLanguage(const std::string &lang_code);
348349

349350
// Bind address
350351
Address m_bind_addr;
@@ -557,6 +558,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
557558
// Mods
558559
std::unique_ptr<ServerModManager> m_modmgr;
559560

561+
std::unordered_map<std::string, Translations> server_translations;
562+
560563
/*
561564
Threads
562565
*/

Diff for: ‎src/serverlist.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ std::vector<ServerListSpec> getLocal()
5252
{
5353
std::string path = ServerList::getFilePath();
5454
std::string liststring;
55-
if (fs::PathExists(path)) {
56-
std::ifstream istream(path.c_str());
57-
if (istream.is_open()) {
58-
std::ostringstream ostream;
59-
ostream << istream.rdbuf();
60-
liststring = ostream.str();
61-
istream.close();
62-
}
63-
}
55+
fs::ReadFile(path, liststring);
6456

6557
return deSerialize(liststring);
6658
}

Diff for: ‎src/translation.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ Translations client_translations;
2929
Translations *g_client_translations = &client_translations;
3030
#endif
3131

32-
// Per language server translations
33-
std::unordered_map<std::string,Translations> server_translations;
34-
std::unordered_map<std::string,Translations> *g_server_translations = &server_translations;
35-
36-
Translations::~Translations()
37-
{
38-
clear();
39-
}
4032

4133
void Translations::clear()
4234
{

Diff for: ‎src/translation.h

-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include <string>
2424

2525
class Translations;
26-
extern std::unordered_map<std::string, Translations> *g_server_translations;
2726
#ifndef SERVER
2827
extern Translations *g_client_translations;
2928
#endif
3029

3130
class Translations
3231
{
3332
public:
34-
Translations() = default;
35-
36-
~Translations();
37-
3833
void loadTranslation(const std::string &data);
3934
void clear();
4035
const std::wstring &getTranslation(

0 commit comments

Comments
 (0)
Please sign in to comment.