-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorModule.cs
More file actions
140 lines (124 loc) · 5.51 KB
/
Copy pathEditorModule.cs
File metadata and controls
140 lines (124 loc) · 5.51 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using SplineTest.GameStudioExt.StrideEditorExt;
using Stride.Assets.Presentation.AssetEditors.EntityHierarchyEditor.Game;
using Stride.Assets.Presentation.AssetEditors.GameEditor.Game;
using Stride.Assets.Presentation.AssetEditors.SceneEditor.Game;
using Stride.Assets.Presentation.AssetEditors.SceneEditor.Services;
using Stride.Core;
using Stride.Core.Extensions;
using Stride.Core.Reflection;
using Stride.Engine;
using System.Diagnostics;
using System.Reflection;
namespace SplineTest.GameStudioExt;
internal class EditorModule
{
[ModuleInitializer]
public static void Initialize()
{
const string AssemblyCommonCategoriesName = "assets";
// Can't use Stride.Core.Reflection.AssemblyCommonCategories.Assets because
// this class is duplicated in two different dlls causing namespace conflict
AssemblyRegistry.Register(typeof(EditorModule).Assembly, AssemblyCommonCategoriesName);
Game.GameStarted += OnGameStarted;
Game.GameDestroyed += OnGameDestroyed;
//AssetsPlugin.RegisterPlugin(typeof(SplineEditorPlugin));
}
private static Dictionary<Game, List<IDisposable>> _gameToDisposables = [];
private static Dictionary<Game, List<IAsyncDisposable>> _gameToAsyncDisposables = [];
private static void OnGameStarted(object? sender, EventArgs e)
{
Debug.WriteLineIf(condition: true, $"OnGameStarted: {sender?.GetType().Name}");
if (sender is not Game game)
{
return;
}
string gameTypeName = sender.GetType().Name;
if (string.Equals(gameTypeName, "PreviewGame", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (sender is SceneEditorGame sceneEditorGame)
{
var disposables = new List<IDisposable>();
_gameToDisposables[game] = disposables;
var asyncDisposables = new List<IAsyncDisposable>();
_gameToAsyncDisposables[game] = asyncDisposables;
// Note that the editor can run multiple SceneEditorGames since these are the opened scene assets
var strideEditorService = new StrideEditorService();
game.Services.AddService<IStrideEditorService>(strideEditorService);
var viewModelServiceProvider = strideEditorService.ViewModelServiceProvider;
if (viewModelServiceProvider is null)
{
throw new Exception("ViewModelServiceProvider was not set.");
}
// HACK: Take IEditorGameController/SceneEditorController from another service because we can't get it ourselves
var cameraService = sceneEditorGame.EditorServices.Get<IEditorGameCameraService>();
var getEditorController_PropertyInfo = typeof(EditorGameCameraService).GetProperty("Controller", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
if (getEditorController_PropertyInfo is null)
{
throw new InvalidOperationException("EditorGameCameraService.Controller no longer exists.");
}
var getEditorController_MethodInfo = getEditorController_PropertyInfo.GetGetMethod(nonPublic: true);
var sceneEditorController = getEditorController_MethodInfo!.Invoke(cameraService, []) as SceneEditorController;
if (sceneEditorController is null)
{
throw new InvalidOperationException("EditorGameCameraService.Controller was null.");
}
var splineEditorService = new EditorGameSplineEditorService(strideEditorService, sceneEditorController);
sceneEditorGame.EditorServices.Add(splineEditorService);
asyncDisposables.Add(splineEditorService);
var splineMeshCompChangeWatcherService = new EditorGameSplineMeshComponentChangeWatcherService(sceneEditorController);
sceneEditorGame.EditorServices.Add(splineMeshCompChangeWatcherService);
asyncDisposables.Add(splineMeshCompChangeWatcherService);
// HACK: forced to do a late service registration
sceneEditorGame.Script.AddTask(async () =>
{
if (!splineEditorService.IsInitialized)
{
await splineEditorService.InitializeService(sceneEditorGame);
}
if (!splineMeshCompChangeWatcherService.IsInitialized)
{
await splineMeshCompChangeWatcherService.InitializeService(sceneEditorGame);
}
});
}
}
private static void OnGameDestroyed(object? sender, EventArgs e)
{
if (sender is not Game game)
{
return;
}
if (_gameToDisposables.Remove(game, out var disposables))
{
foreach (var disp in disposables)
{
disp.Dispose();
}
}
if (_gameToAsyncDisposables.Remove(game, out var asyncDisposables))
{
Task.Run(async () =>
{
foreach (var disp in asyncDisposables)
{
await disp.DisposeAsync();
}
}).Forget();
}
}
}
//internal class SplineEditorPlugin : StrideAssetsPlugin
//{
// protected override void Initialize(ILogger logger)
// {
// }
// /// <inheritdoc />
// public override void InitializeSession([Stride.Core.Annotations.NotNull] SessionViewModel session)
// {
// }
// public override void RegisterAssetPreviewViewTypes(IDictionary<Type, Type> assetPreviewViewTypes)
// {
// }
//}