Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading user plugin from .pck #655

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions addons/material_maker/engine/nodes/gen_base.gd
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ static func remove_comments(s : String) -> String:
if comment_end != -1:
s = s.erase(comment_begin, comment_end-comment_begin+2)
cont = true


s = re_line_comment.sub(s, "", true)
return s

Expand Down
4 changes: 4 additions & 0 deletions addons/material_maker/engine/nodes/gen_graph.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var label : String = "Graph"
var shortdesc = ""
var longdesc = ""
var connections = []
var plugin_data = {}

var editable : bool = false

Expand Down Expand Up @@ -530,6 +531,7 @@ func _serialize(data: Dictionary) -> Dictionary:
data.shortdesc = shortdesc
data.longdesc = longdesc
data.nodes = []
data.plugin_data = plugin_data
for c in get_children():
data.nodes.append(c.serialize())
#data.connections = connections_to_compact(connections)
Expand All @@ -543,6 +545,8 @@ func _deserialize(data : Dictionary) -> void:
shortdesc = data.shortdesc
if data.has("longdesc"):
longdesc = data.longdesc
if data.has("plugin_data"):
plugin_data = data.plugin_data
var nodes = data.nodes if data.has("nodes") else []
var connection_array : Array = []
if data.has("connections"):
Expand Down
29 changes: 29 additions & 0 deletions addons/material_maker/engine/plugin/plugin_context.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class PluginContext:

var plugin_path: String
func _init(file_path: String) -> void:
plugin_path = file_path

func load_resource(resource_path: String) -> Variant:

if ResourceLoader.exists(resource_path):
print("Dependency already exists, either its been loaded or there is a dependency collision")
return ResourceLoader.load(resource_path)

var res_path = resource_path.get_slice("::", 2)
var file_path = res_path.replace("res://", plugin_path + "/")

var dependencies = ResourceLoader.get_dependencies(file_path)
for dependency in dependencies:
load_resource(dependency)

print("Loading dependency: " + file_path)
var resource = ResourceLoader.load(file_path)
if resource == null:
return null

print(resource)
resource.take_over_path(resource_path)
resource.take_over_path(resource_path.get_slice("::", 0))
resource.take_over_path(resource_path.get_slice("::", 2))
return resource
1 change: 1 addition & 0 deletions addons/material_maker/engine/plugin/plugin_context.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://demftccswppj6
13 changes: 13 additions & 0 deletions addons/material_maker/engine/plugin/plugin_instance.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends Node

const PluginContext = preload("res://addons/material_maker/engine/plugin/plugin_context.gd")

var context: PluginContext.PluginContext = null

signal on_ui_loaded

func load(resource_path):
return context.load_resource(resource_path)

func ui_ready():
on_ui_loaded.emit()
1 change: 1 addition & 0 deletions addons/material_maker/engine/plugin/plugin_instance.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://tjn43ecqeoar
6 changes: 6 additions & 0 deletions addons/material_maker/engine/plugin/plugin_instance.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bct66q7hliai2"]

[ext_resource type="Script" path="res://addons/material_maker/engine/plugin/plugin_instance.gd" id="1_51jwo"]

[node name="plugin_instance" type="Node"]
script = ExtResource("1_51jwo")
55 changes: 55 additions & 0 deletions addons/material_maker/engine/plugin/plugin_loader.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
extends Node

const PluginContext = preload("res://addons/material_maker/engine/plugin/plugin_context.gd")
const instance = preload("res://addons/material_maker/engine/plugin/plugin_instance.tscn")
@onready var mm_plugins = get_node("/root/mm_plugins")

func _ready()-> void:
load_plugins()

func load_plugins():
var plugin_dir = DirAccess.open("user://plugins")
if not plugin_dir:
return

var any_plugins_loaded = false

var directories = plugin_dir.get_directories()
print(directories)

for directory in directories:

print(directory)
var plugin_folder = "user://plugins/" + directory
var plugin_content = DirAccess.open(plugin_folder)
var plugin_directories = plugin_content.get_directories()
print(plugin_directories)

var plugin_id = plugin_directories[0]
var scene_path = "user://plugins/" + directory + "/" + plugin_id + "/plugin.tscn"
print(scene_path)

var real_path = "user://plugins/" + directory

var context = PluginContext.PluginContext.new(real_path)

var dependencies = ResourceLoader.get_dependencies(scene_path)
for dependency in dependencies:
context.load_resource(dependency)

var scene = ResourceLoader.load(scene_path)
if scene == null:
continue

if not scene is PackedScene:
continue

var plugin_node = scene.instantiate()
var addon_instance = instance.instantiate()

addon_instance.name = plugin_id
addon_instance.context = context

mm_plugins.add_child(addon_instance)
addon_instance.add_child(plugin_node)
print("Finished loading plugin: " + plugin_id)
1 change: 1 addition & 0 deletions addons/material_maker/engine/plugin/plugin_loader.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://dstdde4y0gqmm
2 changes: 1 addition & 1 deletion export_presets.cfg

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions material_maker/main_window.gd
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func _ready() -> void:
await get_tree().process_frame
new_material()

update_menus()

plugin_ui_loaded.call_deferred()
size = get_window().size
position = Vector2.ZERO
set_anchors_preset(Control.PRESET_FULL_RECT)
Expand All @@ -246,6 +249,11 @@ func update_menus() -> void:
menu_update_requested = true
do_update_menus.call_deferred()

func plugin_ui_loaded():
var children = mm_plugins.get_children()
for child in children:
child.ui_ready()

func do_update_menus() -> void:
# Create menus
var menu_bar_class
Expand Down
1 change: 0 additions & 1 deletion material_maker/main_window.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ layout_mode = 2

[node name="Menu" type="HBoxContainer" parent="VBoxContainer/TopBar"]
layout_mode = 2
size_flags_horizontal = 3

[node name="Layout" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2
Expand Down
7 changes: 6 additions & 1 deletion material_maker/tools/library_manager/library_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ func get_items(filter : String, sorted := false) -> Array:
func save_library_list() -> void:
var library_list = []
for i in range(2, get_child_count()):
library_list.push_back(get_child(i).library_path)
var path = get_child(i).library_path

# Libraries loaded via a plugin might not have a file path, so we dont need to save it
if path != "":
library_list.push_back(get_child(i).library_path)

mm_globals.config.set_value(config_section, "libraries", library_list)

func has_library(path : String) -> bool:
Expand Down
3 changes: 3 additions & 0 deletions material_maker/windows/export_animation/export_animation.gd
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func _on_Export_pressed():
dialog.add_filter("*.png;PNG image files")
add_child(dialog)
var files = await dialog.select_files()
do_export(files)

func do_export(files):
if files.size() > 0:
var filename : String = files[0]
var size : int = 1 << value_size.size_value
Expand Down
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mm_deps="*res://addons/material_maker/engine/dependencies.gd"
mm_renderer="*res://addons/material_maker/engine/multi_renderer.gd"
mm_sdf_builder="*res://addons/material_maker/sdf_builder/sdf_builder.tscn"
mm_preprocessor="*res://addons/material_maker/engine/preprocessor.gd"
mm_plugins="*res://addons/material_maker/engine/plugin/plugin_loader.gd"
Html5="*res://material_maker/html5.gd"

[debug]
Expand Down