multiplayer_api.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #include "core/io/multiplayer_api.h"
  2. #include "core/io/marshalls.h"
  3. #include "scene/main/node.h"
  4. void MultiplayerAPI::poll() {
  5. if (!network_peer.is_valid() || network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED)
  6. return;
  7. network_peer->poll();
  8. if (!network_peer.is_valid()) //it's possible that polling might have resulted in a disconnection, so check here
  9. return;
  10. while (network_peer->get_available_packet_count()) {
  11. int sender = network_peer->get_packet_peer();
  12. const uint8_t *packet;
  13. int len;
  14. Error err = network_peer->get_packet(&packet, len);
  15. if (err != OK) {
  16. ERR_PRINT("Error getting packet!");
  17. }
  18. rpc_sender_id = sender;
  19. _process_packet(sender, packet, len);
  20. rpc_sender_id = 0;
  21. if (!network_peer.is_valid()) {
  22. break; //it's also possible that a packet or RPC caused a disconnection, so also check here
  23. }
  24. }
  25. }
  26. void MultiplayerAPI::clear() {
  27. connected_peers.clear();
  28. path_get_cache.clear();
  29. path_send_cache.clear();
  30. last_send_cache_id = 1;
  31. }
  32. void MultiplayerAPI::set_root_node(Node *p_node) {
  33. root_node = p_node;
  34. }
  35. void MultiplayerAPI::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer) {
  36. if (network_peer.is_valid()) {
  37. network_peer->disconnect("peer_connected", this, "_add_peer");
  38. network_peer->disconnect("peer_disconnected", this, "_del_peer");
  39. network_peer->disconnect("connection_succeeded", this, "_connected_to_server");
  40. network_peer->disconnect("connection_failed", this, "_connection_failed");
  41. network_peer->disconnect("server_disconnected", this, "_server_disconnected");
  42. clear();
  43. }
  44. network_peer = p_peer;
  45. ERR_EXPLAIN("Supplied NetworkedNetworkPeer must be connecting or connected.");
  46. ERR_FAIL_COND(p_peer.is_valid() && p_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED);
  47. if (network_peer.is_valid()) {
  48. network_peer->connect("peer_connected", this, "_add_peer");
  49. network_peer->connect("peer_disconnected", this, "_del_peer");
  50. network_peer->connect("connection_succeeded", this, "_connected_to_server");
  51. network_peer->connect("connection_failed", this, "_connection_failed");
  52. network_peer->connect("server_disconnected", this, "_server_disconnected");
  53. }
  54. }
  55. Ref<NetworkedMultiplayerPeer> MultiplayerAPI::get_network_peer() const {
  56. return network_peer;
  57. }
  58. void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) {
  59. ERR_FAIL_COND(root_node == NULL);
  60. ERR_FAIL_COND(p_packet_len < 5);
  61. uint8_t packet_type = p_packet[0];
  62. switch (packet_type) {
  63. case NETWORK_COMMAND_SIMPLIFY_PATH: {
  64. _process_simplify_path(p_from, p_packet, p_packet_len);
  65. } break;
  66. case NETWORK_COMMAND_CONFIRM_PATH: {
  67. _process_confirm_path(p_from, p_packet, p_packet_len);
  68. } break;
  69. case NETWORK_COMMAND_REMOTE_CALL:
  70. case NETWORK_COMMAND_REMOTE_SET: {
  71. ERR_FAIL_COND(p_packet_len < 6);
  72. Node *node = _process_get_node(p_from, p_packet, p_packet_len);
  73. ERR_FAIL_COND(node == NULL);
  74. //detect cstring end
  75. int len_end = 5;
  76. for (; len_end < p_packet_len; len_end++) {
  77. if (p_packet[len_end] == 0) {
  78. break;
  79. }
  80. }
  81. ERR_FAIL_COND(len_end >= p_packet_len);
  82. StringName name = String::utf8((const char *)&p_packet[5]);
  83. if (packet_type == NETWORK_COMMAND_REMOTE_CALL) {
  84. _process_rpc(node, name, p_from, p_packet, p_packet_len, len_end + 1);
  85. } else {
  86. _process_rset(node, name, p_from, p_packet, p_packet_len, len_end + 1);
  87. }
  88. } break;
  89. }
  90. }
  91. Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, int p_packet_len) {
  92. uint32_t target = decode_uint32(&p_packet[1]);
  93. Node *node = NULL;
  94. if (target & 0x80000000) {
  95. //use full path (not cached yet)
  96. int ofs = target & 0x7FFFFFFF;
  97. ERR_FAIL_COND_V(ofs >= p_packet_len, NULL);
  98. String paths;
  99. paths.parse_utf8((const char *)&p_packet[ofs], p_packet_len - ofs);
  100. NodePath np = paths;
  101. node = root_node->get_node(np);
  102. if (!node)
  103. ERR_PRINTS("Failed to get path from RPC: " + String(np));
  104. } else {
  105. //use cached path
  106. int id = target;
  107. Map<int, PathGetCache>::Element *E = path_get_cache.find(p_from);
  108. ERR_FAIL_COND_V(!E, NULL);
  109. Map<int, PathGetCache::NodeInfo>::Element *F = E->get().nodes.find(id);
  110. ERR_FAIL_COND_V(!F, NULL);
  111. PathGetCache::NodeInfo *ni = &F->get();
  112. //do proper caching later
  113. node = root_node->get_node(ni->path);
  114. if (!node)
  115. ERR_PRINTS("Failed to get cached path from RPC: " + String(ni->path));
  116. }
  117. return node;
  118. }
  119. void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
  120. if (!p_node->can_call_rpc(p_name, p_from))
  121. return;
  122. ERR_FAIL_COND(p_offset >= p_packet_len);
  123. int argc = p_packet[p_offset];
  124. Vector<Variant> args;
  125. Vector<const Variant *> argp;
  126. args.resize(argc);
  127. argp.resize(argc);
  128. p_offset++;
  129. for (int i = 0; i < argc; i++) {
  130. ERR_FAIL_COND(p_offset >= p_packet_len);
  131. int vlen;
  132. Error err = decode_variant(args[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen);
  133. ERR_FAIL_COND(err != OK);
  134. //args[i]=p_packet[3+i];
  135. argp[i] = &args[i];
  136. p_offset += vlen;
  137. }
  138. Variant::CallError ce;
  139. p_node->call(p_name, (const Variant **)argp.ptr(), argc, ce);
  140. if (ce.error != Variant::CallError::CALL_OK) {
  141. String error = Variant::get_call_error_text(p_node, p_name, (const Variant **)argp.ptr(), argc, ce);
  142. error = "RPC - " + error;
  143. ERR_PRINTS(error);
  144. }
  145. }
  146. void MultiplayerAPI::_process_rset(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
  147. if (!p_node->can_call_rset(p_name, p_from))
  148. return;
  149. ERR_FAIL_COND(p_offset >= p_packet_len);
  150. Variant value;
  151. decode_variant(value, &p_packet[p_offset], p_packet_len - p_offset);
  152. bool valid;
  153. p_node->set(p_name, value, &valid);
  154. if (!valid) {
  155. String error = "Error setting remote property '" + String(p_name) + "', not found in object of type " + p_node->get_class();
  156. ERR_PRINTS(error);
  157. }
  158. }
  159. void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  160. ERR_FAIL_COND(p_packet_len < 5);
  161. int id = decode_uint32(&p_packet[1]);
  162. String paths;
  163. paths.parse_utf8((const char *)&p_packet[5], p_packet_len - 5);
  164. NodePath path = paths;
  165. if (!path_get_cache.has(p_from)) {
  166. path_get_cache[p_from] = PathGetCache();
  167. }
  168. PathGetCache::NodeInfo ni;
  169. ni.path = path;
  170. ni.instance = 0;
  171. path_get_cache[p_from].nodes[id] = ni;
  172. //send ack
  173. //encode path
  174. CharString pname = String(path).utf8();
  175. int len = encode_cstring(pname.get_data(), NULL);
  176. Vector<uint8_t> packet;
  177. packet.resize(1 + len);
  178. packet[0] = NETWORK_COMMAND_CONFIRM_PATH;
  179. encode_cstring(pname.get_data(), &packet[1]);
  180. network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  181. network_peer->set_target_peer(p_from);
  182. network_peer->put_packet(packet.ptr(), packet.size());
  183. }
  184. void MultiplayerAPI::_process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  185. String paths;
  186. paths.parse_utf8((const char *)&p_packet[1], p_packet_len - 1);
  187. NodePath path = paths;
  188. PathSentCache *psc = path_send_cache.getptr(path);
  189. ERR_FAIL_COND(!psc);
  190. Map<int, bool>::Element *E = psc->confirmed_peers.find(p_from);
  191. ERR_FAIL_COND(!E);
  192. E->get() = true;
  193. }
  194. bool MultiplayerAPI::_send_confirm_path(NodePath p_path, PathSentCache *psc, int p_target) {
  195. bool has_all_peers = true;
  196. List<int> peers_to_add; //if one is missing, take note to add it
  197. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  198. if (p_target < 0 && E->get() == -p_target)
  199. continue; //continue, excluded
  200. if (p_target > 0 && E->get() != p_target)
  201. continue; //continue, not for this peer
  202. Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get());
  203. if (!F || F->get() == false) {
  204. //path was not cached, or was cached but is unconfirmed
  205. if (!F) {
  206. //not cached at all, take note
  207. peers_to_add.push_back(E->get());
  208. }
  209. has_all_peers = false;
  210. }
  211. }
  212. //those that need to be added, send a message for this
  213. for (List<int>::Element *E = peers_to_add.front(); E; E = E->next()) {
  214. //encode function name
  215. CharString pname = String(p_path).utf8();
  216. int len = encode_cstring(pname.get_data(), NULL);
  217. Vector<uint8_t> packet;
  218. packet.resize(1 + 4 + len);
  219. packet[0] = NETWORK_COMMAND_SIMPLIFY_PATH;
  220. encode_uint32(psc->id, &packet[1]);
  221. encode_cstring(pname.get_data(), &packet[5]);
  222. network_peer->set_target_peer(E->get()); //to all of you
  223. network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  224. network_peer->put_packet(packet.ptr(), packet.size());
  225. psc->confirmed_peers.insert(E->get(), false); //insert into confirmed, but as false since it was not confirmed
  226. }
  227. return has_all_peers;
  228. }
  229. void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p_set, const StringName &p_name, const Variant **p_arg, int p_argcount) {
  230. if (network_peer.is_null()) {
  231. ERR_EXPLAIN("Attempt to remote call/set when networking is not active in SceneTree.");
  232. ERR_FAIL();
  233. }
  234. if (network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_CONNECTING) {
  235. ERR_EXPLAIN("Attempt to remote call/set when networking is not connected yet in SceneTree.");
  236. ERR_FAIL();
  237. }
  238. if (network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED) {
  239. ERR_EXPLAIN("Attempt to remote call/set when networking is disconnected.");
  240. ERR_FAIL();
  241. }
  242. if (p_argcount > 255) {
  243. ERR_EXPLAIN("Too many arguments >255.");
  244. ERR_FAIL();
  245. }
  246. if (p_to != 0 && !connected_peers.has(ABS(p_to))) {
  247. if (p_to == network_peer->get_unique_id()) {
  248. ERR_EXPLAIN("Attempt to remote call/set yourself! unique ID: " + itos(network_peer->get_unique_id()));
  249. } else {
  250. ERR_EXPLAIN("Attempt to remote call unexisting ID: " + itos(p_to));
  251. }
  252. ERR_FAIL();
  253. }
  254. NodePath from_path = (root_node->get_path()).rel_path_to(p_from->get_path());
  255. ERR_FAIL_COND(from_path.is_empty());
  256. //see if the path is cached
  257. PathSentCache *psc = path_send_cache.getptr(from_path);
  258. if (!psc) {
  259. //path is not cached, create
  260. path_send_cache[from_path] = PathSentCache();
  261. psc = path_send_cache.getptr(from_path);
  262. psc->id = last_send_cache_id++;
  263. }
  264. //create base packet, lots of hardcode because it must be tight
  265. int ofs = 0;
  266. #define MAKE_ROOM(m_amount) \
  267. if (packet_cache.size() < m_amount) packet_cache.resize(m_amount);
  268. //encode type
  269. MAKE_ROOM(1);
  270. packet_cache[0] = p_set ? NETWORK_COMMAND_REMOTE_SET : NETWORK_COMMAND_REMOTE_CALL;
  271. ofs += 1;
  272. //encode ID
  273. MAKE_ROOM(ofs + 4);
  274. encode_uint32(psc->id, &(packet_cache[ofs]));
  275. ofs += 4;
  276. //encode function name
  277. CharString name = String(p_name).utf8();
  278. int len = encode_cstring(name.get_data(), NULL);
  279. MAKE_ROOM(ofs + len);
  280. encode_cstring(name.get_data(), &(packet_cache[ofs]));
  281. ofs += len;
  282. if (p_set) {
  283. //set argument
  284. Error err = encode_variant(*p_arg[0], NULL, len);
  285. ERR_FAIL_COND(err != OK);
  286. MAKE_ROOM(ofs + len);
  287. encode_variant(*p_arg[0], &(packet_cache[ofs]), len);
  288. ofs += len;
  289. } else {
  290. //call arguments
  291. MAKE_ROOM(ofs + 1);
  292. packet_cache[ofs] = p_argcount;
  293. ofs += 1;
  294. for (int i = 0; i < p_argcount; i++) {
  295. Error err = encode_variant(*p_arg[i], NULL, len);
  296. ERR_FAIL_COND(err != OK);
  297. MAKE_ROOM(ofs + len);
  298. encode_variant(*p_arg[i], &(packet_cache[ofs]), len);
  299. ofs += len;
  300. }
  301. }
  302. //see if all peers have cached path (is so, call can be fast)
  303. bool has_all_peers = _send_confirm_path(from_path, psc, p_to);
  304. //take chance and set transfer mode, since all send methods will use it
  305. network_peer->set_transfer_mode(p_unreliable ? NetworkedMultiplayerPeer::TRANSFER_MODE_UNRELIABLE : NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  306. if (has_all_peers) {
  307. //they all have verified paths, so send fast
  308. network_peer->set_target_peer(p_to); //to all of you
  309. network_peer->put_packet(packet_cache.ptr(), ofs); //a message with love
  310. } else {
  311. //not all verified path, so send one by one
  312. //apend path at the end, since we will need it for some packets
  313. CharString pname = String(from_path).utf8();
  314. int path_len = encode_cstring(pname.get_data(), NULL);
  315. MAKE_ROOM(ofs + path_len);
  316. encode_cstring(pname.get_data(), &(packet_cache[ofs]));
  317. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  318. if (p_to < 0 && E->get() == -p_to)
  319. continue; //continue, excluded
  320. if (p_to > 0 && E->get() != p_to)
  321. continue; //continue, not for this peer
  322. Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get());
  323. ERR_CONTINUE(!F); //should never happen
  324. network_peer->set_target_peer(E->get()); //to this one specifically
  325. if (F->get() == true) {
  326. //this one confirmed path, so use id
  327. encode_uint32(psc->id, &(packet_cache[1]));
  328. network_peer->put_packet(packet_cache.ptr(), ofs);
  329. } else {
  330. //this one did not confirm path yet, so use entire path (sorry!)
  331. encode_uint32(0x80000000 | ofs, &(packet_cache[1])); //offset to path and flag
  332. network_peer->put_packet(packet_cache.ptr(), ofs + path_len);
  333. }
  334. }
  335. }
  336. }
  337. void MultiplayerAPI::_add_peer(int p_id) {
  338. connected_peers.insert(p_id);
  339. path_get_cache.insert(p_id, PathGetCache());
  340. emit_signal("network_peer_connected", p_id);
  341. }
  342. void MultiplayerAPI::_del_peer(int p_id) {
  343. connected_peers.erase(p_id);
  344. path_get_cache.erase(p_id); //I no longer need your cache, sorry
  345. emit_signal("network_peer_disconnected", p_id);
  346. }
  347. void MultiplayerAPI::_connected_to_server() {
  348. emit_signal("connected_to_server");
  349. }
  350. void MultiplayerAPI::_connection_failed() {
  351. emit_signal("connection_failed");
  352. }
  353. void MultiplayerAPI::_server_disconnected() {
  354. emit_signal("server_disconnected");
  355. }
  356. bool _should_call_native(Node::RPCMode mode, bool is_master, bool &r_skip_rpc) {
  357. switch (mode) {
  358. case Node::RPC_MODE_DISABLED: {
  359. //do nothing
  360. } break;
  361. case Node::RPC_MODE_REMOTE: {
  362. //do nothing also, no need to call local
  363. } break;
  364. case Node::RPC_MODE_SYNC: {
  365. //call it, sync always results in call
  366. return true;
  367. } break;
  368. case Node::RPC_MODE_MASTER: {
  369. if (is_master)
  370. r_skip_rpc = true; //no other master so..
  371. return is_master;
  372. } break;
  373. case Node::RPC_MODE_SLAVE: {
  374. return !is_master;
  375. } break;
  376. }
  377. return false;
  378. }
  379. bool _should_call_script(ScriptInstance::RPCMode mode, bool is_master, bool &r_skip_rpc) {
  380. switch (mode) {
  381. case ScriptInstance::RPC_MODE_DISABLED: {
  382. //do nothing
  383. } break;
  384. case ScriptInstance::RPC_MODE_REMOTE: {
  385. //do nothing also, no need to call local
  386. } break;
  387. case ScriptInstance::RPC_MODE_SYNC: {
  388. //call it, sync always results in call
  389. return true;
  390. } break;
  391. case ScriptInstance::RPC_MODE_MASTER: {
  392. if (is_master)
  393. r_skip_rpc = true; //no other master so..
  394. return is_master;
  395. } break;
  396. case ScriptInstance::RPC_MODE_SLAVE: {
  397. return !is_master;
  398. } break;
  399. }
  400. return false;
  401. }
  402. void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  403. ERR_FAIL_COND(!p_node->is_inside_tree());
  404. ERR_FAIL_COND(!network_peer.is_valid());
  405. int node_id = network_peer->get_unique_id();
  406. bool skip_rpc = false;
  407. bool call_local_native = false;
  408. bool call_local_script = false;
  409. bool is_master = p_node->is_network_master();
  410. if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
  411. //check that send mode can use local call
  412. const Map<StringName, Node::RPCMode>::Element *E = p_node->get_node_rpc_mode(p_method);
  413. if (E) {
  414. call_local_native = _should_call_native(E->get(), is_master, skip_rpc);
  415. }
  416. if (call_local_native) {
  417. // done below
  418. } else if (p_node->get_script_instance()) {
  419. //attempt with script
  420. ScriptInstance::RPCMode rpc_mode = p_node->get_script_instance()->get_rpc_mode(p_method);
  421. call_local_script = _should_call_script(rpc_mode, is_master, skip_rpc);
  422. }
  423. }
  424. if (!skip_rpc) {
  425. _send_rpc(p_node, p_peer_id, p_unreliable, false, p_method, p_arg, p_argcount);
  426. }
  427. if (call_local_native) {
  428. Variant::CallError ce;
  429. p_node->call(p_method, p_arg, p_argcount, ce);
  430. if (ce.error != Variant::CallError::CALL_OK) {
  431. String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
  432. error = "rpc() aborted in local call: - " + error;
  433. ERR_PRINTS(error);
  434. return;
  435. }
  436. }
  437. if (call_local_script) {
  438. Variant::CallError ce;
  439. ce.error = Variant::CallError::CALL_OK;
  440. p_node->get_script_instance()->call(p_method, p_arg, p_argcount, ce);
  441. if (ce.error != Variant::CallError::CALL_OK) {
  442. String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
  443. error = "rpc() aborted in script local call: - " + error;
  444. ERR_PRINTS(error);
  445. return;
  446. }
  447. }
  448. }
  449. void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value) {
  450. ERR_FAIL_COND(!p_node->is_inside_tree());
  451. ERR_FAIL_COND(!network_peer.is_valid());
  452. int node_id = network_peer->get_unique_id();
  453. bool is_master = p_node->is_network_master();
  454. bool skip_rset = false;
  455. if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
  456. //check that send mode can use local call
  457. bool set_local = false;
  458. const Map<StringName, Node::RPCMode>::Element *E = p_node->get_node_rset_mode(p_property);
  459. if (E) {
  460. set_local = _should_call_native(E->get(), is_master, skip_rset);
  461. }
  462. if (set_local) {
  463. bool valid;
  464. p_node->set(p_property, p_value, &valid);
  465. if (!valid) {
  466. String error = "rset() aborted in local set, property not found: - " + String(p_property);
  467. ERR_PRINTS(error);
  468. return;
  469. }
  470. } else if (p_node->get_script_instance()) {
  471. //attempt with script
  472. ScriptInstance::RPCMode rpc_mode = p_node->get_script_instance()->get_rset_mode(p_property);
  473. set_local = _should_call_script(rpc_mode, is_master, skip_rset);
  474. if (set_local) {
  475. bool valid = p_node->get_script_instance()->set(p_property, p_value);
  476. if (!valid) {
  477. String error = "rset() aborted in local script set, property not found: - " + String(p_property);
  478. ERR_PRINTS(error);
  479. return;
  480. }
  481. }
  482. }
  483. }
  484. if (skip_rset)
  485. return;
  486. const Variant *vptr = &p_value;
  487. _send_rpc(p_node, p_peer_id, p_unreliable, true, p_property, &vptr, 1);
  488. }
  489. int MultiplayerAPI::get_network_unique_id() const {
  490. ERR_FAIL_COND_V(!network_peer.is_valid(), 0);
  491. return network_peer->get_unique_id();
  492. }
  493. bool MultiplayerAPI::is_network_server() const {
  494. ERR_FAIL_COND_V(!network_peer.is_valid(), false);
  495. return network_peer->is_server();
  496. }
  497. void MultiplayerAPI::set_refuse_new_network_connections(bool p_refuse) {
  498. ERR_FAIL_COND(!network_peer.is_valid());
  499. network_peer->set_refuse_new_connections(p_refuse);
  500. }
  501. bool MultiplayerAPI::is_refusing_new_network_connections() const {
  502. ERR_FAIL_COND_V(!network_peer.is_valid(), false);
  503. return network_peer->is_refusing_new_connections();
  504. }
  505. Vector<int> MultiplayerAPI::get_network_connected_peers() const {
  506. ERR_FAIL_COND_V(!network_peer.is_valid(), Vector<int>());
  507. Vector<int> ret;
  508. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  509. ret.push_back(E->get());
  510. }
  511. return ret;
  512. }
  513. void MultiplayerAPI::_bind_methods() {
  514. ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node);
  515. ClassDB::bind_method(D_METHOD("has_network_peer"), &MultiplayerAPI::has_network_peer);
  516. ClassDB::bind_method(D_METHOD("get_network_peer"), &MultiplayerAPI::get_network_peer);
  517. ClassDB::bind_method(D_METHOD("get_network_unique_id"), &MultiplayerAPI::get_network_unique_id);
  518. ClassDB::bind_method(D_METHOD("is_network_server"), &MultiplayerAPI::is_network_server);
  519. ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &MultiplayerAPI::get_rpc_sender_id);
  520. ClassDB::bind_method(D_METHOD("_add_peer", "id"), &MultiplayerAPI::_add_peer);
  521. ClassDB::bind_method(D_METHOD("_del_peer", "id"), &MultiplayerAPI::_del_peer);
  522. ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &MultiplayerAPI::set_network_peer);
  523. ClassDB::bind_method(D_METHOD("poll"), &MultiplayerAPI::poll);
  524. ClassDB::bind_method(D_METHOD("clear"), &MultiplayerAPI::clear);
  525. ClassDB::bind_method(D_METHOD("_connected_to_server"), &MultiplayerAPI::_connected_to_server);
  526. ClassDB::bind_method(D_METHOD("_connection_failed"), &MultiplayerAPI::_connection_failed);
  527. ClassDB::bind_method(D_METHOD("_server_disconnected"), &MultiplayerAPI::_server_disconnected);
  528. ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &MultiplayerAPI::get_network_connected_peers);
  529. ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &MultiplayerAPI::set_refuse_new_network_connections);
  530. ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &MultiplayerAPI::is_refusing_new_network_connections);
  531. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
  532. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer");
  533. ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id")));
  534. ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id")));
  535. ADD_SIGNAL(MethodInfo("connected_to_server"));
  536. ADD_SIGNAL(MethodInfo("connection_failed"));
  537. ADD_SIGNAL(MethodInfo("server_disconnected"));
  538. }
  539. MultiplayerAPI::MultiplayerAPI() {
  540. clear();
  541. }
  542. MultiplayerAPI::~MultiplayerAPI() {
  543. clear();
  544. }