📄 main.cpp
字号:
/* by My Destiny Team */
#include "server.h"#include "packets.h"
#include "Main.h"
// Info da janela
wxGameServer *GameServer;
// Arquivo de logFILE *lFile;
// Numero de log na telau32 lNum;
// Escreve o log na janela e no arquivo de log
void Log(eLog log, const char *str, ...)
{
char arg[256];
va_list pArgList;
va_start(pArgList, str);
vsprintf(arg, str, pArgList);
va_end(pArgList);
if(++lNum >= 100) { /* limpa o log */ lNum = 0; GameServer->logText->Clear(); if(lFile != NULL) fclose(lFile); lFile = fopen("./wxGameServer.log", "a"); }
time_t now = time(NULL);
tm *sNow = localtime(&now);
sprintf(buf, "[%02d/%02d/%02d %02d:%02d:%02d] - ",
sNow->tm_mday, (sNow->tm_mon + 1), (sNow->tm_year - 100),
sNow->tm_hour, sNow->tm_min, sNow->tm_sec);
strcat(buf, arg);
strcat(buf, "\n");
if(log == NORMAL)
{
GameServer->logText->SetDefaultStyle(wxTextAttr(*wxBLACK));
}
else if(log == INFO)
{
GameServer->logText->SetDefaultStyle(wxTextAttr(*wxBLUE));
}
else /* log = WARN */
{
GameServer->logText->SetDefaultStyle(wxTextAttr(*wxRED));
}
if(lFile != NULL) fprintf(lFile, "%s", buf);
GameServer->logText->AppendText(buf);
}
// Escreve o log no status barvoid RefStatusBar(){ sprintf(buf, "Players:\t%03d/%03d\t\t\tIP:\t%s:%u\t\tDB:\t%s:%d", Server.currUser, Server.maxUser, Server.GetServerIP(), Server.GetServerPort(), Server.GetDatabaseIP(), Server.GetDatabasePort()); GameServer->statusBar->SetStatusText(buf);}
wxMain::wxMain()
{
}
wxMain::~wxMain()
{ if(lFile != NULL) fclose(lFile);
}
bool wxMain::OnInit()
{ lNum = 0; lFile = fopen("./wxGameServer.log", "w"); if(!Server.ReadLanguage()) return false;
GameServer = new wxGameServer;
GameServer->Show();
SetTopWindow(GameServer);
#ifdef WIN32
if(WSAStartup(MAKEWORD(2,2), &GameServer->wsaData))
{ /* erro ao iniciar a dll de socket do windows */
Log(WARN, "WSAStartup failed in wxMain::OnInit.");
return false;
}
#endif Log(NORMAL, Server.GetMessage(LOADINGFILE));
if(!Server.Init())
{ /* close srv */ wxMessageBox(Server.GetMessage(ERRORINIT), "Error: Initialization", wxOK | wxICON_ERROR);
return false;
}
GameServer->Init(); RefStatusBar();
return true;
}
/* ID's */
const long wxGameServer::ID_WINDOW = 9855;
const long wxGameServer::ID_LOG_TEXT = 9856;
const long wxGameServer::ID_STATUS_BAR = 9857;
const long wxGameServer::ID_SELECT_TIMER = 9858;
const long wxGameServer::ID_GENERAL_TIMER = 9859;
/* Menu ID's */
const long wxGameServer::ID_FILE_ITEM_MENU = 9860;
const long wxGameServer::ID_ABOUT_ITEM_MENU = 9861;
wxGameServer::wxGameServer() :
wxFrame(NULL, ID_WINDOW, _("Game Server - Version 0.04a"), wxDefaultPosition, wxSize(600, 480))
{
menuBar = new wxMenuBar;
wxRegisterId(ID_WINDOW);
wxRegisterId(ID_LOG_TEXT);
wxRegisterId(ID_STATUS_BAR);
wxRegisterId(ID_FILE_ITEM_MENU);
wxRegisterId(ID_ABOUT_ITEM_MENU);
wxRegisterId(ID_SELECT_TIMER);
wxRegisterId(ID_GENERAL_TIMER);
srvMenu = new wxMenu;
fileMenuItem = new wxMenuItem(srvMenu, ID_FILE_ITEM_MENU, _("Close"), _("Exit wxGameServer."));
srvMenu->Append(fileMenuItem);
helpMenu = new wxMenu;
aboutMenuItem = new wxMenuItem(helpMenu, ID_ABOUT_ITEM_MENU, _("About"), _("About wxGameServer."));
helpMenu->Append(aboutMenuItem);
menuBar->Append(srvMenu, _("Server"));
menuBar->Append(helpMenu, _("Help"));
SetMenuBar(menuBar);
statusBar = new wxStatusBar(this, ID_STATUS_BAR);
SetStatusBar(statusBar);
logText = new wxTextCtrl(this, ID_LOG_TEXT, _(""),
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
logText->SetBackgroundColour(*wxLIGHT_GREY);
selTimer.SetOwner(this, ID_SELECT_TIMER);
selTimer.Start(100, false);
generalTimer.SetOwner(this, ID_GENERAL_TIMER);
generalTimer.Start(1000, false);
Connect(ID_ABOUT_ITEM_MENU, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&wxGameServer::OnAboutMenuItemClick);
Connect(ID_FILE_ITEM_MENU, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&wxGameServer::OnCloseMenuItemClick);
Connect(ID_WINDOW, wxEVT_CLOSE_WINDOW, (wxObjectEventFunction)&wxGameServer::OnCloseMenuItemClick);
}
wxGameServer::~wxGameServer()
{
}
void wxGameServer::OnGeneralTimer(wxCommandEvent& event)
{
Server.CheckSockets();
}
void wxGameServer::OnSelectTimer(wxCommandEvent& event)
{
Server.Select();
}
// Inicia os processos do servidorvoid wxGameServer::Init(){ Connect(ID_SELECT_TIMER, wxEVT_TIMER, (wxObjectEventFunction)&wxGameServer::OnSelectTimer); Connect(ID_GENERAL_TIMER, wxEVT_TIMER, (wxObjectEventFunction)&wxGameServer::OnGeneralTimer);}// Fecha o aplicativovoid wxGameServer::CloseApp(){# ifdef WIN32 WSACleanup();# endif Destroy();}
void wxGameServer::OnCloseMenuItemClick(wxCommandEvent& event)
{
int ret = wxMessageBox(_("Close the server?"),
_("Confirm"), wxYES_NO | wxICON_QUESTION, this);
if(ret == wxYES)
{ /* fechando o gs */
Log(INFO, _("Closing the server...")); CloseApp();
return ;
}
}
void wxGameServer::OnAboutMenuItemClick(wxCommandEvent& event)
{
wxAboutDialogInfo info;
info.SetName(_("Game Server"));
info.SetVersion(_(VERSION));
info.SetDescription(_("Game Server for WYD2 Game."));
info.SetCopyright(_("Copyright (C) 2008 My Destiny Team"));
info.SetWebSite(_("http://wydserver.sf.net"), _("My Destiny Project"));
info.AddDeveloper(_("SuperNov4"));
info.AddDeveloper(_("d4rck3r"));
info.AddDeveloper(_("vinles"));
wxAboutBox(info);
}
IMPLEMENT_APP(wxMain);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -