|
| 1 | +#include "storm/storm_net.hpp" |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <memory> |
| 5 | + |
| 6 | +#ifndef NONET |
| 7 | +#include <mutex> |
| 8 | +#include <thread> |
| 9 | +#include <utility> |
| 10 | + |
| 11 | +#include "utils/sdl_mutex.h" |
| 12 | +#endif |
| 13 | + |
| 14 | +#include "dvlnet/abstract_net.h" |
| 15 | +#include "engine/demomode.h" |
| 16 | +#include "menu.h" |
| 17 | +#include "options.h" |
| 18 | +#include "utils/stubs.h" |
| 19 | +#include "utils/utf8.hpp" |
| 20 | + |
| 21 | +namespace devilution { |
| 22 | + |
| 23 | +namespace { |
| 24 | +std::unique_ptr<net::abstract_net> dvlnet_inst; |
| 25 | +bool GameIsPublic = {}; |
| 26 | +thread_local uint32_t dwLastError = 0; |
| 27 | + |
| 28 | +#ifndef NONET |
| 29 | +SdlMutex storm_net_mutex; |
| 30 | +#endif |
| 31 | +} // namespace |
| 32 | + |
| 33 | +uint32_t SErrGetLastError() |
| 34 | +{ |
| 35 | + return dwLastError; |
| 36 | +} |
| 37 | + |
| 38 | +void SErrSetLastError(uint32_t dwErrCode) |
| 39 | +{ |
| 40 | + dwLastError = dwErrCode; |
| 41 | +} |
| 42 | + |
| 43 | +bool SNetReceiveMessage(uint8_t *senderplayerid, void **data, uint32_t *databytes) |
| 44 | +{ |
| 45 | +#ifndef NONET |
| 46 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 47 | +#endif |
| 48 | + if (!dvlnet_inst->SNetReceiveMessage(senderplayerid, data, databytes)) { |
| 49 | + SErrSetLastError(STORM_ERROR_NO_MESSAGES_WAITING); |
| 50 | + return false; |
| 51 | + } |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +bool SNetSendMessage(int playerID, void *data, unsigned int databytes) |
| 56 | +{ |
| 57 | +#ifndef NONET |
| 58 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 59 | +#endif |
| 60 | + return dvlnet_inst->SNetSendMessage(playerID, data, databytes); |
| 61 | +} |
| 62 | + |
| 63 | +bool SNetReceiveTurns(int arraysize, char **arraydata, size_t *arraydatabytes, uint32_t *arrayplayerstatus) |
| 64 | +{ |
| 65 | +#ifndef NONET |
| 66 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 67 | +#endif |
| 68 | + if (arraysize != MAX_PLRS) |
| 69 | + UNIMPLEMENTED(); |
| 70 | + if (!dvlnet_inst->SNetReceiveTurns(arraydata, arraydatabytes, arrayplayerstatus)) { |
| 71 | + SErrSetLastError(STORM_ERROR_NO_MESSAGES_WAITING); |
| 72 | + return false; |
| 73 | + } |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +bool SNetSendTurn(char *data, unsigned int databytes) |
| 78 | +{ |
| 79 | +#ifndef NONET |
| 80 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 81 | +#endif |
| 82 | + return dvlnet_inst->SNetSendTurn(data, databytes); |
| 83 | +} |
| 84 | + |
| 85 | +void SNetGetProviderCaps(struct _SNETCAPS *caps) |
| 86 | +{ |
| 87 | +#ifndef NONET |
| 88 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 89 | +#endif |
| 90 | + dvlnet_inst->SNetGetProviderCaps(caps); |
| 91 | +} |
| 92 | + |
| 93 | +bool SNetUnregisterEventHandler(event_type evtype) |
| 94 | +{ |
| 95 | +#ifndef NONET |
| 96 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 97 | +#endif |
| 98 | + if (dvlnet_inst == nullptr) |
| 99 | + return true; |
| 100 | + return dvlnet_inst->SNetUnregisterEventHandler(evtype); |
| 101 | +} |
| 102 | + |
| 103 | +bool SNetRegisterEventHandler(event_type evtype, SEVTHANDLER func) |
| 104 | +{ |
| 105 | +#ifndef NONET |
| 106 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 107 | +#endif |
| 108 | + return dvlnet_inst->SNetRegisterEventHandler(evtype, func); |
| 109 | +} |
| 110 | + |
| 111 | +bool SNetDestroy() |
| 112 | +{ |
| 113 | +#ifndef NONET |
| 114 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 115 | +#endif |
| 116 | + dvlnet_inst = nullptr; |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +bool SNetDropPlayer(int playerid, uint32_t flags) |
| 121 | +{ |
| 122 | +#ifndef NONET |
| 123 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 124 | +#endif |
| 125 | + return dvlnet_inst->SNetDropPlayer(playerid, flags); |
| 126 | +} |
| 127 | + |
| 128 | +bool SNetLeaveGame(int type) |
| 129 | +{ |
| 130 | +#ifndef NONET |
| 131 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 132 | +#endif |
| 133 | + if (dvlnet_inst == nullptr) |
| 134 | + return true; |
| 135 | + return dvlnet_inst->SNetLeaveGame(type); |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * @brief Called by engine for single, called by ui for multi |
| 140 | + * @param provider BNET, IPXN, MODM, SCBL or UDPN |
| 141 | + * @param gameData The game data |
| 142 | + */ |
| 143 | +bool SNetInitializeProvider(uint32_t provider, struct GameData *gameData) |
| 144 | +{ |
| 145 | +#ifndef NONET |
| 146 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 147 | +#endif |
| 148 | + dvlnet_inst = net::abstract_net::MakeNet(provider); |
| 149 | + return (HeadlessMode && !demo::IsRunning()) || mainmenu_select_hero_dialog(gameData); |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * @brief Called by engine for single, called by ui for multi |
| 154 | + */ |
| 155 | +bool SNetCreateGame(const char *pszGameName, const char *pszGamePassword, char *gameTemplateData, int gameTemplateSize, int *playerID) |
| 156 | +{ |
| 157 | +#ifndef NONET |
| 158 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 159 | +#endif |
| 160 | + if (gameTemplateSize != sizeof(GameData)) |
| 161 | + ABORT(); |
| 162 | + net::buffer_t gameInitInfo(gameTemplateData, gameTemplateData + gameTemplateSize); |
| 163 | + dvlnet_inst->setup_gameinfo(std::move(gameInitInfo)); |
| 164 | + |
| 165 | + std::string defaultName; |
| 166 | + if (pszGameName == nullptr) { |
| 167 | + defaultName = dvlnet_inst->make_default_gamename(); |
| 168 | + pszGameName = defaultName.c_str(); |
| 169 | + } |
| 170 | + |
| 171 | + GameName = pszGameName; |
| 172 | + if (pszGamePassword != nullptr) |
| 173 | + DvlNet_SetPassword(pszGamePassword); |
| 174 | + else |
| 175 | + DvlNet_ClearPassword(); |
| 176 | + *playerID = dvlnet_inst->create(pszGameName); |
| 177 | + return *playerID != -1; |
| 178 | +} |
| 179 | + |
| 180 | +bool SNetJoinGame(char *pszGameName, char *pszGamePassword, int *playerID) |
| 181 | +{ |
| 182 | +#ifndef NONET |
| 183 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 184 | +#endif |
| 185 | + if (pszGameName != nullptr) |
| 186 | + GameName = pszGameName; |
| 187 | + if (pszGamePassword != nullptr) |
| 188 | + DvlNet_SetPassword(pszGamePassword); |
| 189 | + else |
| 190 | + DvlNet_ClearPassword(); |
| 191 | + *playerID = dvlnet_inst->join(pszGameName); |
| 192 | + return *playerID != -1; |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * @brief Is this the mirror image of SNetGetTurnsInTransit? |
| 197 | + */ |
| 198 | +bool SNetGetOwnerTurnsWaiting(uint32_t *turns) |
| 199 | +{ |
| 200 | +#ifndef NONET |
| 201 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 202 | +#endif |
| 203 | + return dvlnet_inst->SNetGetOwnerTurnsWaiting(turns); |
| 204 | +} |
| 205 | + |
| 206 | +bool SNetGetTurnsInTransit(uint32_t *turns) |
| 207 | +{ |
| 208 | +#ifndef NONET |
| 209 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 210 | +#endif |
| 211 | + return dvlnet_inst->SNetGetTurnsInTransit(turns); |
| 212 | +} |
| 213 | + |
| 214 | +/** |
| 215 | + * @brief engine calls this only once with argument 1 |
| 216 | + */ |
| 217 | +bool SNetSetBasePlayer(int /*unused*/) |
| 218 | +{ |
| 219 | +#ifndef NONET |
| 220 | + std::lock_guard<SdlMutex> lg(storm_net_mutex); |
| 221 | +#endif |
| 222 | + return true; |
| 223 | +} |
| 224 | + |
| 225 | +bool DvlNet_SendInfoRequest() |
| 226 | +{ |
| 227 | + return dvlnet_inst->send_info_request(); |
| 228 | +} |
| 229 | + |
| 230 | +void DvlNet_ClearGamelist() |
| 231 | +{ |
| 232 | + return dvlnet_inst->clear_gamelist(); |
| 233 | +} |
| 234 | + |
| 235 | +std::vector<GameInfo> DvlNet_GetGamelist() |
| 236 | +{ |
| 237 | + return dvlnet_inst->get_gamelist(); |
| 238 | +} |
| 239 | + |
| 240 | +void DvlNet_SetPassword(std::string pw) |
| 241 | +{ |
| 242 | + GameIsPublic = false; |
| 243 | + GamePassword = pw; |
| 244 | + dvlnet_inst->setup_password(std::move(pw)); |
| 245 | +} |
| 246 | + |
| 247 | +void DvlNet_ClearPassword() |
| 248 | +{ |
| 249 | + GameIsPublic = true; |
| 250 | + GamePassword.clear(); |
| 251 | + dvlnet_inst->clear_password(); |
| 252 | +} |
| 253 | + |
| 254 | +bool DvlNet_IsPublicGame() |
| 255 | +{ |
| 256 | + return GameIsPublic; |
| 257 | +} |
| 258 | + |
| 259 | +} // namespace devilution |
0 commit comments