Commit 48ecc05a authored by Chen Bill's avatar Chen Bill

fix Game::LoadConfig

max length of filename should be determined by file system
parent 0b1e8a8c
...@@ -119,7 +119,7 @@ bool Game::Initialize() { ...@@ -119,7 +119,7 @@ bool Game::Initialize() {
L"./fonts/numFont.otf" L"./fonts/numFont.otf"
}; };
for(const wchar_t* path : numFontPaths) { for(const wchar_t* path : numFontPaths) {
myswprintf(gameConf.numfont, path); BufferIO::CopyWideString(path, gameConf.numfont);
numFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 16); numFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 16);
if(numFont) if(numFont)
break; break;
...@@ -144,14 +144,14 @@ bool Game::Initialize() { ...@@ -144,14 +144,14 @@ bool Game::Initialize() {
L"./fonts/textFont.otf" L"./fonts/textFont.otf"
}; };
for(const wchar_t* path : textFontPaths) { for(const wchar_t* path : textFontPaths) {
myswprintf(gameConf.textfont, path); BufferIO::CopyWideString(path, gameConf.textfont);
textFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.textfont, gameConf.textfontsize); textFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.textfont, gameConf.textfontsize);
if(textFont) if(textFont)
break; break;
} }
} }
if(!numFont || !textFont) { if(!numFont || !textFont) {
wchar_t fpath[1024]; wchar_t fpath[1024]{};
fpath[0] = 0; fpath[0] = 0;
FileSystem::TraversalDir(L"./fonts", [&fpath](const wchar_t* name, bool isdir) { FileSystem::TraversalDir(L"./fonts", [&fpath](const wchar_t* name, bool isdir) {
if(!isdir && wcsrchr(name, '.') && (!mywcsncasecmp(wcsrchr(name, '.'), L".ttf", 4) || !mywcsncasecmp(wcsrchr(name, '.'), L".ttc", 4) || !mywcsncasecmp(wcsrchr(name, '.'), L".otf", 4))) { if(!isdir && wcsrchr(name, '.') && (!mywcsncasecmp(wcsrchr(name, '.'), L".ttf", 4) || !mywcsncasecmp(wcsrchr(name, '.'), L".ttc", 4) || !mywcsncasecmp(wcsrchr(name, '.'), L".otf", 4))) {
...@@ -163,11 +163,11 @@ bool Game::Initialize() { ...@@ -163,11 +163,11 @@ bool Game::Initialize() {
return false; return false;
} }
if(!numFont) { if(!numFont) {
myswprintf(gameConf.numfont, fpath); BufferIO::CopyWideString(fpath, gameConf.numfont);
numFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 16); numFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 16);
} }
if(!textFont) { if(!textFont) {
myswprintf(gameConf.textfont, fpath); BufferIO::CopyWideString(fpath, gameConf.textfont);
textFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.textfont, gameConf.textfontsize); textFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.textfont, gameConf.textfontsize);
} }
} }
...@@ -1317,12 +1317,11 @@ void Game::LoadConfig() { ...@@ -1317,12 +1317,11 @@ void Game::LoadConfig() {
FILE* fp = fopen("system.conf", "r"); FILE* fp = fopen("system.conf", "r");
if(!fp) if(!fp)
return; return;
char linebuf[256]{}; char linebuf[CONFIG_LINE_SIZE]{};
char strbuf[64]{}; char strbuf[64]{};
char valbuf[256]{}; char valbuf[960]{};
wchar_t wstr[256]{}; while(fgets(linebuf, sizeof linebuf, fp)) {
while(fgets(linebuf, 256, fp)) { if (sscanf(linebuf, "%63s = %959s", strbuf, valbuf) != 2)
if (sscanf(linebuf, "%63s = %255s", strbuf, valbuf) != 2)
continue; continue;
if(!std::strcmp(strbuf, "antialias")) { if(!std::strcmp(strbuf, "antialias")) {
gameConf.antialias = strtol(valbuf, nullptr, 10); gameConf.antialias = strtol(valbuf, nullptr, 10);
...@@ -1335,25 +1334,18 @@ void Game::LoadConfig() { ...@@ -1335,25 +1334,18 @@ void Game::LoadConfig() {
enable_log = val & 0xff; enable_log = val & 0xff;
} else if(!std::strcmp(strbuf, "textfont")) { } else if(!std::strcmp(strbuf, "textfont")) {
int textfontsize = 0; int textfontsize = 0;
if (sscanf(linebuf, "%63s = %255s %d", strbuf, valbuf, &textfontsize) != 3) if (sscanf(linebuf, "%63s = %959s %d", strbuf, valbuf, &textfontsize) != 3)
continue; continue;
gameConf.textfontsize = textfontsize; gameConf.textfontsize = textfontsize;
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.textfont);
BufferIO::CopyWStr(wstr, gameConf.textfont, 256);
} else if(!std::strcmp(strbuf, "numfont")) { } else if(!std::strcmp(strbuf, "numfont")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.numfont);
BufferIO::CopyWStr(wstr, gameConf.numfont, 256);
} else if(!std::strcmp(strbuf, "serverport")) { } else if(!std::strcmp(strbuf, "serverport")) {
gameConf.serverport = strtol(valbuf, nullptr, 10); gameConf.serverport = strtol(valbuf, nullptr, 10);
} else if(!std::strcmp(strbuf, "lasthost")) { } else if(!std::strcmp(strbuf, "lasthost")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.lasthost);
BufferIO::CopyWStr(wstr, gameConf.lasthost, 100);
} else if(!std::strcmp(strbuf, "lastport")) { } else if(!std::strcmp(strbuf, "lastport")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.lastport);
BufferIO::CopyWStr(wstr, gameConf.lastport, 20);
} else if(!std::strcmp(strbuf, "roompass")) {
BufferIO::DecodeUTF8(valbuf, wstr);
BufferIO::CopyWStr(wstr, gameConf.roompass, 20);
} else if(!std::strcmp(strbuf, "automonsterpos")) { } else if(!std::strcmp(strbuf, "automonsterpos")) {
gameConf.chkMAutoPos = strtol(valbuf, nullptr, 10); gameConf.chkMAutoPos = strtol(valbuf, nullptr, 10);
} else if(!std::strcmp(strbuf, "autospellpos")) { } else if(!std::strcmp(strbuf, "autospellpos")) {
...@@ -1440,23 +1432,20 @@ void Game::LoadConfig() { ...@@ -1440,23 +1432,20 @@ void Game::LoadConfig() {
#endif #endif
} else { } else {
// options allowing multiple words // options allowing multiple words
if (sscanf(linebuf, "%63s = %240[^\n]", strbuf, valbuf) != 2) if (sscanf(linebuf, "%63s = %959[^\n]", strbuf, valbuf) != 2)
continue; continue;
if (!std::strcmp(strbuf, "nickname")) { if (!std::strcmp(strbuf, "nickname")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.nickname);
BufferIO::CopyWStr(wstr, gameConf.nickname, 20);
} else if (!std::strcmp(strbuf, "gamename")) { } else if (!std::strcmp(strbuf, "gamename")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.gamename);
BufferIO::CopyWStr(wstr, gameConf.gamename, 20); } else if (!std::strcmp(strbuf, "roompass")) {
BufferIO::DecodeUTF8(valbuf, gameConf.roompass);
} else if (!std::strcmp(strbuf, "lastcategory")) { } else if (!std::strcmp(strbuf, "lastcategory")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.lastcategory);
BufferIO::CopyWStr(wstr, gameConf.lastcategory, 64);
} else if (!std::strcmp(strbuf, "lastdeck")) { } else if (!std::strcmp(strbuf, "lastdeck")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.lastdeck);
BufferIO::CopyWStr(wstr, gameConf.lastdeck, 64);
} else if(!std::strcmp(strbuf, "bot_deck_path")) { } else if(!std::strcmp(strbuf, "bot_deck_path")) {
BufferIO::DecodeUTF8(valbuf, wstr); BufferIO::DecodeUTF8(valbuf, gameConf.bot_deck_path);
BufferIO::CopyWStr(wstr, gameConf.bot_deck_path, 64);
} }
} }
} }
...@@ -1465,12 +1454,12 @@ void Game::LoadConfig() { ...@@ -1465,12 +1454,12 @@ void Game::LoadConfig() {
void Game::SaveConfig() { void Game::SaveConfig() {
FILE* fp = fopen("system.conf", "w"); FILE* fp = fopen("system.conf", "w");
fprintf(fp, "#config file\n#nickname & gamename should be less than 20 characters\n"); fprintf(fp, "#config file\n#nickname & gamename should be less than 20 characters\n");
char linebuf[256]; char linebuf[CONFIG_LINE_SIZE];
fprintf(fp, "use_d3d = %d\n", gameConf.use_d3d ? 1 : 0); fprintf(fp, "use_d3d = %d\n", gameConf.use_d3d ? 1 : 0);
fprintf(fp, "use_image_scale = %d\n", gameConf.use_image_scale ? 1 : 0); fprintf(fp, "use_image_scale = %d\n", gameConf.use_image_scale ? 1 : 0);
fprintf(fp, "antialias = %d\n", gameConf.antialias); fprintf(fp, "antialias = %d\n", gameConf.antialias);
fprintf(fp, "errorlog = %u\n", enable_log); fprintf(fp, "errorlog = %u\n", enable_log);
BufferIO::CopyWStr(ebNickName->getText(), gameConf.nickname, 20); BufferIO::CopyWideString(ebNickName->getText(), gameConf.nickname);
BufferIO::EncodeUTF8(gameConf.nickname, linebuf); BufferIO::EncodeUTF8(gameConf.nickname, linebuf);
fprintf(fp, "nickname = %s\n", linebuf); fprintf(fp, "nickname = %s\n", linebuf);
BufferIO::EncodeUTF8(gameConf.gamename, linebuf); BufferIO::EncodeUTF8(gameConf.gamename, linebuf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment