Skip to content

Commit

Permalink
Merge pull request #633 from Wargus/OnlineService
Browse files Browse the repository at this point in the history
Online service
  • Loading branch information
Jarod42 authored Mar 14, 2024
2 parents 86f4d1b + 3cd8b72 commit 67353de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/include/online_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "network/netsockets.h"

#include <memory>
#include <string>

class OnlineContext
Expand Down Expand Up @@ -32,7 +33,10 @@ class OnlineContext
virtual void reportGameResult() = 0;
};

extern OnlineContext *OnlineContextHandler;
extern std::unique_ptr<OnlineContext> OnlineContextHandler;

void InitOnlineService();
void DeInitOnlineService();

extern void OnlineServiceCclRegister();

Expand Down
55 changes: 53 additions & 2 deletions src/network/online_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2094,11 +2094,13 @@ class C2S_SID_AUTH_INFO : public OnlineState
int retries = 30;
};

static Context _ctx;
OnlineContext *OnlineContextHandler = &_ctx;
std::unique_ptr<OnlineContext> OnlineContextHandler;

static int CclStopAdvertisingOnlineGame(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

if (_ctx.isConnected()) {
_ctx.stopAdvertising();
lua_pushboolean(l, true);
Expand All @@ -2110,6 +2112,9 @@ static int CclStopAdvertisingOnlineGame(lua_State *l)

static int CclStartAdvertisingOnlineGame(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

if (_ctx.isConnected()) {
_ctx.startAdvertising();
lua_pushboolean(l, true);
Expand All @@ -2121,6 +2126,9 @@ static int CclStartAdvertisingOnlineGame(lua_State *l)

static int CclSetup(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 1);
if (!lua_istable(l, 1)) {
LuaError(l, "incorrect argument");
Expand Down Expand Up @@ -2166,13 +2174,19 @@ static int CclSetup(lua_State *l)

static int CclDisconnect(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 0);
_ctx.disconnect();
return 0;
}

static int CclConnect(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

_ctx.disconnect();
LuaCheckArgs(l, 2);
const std::string host = std::string{LuaToString(l, 1)};
Expand All @@ -2185,6 +2199,9 @@ static int CclConnect(lua_State *l)

static int CclLogin(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 2);
_ctx.setCreateAccount(false);
_ctx.setUsername(std::string{LuaToString(l, 1)});
Expand All @@ -2194,6 +2211,9 @@ static int CclLogin(lua_State *l)

static int CclSignUp(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 2);
_ctx.setCreateAccount(true);
_ctx.setUsername(std::string{LuaToString(l, 1)});
Expand All @@ -2203,6 +2223,9 @@ static int CclSignUp(lua_State *l)

static int CclStep(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 0);
if (_ctx.isDisconnected()) {
LuaError(l, "disconnected service cannot step");
Expand All @@ -2221,6 +2244,9 @@ static int CclStep(lua_State *l)

static int CclSendMessage(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 1);
if (!_ctx.isConnected()) {
LuaError(l, "connect first");
Expand All @@ -2231,6 +2257,9 @@ static int CclSendMessage(lua_State *l)

static int CclJoinChannel(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 1);
if (!_ctx.isConnected()) {
LuaError(l, "connect first");
Expand All @@ -2241,6 +2270,9 @@ static int CclJoinChannel(lua_State *l)

static int CclJoinGame(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 2);
if (!_ctx.isConnected()) {
LuaError(l, "connect first");
Expand All @@ -2251,6 +2283,9 @@ static int CclJoinGame(lua_State *l)

static int CclStatus(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 0);
if (_ctx.isConnected()) {
lua_pushstring(l, "connected");
Expand All @@ -2270,6 +2305,9 @@ static int CclStatus(lua_State *l)

static int CclRequestUserInfo(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 1);
if (!_ctx.isConnected()) {
LuaError(l, "not connected");
Expand All @@ -2280,11 +2318,24 @@ static int CclRequestUserInfo(lua_State *l)

static int CclPunchNAT(lua_State *l)
{
Assert(OnlineContextHandler);
Context &_ctx = dynamic_cast<Context &>(*OnlineContextHandler);

LuaCheckArgs(l, 1);
_ctx.punchNAT(std::string{LuaToString(l, 1)});
return 0;
}

void InitOnlineService()
{
OnlineContextHandler = std::make_unique<Context>();
}

void DeInitOnlineService()
{
OnlineContextHandler = nullptr;
}

void OnlineServiceCclRegister()
{
lua_createtable(Lua, 0, 3);
Expand Down
3 changes: 3 additions & 0 deletions src/stratagus/stratagus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ extern void beos_init(int argc, char **argv);
#include "map.h"
#include "netconnect.h"
#include "network.h"
#include "online_service.h"
#include "parameters.h"
#include "player.h"
#include "replay.h"
Expand Down Expand Up @@ -406,6 +407,7 @@ void Exit(int err)
FreeGraphics();
FreePlayerColors();
FreeButtonStyles();
DeInitOnlineService();
freeGuichan();
fprintf(stdout, "Frames %lu, Slow frames %ld = %ld%%\n",
FrameCounter, SlowFrameCounter,
Expand Down Expand Up @@ -758,6 +760,7 @@ try {
Map.AllocateTileset();
UnitManager = new CUnitManager();
FogOfWar = std::make_unique<CFogOfWar>();
InitOnlineService();

LoadCcl(parameters.luaStartFilename, parameters.luaScriptArguments);

Expand Down

0 comments on commit 67353de

Please sign in to comment.