Skip to content

Commit

Permalink
Language packs texts normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
cvet committed Feb 11, 2025
1 parent 1d22223 commit 16b99d5
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 34 deletions.
15 changes: 15 additions & 0 deletions BuildTools/FinalizeGeneration.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,10 @@ if(FO_DEBUGGING_CONFIG)
foreach(entry ${FO_RESOURCES})
list(APPEND FO_CODEGEN_COMMAND_ARGS -config "BakeResourceEntries,+${entry}")
endforeach()

foreach(entry ${FO_BAKING_OPTIONS})
list(APPEND FO_CODEGEN_COMMAND_ARGS -config "${entry}")
endforeach()
endif()

list(APPEND FO_CODEGEN_META_SOURCE
Expand Down Expand Up @@ -1529,6 +1533,13 @@ foreach(entry ${FO_RESOURCES})
list(APPEND bakeResources -BakeResourceEntries "+${entry}")
endforeach()

foreach(entry ${FO_BAKING_OPTIONS})
string(REPLACE "," ";" entry ${entry})
list(GET entry 0 opt)
list(GET entry 1 value)
list(APPEND bakeResources "-${opt}" "${value}")
endforeach()

add_custom_target(BakeResources
COMMAND ${bakeResources} -ForceBaking False
WORKING_DIRECTORY ${FO_OUTPUT_PATH}
Expand Down Expand Up @@ -1645,6 +1656,10 @@ if(FO_MAKE_EXTERNAL_COMMANDS)
set(FO_GEN_FILE_CONTENT "${FO_GEN_FILE_CONTENT} ${breakLine}\n-resource \"${packName},${packEntry}\"")
endforeach()

foreach(entry ${FO_BAKING_OPTIONS})
set(FO_GEN_FILE_CONTENT "${FO_GEN_FILE_CONTENT} ${breakLine}\n-config \"${entry}\"")
endforeach()

configure_file("${FO_ENGINE_ROOT}/BuildTools/blank.cmake.txt" "${FO_OUTPUT_PATH}/Starter.${scriptExt}" FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
endif()

Expand Down
1 change: 1 addition & 0 deletions BuildTools/Init.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ set(FO_CACHE_VARIABLES "")
set(FO_CONTRIBUTION_DIR "")
set(FO_CONTENT "")
set(FO_RESOURCES "")
set(FO_BAKING_OPTIONS "")
set(FO_SPRITE_ATLAS "")
set(FO_CODEGEN_COMMAND "")
set(FO_CODEGEN_COMMAND_ARGS "")
Expand Down
7 changes: 7 additions & 0 deletions BuildTools/StartGeneration.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,13 @@ macro(AddToPackage package binary platform arch packType)
list(APPEND Package_${package}_Parts "${binary},${platform},${arch},${packType},${ARGN}")
endmacro()

macro(SetBakingOption opt)
foreach(arg ${ARGN})
StatusMessage("+ Baking option ${opt} = ${arg}")
list(APPEND FO_BAKING_OPTIONS "${opt},${arg}")
endforeach()
endmacro()

# Core contribution
set(FO_CONTRIBUTION_DIR ${FO_ENGINE_ROOT})
AddNativeIncludeDir("Source/Scripting/Native")
Expand Down
3 changes: 3 additions & 0 deletions BuildTools/starter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
parser.add_argument('-mappercfg', dest='mappercfg', help='Mapper default config')
parser.add_argument('-content', dest='content', action='append', default=[], help='content file path')
parser.add_argument('-resource', dest='resource', action='append', default=[], help='resource file path')
parser.add_argument('-config', dest='config', action='append', default=[], help='config option')
args = parser.parse_args()

assert platform.system() in ['Linux', 'Darwin', 'Windows'], 'Invalid OS'
Expand Down Expand Up @@ -155,6 +156,8 @@ def action(binEntry=binEntry, binType=binType, buildType=buildType):
exeArgs += ['-BakeContentEntries', f'+{c}']
for r in args.resource:
exeArgs += ['-BakeResourceEntries', f'+{r}']
for entry in args.config:
exeArgs += ['-' + entry.split(',')[0], entry.split(',')[1]]

needWait = binType in ['Baker', 'ASCompiler'] or 'San_' in buildType

Expand Down
1 change: 1 addition & 0 deletions Source/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ FOClient::FOClient(GlobalSettings& settings, AppWindow* window, bool mapper_mode
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("Texts"));
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("StaticMaps"));
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("ClientProtos"));

if constexpr (FO_ANGELSCRIPT_SCRIPTING) {
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("ClientAngelScript"));
}
Expand Down
10 changes: 8 additions & 2 deletions Source/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1793,8 +1793,8 @@ struct irect
height {height_}
{
}
[[nodiscard]] constexpr auto operator==(const irect& other) const noexcept -> bool { return x == other.x && y == other.y && width == other.width && width == other.width; }
[[nodiscard]] constexpr auto operator!=(const irect& other) const noexcept -> bool { return x != other.x || y != other.y || height != other.height || height != other.height; }
[[nodiscard]] constexpr auto operator==(const irect& other) const noexcept -> bool { return x == other.x && y == other.y && width == other.width && height == other.height; }
[[nodiscard]] constexpr auto operator!=(const irect& other) const noexcept -> bool { return x != other.x || y != other.y || width != other.width || height != other.height; }

