Skip to content

Commit

Permalink
Lua: add ScriptCB_getlocalizestr
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyuu17 committed Jul 10, 2024
1 parent a959052 commit 9c2c92c
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_sources(${PROJECT_NAME}
"SWBF2/Native/Chunks/WorldChunk.cpp"
"SWBF2/Native/Models/Model.cpp"
"SWBF2/Native/Scripting/Lua/LuaCallbacks.cpp"
"SWBF2/Native/Scripting/Lua/LuaUtils.cpp"
"SWBF2/Native/Scripting/Lua/ScriptCB.cpp"
"SWBF2/Native/Texture/Texture.cpp"
"SWBF2/Native/Texture/TextureUtils.cpp"
Expand Down
2 changes: 1 addition & 1 deletion src/SWBF2/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace SWBF2

Core::Core()
: m_curGamemode(Gamemode::NONE)
, m_language("english")
, m_language(Language::ENGLISH)
, m_platform("pc")
{
m_instance = this;
Expand Down
10 changes: 7 additions & 3 deletions src/SWBF2/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace SWBF2
static Core *m_instance;

godot::String m_curMapName;
godot::String m_language;
godot::String m_platform;

public: // variables
Gamemode m_curGamemode;
Language m_language;

public: // methods
Core();
Expand All @@ -36,8 +36,12 @@ namespace SWBF2
const godot::String &GetMapName() { return m_curMapName; };
void LoadLevel(const godot::String &mapName);

const godot::String &GetLanguage() { return m_language; };
void SetLanguage(const godot::String &language) { m_language = language; };
const godot::String &GetLanguage() const {
static godot::String str{ std::string{ DefaultLanguages.at(m_language) }.c_str() };
return str;
}

void SetLanguage(const godot::String &language) { m_language = DefaultLanguagesByName.at(language.ascii().get_data()); }

const godot::String &GetPlatform() { return m_platform; };

Expand Down
2 changes: 1 addition & 1 deletion src/SWBF2/GameData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace SWBF2
std::unordered_map<std::string, Native::Scripting::Lua::LuaScript> m_luaScripts;

using LoclEntriesMap = std::unordered_map<FNVHash, std::u16string>;
std::unordered_map<std::string, LoclEntriesMap> m_locl;
std::unordered_map<Language, LoclEntriesMap> m_locl;
};

std::unordered_map<std::string, SGameData> m_gameData;
Expand Down
4 changes: 3 additions & 1 deletion src/SWBF2/Native/Chunks/LoclChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace SWBF2::Native

streamReader >> chars;

GameData::Instance()->m_gameData[filename].m_locl[loclName].insert_or_assign(hash, chars.data());
Language langId = DefaultLanguagesByName.at(loclName);

GameData::Instance()->m_gameData[filename].m_locl[langId].insert_or_assign(hash, chars.data());
}
}
19 changes: 19 additions & 0 deletions src/SWBF2/Native/Scripting/Lua/LuaUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#include <lua.h>

#include "Core.hpp"
#include "GameScripts.hpp"

#include "LuaUtils.hpp"

namespace SWBF2::Native::Scripting::Lua
{
namespace LuaUtils
{
void PushUString(const std::u16string &str)
{
const char *s = reinterpret_cast<const char *>(str.data());
lua_pushlstring(GameScripts::Instance()->m_luaState, s, 2 * str.size() + 2);
}
}
}
10 changes: 10 additions & 0 deletions src/SWBF2/Native/Scripting/Lua/LuaUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#pragma once

namespace SWBF2::Native::Scripting::Lua
{
namespace LuaUtils
{
void PushUString(const std::u16string &str);
}
}
17 changes: 15 additions & 2 deletions src/SWBF2/Native/Scripting/Lua/ScriptCB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include <lua.h>

#include "Core.hpp"
#include "GameData.hpp"

#include "LuaCallbacks.hpp"
#include "LuaUtils.hpp"
#include "ScriptCB.hpp"

namespace SWBF2::Native::Scripting::Lua
Expand All @@ -22,7 +24,9 @@ namespace SWBF2::Native::Scripting::Lua

int ScriptCB_GetLanguage(lua_State *L)
{
lua_pushstring(L, Core::Instance()->GetLanguage().ascii().get_data());
Language langId = Core::Instance()->m_language;
lua_pushstring(L, std::string{ DefaultLanguages.at(langId) }.c_str());
lua_pushnumber(L, (lua_Number)langId);

return 1;
}
Expand Down Expand Up @@ -59,7 +63,16 @@ namespace SWBF2::Native::Scripting::Lua

int ScriptCB_getlocalizestr(lua_State *L)
{
return 0;
const char *key = luaL_checkstring(L, 1);

const auto &langId = Core::Instance()->m_language;
const auto &locl = GameData::Instance()->GetMapData().m_locl;
if (locl.contains(langId))
LuaUtils::PushUString(locl.at(langId).at(FNVGenerateHash(key)));
else
LuaUtils::PushUString({});

return 1;
}

int ScriptCB_usprintf(lua_State *L)
Expand Down
39 changes: 39 additions & 0 deletions src/SWBF2/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,45 @@ namespace SWBF2
{ "yav1", "yav/yav1.lvl" }
};

enum class Language
{
ENGLISH = 0,
SPANISH,
ITALIAN,
FRENCH,
GERMAN,
JAPANESE,
UK_ENGLISH,
THAI,
POLISH
};

const static inline std::unordered_map<std::string_view, Language> DefaultLanguagesByName
{
{ "english", Language::ENGLISH },
{ "spanish", Language::SPANISH },
{ "italian", Language::ITALIAN },
{ "french", Language::FRENCH },
{ "german", Language::GERMAN },
{ "japanese", Language::JAPANESE },
{ "uk_english", Language::UK_ENGLISH },
{ "thai", Language::THAI },
{ "polish", Language::POLISH }
};

const static inline std::unordered_map<Language, std::string_view> DefaultLanguages
{
{ Language::ENGLISH, "english" },
{ Language::SPANISH, "spanish" },
{ Language::ITALIAN, "italian" },
{ Language::FRENCH, "french" },
{ Language::GERMAN, "german" },
{ Language::JAPANESE, "japanese" },
{ Language::UK_ENGLISH, "uk_english" },
{ Language::THAI, "thai" },
{ Language::POLISH, "polish" }
};

enum class Gamemode
{
NONE = 0,
Expand Down

0 comments on commit 9c2c92c

Please sign in to comment.