You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
6.0 KiB
246 lines
6.0 KiB
|
7 years ago
|
#include <memory>
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
#include <mutex>
|
||
|
|
#include <thread>
|
||
|
|
#endif
|
||
|
7 years ago
|
|
||
|
|
#include "dvlnet/abstract_net.h"
|
||
|
5 years ago
|
#include "mainmenu.h"
|
||
|
|
#include "options.h"
|
||
|
5 years ago
|
#include "storm/storm_dvlnet.h"
|
||
|
5 years ago
|
#include "utils/stubs.h"
|
||
|
7 years ago
|
|
||
|
5 years ago
|
namespace devilution {
|
||
|
7 years ago
|
|
||
|
|
static std::unique_ptr<net::abstract_net> dvlnet_inst;
|
||
|
7 years ago
|
static char gpszGameName[128] = {};
|
||
|
|
static char gpszGamePassword[128] = {};
|
||
|
7 years ago
|
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
static std::mutex storm_net_mutex;
|
||
|
|
#endif
|
||
|
|
|
||
|
5 years ago
|
bool SNetReceiveMessage(int *senderplayerid, char **data, int *databytes)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
if (!dvlnet_inst->SNetReceiveMessage(senderplayerid, data, databytes)) {
|
||
|
|
SErrSetLastError(STORM_ERROR_NO_MESSAGES_WAITING);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetSendMessage(int playerID, void *data, unsigned int databytes)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return dvlnet_inst->SNetSendMessage(playerID, data, databytes);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetReceiveTurns(int a1, int arraysize, char **arraydata, unsigned int *arraydatabytes,
|
||
|
7 years ago
|
DWORD *arrayplayerstatus)
|
||
|
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
if (a1 != 0)
|
||
|
|
UNIMPLEMENTED();
|
||
|
|
if (arraysize != MAX_PLRS)
|
||
|
|
UNIMPLEMENTED();
|
||
|
|
if (!dvlnet_inst->SNetReceiveTurns(arraydata, arraydatabytes, arrayplayerstatus)) {
|
||
|
|
SErrSetLastError(STORM_ERROR_NO_MESSAGES_WAITING);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetSendTurn(char *data, unsigned int databytes)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return dvlnet_inst->SNetSendTurn(data, databytes);
|
||
|
|
}
|
||
|
|
|
||
|
|
int SNetGetProviderCaps(struct _SNETCAPS *caps)
|
||
|
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return dvlnet_inst->SNetGetProviderCaps(caps);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetUnregisterEventHandler(event_type evtype, SEVTHANDLER func)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
5 years ago
|
return dvlnet_inst->SNetUnregisterEventHandler(evtype, func);
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetRegisterEventHandler(event_type evtype, SEVTHANDLER func)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
5 years ago
|
return dvlnet_inst->SNetRegisterEventHandler(evtype, func);
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetDestroy()
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetDropPlayer(int playerid, DWORD flags)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return dvlnet_inst->SNetDropPlayer(playerid, flags);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetGetGameInfo(game_info type, void *dst, unsigned int length)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
switch (type) {
|
||
|
|
case GAMEINFO_NAME:
|
||
|
|
strncpy((char *)dst, gpszGameName, length);
|
||
|
|
break;
|
||
|
|
case GAMEINFO_PASSWORD:
|
||
|
|
strncpy((char *)dst, gpszGamePassword, length);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetLeaveGame(int type)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
6 years ago
|
if (dvlnet_inst == NULL)
|
||
|
6 years ago
|
return true;
|
||
|
7 years ago
|
return dvlnet_inst->SNetLeaveGame(type);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Called by engine for single, called by ui for multi
|
||
|
|
* @param provider BNET, IPXN, MODM, SCBL or UDPN
|
||
|
|
*/
|
||
|
5 years ago
|
int SNetInitializeProvider(Uint32 provider, struct GameData *gameData)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
dvlnet_inst = net::abstract_net::make_net(provider);
|
||
|
5 years ago
|
return mainmenu_select_hero_dialog(gameData);
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Called by engine for single, called by ui for multi
|
||
|
|
*/
|
||
|
5 years ago
|
bool SNetCreateGame(const char *pszGameName, const char *pszGamePassword, const char *pszGameStatString,
|
||
|
5 years ago
|
DWORD dwGameType, char *GameTemplateData, int GameTemplateSize, int playerCount,
|
||
|
5 years ago
|
const char *creatorName, const char *a11, int *playerID)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
5 years ago
|
if (GameTemplateSize != sizeof(GameData))
|
||
|
7 years ago
|
ABORT();
|
||
|
|
net::buffer_t game_init_info(GameTemplateData, GameTemplateData + GameTemplateSize);
|
||
|
|
dvlnet_inst->setup_gameinfo(std::move(game_init_info));
|
||
|
|
|
||
|
5 years ago
|
std::string default_name;
|
||
|
5 years ago
|
if (!pszGameName) {
|
||
|
5 years ago
|
default_name = dvlnet_inst->make_default_gamename();
|
||
|
|
pszGameName = default_name.c_str();
|
||
|
|
}
|
||
|
|
|
||
|
|
strncpy(gpszGameName, pszGameName, sizeof(gpszGameName) - 1);
|
||
|
7 years ago
|
if (pszGamePassword)
|
||
|
|
strncpy(gpszGamePassword, pszGamePassword, sizeof(gpszGamePassword) - 1);
|
||
|
5 years ago
|
*playerID = dvlnet_inst->create(pszGameName, pszGamePassword);
|
||
|
7 years ago
|
return *playerID != -1;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetJoinGame(int id, char *pszGameName, char *pszGamePassword, char *playerName, char *userStats, int *playerID)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
if (pszGameName)
|
||
|
|
strncpy(gpszGameName, pszGameName, sizeof(gpszGameName) - 1);
|
||
|
|
if (pszGamePassword)
|
||
|
|
strncpy(gpszGamePassword, pszGamePassword, sizeof(gpszGamePassword) - 1);
|
||
|
7 years ago
|
*playerID = dvlnet_inst->join(pszGameName, pszGamePassword);
|
||
|
|
return *playerID != -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Is this the mirror image of SNetGetTurnsInTransit?
|
||
|
|
*/
|
||
|
5 years ago
|
bool SNetGetOwnerTurnsWaiting(DWORD *turns)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return dvlnet_inst->SNetGetOwnerTurnsWaiting(turns);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool SNetGetTurnsInTransit(DWORD *turns)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return dvlnet_inst->SNetGetTurnsInTransit(turns);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief engine calls this only once with argument 1
|
||
|
|
*/
|
||
|
5 years ago
|
bool SNetSetBasePlayer(int)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief since we never signal STORM_ERROR_REQUIRES_UPGRADE the engine will not call this function
|
||
|
|
*/
|
||
|
5 years ago
|
bool SNetPerformUpgrade(DWORD *upgradestatus)
|
||
|
7 years ago
|
{
|
||
|
5 years ago
|
#ifndef NONET
|
||
|
|
std::lock_guard<std::mutex> lg(storm_net_mutex);
|
||
|
|
#endif
|
||
|
7 years ago
|
UNIMPLEMENTED();
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
void DvlNet_SendInfoRequest()
|
||
|
|
{
|
||
|
|
dvlnet_inst->send_info_request();
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<std::string> DvlNet_GetGamelist()
|
||
|
|
{
|
||
|
|
return dvlnet_inst->get_gamelist();
|
||
|
|
}
|
||
|
|
|
||
|
|
void DvlNet_SetPassword(std::string pw)
|
||
|
|
{
|
||
|
|
dvlnet_inst->setup_password(pw);
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
} // namespace devilution
|