int x {};
int y {};
Expand Down Expand Up @@ -2379,6 +2379,12 @@ constexpr auto vec_transform(const vector<T>& vec, const U& transfromer) -> auto
return result;
}

template<typename T>
constexpr auto vec_exists(const vector<T>& vec, const T& value) noexcept -> bool
{
return std::find(vec.begin(), vec.end(), value) != vec.end();
}

// Numeric cast
DECLARE_EXCEPTION(OverflowException);

Expand Down
3 changes: 2 additions & 1 deletion Source/Common/ProtoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ void ProtoManager::ParseProtos(const FileSystem& resources)
}

// Texts
const string default_lang = !_engine->Settings.Languages.empty() ? _engine->Settings.Languages.front() : "";
RUNTIME_ASSERT(!_engine->Settings.BakeLanguages.empty());
const string& default_lang = _engine->Settings.BakeLanguages.front();

_parsedTexts[type_name][pid] = {};

Expand Down
5 changes: 3 additions & 2 deletions Source/Common/Settings-Include.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ SETTING_GROUP_END();
SETTING_GROUP(CommonGameplaySettings, virtual DummySettings);
FIXED_SETTING(uint, MinNameLength, 4);
FIXED_SETTING(uint, MaxNameLength, 12);
FIXED_SETTING(vector<string>, Languages, "engl");
FIXED_SETTING(uint, TalkDistance, 3);
FIXED_SETTING(uint, LookChecks, 0);
FIXED_SETTING(vector<uint>, LookDir, 0, 20, 40, 60, 60);
Expand Down Expand Up @@ -212,7 +211,9 @@ FIXED_SETTING(bool, SingleThreadBaking, false);
FIXED_SETTING(string, BakeOutput);
FIXED_SETTING(vector<string>, BakeResourceEntries);
FIXED_SETTING(vector<string>, BakeContentEntries);
FIXED_SETTING(vector<string>, BakeExtraFileExtensions, "fopts", "fofnt", "bmfc", "fnt", "acm", "ogg", "wav", "ogv", "json", "ini", "lfspine"); // Todo: move resource files control (include/exclude/pack rules) to cmake
FIXED_SETTING(vector<string>, BakeBaseFileExtensions, "fopts", "fofnt", "bmfc", "fnt", "acm", "ogg", "wav", "ogv", "json", "ini");
FIXED_SETTING(vector<string>, BakeExtraFileExtensions);
FIXED_SETTING(vector<string>, BakeLanguages);
SETTING_GROUP_END();

///@ ExportSettings Server
Expand Down
50 changes: 50 additions & 0 deletions Source/Common/TextPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ void TextPack::Merge(const TextPack& other)
}
}

void TextPack::FixStr(const TextPack& base_pack)
{
STACK_TRACE_ENTRY();

// Add keys that are in the base pack but not in this pack
for (auto&& [key, value] : base_pack._strData) {
if (_strData.count(key) == 0) {
AddStr(key, value);
}
}

// Remove keys that are not in the base pack
for (auto it = _strData.begin(); it != _strData.end();) {
if (base_pack._strData.count(it->first) == 0) {
it = _strData.erase(it);
}
else {
++it;
}
}
}

