From acf4f0b0eee155f453245bf96a28cc217c73e0aa Mon Sep 17 00:00:00 2001 From: darkrose Date: Thu, 17 Dec 2015 16:48:32 +1000 Subject: [PATCH] new multiplayer menu, pt3 --- .gitignore | 2 + src/guiMultiplayerMenu.cpp | 213 ++++++++++++++++++++++++------------- src/guiMultiplayerMenu.h | 5 +- src/path.cpp | 6 +- 4 files changed, 149 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index cc33a2f..fa19cda 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ src/jthread/jthreadconfig.h cmake_config.h data/textureshd +data/servers.txt +data/favourites.txt diff --git a/src/guiMultiplayerMenu.cpp b/src/guiMultiplayerMenu.cpp index b22d796..d3171ea 100644 --- a/src/guiMultiplayerMenu.cpp +++ b/src/guiMultiplayerMenu.cpp @@ -34,6 +34,7 @@ #include #include "settings.h" #include "gui_colours.h" +#include "http.h" GUIMultiplayerMenu::GUIMultiplayerMenu( gui::IGUIEnvironment* env, @@ -50,79 +51,7 @@ GUIMultiplayerMenu::GUIMultiplayerMenu( { m_data.mmdata = data; - { - ServerInfo i; - i.name = L"Voxelands Survival Server"; - i.addr = L"servers.voxelands.com:30000"; - i.mode = L"survival"; - i.is_favourite = false; - - m_data.servers.push_back(i); - } - { - ServerInfo i; - i.name = L"Voxelands Adventure Server"; - i.addr = L"servers.voxelands.com:30001"; - i.mode = L"adventure"; - i.is_favourite = true; - - m_data.servers.push_back(i); - m_data.favourites.push_back(i); - } - { - ServerInfo i; - i.name = L"Voxelands Creative Server"; - i.addr = L"servers.voxelands.com:30002"; - i.mode = L"creative"; - i.is_favourite = false; - - m_data.servers.push_back(i); - } - { - ServerInfo i; - i.name = L"old public"; - i.addr = L"106.187.103.195:30003"; - i.mode = L"adventure"; - i.is_favourite = false; - - m_data.servers.push_back(i); - } - { - ServerInfo i; - i.name = L"UK Voxelands Survival Server"; - i.addr = L"uk.servers.voxelands.com:30000"; - i.mode = L"survival"; - i.is_favourite = false; - - m_data.servers.push_back(i); - } - { - ServerInfo i; - i.name = L"UK Voxelands Adventure Server"; - i.addr = L"uk.servers.voxelands.com:30001"; - i.mode = L"adventure"; - i.is_favourite = false; - - m_data.servers.push_back(i); - } - { - ServerInfo i; - i.name = L"UK Voxelands Creative Server"; - i.addr = L"uk.servers.voxelands.com:30002"; - i.mode = L"creative"; - i.is_favourite = false; - - m_data.servers.push_back(i); - } - { - ServerInfo i; - i.name = L"DE Holarse Linux-Gaming Voxelands-Server"; - i.addr = L"holarse-linuxgaming.de:30000"; - i.mode = L"survival"; - i.is_favourite = false; - - m_data.servers.push_back(i); - } + loadServers(); } GUIMultiplayerMenu::~GUIMultiplayerMenu() @@ -540,6 +469,7 @@ bool GUIMultiplayerMenu::OnEvent(const SEvent& event) break; } } + saveFavourites(); } }else if (m_data.selected_tab == TAB_MP_FAVOURITES) { ServerInfo &info = m_data.favourites.at(m_data.selected_row); @@ -559,6 +489,7 @@ bool GUIMultiplayerMenu::OnEvent(const SEvent& event) break; } } + saveFavourites(); m_data.selected_row = -1; } } @@ -572,10 +503,27 @@ bool GUIMultiplayerMenu::OnEvent(const SEvent& event) if (info.name != L"" && !info.is_favourite) { info.is_favourite = true; m_data.favourites.push_back(info); + saveFavourites(); } } regenerateGui(m_screensize); return true; + case GUI_ID_REFRESH_BUTTON: + acceptInput(); + fetchServers(); + if ( + ( + m_data.selected_tab == TAB_MP_LIST + && m_data.selected_row >= (int)m_data.servers.size() + ) || ( + m_data.selected_tab == TAB_MP_FAVOURITES + && m_data.selected_row >= (int)m_data.favourites.size() + ) + ) { + m_data.selected_row = -1; + } + regenerateGui(m_screensize); + return true; case GUI_ID_TAB_MP_LIST: acceptInput(); m_accepted = false; @@ -632,3 +580,122 @@ bool GUIMultiplayerMenu::OnEvent(const SEvent& event) return Parent ? Parent->OnEvent(event) : false; } +bool GUIMultiplayerMenu::fetchServers() +{ + char* u = const_cast("/list"); + std::string data = http_request(NULL,u); + std::string path = getPath("","servers.txt",false); + + std::ofstream f; + f.open(path.c_str()); + if (!f.is_open()) + return false; + + f << data; + f.close(); + + return loadServers(); +} +bool GUIMultiplayerMenu::loadServers() +{ + std::string path = getPath("","servers.txt",true); + if (path != "") + parseFile(path,m_data.servers); + + path = getPath("","favourites.txt",true); + if (path != "") + parseFile(path,m_data.favourites); + + if (m_data.favourites.size()) { + for ( + std::vector::iterator k = m_data.favourites.begin(); + k != m_data.favourites.end(); + k++ + ) { + bool add = true; + k->is_favourite = true; + for ( + std::vector::iterator i = m_data.servers.begin(); + i != m_data.servers.end(); + i++ + ) { + if ( + i->name == k->name + && i->addr == k->addr + && i->mode == k->mode + ) { + i->is_favourite = true; + add = false; + break; + } + } + if (add) + m_data.servers.push_back(*k); + } + } + + return true; +} +bool GUIMultiplayerMenu::saveFavourites() +{ + std::string path = getPath("","favourites.txt",false); + std::ofstream f; + f.open(path.c_str()); + if (!f.is_open()) + return false; + + f << "servers: "; + f << m_data.favourites.size(); + f << "\n\n"; + + for ( + std::vector::iterator k = m_data.favourites.begin(); + k != m_data.favourites.end(); + k++ + ) { + f << wide_to_narrow(k->name) << '\n'; + f << wide_to_narrow(k->mode) << '\n'; + f << wide_to_narrow(k->addr) << '\n'; + f << '\n'; + } + + f.close(); + return false; +} +bool GUIMultiplayerMenu::parseFile(std::string path, std::vector &list) +{ + list.clear(); + std::ifstream f(path.c_str()); + if (!f.is_open()) + return false; + + std::string line; + std::getline(f,line); + if (line.substr(0,9) != "servers: ") { + f.close(); + return false; + } + std::getline(f,line); + + while (true) { + ServerInfo i; + i.is_favourite = false; + std::getline(f,line); + if (line == "") + break; + i.name = narrow_to_wide(line); + std::getline(f,line); + if (line == "") + break; + i.mode = narrow_to_wide(line); + std::getline(f,line); + if (line == "") + break; + i.addr = narrow_to_wide(line); + std::getline(f,line); + list.push_back(i); + } + + f.close(); + return true; +} diff --git a/src/guiMultiplayerMenu.h b/src/guiMultiplayerMenu.h index 9b366b0..9f64cf5 100644 --- a/src/guiMultiplayerMenu.h +++ b/src/guiMultiplayerMenu.h @@ -119,7 +119,10 @@ private: IGameCallback *m_gamecallback; v2u32 m_screensize; - bool resetMenu(); + bool fetchServers(); + bool loadServers(); + bool saveFavourites(); + bool parseFile(std::string data, std::vector &list); }; #endif diff --git a/src/path.cpp b/src/path.cpp index 3e9dc22..4b21e73 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -154,7 +154,7 @@ std::string getPath(const char* tp, const std::string &filename, bool must_exist std::string data_path = g_settings->get("data_path"); if (data_path != "") { std::string testpath = data_path + DIR_DELIM + rel_path; - if (type == "model" || type == "html" || type == "sound" || type == "skin" || type == "font" || type == "translation") { + if (type != "texture") { if (fs::PathExists(testpath)) fullpath = std::string(testpath); }else{ @@ -166,7 +166,7 @@ std::string getPath(const char* tp, const std::string &filename, bool must_exist /* check from user data directory */ if (fullpath == "") { std::string testpath = porting::path_userdata + DIR_DELIM + rel_path; - if (type == "model" || type == "html" || type == "sound" || type == "skin" || type == "font" || type == "translation") { + if (type != "texture") { if (fs::PathExists(testpath)) fullpath = std::string(testpath); }else{ @@ -177,7 +177,7 @@ std::string getPath(const char* tp, const std::string &filename, bool must_exist /* check from default data directory */ if (fullpath == "") { std::string testpath = porting::path_data + DIR_DELIM + rel_path; - if (type == "model" || type == "html" || type == "sound" || type == "skin" || type == "font" || type == "translation") { + if (type != "texture") { if (fs::PathExists(testpath)) fullpath = std::string(testpath); }else{