From 739c909ad4203d2ee3c008c46ede0b025b9752d5 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 4 Jan 2023 00:44:45 +0100 Subject: [PATCH] Added a simple web server to the static exporter to serve the resulting folder for tests. --- game/StaticMain.gd | 1 + game/StaticMain.tscn | 51 ++++++++++++++++++++++++++--------- game/StaticMainNode.gd | 6 +++++ game/StaticWebServerSimple.gd | 9 +++++++ game/WebRoot.gd | 47 ++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 game/StaticMainNode.gd create mode 100644 game/StaticWebServerSimple.gd create mode 100644 game/WebRoot.gd diff --git a/game/StaticMain.gd b/game/StaticMain.gd index 17af87b..2dbd71e 100644 --- a/game/StaticMain.gd +++ b/game/StaticMain.gd @@ -113,3 +113,4 @@ func _on_ExportButton_pressed() -> void: func _on_OpenFolderButton_pressed() -> void: OS.shell_open(OS.get_user_data_dir()) + diff --git a/game/StaticMain.tscn b/game/StaticMain.tscn index ccf2edb..a29336a 100644 --- a/game/StaticMain.tscn +++ b/game/StaticMain.tscn @@ -1,36 +1,61 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://WebRoot.tscn" type="PackedScene" id=1] [ext_resource path="res://StaticMain.gd" type="Script" id=2] +[ext_resource path="res://StaticWebServerSimple.gd" type="Script" id=3] +[ext_resource path="res://StaticMainNode.gd" type="Script" id=4] +[ext_resource path="res://WebRoot.gd" type="Script" id=5] -[node name="StaticMain" type="WebServer"] +[node name="StaticMain" type="Node"] +script = ExtResource( 4 ) + +[node name="StaticMain" type="WebServer" parent="."] script = ExtResource( 2 ) -[node name="WebRoot" parent="." instance=ExtResource( 1 )] +[node name="WebRoot" parent="StaticMain" instance=ExtResource( 1 )] www_root_path = "res://www/" -[node name="PanelContainer" type="PanelContainer" parent="."] +[node name="PanelContainer" type="PanelContainer" parent="StaticMain"] anchor_right = 1.0 anchor_bottom = 1.0 -[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer"] +[node name="VBoxContainer" type="VBoxContainer" parent="StaticMain/PanelContainer"] margin_left = 7.0 margin_top = 7.0 margin_right = 1017.0 margin_bottom = 593.0 alignment = 1 -[node name="ExportButton" type="Button" parent="PanelContainer/VBoxContainer"] -margin_top = 271.0 +[node name="ExportButton" type="Button" parent="StaticMain/PanelContainer/VBoxContainer"] +margin_top = 255.0 margin_right = 1010.0 -margin_bottom = 291.0 +margin_bottom = 275.0 text = "Export" -[node name="OpenFolderButton" type="Button" parent="PanelContainer/VBoxContainer"] -margin_top = 295.0 +[node name="OpenFolderButton" type="Button" parent="StaticMain/PanelContainer/VBoxContainer"] +margin_top = 279.0 margin_right = 1010.0 -margin_bottom = 315.0 +margin_bottom = 299.0 text = "Open Export Folder" -[connection signal="pressed" from="PanelContainer/VBoxContainer/ExportButton" to="." method="_on_ExportButton_pressed"] -[connection signal="pressed" from="PanelContainer/VBoxContainer/OpenFolderButton" to="." method="_on_OpenFolderButton_pressed"] +[node name="HSeparator" type="HSeparator" parent="StaticMain/PanelContainer/VBoxContainer"] +margin_top = 303.0 +margin_right = 1010.0 +margin_bottom = 307.0 + +[node name="ServeON" type="Button" parent="StaticMain/PanelContainer/VBoxContainer"] +margin_top = 311.0 +margin_right = 1010.0 +margin_bottom = 331.0 +text = "Serve Exported Folder ON" + +[node name="WebServerSimple" type="WebServerSimple" parent="."] +script = ExtResource( 3 ) + +[node name="WebRoot" type="WebRoot" parent="WebServerSimple"] +routing_enabled = false +script = ExtResource( 5 ) + +[connection signal="pressed" from="StaticMain/PanelContainer/VBoxContainer/ExportButton" to="StaticMain" method="_on_ExportButton_pressed"] +[connection signal="pressed" from="StaticMain/PanelContainer/VBoxContainer/OpenFolderButton" to="StaticMain" method="_on_OpenFolderButton_pressed"] +[connection signal="pressed" from="StaticMain/PanelContainer/VBoxContainer/ServeON" to="." method="_on_ServeON_pressed"] diff --git a/game/StaticMainNode.gd b/game/StaticMainNode.gd new file mode 100644 index 0000000..210b663 --- /dev/null +++ b/game/StaticMainNode.gd @@ -0,0 +1,6 @@ +extends Node + + +func _on_ServeON_pressed() -> void: + $WebServerSimple.start() + diff --git a/game/StaticWebServerSimple.gd b/game/StaticWebServerSimple.gd new file mode 100644 index 0000000..eebc069 --- /dev/null +++ b/game/StaticWebServerSimple.gd @@ -0,0 +1,9 @@ +extends WebServerSimple + + + + +func _start() -> void: + get_node("WebRoot").www_root_path = OS.get_user_data_dir().append_path("export/") + + ._start() diff --git a/game/WebRoot.gd b/game/WebRoot.gd new file mode 100644 index 0000000..eea5105 --- /dev/null +++ b/game/WebRoot.gd @@ -0,0 +1,47 @@ +extends WebRoot + +func _handle_request_main(request : WebServerRequest) -> void: + #if (process_middlewares(request)): + # return; + + #if (web_permission.is_valid()): + # if (web_permission.activate(request)): + # return; + + #handle_request(request); + + # handle files first + if (try_send_wwwroot_file_gd(request)): + return; + + handle_request(request); + + # normal routing +# if (!routing_enabled): +# handle_request(request); +# return; +# +# if (!try_route_request_to_children(request)): +# handle_request(request); + +func try_send_wwwroot_file_gd(request : WebServerRequest) -> bool: + var path : String = request.get_path_full(); + path = path.to_lower(); + + var file_indx : int = www_root_file_cache.wwwroot_get_file_index(path); + + if (file_indx != -1): + send_file(www_root_file_cache.wwwroot_get_file_orig_path(file_indx), request); + + return true; + else: + path = path.plus_file("index.html") + + file_indx = www_root_file_cache.wwwroot_get_file_index(path); + + if (file_indx != -1): + send_file(www_root_file_cache.wwwroot_get_file_orig_path(file_indx), request); + + return true; + + return false;