32 #include <stdlib.h> |
32 #include <stdlib.h> |
33 #include <string.h> |
33 #include <string.h> |
34 #include <errno.h> |
34 #include <errno.h> |
35 #include <ctype.h> |
35 #include <ctype.h> |
36 |
36 |
37 flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *dataDirPath, const char *host, uint16_t port) { |
37 flib_netconn *flib_netconn_create(const char *playerName, flib_metascheme *metacfg, const char *dataDirPath, const char *host, int port) { |
38 flib_netconn *result = NULL; |
38 flib_netconn *result = NULL; |
39 if(!log_badargs_if3(playerName==NULL, metacfg==NULL, host==NULL)) { |
39 if(!log_badargs_if5(playerName==NULL, metacfg==NULL, host==NULL, port<1, port>65535)) { |
40 flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn)); |
40 flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn)); |
41 if(newConn) { |
41 if(newConn) { |
42 newConn->netBase = flib_netbase_create(host, port); |
42 newConn->netBase = flib_netbase_create(host, port); |
43 newConn->playerName = flib_strdupnull(playerName); |
43 newConn->playerName = flib_strdupnull(playerName); |
44 newConn->dataDirPath = flib_strdupnull(dataDirPath); |
44 newConn->dataDirPath = flib_strdupnull(dataDirPath); |
45 |
45 |
46 newConn->netconnState = NETCONN_STATE_CONNECTING; |
46 newConn->netconnState = NETCONN_STATE_CONNECTING; |
47 newConn->isAdmin = false; |
47 newConn->isAdmin = false; |
48 newConn->metaCfg = flib_cfg_meta_retain(metacfg); |
48 newConn->metaCfg = flib_metascheme_retain(metacfg); |
49 newConn->roomList.roomCount = 0; |
49 newConn->roomList.roomCount = 0; |
50 newConn->roomList.rooms = NULL; |
50 newConn->roomList.rooms = NULL; |
51 |
51 |
52 newConn->isChief = false; |
52 newConn->isChief = false; |
53 newConn->map = flib_map_create_named("", "NoSuchMap"); |
53 newConn->map = flib_map_create_named("", "NoSuchMap"); |
85 } else { |
85 } else { |
86 flib_netbase_destroy(conn->netBase); |
86 flib_netbase_destroy(conn->netBase); |
87 free(conn->playerName); |
87 free(conn->playerName); |
88 free(conn->dataDirPath); |
88 free(conn->dataDirPath); |
89 |
89 |
90 flib_cfg_meta_release(conn->metaCfg); |
90 flib_metascheme_release(conn->metaCfg); |
91 flib_roomlist_clear(&conn->roomList); |
91 flib_roomlist_clear(&conn->roomList); |
92 |
92 |
93 flib_map_release(conn->map); |
93 flib_map_release(conn->map); |
94 flib_teamlist_clear(&conn->pendingTeamlist); |
94 flib_teamlist_clear(&conn->pendingTeamlist); |
95 flib_teamlist_clear(&conn->teamlist); |
95 flib_teamlist_clear(&conn->teamlist); |
96 flib_cfg_release(conn->scheme); |
96 flib_scheme_release(conn->scheme); |
97 free(conn->script); |
97 free(conn->script); |
98 flib_weaponset_release(conn->weaponset); |
98 flib_weaponset_release(conn->weaponset); |
99 |
99 |
100 free(conn); |
100 free(conn); |
101 } |
101 } |
121 conn->isChief = false; |
121 conn->isChief = false; |
122 flib_map_release(conn->map); |
122 flib_map_release(conn->map); |
123 conn->map = flib_map_create_named("", "NoSuchMap"); |
123 conn->map = flib_map_create_named("", "NoSuchMap"); |
124 flib_teamlist_clear(&conn->pendingTeamlist); |
124 flib_teamlist_clear(&conn->pendingTeamlist); |
125 flib_teamlist_clear(&conn->teamlist); |
125 flib_teamlist_clear(&conn->teamlist); |
126 flib_cfg_release(conn->scheme); |
126 flib_scheme_release(conn->scheme); |
127 conn->scheme = NULL; |
127 conn->scheme = NULL; |
128 free(conn->script); |
128 free(conn->script); |
129 conn->script = NULL; |
129 conn->script = NULL; |
130 flib_weaponset_release(conn->weaponset); |
130 flib_weaponset_release(conn->weaponset); |
131 conn->weaponset = NULL; |
131 conn->weaponset = NULL; |
180 stackSetup.teamlist = &conn->teamlist; |
180 stackSetup.teamlist = &conn->teamlist; |
181 flib_gamesetup *tmpSetup = flib_gamesetup_copy(&stackSetup); |
181 flib_gamesetup *tmpSetup = flib_gamesetup_copy(&stackSetup); |
182 if(tmpSetup) { |
182 if(tmpSetup) { |
183 for(int i=0; i<tmpSetup->teamlist->teamCount; i++) { |
183 for(int i=0; i<tmpSetup->teamlist->teamCount; i++) { |
184 flib_team_set_weaponset(tmpSetup->teamlist->teams[i], conn->weaponset); |
184 flib_team_set_weaponset(tmpSetup->teamlist->teams[i], conn->weaponset); |
185 flib_team_set_health(tmpSetup->teamlist->teams[i], flib_cfg_get_setting(conn->scheme, "health", 100)); |
185 flib_team_set_health(tmpSetup->teamlist->teams[i], flib_scheme_get_setting(conn->scheme, "health", 100)); |
186 } |
186 } |
187 if(tmpSetup->map->mapgen == MAPGEN_NAMED && tmpSetup->map->name) { |
187 if(tmpSetup->map->mapgen == MAPGEN_NAMED && tmpSetup->map->name) { |
188 flib_mapcfg mapcfg; |
188 flib_mapcfg mapcfg; |
189 if(!flib_mapcfg_read(conn->dataDirPath, tmpSetup->map->name, &mapcfg)) { |
189 if(!flib_mapcfg_read(conn->dataDirPath, tmpSetup->map->name, &mapcfg)) { |
190 free(tmpSetup->map->theme); |
190 free(tmpSetup->map->theme); |
330 } else if (!strcmp(cmd, "ADD_TEAM")) { |
330 } else if (!strcmp(cmd, "ADD_TEAM")) { |
331 if(netmsg->partCount != 24 || !flib_netconn_is_in_room_context(conn)) { |
331 if(netmsg->partCount != 24 || !flib_netconn_is_in_room_context(conn)) { |
332 flib_log_w("Net: Bad ADD_TEAM message"); |
332 flib_log_w("Net: Bad ADD_TEAM message"); |
333 } else { |
333 } else { |
334 flib_team *team = flib_team_from_netmsg(netmsg->parts+1); |
334 flib_team *team = flib_team_from_netmsg(netmsg->parts+1); |
335 flib_team *teamcopy = flib_team_from_netmsg(netmsg->parts+1); |
335 if(!team || flib_teamlist_insert(&conn->teamlist, team, conn->teamlist.teamCount)) { |
336 if(!team || !teamcopy) { |
336 flib_team_destroy(team); |
337 conn->netconnState = NETCONN_STATE_DISCONNECTED; |
337 conn->netconnState = NETCONN_STATE_DISCONNECTED; |
338 conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_INTERNAL_ERROR, "Internal error"); |
338 conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_INTERNAL_ERROR, "Internal error"); |
339 exit = true; |
339 exit = true; |
340 } else { |
340 } else { |
341 team->remoteDriven = true; |
341 team->remoteDriven = true; |
342 teamcopy->remoteDriven = true; |
|
343 flib_teamlist_insert(&conn->teamlist, teamcopy, conn->teamlist.teamCount); |
|
344 conn->onTeamAddCb(conn->onTeamAddCtx, team); |
342 conn->onTeamAddCb(conn->onTeamAddCtx, team); |
345 } |
343 } |
346 flib_team_release(team); |
|
347 flib_team_release(teamcopy); |
|
348 } |
344 } |
349 } else if (!strcmp(cmd, "REMOVE_TEAM")) { |
345 } else if (!strcmp(cmd, "REMOVE_TEAM")) { |
350 if(netmsg->partCount != 2 || !flib_netconn_is_in_room_context(conn)) { |
346 if(netmsg->partCount != 2 || !flib_netconn_is_in_room_context(conn)) { |
351 flib_log_w("Net: Bad REMOVETEAM message"); |
347 flib_log_w("Net: Bad REMOVETEAM message"); |
352 } else { |
348 } else { |
465 if(netmsg->partCount < 3 || !flib_netconn_is_in_room_context(conn)) { |
461 if(netmsg->partCount < 3 || !flib_netconn_is_in_room_context(conn)) { |
466 flib_log_w("Net: Bad CFG message"); |
462 flib_log_w("Net: Bad CFG message"); |
467 } else { |
463 } else { |
468 const char *subcmd = netmsg->parts[1]; |
464 const char *subcmd = netmsg->parts[1]; |
469 if(!strcmp(subcmd, "SCHEME") && netmsg->partCount == conn->metaCfg->modCount + conn->metaCfg->settingCount + 3) { |
465 if(!strcmp(subcmd, "SCHEME") && netmsg->partCount == conn->metaCfg->modCount + conn->metaCfg->settingCount + 3) { |
470 flib_cfg *cfg = flib_netmsg_to_cfg(conn->metaCfg, netmsg->parts+2); |
466 flib_scheme *cfg = flib_netmsg_to_cfg(conn->metaCfg, netmsg->parts+2); |
471 if(cfg) { |
467 if(cfg) { |
472 netconn_setScheme(conn, cfg); |
468 netconn_setScheme(conn, cfg); |
473 conn->onCfgSchemeCb(conn->onCfgSchemeCtx, cfg); |
469 conn->onCfgSchemeCb(conn->onCfgSchemeCtx, cfg); |
474 } else { |
470 } else { |
475 flib_log_e("Error processing CFG SCHEME message"); |
471 flib_log_e("Error processing CFG SCHEME message"); |
476 } |
472 } |
477 flib_cfg_release(cfg); |
473 flib_scheme_release(cfg); |
478 } else if(!strcmp(subcmd, "FULLMAPCONFIG") && netmsg->partCount == 7) { |
474 } else if(!strcmp(subcmd, "FULLMAPCONFIG") && netmsg->partCount == 7) { |
479 flib_map *map = flib_netmsg_to_map(netmsg->parts+2); |
475 flib_map *map = flib_netmsg_to_map(netmsg->parts+2); |
480 if(map) { |
476 if(map) { |
481 netconn_setMap(conn, map); |
477 netconn_setMap(conn, map); |
482 conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_FULL); |
478 conn->onMapChangedCb(conn->onMapChangedCtx, conn->map, NETCONN_MAPCHANGE_FULL); |