📄 main.cpp
字号:
/* by My Destiny Team */
#include "server.h"
#include "Main.h"
// Info da janela
wxGameServer *GameServer;
// Arquivo de logFILE *lFile;
// Buffer
char buf[1024];
// 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(); }
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%02d/10\t\tIP:\t%s:%u", Server.currUser, Server.GetServerIP(), Server.GetServerPort()); GameServer->statusBar->SetStatusText(buf);}
wxMain::wxMain()
{
}
wxMain::~wxMain()
{ if(lFile != NULL) fclose(lFile);
}
bool wxMain::OnInit()
{ lNum = 0; lFile = fopen("./wxDataServer.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 */
wxMessageBox("WSAStartup failed in wxMain::OnInit",
"Error: InitSocket", wxOK | wxICON_ERROR);
return false;
}
#endif
Log(NORMAL, Server.GetMessage(LOADING));
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;const long wxGameServer::ID_CHARBASE_MENU_ITEM = 9862;
wxGameServer::wxGameServer() :
wxFrame(NULL, ID_WINDOW, _("Data 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); wxRegisterId(ID_CHARBASE_MENU_ITEM);
srvMenu = new wxMenu; fileMenuItem = new wxMenuItem(srvMenu, ID_FILE_ITEM_MENU, _("Close"), _("Exit wxGameServer.")); srvMenu->Append(fileMenuItem); reloadMenuItem = new wxMenu; charbaseMenuItem = new wxMenuItem(reloadMenuItem, ID_CHARBASE_MENU_ITEM, _("Charbase"), _("Reload charbase")); reloadMenuItem->Append(charbaseMenuItem); helpMenu = new wxMenu; aboutMenuItem = new wxMenuItem(helpMenu, ID_ABOUT_ITEM_MENU, _("About"), _("About wxGameServer.")); helpMenu->Append(aboutMenuItem); menuBar->Append(srvMenu, _("Server")); menuBar->Append(reloadMenuItem, _("Reload")); 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_CHARBASE_MENU_ITEM, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&wxGameServer::OnCharBaseMenuItemClick);
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();
}// Fecha o aplicativovoid wxGameServer::CloseApp(){#ifdef WIN32 WSACleanup();#endif Destroy();}// 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);}
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::OnCharBaseMenuItemClick(wxCommandEvent& event){ Log(INFO, "Loading charbase..."); for(u32 x = 0; x < MAX_CHARBASE; ++x) Server.ReadCharacter(nameBase[x], &charBase[x]);}
void wxGameServer::OnAboutMenuItemClick(wxCommandEvent& event)
{
wxAboutDialogInfo info;
info.SetName(_("Data Server"));
info.SetVersion(_(VERSION));
info.SetDescription(_("Data 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 + -