1 /*************************************************************************/
2 /*  script_debugger_remote.h                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #ifndef SCRIPT_DEBUGGER_REMOTE_H
31 #define SCRIPT_DEBUGGER_REMOTE_H
32 
33 #include "io/packet_peer.h"
34 #include "io/stream_peer_tcp.h"
35 #include "list.h"
36 #include "script_language.h"
37 
38 class ScriptDebuggerRemote : public ScriptDebugger {
39 
40 	struct Message {
41 
42 		String message;
43 		Array data;
44 	};
45 
46 	struct ProfileInfoSort {
47 
operatorProfileInfoSort48 		bool operator()(ScriptLanguage::ProfilingInfo *A, ScriptLanguage::ProfilingInfo *B) const {
49 			return A->total_time < B->total_time;
50 		}
51 	};
52 
53 	Vector<ScriptLanguage::ProfilingInfo> profile_info;
54 	Vector<ScriptLanguage::ProfilingInfo *> profile_info_ptrs;
55 
56 	Map<StringName, int> profiler_function_signature_map;
57 	float frame_time, idle_time, fixed_time, fixed_frame_time;
58 
59 	bool profiling;
60 	int max_frame_functions;
61 	bool skip_profile_frame;
62 	bool reload_all_scripts;
63 
64 	Ref<StreamPeerTCP> tcp_client;
65 	Ref<PacketPeerStream> packet_peer_stream;
66 
67 	uint64_t last_perf_time;
68 	Object *performance;
69 	bool requested_quit;
70 	Mutex *mutex;
71 
72 	struct OutputError {
73 
74 		int hr;
75 		int min;
76 		int sec;
77 		int msec;
78 		String source_file;
79 		String source_func;
80 		int source_line;
81 		String error;
82 		String error_descr;
83 		bool warning;
84 		Array callstack;
85 	};
86 
87 	List<String> output_strings;
88 	List<Message> messages;
89 	List<OutputError> errors;
90 
91 	int max_cps;
92 	int char_count;
93 	uint64_t last_msec;
94 	uint64_t msec_count;
95 
96 	bool locking; //hack to avoid a deadloop
97 	static void _print_handler(void *p_this, const String &p_string);
98 
99 	PrintHandlerList phl;
100 
101 	void _get_output();
102 	void _poll_events();
103 	uint32_t poll_every;
104 
105 	bool _parse_live_edit(const Array &p_command);
106 
107 	RequestSceneTreeMessageFunc request_scene_tree;
108 	void *request_scene_tree_ud;
109 
110 	void _set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value);
111 	void _send_object_id(ObjectID p_id);
112 	void _send_video_memory();
113 	LiveEditFuncs *live_edit_funcs;
114 
115 	ErrorHandlerList eh;
116 	static void _err_handler(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type);
117 
118 	void _send_profiling_data(bool p_for_frame);
119 
120 	int _serialize_variant(const Variant &var, const PropertyInfo &p_info, DVector<uint8_t> &buff);
121 	DVector<uint8_t> _serialize(const Variant &var, const PropertyInfo &p_info);
122 
123 	struct FrameData {
124 
125 		StringName name;
126 		Array data;
127 	};
128 
129 	Vector<FrameData> profile_frame_data;
130 
131 public:
132 	struct ResourceUsage {
133 
134 		String path;
135 		String format;
136 		String type;
137 		RID id;
138 		int vram;
139 		bool operator<(const ResourceUsage &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
140 	};
141 
142 	typedef void (*ResourceUsageFunc)(List<ResourceUsage> *);
143 
144 	static ResourceUsageFunc resource_usage_func;
145 
146 	Error connect_to_host(const String &p_host, uint16_t p_port);
147 	virtual void debug(ScriptLanguage *p_script, bool p_can_continue = true);
148 	virtual void idle_poll();
149 	virtual void line_poll();
150 
is_remote()151 	virtual bool is_remote() const { return true; }
152 	virtual void request_quit();
153 
154 	virtual void send_message(const String &p_message, const Array &p_args);
155 
156 	virtual void set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata);
157 	virtual void set_live_edit_funcs(LiveEditFuncs *p_funcs);
158 
159 	virtual bool is_profiling() const;
160 	virtual void add_profiling_frame_data(const StringName &p_name, const Array &p_data);
161 
162 	virtual void profiling_start();
163 	virtual void profiling_end();
164 	virtual void profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_fixed_time, float p_fixed_frame_time);
165 
166 	ScriptDebuggerRemote();
167 	~ScriptDebuggerRemote();
168 };
169 
170 #endif // SCRIPT_DEBUGGER_REMOTE_H
171