Skip to content

Commit

Permalink
Using thread to improve localization loading speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-Chara committed Sep 17, 2024
1 parent a0e4409 commit b2f5ebb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/engine/localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ILocalization : public IInterface
MACRO_INTERFACE("localization", 0)
public:

virtual bool Init() = 0;
virtual void Init() = 0;
virtual const char *GetLanguageCode(int Country) = 0;
virtual const char *Localize(const char *pLanguage, const char *pText) = 0;
};
Expand Down
25 changes: 12 additions & 13 deletions src/engine/server/localization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ CLocalization::CLocalization(IStorage *pStorage)
m_pStorage = pStorage;
}

// TODO: Using thread to improve loading speed
bool CLocalization::Init()
void CLocalization::Init()
{
if(LoadLocalizations())
return true;
return false;
thread_init(LoadLocalizations, this);
}

bool CLocalization::LoadLocalizations()
void CLocalization::LoadLocalizations(void *pUser)
{
CLocalization *pThis = (CLocalization *)pUser;

const char *pIndex = "./data/server/languages/index.json";
IOHANDLE File = m_pStorage->OpenFile(pIndex, IOFLAG_READ, IStorage::TYPE_ALL);
IOHANDLE File = pThis->m_pStorage->OpenFile(pIndex, IOFLAG_READ, IStorage::TYPE_ALL);
if(!File)
{
dbg_msg("Localization", "can't open ./server_lang/index.json");
return false;
return;
}

const int FileSize = (int)io_length(File);
Expand All @@ -38,7 +37,7 @@ bool CLocalization::LoadLocalizations()
if(pJsonData == nullptr)
{
dbg_msg("Localization", "Can't load the localization file %s : %s", pIndex, aError);
return false;
return;
}

const json_value &rStart = (*pJsonData)["language indices"];
Expand All @@ -48,15 +47,15 @@ bool CLocalization::LoadLocalizations()
// Set i = 1, Skip English
for (unsigned i = 1; i < rStart.u.array.length; ++i)
{
if(!LoadLanguage(rStart[i]["file"]))
return false;
}
if(!pThis->LoadLanguage(rStart[i]["file"]))
dbg_msg("Localization", "Can't load the localization file %s", (const char *)rStart[i]["file"]);
}
}

// clean up
json_value_free(pJsonData);

return true;
dbg_msg("Localization", "Localization loaded");
}

bool CLocalization::LoadLanguage(const char *pFile)
Expand Down
15 changes: 8 additions & 7 deletions src/engine/server/localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ class CLocalization : public ILocalization
private:
IStorage *m_pStorage;
std::map<std::string, SLanguageFile> m_aLocalize;

static void LoadLocalizations(void *pUser);

bool LoadLanguage(const char *aFile);
void AddNewLocalize(const char *pName, const char *pKey, const char *pValue);

public:
CLocalization(IStorage *pStorage);


virtual void Init();
virtual const char *GetLanguageCode(int Country);
virtual bool Init();
virtual const char *Localize(const char *pLanguage, const char *pText);

bool LoadLocalizations();
bool LoadLanguage(const char *aFile);

void AddNewLocalize(const char *pName, const char *pKey, const char *pValue);
};

#endif
8 changes: 4 additions & 4 deletions src/engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,12 +2083,12 @@ void CServer::ConReloadLocalizations(IConsole::IResult *pResult, void *pUserData
CServer *pSelf = static_cast<CServer *>(pUserData);

ILocalization *pLocalization = pSelf->Kernel()->RequestInterface<ILocalization>();
if(pLocalization && !pLocalization->Init())
if(!pLocalization)
{
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "Localization reload failed.");
return;
}
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "Localization texts reloaded.");
pLocalization->Init();
}

void CServer::RegisterCommands()
Expand Down Expand Up @@ -2175,8 +2175,8 @@ int main(int argc, const char **argv) // ignore_convention
IStorage *pStorage = CreateStorage("Ninslash", IStorage::STORAGETYPE_SERVER, argc, argv); // ignore_convention
IConfig *pConfig = CreateConfig();
ILocalization *pLocalization = CreateLocalization(pStorage);
if(!pLocalization->Init())
dbg_msg("Localization", "Failed to Init localization.");

pLocalization->Init();

pServer->InitRegister(&pServer->m_NetServer, pEngineMasterServer, pConsole);

Expand Down

0 comments on commit b2f5ebb

Please sign in to comment.