Static site generation prototype implementation.

This commit is contained in:
Relintai 2023-01-03 23:10:49 +01:00
parent 410803d818
commit 4d6d0eadae
8 changed files with 214 additions and 1 deletions

92
game/StaticMain.gd Normal file
View File

@ -0,0 +1,92 @@
extends WebServer
# Maybe WebNodes should get a method like get_property_list?
# (like get_uri_list, get_served_folder_list, get_served_file_list)
# Would likely be good for other things, like cleanups aswell.
var StaticWebServerRequest = preload("res://StaticWebServerRequest.gd")
func _ready() -> void:
start()
func prepare_dir(export_folder : String) -> void:
var dir : Directory = Directory.new()
if dir.dir_exists(export_folder):
OS.move_to_trash(export_folder)
dir.make_dir_recursive(export_folder)
func make_dir_recursive(folder : String) -> void:
var dir : Directory = Directory.new()
dir.make_dir_recursive(folder)
func export_web_project() -> void:
if !get_web_root():
print("!get_web_root()")
return
var export_folder : String = OS.get_user_data_dir().append_path("export/")
prepare_dir(export_folder)
evaluate_web_node(get_web_root(), "/", export_folder)
func copy_folder(path_from : String, path_to : String) -> void:
var dir : Directory = Directory.new()
dir.make_dir_recursive(path_to)
if dir.open(path_from) == OK:
dir.list_dir_begin(true)
var file_name : String = dir.get_next()
while !file_name.empty():
if dir.current_is_dir():
copy_folder(path_from.append_path(file_name), path_to.append_path(file_name))
else:
dir.copy(path_from.plus_file(file_name), path_to.plus_file(file_name))
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path: %s " % path_from)
func write_index(n : WebNode, path : String) -> void:
var request : WebServerRequestScriptable = StaticWebServerRequest.new()
request._set_server(self)
request._set_web_root(get_web_root())
n.handle_request(request)
var file : File = File.new()
file.open(path.plus_file("index.html"), File.WRITE)
file.store_string(request.compiled_body)
file.close()
func evaluate_web_node(n : WebNode, web_node_path : String, path : String) -> void:
make_dir_recursive(path)
if n is WebRoot:
if n.www_root_path != "":
copy_folder(n.www_root_path, path)
for c in n.get_children():
if c is WebNode:
if c.uri_segment != "/":
continue
write_index(c, path)
break
else:
write_index(n, path)
for c in n.get_children():
if c is WebNode:
if c.uri_segment.empty() || c.uri_segment == "/":
continue
evaluate_web_node(c, web_node_path.append_path(c.uri_segment), path.append_path(c.uri_segment))
func _on_ExportButton_pressed() -> void:
export_web_project()
func _on_OpenFolderButton_pressed() -> void:
OS.shell_open(OS.get_user_data_dir())

36
game/StaticMain.tscn Normal file
View File

@ -0,0 +1,36 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://WebRoot.tscn" type="PackedScene" id=1]
[ext_resource path="res://StaticMain.gd" type="Script" id=2]
[node name="StaticMain" type="WebServer"]
script = ExtResource( 2 )
[node name="WebRoot" parent="." instance=ExtResource( 1 )]
www_root_path = "res://www/"
[node name="PanelContainer" type="PanelContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
[node name="VBoxContainer" type="VBoxContainer" parent="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
margin_right = 1010.0
margin_bottom = 291.0
text = "Export"
[node name="OpenFolderButton" type="Button" parent="PanelContainer/VBoxContainer"]
margin_top = 295.0
margin_right = 1010.0
margin_bottom = 315.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"]

View File

@ -0,0 +1,33 @@
extends WebServerRequestScriptable
enum ResponseType {
RESPONSE_TYPE_NONE = 0,
RESPONSE_TYPE_NORMAL = 1,
RESPONSE_TYPE_FILE = 2,
RESPONSE_TYPE_REDIRECT = 3,
};
var _response_type : int = 0
var _status_code : int = HTTPServerEnums.HTTP_STATUS_CODE_200_OK
var _sent_message : String = ""
var _error_handler_called : bool = false
func _send_redirect(location: String, status_code: int) -> void:
_response_type = ResponseType.RESPONSE_TYPE_REDIRECT;
_status_code = status_code;
_sent_message = location;
func _send() -> void:
_response_type = ResponseType.RESPONSE_TYPE_NORMAL;
_sent_message = get_compiled_body();
func _send_file(p_file_path: String) -> void:
_response_type = ResponseType.RESPONSE_TYPE_FILE;
_sent_message = p_file_path;
func _send_error(error_code: int) -> void:
_error_handler_called = true;
get_server().get_web_root().handle_error_send_request(self, error_code);

View File

@ -0,0 +1,7 @@
[plugin]
name="Static Web Root"
description=""
author="Relintai"
version=""
script="plugin.gd"

View File

@ -0,0 +1,10 @@
tool
extends EditorPlugin
func _enter_tree() -> void:
pass
func _exit_tree() -> void:
pass

View File

@ -56,7 +56,7 @@ config/icon="res://icon.png"
[editor_plugins]
enabled=PoolStringArray( "res://addons/web_gallery/plugin.cfg", "res://addons/web_pages/plugin.cfg" )
enabled=PoolStringArray( "res://addons/static_web_root/plugin.cfg", "res://addons/web_gallery/plugin.cfg", "res://addons/web_pages/plugin.cfg" )
[physics]

BIN
game/www/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

35
game/www/test.png.import Normal file
View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/test.png-0eb5febe8ea387b363ac224917537c15.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://www/test.png"
dest_files=[ "res://.import/test.png-0eb5febe8ea387b363ac224917537c15.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0