summaryrefslogtreecommitdiff
path: root/addons/ClassExporter/Plugin.cs
blob: 8a7e3494f00aa3fc1fb7c48eedf8814ad66a5ee3 (plain)
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
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 !type.IsAbstract
                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