1/**************************************************************************/
2/* export_plugin.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31#ifndef WEB_EXPORT_PLUGIN_H
32#define WEB_EXPORT_PLUGIN_H
33
34#include "editor_http_server.h"
35
36#include "core/config/project_settings.h"
37#include "core/io/image_loader.h"
38#include "core/io/stream_peer_tls.h"
39#include "core/io/tcp_server.h"
40#include "core/io/zip_io.h"
41#include "editor/editor_node.h"
42#include "editor/editor_string_names.h"
43#include "editor/export/editor_export_platform.h"
44#include "main/splash.gen.h"
45
46class EditorExportPlatformWeb : public EditorExportPlatform {
47 GDCLASS(EditorExportPlatformWeb, EditorExportPlatform);
48
49 Ref<ImageTexture> ;
50 Ref<ImageTexture> run_icon;
51 Ref<ImageTexture> stop_icon;
52 int menu_options = 0;
53
54 Ref<EditorHTTPServer> server;
55 bool server_quit = false;
56 Mutex server_lock;
57 Thread server_thread;
58
59 String _get_template_name(bool p_extension, bool p_debug) const {
60 String name = "web";
61 if (p_extension) {
62 name += "_dlink";
63 }
64 if (p_debug) {
65 name += "_debug.zip";
66 } else {
67 name += "_release.zip";
68 }
69 return name;
70 }
71
72 Ref<Image> _get_project_icon() const {
73 Ref<Image> icon;
74 icon.instantiate();
75 const String icon_path = String(GLOBAL_GET("application/config/icon")).strip_edges();
76 if (icon_path.is_empty() || ImageLoader::load_image(icon_path, icon) != OK) {
77 return EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("DefaultProjectIcon"), EditorStringName(EditorIcons))->get_image();
78 }
79 return icon;
80 }
81
82 Ref<Image> _get_project_splash() const {
83 Ref<Image> splash;
84 splash.instantiate();
85 const String splash_path = String(GLOBAL_GET("application/boot_splash/image")).strip_edges();
86 if (splash_path.is_empty() || ImageLoader::load_image(splash_path, splash) != OK) {
87 return Ref<Image>(memnew(Image(boot_splash_png)));
88 }
89 return splash;
90 }
91
92 Error _extract_template(const String &p_template, const String &p_dir, const String &p_name, bool pwa);
93 void _replace_strings(HashMap<String, String> p_replaces, Vector<uint8_t> &r_template);
94 void _fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug, int p_flags, const Vector<SharedObject> p_shared_objects, const Dictionary &p_file_sizes);
95 Error _add_manifest_icon(const String &p_path, const String &p_icon, int p_size, Array &r_arr);
96 Error _build_pwa(const Ref<EditorExportPreset> &p_preset, const String p_path, const Vector<SharedObject> &p_shared_objects);
97 Error _write_or_error(const uint8_t *p_content, int p_len, String p_path);
98
99 static void _server_thread_poll(void *data);
100
101public:
102 virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;
103
104 virtual void get_export_options(List<ExportOption> *r_options) const override;
105
106 virtual String get_name() const override;
107 virtual String get_os_name() const override;
108 virtual Ref<Texture2D> get_logo() const override;
109
110 virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug = false) const override;
111 virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const override;
112 virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override;
113 virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
114
115 virtual bool poll_export() override;
116 virtual int get_options_count() const override;
117 virtual String get_option_label(int p_index) const override { return p_index ? TTR("Stop HTTP Server") : TTR("Run in Browser"); }
118 virtual String get_option_tooltip(int p_index) const override { return p_index ? TTR("Stop HTTP Server") : TTR("Run exported HTML in the system's default browser."); }
119 virtual Ref<ImageTexture> get_option_icon(int p_index) const override;
120 virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_option, int p_debug_flags) override;
121 virtual Ref<Texture2D> get_run_icon() const override;
122
123 virtual void get_platform_features(List<String> *r_features) const override {
124 r_features->push_back("web");
125 r_features->push_back(get_os_name().to_lower());
126 }
127
128 virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) override {
129 }
130
131 String get_debug_protocol() const override { return "ws://"; }
132
133 EditorExportPlatformWeb();
134 ~EditorExportPlatformWeb();
135};
136
137#endif // WEB_EXPORT_PLUGIN_H
138