Commit be5c4051 authored by nanahira's avatar nanahira

Merge branch 'master' into server

parents 7668b2e3 85201b70
......@@ -4425,30 +4425,52 @@ bool DuelClient::LookupSRV(char *hostname, HostResult* result) {
return true;
}
HostResult DuelClient::ParseHost(char *hostname) {
bool DuelClient::CheckHostnameSplitter(char* hostname, HostResult* result) {
auto trySplitter = strchr(hostname, ':');
if(trySplitter == NULL)
return false;
char host[100];
auto hostLength = trySplitter - hostname;
strncpy(host, hostname, hostLength);
host[hostLength] = '\0';
result->host = LookupHost(host);
result->port = atoi(trySplitter + 1);
return true;
}
HostResult DuelClient::ParseHost(char *hostname, unsigned short port) {
HostResult result;
// if port found, use port directly
if(port) {
// if hostname contains splitter, use port after splitter in priority
if(!CheckHostnameSplitter(hostname, &result)) {
result.host = LookupHost(hostname);
result.port = port;
}
return result;
}
// if hostname is an IP, use it directly and use default port
unsigned int tryAddress = htonl(inet_addr(hostname));
if(tryAddress != -1) {
result.host = tryAddress;
result.port = 7911;
return result;
}
auto trySplitter = strchr(hostname, ':');
if(trySplitter == NULL) {
char srvHostname[114];
sprintf(srvHostname, "_ygopro._tcp.%s", hostname);
if(!LookupSRV(srvHostname, &result)) {
result.host = LookupHost(hostname);
result.port = 7911;
}
} else {
char host[100];
auto hostLength = trySplitter - hostname;
strncpy(host, hostname, hostLength);
host[hostLength] = '\0';
result.host = LookupHost(host);
result.port = atoi(trySplitter + 1);
}
// if hostname contains splitter, use it first
if(CheckHostnameSplitter(hostname, &result))
return result;
// lookup SRV record
char srvHostname[114];
sprintf(srvHostname, "_ygopro._tcp.%s", hostname);
if(LookupSRV(srvHostname, &result))
return result;
// finally, use default port
result.host = LookupHost(hostname);
result.port = 7911;
return result;
}
}
......@@ -96,8 +96,9 @@ public:
static void SetResponseB(void* respB, unsigned char len);
static void SendResponse();
static unsigned int LookupHost(char *host);
static bool LookupSRV(char *hostname, HostResult *result);
static HostResult ParseHost(char *hostname);
static bool LookupSRV(char *hostname, HostResult* result);
static bool CheckHostnameSplitter(char *hostname, HostResult *result);
static HostResult ParseHost(char *hostname, unsigned short port);
static void SendPacketToServer(unsigned char proto) {
char* p = duel_client_write;
BufferIO::WriteInt16(p, 1);
......
......@@ -250,8 +250,15 @@ bool Game::Initialize() {
lstHostList->setItemHeight(18);
btnLanRefresh = env->addButton(rect<s32>(240, 325, 340, 350), wLanWindow, BUTTON_LAN_REFRESH, dataManager.GetSysString(1217));
env->addStaticText(dataManager.GetSysString(1221), rect<s32>(10, 360, 220, 380), false, false, wLanWindow);
ebJoinHost = env->addEditBox(gameConf.lasthost, rect<s32>(110, 355, 420, 380), true, wLanWindow);
ebJoinHost = env->addEditBox(gameConf.lasthost, rect<s32>(110, 355, 350, 380), true, wLanWindow);
ebJoinHost->setTextAlignment(irr::gui::EGUIA_CENTER, irr::gui::EGUIA_CENTER);
wchar_t lastPortText[6];
if(gameConf.lastport)
myswprintf(lastPortText, L"%d", gameConf.lastport);
else
myswprintf(lastPortText, L"");
ebJoinPort = env->addEditBox(lastPortText, rect<s32>(360, 355, 420, 380), true, wLanWindow);
ebJoinPort->setTextAlignment(irr::gui::EGUIA_CENTER, irr::gui::EGUIA_CENTER);
env->addStaticText(dataManager.GetSysString(1222), rect<s32>(10, 390, 220, 410), false, false, wLanWindow);
ebJoinPass = env->addEditBox(gameConf.roompass, rect<s32>(110, 385, 420, 410), true, wLanWindow);
ebJoinPass->setTextAlignment(irr::gui::EGUIA_CENTER, irr::gui::EGUIA_CENTER);
......@@ -1467,11 +1474,8 @@ bool Game::LoadConfigFromFile(const char* file) {
} else if(!strcmp(strbuf, "lasthost")) {
BufferIO::DecodeUTF8(valbuf, wstr);
BufferIO::CopyWStr(wstr, gameConf.lasthost, 100);
} else if(!strcmp(strbuf, "lastport")) { // for config migration
BufferIO::DecodeUTF8(valbuf, wstr);
wchar_t tmpLastHost[100];
BufferIO::CopyWStr(gameConf.lasthost, tmpLastHost, 100);
myswprintf(gameConf.lasthost, L"%ls:%ls", tmpLastHost, wstr);
} else if(!strcmp(strbuf, "lastport")) {
gameConf.lastport = (unsigned short)atoi(valbuf);
} else if(!strcmp(strbuf, "roompass")) {
BufferIO::DecodeUTF8(valbuf, wstr);
BufferIO::CopyWStr(wstr, gameConf.roompass, 20);
......@@ -1595,6 +1599,7 @@ void Game::LoadConfig() {
gameConf.numfont[0] = 0;
gameConf.textfont[0] = 0;
gameConf.lasthost[0] = 0;
gameConf.lastport = 0;
gameConf.roompass[0] = 0;
//settings
gameConf.chkMAutoPos = 0;
......@@ -1729,6 +1734,7 @@ void Game::SaveConfig() {
fprintf(fp, "serverport = %d\n", gameConf.serverport);
BufferIO::EncodeUTF8(gameConf.lasthost, linebuf);
fprintf(fp, "lasthost = %s\n", linebuf);
fprintf(fp, "lastport = %d\n", gameConf.lastport);
//settings
fprintf(fp, "automonsterpos = %d\n", (chkMAutoPos->isChecked() ? 1 : 0));
fprintf(fp, "autospellpos = %d\n", (chkSTAutoPos->isChecked() ? 1 : 0));
......
......@@ -24,6 +24,7 @@ struct Config {
unsigned short serverport;
unsigned char textfontsize;
wchar_t lasthost[100];
unsigned short lastport;
wchar_t nickname[20];
wchar_t gamename[20];
wchar_t lastcategory[64];
......@@ -373,6 +374,7 @@ public:
irr::gui::IGUIListBox* lstHostList;
irr::gui::IGUIButton* btnLanRefresh;
irr::gui::IGUIEditBox* ebJoinHost;
irr::gui::IGUIEditBox* ebJoinPort;
irr::gui::IGUIEditBox* ebJoinPass;
irr::gui::IGUIButton* btnJoinHost;
irr::gui::IGUIButton* btnJoinCancel;
......
......@@ -145,6 +145,7 @@ int main(int argc, char* argv[]) {
bool keep_on_return = false;
bool deckCategorySpecified = false;
bool portSpecified = false;
for(int i = 1; i < wargc; ++i) {
if(wargv[i][0] == L'-' && wargv[i][1] == L'e' && wargv[i][2] != L'\0') {
ygo::dataManager.LoadDB(&wargv[i][2]);
......@@ -163,17 +164,23 @@ int main(int argc, char* argv[]) {
continue;
} else if(!wcscmp(wargv[i], L"-h")) { // Host address
++i;
if(i < wargc)
if(i < wargc) {
ygo::mainGame->ebJoinHost->setText(wargv[i]);
if(!portSpecified)
ygo::mainGame->ebJoinPort->setText(L"");
}
continue;
} else if(!wcscmp(wargv[i], L"-p")) { // host port, deprecated, and should use 1.1.1.1:7911 instead
} else if(!wcscmp(wargv[i], L"-p")) { // host Port
++i;
if(i < wargc) {
auto host = ygo::mainGame->ebJoinHost->getText();
if(wcslen(host) > 0) {
wchar_t appended[100];
myswprintf(appended, L"%ls:%ls", host, wargv[i]);
ygo::mainGame->ebJoinHost->setText(appended);
portSpecified = true;
auto port = _wtoi(wargv[i]);
if(port) {
wchar_t portStr[6];
myswprintf(portStr, L"%d", port);
ygo::mainGame->ebJoinPort->setText(portStr);
} else {
ygo::mainGame->ebJoinPort->setText(L"");
}
}
continue;
......
......@@ -68,7 +68,8 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
char hostname[100];
const wchar_t* pstr = mainGame->ebJoinHost->getText();
BufferIO::CopyWStr(pstr, hostname, 100);
HostResult remote = DuelClient::ParseHost(hostname);
auto port = (unsigned short)_wtoi(mainGame->ebJoinPort->getText());
HostResult remote = DuelClient::ParseHost(hostname, port);
if(!remote.isValid()) {
mainGame->gMutex.lock();
soundManager.PlaySoundEffect(SOUND_INFO);
......@@ -81,6 +82,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
break;
}
BufferIO::CopyWStr(pstr, mainGame->gameConf.lasthost, 100);
mainGame->gameConf.lastport = port;
if(DuelClient::StartClient(remote.host, remote.port, false)) {
mainGame->btnCreateHost->setEnabled(false);
mainGame->btnJoinHost->setEnabled(false);
......@@ -484,9 +486,11 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
}
int addr = DuelClient::hosts[sel].ipaddr;
int port = DuelClient::hosts[sel].port;
wchar_t buf[26];
myswprintf(buf, L"%d.%d.%d.%d:%d", addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff, port);
wchar_t buf[20];
myswprintf(buf, L"%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff);
mainGame->ebJoinHost->setText(buf);
myswprintf(buf, L"%d", port);
mainGame->ebJoinPort->setText(buf);
break;
}
case LISTBOX_REPLAY_LIST: {
......
......@@ -12,7 +12,8 @@ lastdeck = new
textfont = ./fonts/simhei.ttf 14
numfont = ./fonts/arial.ttf
serverport = 7911
lasthost = 127.0.0.1:7911
lasthost = 127.0.0.1
lastport = 0
automonsterpos = 0
autospellpos = 0
randompos = 0
......
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