summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSophia Pearson <codergal89@gmail.com>2022-05-24 14:59:58 +0200
committerSophia Pearson <codergal89@gmail.com>2022-05-24 14:59:58 +0200
commit8f3ed6ba952b1dd1f457b305d82c8165a324bc48 (patch)
treeb78f3cf5a14bce70744215deb74003ceb4a5db7f
parent6fe4972aa48a29d0aafee0461ccd6c635ca9ee6c (diff)
downloadtexty-8f3ed6ba952b1dd1f457b305d82c8165a324bc48.tar.xz
texty-8f3ed6ba952b1dd1f457b305d82c8165a324bc48.zip
addons: implement automatic class exporter
-rw-r--r--Texty.csproj3
-rw-r--r--addons/ClassExporter/Plugin.cs94
-rw-r--r--addons/ClassExporter/plugin.cfg7
-rw-r--r--project.godot2
4 files changed, 105 insertions, 1 deletions
diff --git a/Texty.csproj b/Texty.csproj
index fa11591..9d10a0b 100644
--- a/Texty.csproj
+++ b/Texty.csproj
@@ -13,4 +13,7 @@
<EmbeddedResource Remove="Scenes\**" />
<EmbeddedResource Remove="Tests\**" />
</ItemGroup>
+ <ItemGroup>
+ <Content Include="addons\ClassExporter\plugin.cfg" />
+ </ItemGroup>
</Project> \ No newline at end of file
diff --git a/addons/ClassExporter/Plugin.cs b/addons/ClassExporter/Plugin.cs
new file mode 100644
index 0000000..72df4bf
--- /dev/null
+++ b/addons/ClassExporter/Plugin.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using Godot;
+
+#if TOOLS
+
+namespace Texty.addons.ClassExporter
+{
+ [Tool]
+ public class Plugin : EditorPlugin
+ {
+ private const string LoadTypesMenuItem = "Load types";
+ private const string ReloadTypesMenuItem = "Reload types";
+ private readonly List<Type> _types = new List<Type>();
+
+ public override void _EnterTree()
+ {
+ Connect("resource_saved", this, nameof(OnResourceSaved));
+ AddToolMenuItem(LoadTypesMenuItem, this, nameof(OnReloadTypes), false);
+ AddToolMenuItem(ReloadTypesMenuItem, this, nameof(OnReloadTypes), true);
+ RegisterTypes();
+ }
+
+ public override void _ExitTree()
+ {
+ DeRegisterTypes();
+ RemoveToolMenuItem(LoadTypesMenuItem);
+ RemoveToolMenuItem(ReloadTypesMenuItem);
+ Disconnect("resource_saved", this, nameof(OnResourceSaved));
+ }
+
+ // ReSharper disable once UnusedParameter.Local
+ private void OnResourceSaved(Resource resource)
+ {
+ RegisterTypes();
+ }
+
+ private void OnReloadTypes(bool reload)
+ {
+ if (reload) DeRegisterTypes();
+ RegisterTypes();
+ }
+
+ private void RegisterTypes()
+ {
+ var assembly = Assembly.GetExecutingAssembly();
+ var newTypes = from type in assembly.GetTypes()
+ where type.IsSubclassOf(typeof(Resource)) || type.IsSubclassOf(typeof(Node))
+ where !type.IsSubclassOf(typeof(EditorPlugin))
+ where !_types.Contains(type)
+ select new RegistrableType(type);
+
+ foreach (var type in newTypes)
+ {
+ GD.Print($"Exporting class {type.Name}: {type.Script.ResourcePath}");
+ AddCustomType(type.Name, type.Base, type.Script, null);
+ _types.Add(type.Type);
+ }
+ }
+
+ private void DeRegisterTypes()
+ {
+ _types.ForEach(type => RemoveCustomType(type.Name));
+ _types.Clear();
+ }
+
+ private readonly struct RegistrableType
+ {
+ public RegistrableType(Type type)
+ {
+ Type = type;
+ }
+
+ public readonly Type Type;
+
+ public CSharpScript Script
+ {
+ get
+ {
+ var components = (Type.FullName ?? "").Split('.');
+ var path = string.Join("/", components.Skip(1)) + ".cs";
+ return ResourceLoader.Load<CSharpScript>(path);
+ }
+ }
+
+ public string Name => Type.Name;
+ public string Base => Type.BaseType?.Name ?? "";
+ }
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/addons/ClassExporter/plugin.cfg b/addons/ClassExporter/plugin.cfg
new file mode 100644
index 0000000..bc7c3b7
--- /dev/null
+++ b/addons/ClassExporter/plugin.cfg
@@ -0,0 +1,7 @@
+[plugin]
+
+name = "C# Class Exporter"
+description = "Add C# Node and Resource classes to Godot"
+author = "Sophia Pearson"
+version = "1.0.0"
+script = "Plugin.cs" \ No newline at end of file
diff --git a/project.godot b/project.godot
index 239d5ea..08204de 100644
--- a/project.godot
+++ b/project.godot
@@ -54,7 +54,7 @@ script_templates_search_path="res://ScriptTemplates"
[editor_plugins]
-enabled=PoolStringArray( "res://addons/gut/plugin.cfg" )
+enabled=PoolStringArray( "res://addons/ClassExporter/plugin.cfg", "res://addons/gut/plugin.cfg" )
[gui]