Skip to content

Commit

Permalink
Added Language select.
Browse files Browse the repository at this point in the history
Added Language select. More map validations...
  • Loading branch information
UnrealKaraulov committed Dec 10, 2023
1 parent 3702e10 commit 3f498c2
Show file tree
Hide file tree
Showing 14 changed files with 1,315 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
build/bspguy
cfg/bspguy.cfg
cfg/language.ini
cfg/language_ru.ini
window_build:

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ vs-project/fmt/fmt.dir/Release/fmt.vcxproj.FileListAbsolute.txt
vs-project/Release/newbspguy.zip
vs-project/Release/language.ini
vs-project/log.txt
vs-project/Release/language_ru.ini
1 change: 1 addition & 0 deletions cfg/language.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ LANG_1177 = Clipnodes
LANG_1178 = ent_context
LANG_1179 = (WIP)
LANG_1180 = No entity selected
LANG_ERROR_TEXLEN = Warning: {} texture can not be readed.\n
LANG_FGD_BAD_OFFSET = ERROR: Expected 3 components in offset() property (line {}) in FGD {}\n
LANG_DUMP_TEX = Dump Textures
LANG_DUMP_TEX_DESC = Dump all loaded textures to .png files.
1,183 changes: 1,183 additions & 0 deletions cfg/language_ru.ini

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4251,11 +4251,24 @@ bool Bsp::validate()
if (tex->nOffsets[0] > 0 && dataOffset + texOffset + texlen > bsp_header.lump[LUMP_TEXTURES].nLength)
{
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0135),i,dataOffset + texOffset + texlen,bsp_header.lump[LUMP_TEXTURES].nLength);
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0136),i,tex->szName[0] != '\0' ? tex->szName : "UNKNOWN_NAME",texOffset,dataOffset);
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0136), texlen, tex->szName[0] != '\0' ? tex->szName : "UNKNOWN_NAME",texOffset,dataOffset);
}
if (texlen == 0)
{
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string("LANG_ERROR_TEXLEN"), i);
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0136), texlen, tex->szName[0] != '\0' ? tex->szName : "UNKNOWN_NAME", texOffset, dataOffset);
}
}
}

unsigned int newVisRowSize = ((leafCount + 63) & ~63) >> 3;
int decompressedVisSize = leafCount * newVisRowSize;
unsigned char* decompressedVis = new unsigned char[decompressedVisSize];
memset(decompressedVis, 0xFF, decompressedVisSize);
decompress_vis_lump(leaves, visdata, decompressedVis,
models[0].nVisLeafs, leafCount, leafCount, decompressedVisSize, bsp_header.lump[LUMP_VISIBILITY].nLength);
delete decompressedVis;

return isValid;
}

Expand Down Expand Up @@ -7427,22 +7440,22 @@ int Bsp::getBspTextureSize(int textureid)

int iStartOffset = ((int*)textures)[textureid + 1];

if (iStartOffset < 0)
if (iStartOffset < 0 || iStartOffset + sizeof(BSPMIPTEX) > textureDataLength)
return 0;

BSPMIPTEX* tex = ((BSPMIPTEX*)(textures + iStartOffset));

if (tex->nOffsets[0] > textureDataLength)
return 0;

