Refactoring

This commit is contained in:
yvt 2017-01-05 03:03:39 +09:00
parent 0a21093b26
commit a7b9909b2e
15 changed files with 105 additions and 121 deletions

View File

@ -747,7 +747,7 @@ namespace spades {
}
private void Connect() {
string msg = helper.ConnectServer();
string msg = helper.ConnectServer(addressField.Text, cg_protocolVersion.IntValue);
if(msg.length > 0) {
// failde to initialize client.
AlertScreen al(this, msg);

View File

@ -59,6 +59,8 @@ DEFINE_SPADES_SETTING(cg_chatBeep, "1");
DEFINE_SPADES_SETTING(cg_serverAlert, "1");
SPADES_SETTING(cg_playerName);
namespace spades {
namespace client {
@ -67,10 +69,10 @@ namespace spades {
r_device_client()); // Seed Mersenne twister with non-deterministic 32-bit seed
Client::Client(IRenderer *r, IAudioDevice *audioDev, const ServerAddress &host,
std::string playerName, FontManager *fontManager)
FontManager *fontManager)
: renderer(r),
audioDevice(audioDev),
playerName(playerName),
playerName(cg_playerName.operator std::string().substr(0, 15)),
hasDelayedReload(false),
hostname(host),
logStream(nullptr),
@ -343,15 +345,15 @@ namespace spades {
else
SPLog("Mumble link failed");
mumbleLink.setContext(hostname.asString(false));
mumbleLink.setContext(hostname.ToString(false));
mumbleLink.setIdentity(playerName);
SPLog("Started connecting to '%s'", hostname.asString(true).c_str());
SPLog("Started connecting to '%s'", hostname.ToString(true).c_str());
net.reset(new NetClient(this));
net->Connect(hostname);
// decide log file name
std::string fn = hostname.asString(false);
std::string fn = hostname.ToString(false);
std::string fn2;
{
time_t t;

View File

@ -144,7 +144,7 @@ namespace spades {
KeypadInput keypadInput;
Player::ToolType lastTool;
bool hasLastTool;
bool FirstPersonSpectate;
bool firstPersonSpectate;
Vector3 lastFront;
float lastPosSentTime;
int lastHealth;
@ -296,8 +296,7 @@ namespace spades {
virtual ~Client();
public:
Client(IRenderer *, IAudioDevice *, const ServerAddress &host, std::string playerName,
FontManager *);
Client(IRenderer *, IAudioDevice *, const ServerAddress &host, FontManager *);
virtual void RunFrame(float dt);

View File

@ -90,8 +90,6 @@ namespace spades {
bool Client::WantsToBeClosed() { return readyToClose; }
bool FirstPersonSpectate = false;
void Client::Closing() { SPADES_MARK_FUNCTION(); }
bool Client::NeedsAbsoluteMouseCoordinate() {
@ -370,7 +368,7 @@ namespace spades {
playerInput.sneak = down;
} else if (CheckKey(cg_keyJump, name)) {
if (down) {
FirstPersonSpectate = !FirstPersonSpectate;
firstPersonSpectate = !firstPersonSpectate;
}
playerInput.jump = down;
} else if (CheckKey(cg_keyAttack, name)) {

View File

@ -195,7 +195,7 @@ namespace spades {
Vector3 front = center - eye;
front = front.Normalize();
if (FirstPersonSpectate == false) {
if (firstPersonSpectate == false) {
def.viewOrigin = eye;
def.viewAxis[0] = -Vector3::Cross(up, front).Normalize();
def.viewAxis[1] = -Vector3::Cross(front, def.viewAxis[0]).Normalize();

View File

@ -31,8 +31,8 @@
#include <Core/Settings.h>
DEFINE_SPADES_SETTING(cg_minimapSize, "128");
DEFINE_SPADES_SETTING(cg_Minimap_Player_Color, "1");
DEFINE_SPADES_SETTING(cg_Minimap_Player_Icon, "1");
DEFINE_SPADES_SETTING(cg_minimapPlayerColor, "1");
DEFINE_SPADES_SETTING(cg_minimapPlayerIcon, "1");
namespace spades {
namespace client {
@ -397,10 +397,10 @@ namespace spades {
}
// draw objects
std::string iconmode = cg_Minimap_Player_Icon; // import variable from configuration
std::string iconmode = cg_minimapPlayerIcon; // import variable from configuration
// file
std::string colormode =
cg_Minimap_Player_Color; // import variable from configuration file
cg_minimapPlayerColor; // import variable from configuration file
Handle<IImage> playerSMG = renderer->RegisterImage("Gfx/Map/SMG.png");
Handle<IImage> playerRifle = renderer->RegisterImage("Gfx/Map/Rifle.png");

View File

@ -43,7 +43,6 @@
#include <Core/Settings.h>
#include <Core/Strings.h>
DEFINE_SPADES_SETTING(cg_protocolVersion, "3");
DEFINE_SPADES_SETTING(cg_unicode, "1");
namespace spades {
@ -352,22 +351,24 @@ namespace spades {
Disconnect();
SPAssert(status == NetClientStatusNotConnected);
if (hostname.protocol() != ProtocolVersion::Unknown) {
cg_protocolVersion = (int)hostname.protocol();
switch (hostname.GetProtocolVersion()) {
case ProtocolVersion::v075:
SPLog("Using Ace of Spades 0.75 protocol");
protocolVersion = 3;
break;
case ProtocolVersion::v076:
SPLog("Using Ace of Spades 0.76 protocol");
protocolVersion = 4;
break;
default: SPRaise("Invalid ProtocolVersion"); break;
}
switch ((int)cg_protocolVersion) {
case 3: SPLog("Using Ace of Spades 0.75 protocol"); break;
case 4: SPLog("Using Ace of Spades 0.76 protocol"); break;
default: SPRaise("Invalid cg_protocolVersion, should be 3 or 4"); break;
}
ENetAddress addr = hostname.asAddress();
ENetAddress addr = hostname.GetENetAddress();
SPLog("Connecting to %u:%u", (unsigned int)addr.host, (unsigned int)addr.port);
savedPackets.clear();
peer = enet_host_connect(host, &addr, 1, (int)cg_protocolVersion);
peer = enet_host_connect(host, &addr, 1, protocolVersion);
if (peer == NULL) {
SPRaise("Failed to create ENet peer");
}
@ -375,8 +376,6 @@ namespace spades {
status = NetClientStatusConnecting;
statusString = _Tr("NetClient", "Connecting to the server");
timeToTryMapLoad = 0;
protocolVersion = cg_protocolVersion;
}
void NetClient::Disconnect() {
@ -431,11 +430,6 @@ namespace spades {
if (status == NetClientStatusNotConnected)
return;
// don't allow changin cg_protocolVersion
if ((int)cg_protocolVersion != protocolVersion) {
cg_protocolVersion = protocolVersion;
}
if (bandwidthMonitor)
bandwidthMonitor->Update();
@ -701,13 +695,13 @@ namespace spades {
case PacketTypeWorldUpdate: {
// reader.DumpDebug();
int bytesPerEntry = 24;
if ((int)cg_protocolVersion == 4)
if (protocolVersion == 4)
bytesPerEntry++;
int entries = static_cast<int>(reader.GetData().size() / bytesPerEntry);
for (int i = 0; i < entries; i++) {
int idx = i;
if ((int)cg_protocolVersion == 4) {
if (protocolVersion == 4) {
idx = reader.ReadByte();
if (idx < 0 || idx >= 32) {
SPRaise("Invalid player number %d received with WorldUpdate", idx);

View File

@ -40,10 +40,10 @@ namespace spades {
return vl;
}
ServerAddress::ServerAddress(std::string address, ProtocolVersion::Version version)
ServerAddress::ServerAddress(std::string address, ProtocolVersion version)
: mAddress(address), mVersion(version) {}
std::string ServerAddress::stripProtocol(const std::string &address) const {
std::string ServerAddress::StripProtocol(const std::string &address) {
if (address.find("aos:///") == 0) {
return address.substr(7);
} else if (address.find("aos://") == 0) {
@ -52,8 +52,8 @@ namespace spades {
return address;
}
ENetAddress ServerAddress::asAddress() const {
std::string address = stripProtocol(mAddress);
ENetAddress ServerAddress::GetENetAddress() const {
std::string address = StripProtocol(mAddress);
ENetAddress addr;
size_t pos = address.find(':');
@ -73,11 +73,11 @@ namespace spades {
return addr;
}
std::string ServerAddress::asString(bool includeProtocol) const {
std::string ServerAddress::ToString(bool includeProtocol) const {
if (!includeProtocol) {
return stripProtocol(mAddress);
return StripProtocol(mAddress);
}
return mAddress;
}
}; // namespace spades
}; // namespace spades

View File

@ -27,27 +27,21 @@ struct _ENetAddress;
typedef _ENetAddress ENetAddress;
namespace spades {
struct ProtocolVersion {
enum Version {
Unknown = 0,
v075 = 3,
v076 = 4,
};
};
enum class ProtocolVersion { Unknown = 0, v075 = 3, v076 = 4 };
class ServerAddress {
std::string mAddress;
ProtocolVersion::Version mVersion;
ProtocolVersion mVersion;
std::string stripProtocol(const std::string &address) const;
static std::string StripProtocol(const std::string &address);
public:
explicit ServerAddress(std::string address = "",
ProtocolVersion::Version version = ProtocolVersion::Unknown);
ProtocolVersion version = ProtocolVersion::Unknown);
bool valid() const;
ENetAddress asAddress() const;
ProtocolVersion::Version protocol() const { return mVersion; }
std::string asString(bool includeProtocol = true) const;
ENetAddress GetENetAddress() const;
ProtocolVersion GetProtocolVersion() const { return mVersion; }
std::string ToString(bool includeProtocol = true) const;
static uint32_t ParseIntegerAddress(const std::string &str);
};

View File

@ -20,6 +20,7 @@
#include <algorithm> //std::sort
#include <memory>
#include <regex>
#if !defined(__APPLE__) && (__unix || __unix__)
#include <sys/stat.h>
@ -174,71 +175,71 @@ LONG WINAPI UnhandledExceptionProc(LPEXCEPTION_POINTERS lpEx) {
class ThreadQuantumSetter {};
#endif
SPADES_SETTING(cg_lastQuickConnectHost);
SPADES_SETTING(cg_protocolVersion);
SPADES_SETTING(cg_playerName);
int cg_autoConnect = 0;
bool cg_printVersion = false;
bool cg_printHelp = false;
namespace {
bool g_autoconnect = false;
std::string g_autoconnectHostName;
spades::ProtocolVersion g_autoconnectProtocolVersion = spades::ProtocolVersion::v075;
void printHelp(char *binaryName) {
printf("usage: %s [server_address] [v=protocol_version] [-h|--help] [-v|--version] \n",
binaryName);
}
bool g_printVersion = false;
bool g_printHelp = false;
int argsHandler(int argc, char **argv, int &i) {
if (char *a = argv[i]) {
if (!strncasecmp(a, "aos://", 6)) {
cg_lastQuickConnectHost = a;
cg_autoConnect = 1;
return ++i;
}
// lm: we attempt to detect protocol version, allowing with or without a prefix 'v='
// we set proto, but without url we will not auto-connect
if (a[0] == 'v' && a[1] == '=') {
a += 2;
}
if (!strcasecmp(a, "75") || !strcasecmp(a, "075") || !strcasecmp(a, "0.75")) {
cg_protocolVersion = 3;
return ++i;
}
if (!strcasecmp(a, "76") || !strcasecmp(a, "076") || !strcasecmp(a, "0.76")) {
cg_protocolVersion = 4;
return ++i;
}
if (!strcasecmp(a, "--version") || !strcasecmp(a, "-v")) {
cg_printVersion = true;
return ++i;
}
if (!strcasecmp(a, "--help") || !strcasecmp(a, "-h")) {
cg_printHelp = true;
return ++i;
}
void printHelp(char *binaryName) {
printf("usage: %s [server_address] [v=protocol_version] [-h|--help] [-v|--version] \n",
binaryName);
}
return 0;
int handleCommandLineArgument(int argc, char **argv, int &i) {
if (char *a = argv[i]) {
static std::regex hostNameRegex{"aos://.*"};
static std::regex v075Regex{"(?:v=)?0?\\.?75"};
static std::regex v076Regex{"(?:v=)?0?\\.?76"};
if (std::regex_match(a, hostNameRegex)) {
g_autoconnect = true;
g_autoconnectHostName = a;
return ++i;
}
if (std::regex_match(a, v075Regex)) {
g_autoconnectProtocolVersion = spades::ProtocolVersion::v075;
return ++i;
}
if (std::regex_match(a, v076Regex)) {
g_autoconnectProtocolVersion = spades::ProtocolVersion::v076;
return ++i;
}
if (!strcasecmp(a, "--version") || !strcasecmp(a, "-v")) {
g_printVersion = true;
return ++i;
}
if (!strcasecmp(a, "--help") || !strcasecmp(a, "-h")) {
g_printHelp = true;
return ++i;
}
}
return 0;
}
}
namespace spades {
std::string g_userResourceDirectory;
void StartClient(const spades::ServerAddress &addr, const std::string &playerName) {
void StartClient(const spades::ServerAddress &addr) {
class ConcreteRunner : public spades::gui::Runner {
spades::ServerAddress addr;
std::string playerName;
protected:
virtual spades::gui::View *CreateView(spades::client::IRenderer *renderer,
spades::client::IAudioDevice *audio) {
Handle<client::FontManager> fontManager(new client::FontManager(renderer), false);
return new spades::client::Client(renderer, audio, addr, playerName, fontManager);
return new spades::client::Client(renderer, audio, addr, fontManager);
}
public:
ConcreteRunner(const spades::ServerAddress &addr, const std::string &playerName)
: addr(addr), playerName(playerName) {}
ConcreteRunner(const spades::ServerAddress &addr)
: addr(addr) {}
};
ConcreteRunner runner(addr, playerName);
ConcreteRunner runner(addr);
runner.RunProtected();
}
void StartMainScreen() {
@ -374,19 +375,19 @@ int main(int argc, char **argv) {
#endif
for (int i = 1; i < argc;) {
int ret = argsHandler(argc, argv, i);
int ret = handleCommandLineArgument(argc, argv, i);
if (!ret) {
// ignore unknown arg
i++;
}
}
if (cg_printVersion) {
if (g_printVersion) {
printf("%s\n", PACKAGE_STRING);
return 0;
}
if (cg_printHelp) {
if (g_printHelp) {
printHelp(argv[0]);
return 0;
}
@ -658,7 +659,7 @@ int main(int argc, char **argv) {
pumpEvents();
// everything is now ready!
if (!cg_autoConnect) {
if (!g_autoconnect) {
if (!((int)cl_showStartupWindow != 0 || splashWindow->IsStartupScreenRequested())) {
splashWindow.reset();
@ -673,11 +674,9 @@ int main(int argc, char **argv) {
} else {
splashWindow.reset();
spades::ServerAddress host(cg_lastQuickConnectHost.CString(),
(int)cg_protocolVersion == 3
? spades::ProtocolVersion::v075
: spades::ProtocolVersion::v076);
spades::StartClient(host, std::string(cg_playerName).substr(0, 15));
spades::ServerAddress host(g_autoconnectHostName,
g_autoconnectProtocolVersion);
spades::StartClient(host);
}
spades::Settings::GetInstance()->Flush();

View File

@ -310,13 +310,9 @@ namespace spades {
c.ExecuteChecked();
}
std::string MainScreen::Connect() {
spades::ServerAddress host(cg_lastQuickConnectHost, (int)cg_protocolVersion == 3
? spades::ProtocolVersion::v075
: spades::ProtocolVersion::v076);
std::string MainScreen::Connect(const ServerAddress &host) {
try {
subview.Set(new client::Client(&*renderer, &*audioDevice, host,
std::string(cg_playerName).substr(0, 15),
fontManager),
false);
} catch (const std::exception &ex) {

View File

@ -25,6 +25,7 @@
#include <Client/IRenderer.h>
#include <Core/RefCountedObject.h>
#include <ScriptBindings/ScriptManager.h>
#include <Core/ServerAddress.h>
namespace spades {
namespace client {
@ -48,7 +49,7 @@ namespace spades {
void RestoreRenderer();
std::string Connect();
std::string Connect(const ServerAddress &host);
protected:
virtual ~MainScreen();

View File

@ -380,11 +380,12 @@ namespace spades {
return arr;
}
std::string MainScreenHelper::ConnectServer() {
std::string MainScreenHelper::ConnectServer(std::string hostname, int protocolVersion) {
if (mainScreen == NULL) {
return "mainScreen == NULL";
}
return mainScreen->Connect();
return mainScreen->Connect(ServerAddress(
hostname, protocolVersion == 3 ? ProtocolVersion::v075 : ProtocolVersion::v076));
}
std::string MainScreenHelper::GetServerListQueryMessage() {

View File

@ -100,7 +100,7 @@ namespace spades {
CScriptArray *GetServerList(std::string sortKey, bool descending);
std::string GetServerListQueryMessage();
std::string ConnectServer();
std::string ConnectServer(std::string hostname, int protocolVersion);
std::string GetPendingErrorMessage();
std::string GetCredits();

View File

@ -82,7 +82,7 @@ namespace spades {
asCALL_THISCALL);
manager->CheckError(r);
r = eng->RegisterObjectMethod("MainScreenHelper",
"string ConnectServer()",
"string ConnectServer(string, int)",
asMETHOD(gui::MainScreenHelper, ConnectServer),
asCALL_THISCALL);
manager->CheckError(r);