Commit 581bb20c authored by xiaoye's avatar xiaoye

fix

parent 5df3ab32
...@@ -167,6 +167,71 @@ void DataManager::ReadStringConfLine(const char* linebuf) { ...@@ -167,6 +167,71 @@ void DataManager::ReadStringConfLine(const char* linebuf) {
_setnameStrings[value] = strBuffer; _setnameStrings[value] = strBuffer;
} }
} }
bool DataManager::LoadServerList(const char* file) {
FILE* fp = myfopen(file, "r");
if(!fp)
return false;
char linebuf[TEXT_LINE_SIZE]{};
while(std::fgets(linebuf, sizeof linebuf, fp)) {
ReadServerConfLine(linebuf);
}
std::fclose(fp);
return true;
}
bool DataManager::LoadServerList(const wchar_t* file) {
FILE* fp = mywfopen(file, "r");
if(!fp)
return false;
char linebuf[TEXT_LINE_SIZE]{};
while(std::fgets(linebuf, sizeof linebuf, fp)) {
ReadServerConfLine(linebuf);
}
std::fclose(fp);
return true;
}
bool DataManager::LoadServerList(irr::io::IReadFile* reader) {
char ch{};
std::string linebuf;
while (reader->read(&ch, 1)) {
if (ch == '\0')
break;
linebuf.push_back(ch);
if (ch == '\n' || linebuf.size() >= TEXT_LINE_SIZE - 1) {
ReadServerConfLine(linebuf.data());
linebuf.clear();
}
}
reader->drop();
return true;
}
void DataManager::ReadServerConfLine(const char* linebuf) {
if(strchr(linebuf, '|') == nullptr)
return;
if (_serverStrings.empty())
_serverStrings.emplace_back(L"清空", L"");
char* buffer = const_cast<char*>(linebuf);
buffer[strcspn(buffer, "\n")] = '\0';
char *separator = strchr(buffer, '|');
if (separator != NULL) {
*separator = '\0';
wchar_t wname[256];
wchar_t wip[256];
if (mbstowcs(wname, buffer, 256) != (size_t)-1 && mbstowcs(wip, separator + 1, 256) != (size_t)-1) {
wchar_t* name = new wchar_t[256];
wchar_t* ip = new wchar_t[256];
wcscpy(name, wname);
wcscpy(ip, wip);
auto it = std::find_if(_serverStrings.begin(), _serverStrings.end(),
[name](const auto& pair) { return wcscmp(pair.first, name) == 0; }
);
if (it != _serverStrings.end())
it->second = ip;
else
_serverStrings.emplace_back(name, ip);
}
}
}
bool DataManager::Error(sqlite3* pDB, sqlite3_stmt* pStmt) { bool DataManager::Error(sqlite3* pDB, sqlite3_stmt* pStmt) {
std::snprintf(errmsg, sizeof errmsg, "%s", sqlite3_errmsg(pDB)); std::snprintf(errmsg, sizeof errmsg, "%s", sqlite3_errmsg(pDB));
if(pStmt) if(pStmt)
...@@ -519,5 +584,4 @@ bool DataManager::deck_sort_name(code_pointer p1, code_pointer p2) { ...@@ -519,5 +584,4 @@ bool DataManager::deck_sort_name(code_pointer p1, code_pointer p2) {
return res < 0; return res < 0;
return p1->first < p2->first; return p1->first < p2->first;
} }
} }
...@@ -49,6 +49,10 @@ public: ...@@ -49,6 +49,10 @@ public:
bool LoadStrings(const char* file); bool LoadStrings(const char* file);
bool LoadStrings(irr::io::IReadFile* reader); bool LoadStrings(irr::io::IReadFile* reader);
void ReadStringConfLine(const char* linebuf); void ReadStringConfLine(const char* linebuf);
bool LoadServerList(const char* file);
bool LoadServerList(const wchar_t* file);
bool LoadServerList(irr::io::IReadFile* reader);
void ReadServerConfLine(const char* linebuf);
bool Error(sqlite3* pDB, sqlite3_stmt* pStmt = nullptr); bool Error(sqlite3* pDB, sqlite3_stmt* pStmt = nullptr);
code_pointer GetCodePointer(unsigned int code) const; code_pointer GetCodePointer(unsigned int code) const;
...@@ -79,6 +83,7 @@ public: ...@@ -79,6 +83,7 @@ public:
std::unordered_map<unsigned int, std::wstring> _victoryStrings; std::unordered_map<unsigned int, std::wstring> _victoryStrings;
std::unordered_map<unsigned int, std::wstring> _setnameStrings; std::unordered_map<unsigned int, std::wstring> _setnameStrings;
std::unordered_map<unsigned int, std::wstring> _sysStrings; std::unordered_map<unsigned int, std::wstring> _sysStrings;
std::vector<std::pair<const wchar_t*, const wchar_t*>> _serverStrings;
char errmsg[512]{}; char errmsg[512]{};
static unsigned char scriptBuffer[0x100000]; static unsigned char scriptBuffer[0x100000];
......
...@@ -124,6 +124,7 @@ bool Game::Initialize() { ...@@ -124,6 +124,7 @@ bool Game::Initialize() {
ErrorLog("Failed to load strings!"); ErrorLog("Failed to load strings!");
return false; return false;
} }
dataManager.LoadServerList(GetLocaleDir("server.conf"));
dataManager.LoadDB(L"specials/special.cdb"); dataManager.LoadDB(L"specials/special.cdb");
env = device->getGUIEnvironment(); env = device->getGUIEnvironment();
numFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 16); numFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 16);
...@@ -367,7 +368,7 @@ bool Game::Initialize() { ...@@ -367,7 +368,7 @@ bool Game::Initialize() {
wServerList->setDraggable(true); wServerList->setDraggable(true);
lstServerList = env->addListBox(irr::core::rect<irr::s32>(10, 20, 290, 270), wServerList, LISTBOX_SERVER_LIST, true); lstServerList = env->addListBox(irr::core::rect<irr::s32>(10, 20, 290, 270), wServerList, LISTBOX_SERVER_LIST, true);
lstServerList->setItemHeight(18); lstServerList->setItemHeight(18);
AddServerList(lstServerList); RefreshServerList();
btnServerSelected = env->addButton(irr::core::rect<irr::s32>(30, 280, 130, 310), wServerList, BUTTON_SERVER_SELECTED, dataManager.GetSysString(1211)); btnServerSelected = env->addButton(irr::core::rect<irr::s32>(30, 280, 130, 310), wServerList, BUTTON_SERVER_SELECTED, dataManager.GetSysString(1211));
btnServerCancel = env->addButton(irr::core::rect<irr::s32>(170, 280, 270, 310), wServerList, BUTTON_SERVER_CANCEL, dataManager.GetSysString(1212)); btnServerCancel = env->addButton(irr::core::rect<irr::s32>(170, 280, 270, 310), wServerList, BUTTON_SERVER_CANCEL, dataManager.GetSysString(1212));
//img //img
...@@ -1243,6 +1244,7 @@ std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cW ...@@ -1243,6 +1244,7 @@ std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cW
} }
void Game::LoadExpansions(const wchar_t* expansions_path) { void Game::LoadExpansions(const wchar_t* expansions_path) {
bool lflist_changed = false; bool lflist_changed = false;
bool server_list_changed = false;
FileSystem::TraversalDir(expansions_path, [&](const wchar_t* name, bool isdir) { FileSystem::TraversalDir(expansions_path, [&](const wchar_t* name, bool isdir) {
if (isdir) if (isdir)
return; return;
...@@ -1256,6 +1258,11 @@ void Game::LoadExpansions(const wchar_t* expansions_path) { ...@@ -1256,6 +1258,11 @@ void Game::LoadExpansions(const wchar_t* expansions_path) {
if(!std::wcscmp(name, L"lflist.conf")) { if(!std::wcscmp(name, L"lflist.conf")) {
deckManager.LoadLFListSingle(fpath, true); deckManager.LoadLFListSingle(fpath, true);
lflist_changed = true; lflist_changed = true;
} else if(!std::wcscmp(name, L"server.conf")) {
char upath[1024];
BufferIO::EncodeUTF8(fpath, upath);
dataManager.LoadServerList(upath);
server_list_changed = true;
} else { } else {
char upath[1024]; char upath[1024];
BufferIO::EncodeUTF8(fpath, upath); BufferIO::EncodeUTF8(fpath, upath);
...@@ -1301,6 +1308,9 @@ void Game::LoadExpansions(const wchar_t* expansions_path) { ...@@ -1301,6 +1308,9 @@ void Game::LoadExpansions(const wchar_t* expansions_path) {
if(!std::wcscmp(fname, L"lflist.conf")) { if(!std::wcscmp(fname, L"lflist.conf")) {
deckManager.LoadLFListSingle(reader, true); deckManager.LoadLFListSingle(reader, true);
lflist_changed = true; lflist_changed = true;
}} else if(!std::wcscmp(fname, L"server.conf")) {
dataManager.LoadServerList(reader);
server_list_changed = true;
} else { } else {
dataManager.LoadStrings(reader); dataManager.LoadStrings(reader);
} }
...@@ -1314,6 +1324,8 @@ void Game::LoadExpansions(const wchar_t* expansions_path) { ...@@ -1314,6 +1324,8 @@ void Game::LoadExpansions(const wchar_t* expansions_path) {
} }
if(lflist_changed) if(lflist_changed)
RefreshLFList(); RefreshLFList();
if(server_list_changed)
RefreshServerList();
} }
void Game::LoadExpansionsAll() { void Game::LoadExpansionsAll() {
auto list = GetExpansionsList(); auto list = GetExpansionsList();
...@@ -1509,6 +1521,13 @@ void Game::RefreshBot() { ...@@ -1509,6 +1521,13 @@ void Game::RefreshBot() {
RefreshCategoryDeck(cbBotDeckCategory, cbBotDeck); RefreshCategoryDeck(cbBotDeckCategory, cbBotDeck);
} }
} }
void Game::RefreshServerList() {
lstServerList->clear();
for (const auto& pair : dataManager._serverStrings) {
const wchar_t* key = pair.first;
lstServerList->addItem(key);
}
}
bool Game::LoadConfigFromFile(const char* file) { bool Game::LoadConfigFromFile(const char* file) {
FILE* fp = myfopen(file, "r"); FILE* fp = myfopen(file, "r");
if(!fp){ if(!fp){
...@@ -2656,33 +2675,5 @@ void Game::InjectEnvToRegistry(intptr_t pduel) { ...@@ -2656,33 +2675,5 @@ void Game::InjectEnvToRegistry(intptr_t pduel) {
} }
#endif #endif
} }
void Game::AddServerList(irr::gui::IGUIListBox* i) {
i->addItem(L"清空");
serverIP.push_back(L"");
FILE* fp = myfopen("server.conf", "r");
if(!fp){
return ;
}
char buffer[256];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
buffer[strcspn(buffer, "\n")] = '\0';
char *separator = strchr(buffer, '|');
if (separator != NULL) {
*separator = '\0';
wchar_t wname[256];
wchar_t wip[256];
if (mbstowcs(wname, buffer, 256) != (size_t)-1 && mbstowcs(wip, separator + 1, 256) != (size_t)-1) {
i->addItem(wname);
wchar_t* ip = new wchar_t[256];
wcscpy(ip, wip);
serverIP.push_back(ip);
}
}
}
fclose(fp);
}
} }
...@@ -199,6 +199,7 @@ public: ...@@ -199,6 +199,7 @@ public:
void RefreshBot(); void RefreshBot();
void RefreshLocales(); void RefreshLocales();
void RefreshLFList(); void RefreshLFList();
void RefreshServerList();
void DrawSelectionLine(irr::video::S3DVertex* vec, bool strip, int width, float* cv); void DrawSelectionLine(irr::video::S3DVertex* vec, bool strip, int width, float* cv);
void DrawSelectionLine(irr::gui::IGUIElement* element, int width, irr::video::SColor color); void DrawSelectionLine(irr::gui::IGUIElement* element, int width, irr::video::SColor color);
void DrawBackGround(); void DrawBackGround();
...@@ -274,7 +275,6 @@ public: ...@@ -274,7 +275,6 @@ public:
void takeScreenshot(); void takeScreenshot();
void SetCursor(irr::gui::ECURSOR_ICON icon); void SetCursor(irr::gui::ECURSOR_ICON icon);
void InjectEnvToRegistry(intptr_t pduel); void InjectEnvToRegistry(intptr_t pduel);
void AddServerList(irr::gui::IGUIListBox* i);
template<typename T> template<typename T>
static void DrawShadowText(irr::gui::CGUITTFont* font, const T& text, const irr::core::rect<irr::s32>& position, const irr::core::rect<irr::s32>& padding, static void DrawShadowText(irr::gui::CGUITTFont* font, const T& text, const irr::core::rect<irr::s32>& position, const irr::core::rect<irr::s32>& padding,
irr::video::SColor color = 0xffffffff, irr::video::SColor shadowcolor = 0xff000000, bool hcenter = false, bool vcenter = false, const irr::core::rect<irr::s32>* clip = nullptr); irr::video::SColor color = 0xffffffff, irr::video::SColor shadowcolor = 0xff000000, bool hcenter = false, bool vcenter = false, const irr::core::rect<irr::s32>* clip = nullptr);
...@@ -699,7 +699,6 @@ public: ...@@ -699,7 +699,6 @@ public:
irr::gui::IGUIListBox* lstServerList; irr::gui::IGUIListBox* lstServerList;
irr::gui::IGUIButton* btnServerSelected; irr::gui::IGUIButton* btnServerSelected;
irr::gui::IGUIButton* btnServerCancel; irr::gui::IGUIButton* btnServerCancel;
std::vector<const wchar_t*> serverIP;
}; };
extern Game* mainGame; extern Game* mainGame;
......
...@@ -492,12 +492,19 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -492,12 +492,19 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
} }
case BUTTON_SERVER_SELECTED: { case BUTTON_SERVER_SELECTED: {
int sel = mainGame->lstServerList->getSelected(); int sel = mainGame->lstServerList->getSelected();
if (selectedIndex != -1) { if (sel == -1)
wcscpy(mainGame->gameConf.lasthost, mainGame->serverIP[sel]); wcscpy(mainGame->gameConf.lasthost, L"");
wchar_t buf[256]; else {
myswprintf(buf, L"%s", mainGame->gameConf.lasthost); const wchar_t* key = mainGame->lstServerList->getListItem(sel);
mainGame->ebJoinHost->setText(buf); auto it = std::find_if(dataManager._serverStrings.begin(),
dataManager._serverStrings.end(),
[key](const auto& pair) { return wcscmp(pair.first, key) == 0; }
);
wcscpy(mainGame->gameConf.lasthost, it == dataManager._serverStrings.end() ? L"" : it->second);
} }
wchar_t buf[256];
myswprintf(buf, L"%s", mainGame->gameConf.lasthost);
mainGame->ebJoinHost->setText(buf);
mainGame->HideElement(mainGame->wServerList); mainGame->HideElement(mainGame->wServerList);
break; break;
} }
......
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