int sz = sizeof(BSPMIPTEX);
if (tex->nOffsets[0] > 0)
{
sz += sizeof(short); /* pal size */

if (is_texture_with_pal(textureid))
{
sz += sizeof(COLOR3) * 256; // pallette
}

for (int i = 0; i < MIPLEVELS; i++)
{
sz += (tex->nWidth >> i) * (tex->nHeight >> i);
Expand All @@ -7459,7 +7472,6 @@ bool Bsp::is_texture_with_pal(int textureid)
if (bsp_header.nVersion == 30)
return true;


int iStartOffset = ((int*)textures)[textureid + 1];
unsigned char* pStartOffset = (unsigned char*)textures + iStartOffset;
unsigned char* pEndOffset = (unsigned char*)textures + textureDataLength;
Expand Down Expand Up @@ -7490,8 +7502,7 @@ bool Bsp::is_texture_with_pal(int textureid)
int lastMipSize = (tex->nWidth / 8) * (tex->nHeight / 8);
unsigned char* palOffset = pStartOffset + tex->nOffsets[3] + lastMipSize;

//print_log("{}-{}={}\n", (void*)(pStartOffset + palOffset), (void*)(pEndOffset), (int)((pStartOffset + palOffset) - pEndOffset));
if (abs(palOffset - pEndOffset) >= sizeof(COLOR3) * 256) // No align check
if (abs(palOffset - pEndOffset) >= sizeof(COLOR3) * 256)
{
return true;
}
Expand Down
17 changes: 16 additions & 1 deletion src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5288,6 +5288,7 @@ void Gui::drawSettings()

bool oldShowSettings = showSettingsWidget;
bool apply_settings_pressed = false;
static std::string langForSelect = g_settings.selected_lang;

if (ImGui::Begin(get_localized_string(LANG_1114).c_str(), &showSettingsWidget))
{
Expand All @@ -5297,6 +5298,7 @@ void Gui::drawSettings()
static int resSelected = 0;
static int fgdSelected = 0;


static const char* tab_titles[settings_tabs] = {
"General",
"FGDs",
Expand Down Expand Up @@ -5496,7 +5498,18 @@ void Gui::drawSettings()
ImGui::EndTooltip();
}
ImGui::Separator();

if (ImGui::BeginCombo("##lang", langForSelect.c_str()))
{
for (const auto& s : g_settings.languages)
{
if (ImGui::Selectable(s.c_str(), s == langForSelect))
{
langForSelect = s;
}
}
ImGui::EndCombo();
}
ImGui::Separator();
if (ImGui::Button(get_localized_string(LANG_0739).c_str()))
{
g_settings.reset();
Expand Down Expand Up @@ -5992,6 +6005,8 @@ void Gui::drawSettings()

if (oldShowSettings && !showSettingsWidget || apply_settings_pressed)
{
g_settings.selected_lang = langForSelect;
set_localize_lang(g_settings.selected_lang);
g_settings.save();
if (!app->reloading)
{
Expand Down
41 changes: 35 additions & 6 deletions src/editor/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ void AppSettings::loadDefault()
workingdir = "./bspguy_work/";

lastdir = "";
language = "EN";
selected_lang = "EN";
languages.clear();
languages.push_back("EN");

undoLevels = 64;

verboseLogs = false;
Expand Down Expand Up @@ -158,18 +161,44 @@ void AppSettings::reset()
transparentEntities.push_back("func_buyzone");
}

void AppSettings::fillLanguages(const std::string& folderPath)
{
languages.clear();
languages.push_back("EN");
for (const auto& entry : fs::directory_iterator(folderPath))
{
if (!entry.is_directory()) {
std::string filename = entry.path().filename().string();
if (filename.starts_with("language_") && filename.ends_with(".ini"))
{
std::string language = filename.substr(9);
language.erase(language.size() - 4);
language = toUpperCase(language);
if (std::find(languages.begin(), languages.end(), language) == languages.end())
languages.push_back(language);
}
}
}
}

void AppSettings::load()
{
set_localize_lang("EN");

std::ifstream file(g_settings_path);
if (!file.is_open())
{
print_log(get_localized_string(LANG_0926),g_settings_path);
print_log(get_localized_string(LANG_0926), g_settings_path);
reset();
return;
}

fillLanguages(g_config_dir);
if (GetCurrentDir() != g_config_dir)
{
fillLanguages(GetCurrentDir());
}

int lines_readed = 0;
std::string line;
while (getline(file, line))
Expand Down Expand Up @@ -318,8 +347,8 @@ void AppSettings::load()
}
else if (key == "language")
{
g_settings.language = val;
set_localize_lang(g_settings.language);
g_settings.selected_lang = val;
set_localize_lang(g_settings.selected_lang);
}
else if (key == "fgd")
{
Expand Down Expand Up @@ -492,7 +521,7 @@ void AppSettings::load()
if (lines_readed > 0)
g_settings.settingLoaded = true;
else
print_log(get_localized_string(LANG_0928),g_settings_path);
print_log(get_localized_string(LANG_0928), g_settings_path);

if (defaultIsEmpty && fgdPaths.empty())
{
Expand Down Expand Up @@ -613,7 +642,7 @@ void AppSettings::save(std::string path)
file << "gamedir=" << g_settings.gamedir << std::endl;
file << "workingdir=" << g_settings.workingdir << std::endl;
file << "lastdir=" << g_settings.lastdir << std::endl;
file << "language=" << g_settings.language << std::endl;
file << "language=" << g_settings.selected_lang << std::endl;

for (int i = 0; i < fgdPaths.size(); i++)
{
Expand Down
5 changes: 4 additions & 1 deletion src/editor/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ struct AppSettings
std::string workingdir;
std::string lastdir;

std::string language;
std::string selected_lang;

std::vector<std::string> languages;

bool settingLoaded; // Settings loaded
bool verboseLogs;
Expand Down Expand Up @@ -109,6 +111,7 @@ struct AppSettings
void reset();
void save();
void save(std::string path);
void fillLanguages(const std::string& folderPath);
};

extern AppSettings g_settings;
6 changes: 4 additions & 2 deletions src/qtools/vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ extern bool g_debug_shift;

bool CHECKBITFROMBYTES(unsigned char* bytes, int bitid);

#define CHECKVISBIT( vis, b ) ((b) >= 0 ? ((vis)[(b) >> 3] & (1 << ((b) & 7))) != 0 : false )/*
#define CHECKVISBIT( vis, b ) ((b) >= 0 ? ((vis)[(b) >> 3] & (1 << ((b) & 7))) != 0 : false )
/*
#define SETVISBIT( vis, b )( void ) ((b) >= 0 ? (unsigned char)((vis)[(b) >> 3] |= (1 << ((b) & 7))) : (unsigned char)false )
#define CLEARVISBIT( vis, b )( void ) ((b) >= 0 ? (unsigned char)((vis)[(b) >> 3] &= ~(1 << ((b) & 7))) : (unsigned char)false )*/
#define CLEARVISBIT( vis, b )( void ) ((b) >= 0 ? (unsigned char)((vis)[(b) >> 3] &= ~(1 << ((b) & 7))) : (unsigned char)false )
*/
41 changes: 31 additions & 10 deletions src/util/lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
;
*/

inih::INIReader * ft = NULL;
inih::INIReader* ft = NULL;

std::map<int, std::string> lang_db;
std::map<std::string, std::string> lang_db_str;
Expand All @@ -30,7 +30,7 @@ std::string get_localized_string(int id)

if (itr == lang_db.end())
{
std::string value = ft->Get<std::string>(g_settings.language, fmt::format("LANG_{:04}", id), fmt::format("NO LANG_{:04}", id));
std::string value = ft->Get<std::string>(g_settings.selected_lang, fmt::format("LANG_{:04}", id), fmt::format("NO LANG_{:04}", id));
replaceAll(value, "\\n", "\n");
lang_db[id] = value;
return value;
Expand All @@ -41,7 +41,7 @@ std::string get_localized_string(int id)
return "LANG_ERROR_INDEX\n";
}

std::string get_localized_string(const std::string & str_id)
std::string get_localized_string(const std::string& str_id)
{
if (ft == NULL)
{
Expand All @@ -54,7 +54,7 @@ std::string get_localized_string(const std::string & str_id)

if (itr == lang_db_str.end())
{
std::string value = ft->Get<std::string>(g_settings.language, str_id, fmt::format("NO {}", str_id));
std::string value = ft->Get<std::string>(g_settings.selected_lang, str_id, fmt::format("NO {}", str_id));
replaceAll(value, "\\n", "\n");
lang_db_str[str_id] = value;
return value;
Expand All @@ -74,19 +74,40 @@ void set_localize_lang(std::string lang)
if (ft != NULL)
{
delete ft;
ft = NULL;
}

try
// Search lang file in config/current directories
std::string langfile = g_config_dir + "language_" + toLowerCase(lang) + ".ini";
if (!fileExists(langfile))
{
langfile = g_current_dir + "language_" + toLowerCase(lang) + ".ini";
}
if (!fileExists(langfile))
{
langfile = g_config_dir + "language.ini";
}
if (!fileExists(langfile))
{
langfile = g_current_dir + "language.ini";
}
if (!fileExists(langfile))
{
ft = new inih::INIReader(g_config_dir + "language.ini");
print_log(PRINT_RED | PRINT_INTENSITY, "Fatal error");
}
catch (std::runtime_error runtime)
else
{
print_log("Language parse from {} fatal error: {}\n", g_config_dir + "language.ini", runtime.what());
try
{
ft = new inih::INIReader(langfile);
}
catch (std::runtime_error runtime)
{
print_log(PRINT_RED | PRINT_INTENSITY, "Language parse from {} fatal error: {}\n", g_config_dir + "language.ini", runtime.what());
}
}
last_lang = lang;
}
g_settings.language = lang;
g_settings.selected_lang = lang;
lang_db.clear();
lang_db_str.clear();
}
8 changes: 8 additions & 0 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ std::string toLowerCase(const std::string& s)
);
return ret;
}
std::string toUpperCase(const std::string& s)
{
std::string ret = s;
std::transform(ret.begin(), ret.end(), ret.begin(),
[](unsigned char c) { return (unsigned char)std::toupper(c); }
);
return ret;
}

std::string trimSpaces(const std::string& str)
{
Expand Down
1 change: 1 addition & 0 deletions src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ bool createDir(const std::string& dirName);
void removeDir(const std::string& dirName);

std::string toLowerCase(const std::string& s);
std::string toUpperCase(const std::string& s);

std::string trimSpaces(const std::string& str);

Expand Down
9 changes: 9 additions & 0 deletions vs-project/bspguy.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
<ProjectReference>
<LinkLibraryDependencies>false</LinkLibraryDependencies>
</ProjectReference>
<PostBuildEvent>
<Command>copy "$(SolutionDir)\..\cfg\language_ru.ini" "$(TargetDir)\language_ru.ini"
copy "$(SolutionDir)\..\cfg\language.ini" "$(TargetDir)\language.ini"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugGithub|x64'">
<ClCompile>
Expand Down Expand Up @@ -285,6 +289,10 @@
<LinkLibraryDependencies>
</LinkLibraryDependencies>
</ProjectReference>
<PostBuildEvent>
<Command>copy "$(SolutionDir)\..\cfg\language_ru.ini" "$(TargetDir)\language_ru.ini"
copy "$(SolutionDir)\..\cfg\language.ini" "$(TargetDir)\language.ini"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include=".\..\src\main.cpp" />
Expand Down Expand Up @@ -383,6 +391,7 @@
<None Include="..\cfg\language.ini">
<FileType>Text</FileType>
</None>
<None Include="..\cfg\language_ru.ini" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Loading

0 comments on commit 3f498c2

Please sign in to comment.