1 /**
2 High Level Multiplayer API.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.multiplayerapi;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.meta;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.object;
22 import godot.classdb;
23 import godot.reference;
24 import godot.networkedmultiplayerpeer;
25 /**
26 High Level Multiplayer API.
27 
28 This class implements most of the logic behind the high level multiplayer API.
29 By default, $(D SceneTree) has a reference to this class that is used to provide multiplayer capabilities (i.e. RPC/RSET) across the whole scene.
30 It is possible to override the MultiplayerAPI instance used by specific Nodes by setting the $(D Node.customMultiplayer) property, effectively allowing to run both client and server in the same scene.
31 */
32 @GodotBaseClass struct MultiplayerAPI
33 {
34 	enum string _GODOT_internal_name = "MultiplayerAPI";
35 public:
36 @nogc nothrow:
37 	union { godot_object _godot_object; Reference _GODOT_base; }
38 	alias _GODOT_base this;
39 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
40 	package(godot) __gshared bool _classBindingInitialized = false;
41 	package(godot) static struct _classBinding
42 	{
43 		__gshared:
44 		@GodotName("set_root_node") GodotMethod!(void, GodotObject) setRootNode;
45 		@GodotName("send_bytes") GodotMethod!(GodotError, PoolByteArray, long, long) sendBytes;
46 		@GodotName("has_network_peer") GodotMethod!(bool) hasNetworkPeer;
47 		@GodotName("get_network_peer") GodotMethod!(NetworkedMultiplayerPeer) getNetworkPeer;
48 		@GodotName("get_network_unique_id") GodotMethod!(long) getNetworkUniqueId;
49 		@GodotName("is_network_server") GodotMethod!(bool) isNetworkServer;
50 		@GodotName("get_rpc_sender_id") GodotMethod!(long) getRpcSenderId;
51 		@GodotName("_add_peer") GodotMethod!(void, long) _addPeer;
52 		@GodotName("_del_peer") GodotMethod!(void, long) _delPeer;
53 		@GodotName("set_network_peer") GodotMethod!(void, NetworkedMultiplayerPeer) setNetworkPeer;
54 		@GodotName("poll") GodotMethod!(void) poll;
55 		@GodotName("clear") GodotMethod!(void) clear;
56 		@GodotName("_connected_to_server") GodotMethod!(void) _connectedToServer;
57 		@GodotName("_connection_failed") GodotMethod!(void) _connectionFailed;
58 		@GodotName("_server_disconnected") GodotMethod!(void) _serverDisconnected;
59 		@GodotName("get_network_connected_peers") GodotMethod!(PoolIntArray) getNetworkConnectedPeers;
60 		@GodotName("set_refuse_new_network_connections") GodotMethod!(void, bool) setRefuseNewNetworkConnections;
61 		@GodotName("is_refusing_new_network_connections") GodotMethod!(bool) isRefusingNewNetworkConnections;
62 	}
63 	bool opEquals(in MultiplayerAPI other) const { return _godot_object.ptr is other._godot_object.ptr; }
64 	MultiplayerAPI opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; }
65 	bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; }
66 	mixin baseCasts;
67 	static MultiplayerAPI _new()
68 	{
69 		static godot_class_constructor constructor;
70 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("MultiplayerAPI");
71 		if(constructor is null) return typeof(this).init;
72 		return cast(MultiplayerAPI)(constructor());
73 	}
74 	@disable new(size_t s);
75 	/// 
76 	enum RPCMode : int
77 	{
78 		/**
79 		Used with $(D Node.rpcConfig) or $(D Node.rsetConfig) to disable a method or property for all RPC calls, making it unavailable. Default for all methods.
80 		*/
81 		rpcModeDisabled = 0,
82 		/**
83 		Used with $(D Node.rpcConfig) or $(D Node.rsetConfig) to set a method to be called or a property to be changed only on the remote end, not locally. Analogous to the `remote` keyword. Calls and property changes are accepted from all remote peers, no matter if they are node's master or puppets.
84 		*/
85 		rpcModeRemote = 1,
86 		/**
87 		Used with $(D Node.rpcConfig) or $(D Node.rsetConfig) to set a method to be called or a property to be changed only on the network master for this node. Analogous to the `master` keyword. Only accepts calls or property changes from the node's network puppets, see $(D Node.setNetworkMaster).
88 		*/
89 		rpcModeMaster = 2,
90 		/**
91 		Used with $(D Node.rpcConfig) or $(D Node.rsetConfig) to set a method to be called or a property to be changed only on puppets for this node. Analogous to the `puppet` keyword. Only accepts calls or property changes from the node's network master, see $(D Node.setNetworkMaster).
92 		*/
93 		rpcModePuppet = 3,
94 		/**
95 		Deprecated. Use `RPC_MODE_PUPPET` instead. Analogous to the `slave` keyword.
96 		*/
97 		rpcModeSlave = 3,
98 		/**
99 		Deprecated. Use `RPC_MODE_REMOTESYNC` instead. Analogous to the `sync` keyword.
100 		*/
101 		rpcModeSync = 4,
102 		/**
103 		Behave like `RPC_MODE_REMOTE` but also make the call or property change locally. Analogous to the `remotesync` keyword.
104 		*/
105 		rpcModeRemotesync = 4,
106 		/**
107 		Behave like `RPC_MODE_MASTER` but also make the call or property change locally. Analogous to the `mastersync` keyword.
108 		*/
109 		rpcModeMastersync = 5,
110 		/**
111 		Behave like `RPC_MODE_PUPPET` but also make the call or property change locally. Analogous to the `puppetsync` keyword.
112 		*/
113 		rpcModePuppetsync = 6,
114 	}
115 	/// 
116 	enum Constants : int
117 	{
118 		rpcModeDisabled = 0,
119 		rpcModeRemote = 1,
120 		rpcModeMaster = 2,
121 		rpcModePuppet = 3,
122 		rpcModeSlave = 3,
123 		rpcModeSync = 4,
124 		rpcModeRemotesync = 4,
125 		rpcModeMastersync = 5,
126 		rpcModePuppetsync = 6,
127 	}
128 	/**
129 	Sets the base root node to use for RPCs. Instead of an absolute path, a relative path will be used to find the node upon which the RPC should be executed.
130 	This effectively allows to have different branches of the scene tree to be managed by different MultiplayerAPI, allowing for example to run both client and server in the same scene.
131 	*/
132 	void setRootNode(GodotObject node)
133 	{
134 		checkClassBinding!(typeof(this))();
135 		ptrcall!(void)(_classBinding.setRootNode, _godot_object, node);
136 	}
137 	/**
138 	Sends the given raw `bytes` to a specific peer identified by `id` (see $(D NetworkedMultiplayerPeer.setTargetPeer)). Default ID is `0`, i.e. broadcast to all peers.
139 	*/
140 	GodotError sendBytes(in PoolByteArray bytes, in long id = 0, in long mode = 2)
141 	{
142 		checkClassBinding!(typeof(this))();
143 		return ptrcall!(GodotError)(_classBinding.sendBytes, _godot_object, bytes, id, mode);
144 	}
145 	/**
146 	Returns `true` if there is a $(D networkPeer) set.
147 	*/
148 	bool hasNetworkPeer() const
149 	{
150 		checkClassBinding!(typeof(this))();
151 		return ptrcall!(bool)(_classBinding.hasNetworkPeer, _godot_object);
152 	}
153 	/**
154 	
155 	*/
156 	Ref!NetworkedMultiplayerPeer getNetworkPeer() const
157 	{
158 		checkClassBinding!(typeof(this))();
159 		return ptrcall!(NetworkedMultiplayerPeer)(_classBinding.getNetworkPeer, _godot_object);
160 	}
161 	/**
162 	Returns the unique peer ID of this MultiplayerAPI's $(D networkPeer).
163 	*/
164 	long getNetworkUniqueId() const
165 	{
166 		checkClassBinding!(typeof(this))();
167 		return ptrcall!(long)(_classBinding.getNetworkUniqueId, _godot_object);
168 	}
169 	/**
170 	Returns `true` if this MultiplayerAPI's $(D networkPeer) is in server mode (listening for connections).
171 	*/
172 	bool isNetworkServer() const
173 	{
174 		checkClassBinding!(typeof(this))();
175 		return ptrcall!(bool)(_classBinding.isNetworkServer, _godot_object);
176 	}
177 	/**
178 	Returns the sender's peer ID for the RPC currently being executed.
179 	NOTE: If not inside an RPC this method will return 0.
180 	*/
181 	long getRpcSenderId() const
182 	{
183 		checkClassBinding!(typeof(this))();
184 		return ptrcall!(long)(_classBinding.getRpcSenderId, _godot_object);
185 	}
186 	/**
187 	
188 	*/
189 	void _addPeer(in long id)
190 	{
191 		Array _GODOT_args = Array.empty_array;
192 		_GODOT_args.append(id);
193 		String _GODOT_method_name = String("_add_peer");
194 		this.callv(_GODOT_method_name, _GODOT_args);
195 	}
196 	/**
197 	
198 	*/
199 	void _delPeer(in long id)
200 	{
201 		Array _GODOT_args = Array.empty_array;
202 		_GODOT_args.append(id);
203 		String _GODOT_method_name = String("_del_peer");
204 		this.callv(_GODOT_method_name, _GODOT_args);
205 	}
206 	/**
207 	
208 	*/
209 	void setNetworkPeer(NetworkedMultiplayerPeer peer)
210 	{
211 		checkClassBinding!(typeof(this))();
212 		ptrcall!(void)(_classBinding.setNetworkPeer, _godot_object, peer);
213 	}
214 	/**
215 	Method used for polling the MultiplayerAPI. You only need to worry about this if you are using $(D Node.customMultiplayer) override or you set $(D SceneTree.multiplayerPoll) to `false`. By default $(D SceneTree) will poll its MultiplayerAPI for you.
216 	NOTE: This method results in RPCs and RSETs being called, so they will be executed in the same context of this function (e.g. `_process`, `physics`, $(D Thread)).
217 	*/
218 	void poll()
219 	{
220 		checkClassBinding!(typeof(this))();
221 		ptrcall!(void)(_classBinding.poll, _godot_object);
222 	}
223 	/**
224 	Clears the current MultiplayerAPI network state (you shouldn't call this unless you know what you are doing).
225 	*/
226 	void clear()
227 	{
228 		checkClassBinding!(typeof(this))();
229 		ptrcall!(void)(_classBinding.clear, _godot_object);
230 	}
231 	/**
232 	
233 	*/
234 	void _connectedToServer()
235 	{
236 		Array _GODOT_args = Array.empty_array;
237 		String _GODOT_method_name = String("_connected_to_server");
238 		this.callv(_GODOT_method_name, _GODOT_args);
239 	}
240 	/**
241 	
242 	*/
243 	void _connectionFailed()
244 	{
245 		Array _GODOT_args = Array.empty_array;
246 		String _GODOT_method_name = String("_connection_failed");
247 		this.callv(_GODOT_method_name, _GODOT_args);
248 	}
249 	/**
250 	
251 	*/
252 	void _serverDisconnected()
253 	{
254 		Array _GODOT_args = Array.empty_array;
255 		String _GODOT_method_name = String("_server_disconnected");
256 		this.callv(_GODOT_method_name, _GODOT_args);
257 	}
258 	/**
259 	Returns the peer IDs of all connected peers of this MultiplayerAPI's $(D networkPeer).
260 	*/
261 	PoolIntArray getNetworkConnectedPeers() const
262 	{
263 		checkClassBinding!(typeof(this))();
264 		return ptrcall!(PoolIntArray)(_classBinding.getNetworkConnectedPeers, _godot_object);
265 	}
266 	/**
267 	
268 	*/
269 	void setRefuseNewNetworkConnections(in bool refuse)
270 	{
271 		checkClassBinding!(typeof(this))();
272 		ptrcall!(void)(_classBinding.setRefuseNewNetworkConnections, _godot_object, refuse);
273 	}
274 	/**
275 	
276 	*/
277 	bool isRefusingNewNetworkConnections() const
278 	{
279 		checkClassBinding!(typeof(this))();
280 		return ptrcall!(bool)(_classBinding.isRefusingNewNetworkConnections, _godot_object);
281 	}
282 	/**
283 	If `true` the MultiplayerAPI's $(D networkPeer) refuses new incoming connections.
284 	*/
285 	@property bool refuseNewNetworkConnections()
286 	{
287 		return isRefusingNewNetworkConnections();
288 	}
289 	/// ditto
290 	@property void refuseNewNetworkConnections(bool v)
291 	{
292 		setRefuseNewNetworkConnections(v);
293 	}
294 	/**
295 	The peer object to handle the RPC system (effectively enabling networking when set). Depending on the peer itself, the MultiplayerAPI will become a network server (check with $(D isNetworkServer)) and will set root node's network mode to master (see NETWORK_MODE_* constants in $(D Node)), or it will become a regular peer with root node set to puppet. All child nodes are set to inherit the network mode by default. Handling of networking-related events (connection, disconnection, new clients) is done by connecting to MultiplayerAPI's signals.
296 	*/
297 	@property NetworkedMultiplayerPeer networkPeer()
298 	{
299 		return getNetworkPeer();
300 	}
301 	/// ditto
302 	@property void networkPeer(NetworkedMultiplayerPeer v)
303 	{
304 		setNetworkPeer(v);
305 	}
306 }