1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
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<TypeEntry> _registeredTypes = new List<TypeEntry>();
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 EnablePlugin()
{
RegisterTypes();
}
public override void DisablePlugin()
{
DeRegisterTypes();
}
public override void _ExitTree()
{
DeRegisterTypes();
RemoveToolMenuItem(LoadTypesMenuItem);
RemoveToolMenuItem(ReloadTypesMenuItem);
Disconnect("resource_saved", this, nameof(OnResourceSaved));
}
// Required by Godot
// ReSharper disable once UnusedParameter.Local
private void OnResourceSaved(Resource resource)
{
RegisterTypes();
}
private void OnReloadTypes(bool reload)
{
if (reload) DeRegisterTypes();
RegisterTypes();
}
private void RegisterTypes()
{
DeRegisterTypes();
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 _registeredTypes.Find(r => r.Equals(type)) == null
select new TypeEntry(type);
foreach (var type in newTypes)
{
GD.Print($"Exporting class {type.Name}");
AddCustomType(type.Name, type.Base, type.Script, null);
_registeredTypes.Add(type);
}
}
private void DeRegisterTypes()
{
foreach (var type in _registeredTypes)
{
GD.Print($"Removing class {type.Name}");
RemoveCustomType(type.Name);
}
_registeredTypes.Clear();
}
private class TypeEntry : Reference, IEquatable<Type>
{
public readonly string Base = "";
public readonly string Name = "";
public readonly Script Script = new CSharpScript();
// Required by Godot
// ReSharper disable once UnusedMember.Local
public TypeEntry()
{
}
public TypeEntry(Type type)
{
Name = type.Name;
Base = type.BaseType?.Name ?? "Object";
Script = LoadScript(type.FullName ?? "");
}
public bool Equals(Type other)
{
return other != null && Name == other.Name && Base == other.BaseType?.Name;
}
private static CSharpScript LoadScript(string fullName)
{
var components = fullName.Split('.');
var path = string.Join("/", components.Skip(1)) + ".cs";
return ResourceLoader.Load<CSharpScript>(path);
}
}
}
}
#endif
|