auto TextPack::GetSize() const noexcept -> size_t
{
STACK_TRACE_ENTRY();
Expand Down Expand Up @@ -437,3 +459,31 @@ void LanguagePack::LoadTexts(FileSystem& resources)
throw LanguagePackException("Unable to load game texts from file", _langName);
}
}

void LanguagePack::FixTexts(const LanguagePack& base_lang)
{
STACK_TRACE_ENTRY();

// Normalize texts to the base language
_textPacks.resize(base_lang._textPacks.size());

for (size_t i = 0; i < base_lang._textPacks.size(); i++) {
auto&& base_text_pack = base_lang._textPacks[i];
auto&& text_pack = _textPacks[i];

if (base_text_pack) {
if (!text_pack) {
text_pack = SafeAlloc::MakeUnique<TextPack>();
text_pack->Merge(*base_text_pack);
}
else {
text_pack->FixStr(*base_text_pack);
}
}
else {
if (text_pack) {
text_pack.reset();
}
}
}
}
2 changes: 2 additions & 0 deletions Source/Common/TextPack.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class TextPack final
void AddStr(TextPackKey num, string&& str);
void EraseStr(TextPackKey num);
void Merge(const TextPack& other);
void FixStr(const TextPack& base_pack);
void Clear();

private:
Expand Down Expand Up @@ -106,6 +107,7 @@ class LanguagePack final
void ParseTexts(FileSystem& resources, HashResolver& hash_resolver);
void SaveTextsToDisk(string_view dir) const;
void LoadTexts(FileSystem& resources);
void FixTexts(const LanguagePack& base_lang);

private:
string _langName {};
Expand Down
6 changes: 6 additions & 0 deletions Source/Scripting/ServerGlobalScriptMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,3 +1130,9 @@ FO_SCRIPT_API vector<StaticItem*> Server_Game_GetStaticItemsForProtoMap(FOServer

return static_map->StaticItems;
}

///@ ExportMethod
FO_SCRIPT_API bool Server_Game_IsTextPresent(FOServer* server, TextPackName textPack, uint strNum)
{
return server->GetLangPack().GetTextPack(textPack).GetStrCount(strNum) != 0;
}
5 changes: 5 additions & 0 deletions Source/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ FOServer::FOServer(GlobalSettings& settings) :
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("Maps"));
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("ServerProtos"));
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("Dialogs"));
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("Texts"));

if constexpr (FO_ANGELSCRIPT_SCRIPTING) {
Resources.AddDataSource(strex(Settings.ResourcesDir).combinePath("ServerAngelScript"));
}
Expand Down Expand Up @@ -327,6 +329,9 @@ FOServer::FOServer(GlobalSettings& settings) :

ProtoMngr.LoadFromResources();

_defaultLang = LanguagePack {Settings.Language, *this};
_defaultLang.LoadTexts(Resources);

return std::nullopt;
});

Expand Down
2 changes: 2 additions & 0 deletions Source/Server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class FOServer : SINGLEPLAYER_VIRTUAL public FOEngineBase
[[nodiscard]] auto GetHealthInfo() const -> string;
[[nodiscard]] auto GetIngamePlayersStatistics() -> string;
[[nodiscard]] auto MakePlayerId(string_view player_name) const -> ident_t;
[[nodiscard]] auto GetLangPack() const -> const LanguagePack& { return _defaultLang; }

auto Lock(optional<time_duration> max_wait_time) -> bool;
void Unlock();
Expand Down Expand Up @@ -310,6 +311,7 @@ class FOServer : SINGLEPLAYER_VIRTUAL public FOEngineBase
vector<TextListener> _textListeners {};
vector<Player*> _logClients {};
vector<string> _logLines {};
LanguagePack _defaultLang {};
vector<NetServerBase*> _connectionServers {}; // Todo: run network listeners dynamically, without restriction, based on server settings
vector<ClientConnection*> _newConnections {};
mutable std::mutex _newConnectionsLocker {};
Expand Down
Loading

0 comments on commit 16b99d5

Please sign in to comment.