-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGameStudioPreviewService.cs
More file actions
333 lines (282 loc) · 12.4 KB
/
GameStudioPreviewService.cs
File metadata and controls
333 lines (282 loc) · 12.4 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Stride.Core.Assets;
using Stride.Core.Assets.Analysis;
using Stride.Core.Assets.Compiler;
using Stride.Core.Assets.Editor.Extensions;
using Stride.Core.Assets.Editor.Services;
using Stride.Core.Assets.Editor.ViewModel;
using Stride.Core.BuildEngine;
using Stride.Core;
using Stride.Core.Diagnostics;
using Stride.Core.Extensions;
using Stride.Core.Presentation.Controls;
using Stride.Core.Presentation.Services;
using Stride.Assets;
using Stride.Editor.Build;
using Stride.Editor.Engine;
using Stride.Games;
using Stride.Graphics;
namespace Stride.Editor.Preview
{
public class GameStudioPreviewService : IAssetPreviewService, IPreviewBuilder
{
public static bool DisablePreview = false;
private readonly SessionViewModel session;
private readonly AutoResetEvent initializationSignal = new AutoResetEvent(false);
private readonly GameEngineHost host;
private readonly IDebugPage loggerDebugPage;
private IAssetPreview currentPreview;
private IntPtr windowHandle;
private EmbeddedGameForm gameForm;
private object previewView;
private AssetViewModel previewBuildQueue;
private readonly SemaphoreSlim previewChangeLock = new SemaphoreSlim(1, 1);
/// <summary>
/// A lock used to access the <see cref="currentPreview"/> and <see cref="previewBuildQueue"/> fields safely.
/// </summary>
private readonly object previewLock = new object();
private readonly Thread previewGameThread;
private readonly Dictionary<Type, AssetPreviewFactory> assetPreviewFactories = new Dictionary<Type, AssetPreviewFactory>();
private readonly AssetCompilerContext previewCompileContext = new AssetCompilerContext { Platform = PlatformType.Windows };
private readonly AssetDependenciesCompiler previewCompiler = new AssetDependenciesCompiler(typeof(PreviewCompilationContext));
private readonly GameSettingsAsset previewGameSettings;
private readonly GameSettingsProviderService gameSettingsProvider;
public GameStudioPreviewService(SessionViewModel session)
{
if (session == null) throw new ArgumentNullException(nameof(session));
this.session = session;
Dispatcher = session.Dispatcher;
AssetBuilderService = session.ServiceProvider.Get<GameStudioBuilderService>();
gameSettingsProvider = session.ServiceProvider.Get<GameSettingsProviderService>();
Logger = GlobalLogger.GetLogger("Preview");
loggerDebugPage = EditorDebugTools.CreateLogDebugPage(Logger, "Preview");
previewGameSettings = GameSettingsFactory.Create();
previewGameSettings.GetOrCreate<RenderingSettings>().DefaultGraphicsProfile = GraphicsProfile.Level_11_0;
UpdateGameSettings(gameSettingsProvider.CurrentGameSettings);
previewCompileContext.SetGameSettingsAsset(previewGameSettings);
previewCompileContext.CompilationContext = typeof(PreviewCompilationContext);
previewGameThread = new Thread(SafeAction.Wrap(StrideUIThread)) { IsBackground = true, Name = "PreviewGame Thread" };
previewGameThread.SetApartmentState(ApartmentState.STA);
previewGameThread.Start();
// Wait for the window handle to be generated on the proper thread
initializationSignal.WaitOne();
host = new GameEngineHost(windowHandle);
session.AssetPropertiesChanged += OnAssetPropertyChanged;
gameSettingsProvider.GameSettingsChanged += OnGameSettingsChanged;
}
/// <summary>
/// Gets whether this instance of <see cref="GameStudioPreviewService"/> has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }
public GameStudioBuilderService AssetBuilderService { get; }
public IDispatcherService Dispatcher { get; }
public Logger Logger { get; }
public PreviewGame PreviewGame { get; private set; }
public event EventHandler<EventArgs> PreviewAssetUpdated;
public void Dispose()
{
if (!IsDisposed)
{
// Terminate preview control thread
previewBuildQueue = null;
session.AssetPropertiesChanged -= OnAssetPropertyChanged;
gameSettingsProvider.GameSettingsChanged -= OnGameSettingsChanged;
if (PreviewGame.IsRunning)
{
PreviewGame.Exit();
}
// Wait for the game thread to terminate
previewGameThread.Join();
//Game = null;
host.Dispose();
//host = null;
//gameForm = null;
//windowHandle = IntPtr.Zero;
previewCompileContext?.Dispose();
EditorDebugTools.UnregisterDebugPage(loggerDebugPage);
IsDisposed = true;
}
}
private void StrideUIThread()
{
gameForm = new EmbeddedGameForm { TopLevel = false, Visible = false };
windowHandle = gameForm.Handle;
initializationSignal.Set();
var context = new GameContextWinforms(gameForm) { InitializeDatabase = false };
PreviewGame = new PreviewGame(AssetBuilderService.EffectCompiler, context);
// Wait for shaders to be loaded
AssetBuilderService.WaitForShaders();
// TODO: For now we stop if there is an exception
// Ideally, we should try to recreate the game.
if (!DisablePreview)
{
PreviewGame.GraphicsDeviceManager.DeviceCreated += GraphicsDeviceManagerDeviceCreated;
PreviewGame.Run(context);
PreviewGame.Dispose();
}
}
private void OnGameSettingsChanged(object sender, GameSettingsChangedEventArgs e)
{
UpdateGameSettings(e.GameSettings);
}
private void UpdateGameSettings(GameSettingsAsset currentGameSettings)
{
previewGameSettings.GetOrCreate<EditorSettings>().RenderingMode = currentGameSettings.GetOrCreate<EditorSettings>().RenderingMode;
previewGameSettings.GetOrCreate<RenderingSettings>().ColorSpace = currentGameSettings.GetOrCreate<RenderingSettings>().ColorSpace;
}
private void OnAssetPropertyChanged(object sender, AssetChangedEventArgs e)
{
lock (previewLock)
{
var allAssets = AssetViewModel.ComputeRecursiveReferencerAssets(e.Assets);
allAssets.AddRange(e.Assets);
if (currentPreview != null && allAssets.Contains(currentPreview.AssetViewModel))
{
PreviewGame.Script.AddTask(UpdatePreviewAsset);
}
}
}
private void GraphicsDeviceManagerDeviceCreated(object sender, EventArgs e)
{
// Transmit actual GraphicsProfile to preview and thumbnail builder context
var graphicsProfile = PreviewGame.GraphicsDeviceManager.GraphicsDevice.Features.CurrentProfile;
//ThumbnailService.ThumbnailBuilderContext.GetGameSettingsAsset().Get<RenderingSettings>().DefaultGraphicsProfile = graphicsProfile;
previewCompileContext.GetGameSettingsAsset().GetOrCreate<RenderingSettings>().DefaultGraphicsProfile = graphicsProfile;
}
public void SetAssetToPreview(AssetViewModel asset)
{
lock (previewLock)
{
previewBuildQueue = asset;
}
PreviewGame.Script.AddTask(ChangePreviewAsset);
}
public AssetCompilerResult Compile(AssetItem asset)
{
return previewCompiler.Prepare(previewCompileContext, asset);
}
public FrameworkElement GetStrideView()
{
return !IsDisposed ? host : null;
}
private async Task UpdatePreviewAsset()
{
if (!previewChangeLock.Wait(0))
return;
try
{
// copy currentPreview to a local variable because it might be modified in a different thread.
IAssetPreview localPreview = currentPreview;
if (localPreview != null)
{
await localPreview.Update();
}
Logger.Info("Preview updated following to a a property change.");
}
catch (Exception e)
{
Logger.Error("Unable to update the preview after a property change.", e);
}
finally
{
previewChangeLock.Release();
}
}
private async Task ChangePreviewAsset()
{
if (!previewChangeLock.Wait(0))
return;
AssetViewModel asset = null;
IAssetPreview nextPreview = null;
try
{
if (currentPreview != null)
{
IAssetPreview previousPreview;
// Ensure that the current preview won't be disposed twice with a lock
lock (previewLock)
{
previousPreview = currentPreview;
currentPreview = null;
}
await previousPreview.IsInitialized();
await previousPreview.Dispose();
Logger.Info($"Unloaded previous preview of {previousPreview.AssetViewModel.Url}.");
}
lock (previewLock)
{
if (previewBuildQueue != null)
{
asset = previewBuildQueue;
nextPreview = GetPreviewForAsset(previewBuildQueue);
previewBuildQueue = null;
}
currentPreview = nextPreview;
}
if (asset != null && nextPreview != null)
{
previewView = await nextPreview.Initialize(asset, this);
if (previewView != null)
Logger.Info($"Initialized preview of {nextPreview.AssetViewModel.Url}.");
}
else
{
previewView = null;
}
}
catch (Exception e)
{
lock (previewLock)
{
currentPreview = null;
}
previewView = null;
Logger.Error("An exception occurred while changing the previewed asset", e);
}
finally
{
previewChangeLock.Release();
}
// Notify that the previewed asset has changed, so the editor view can update its visual tree.
PreviewAssetUpdated?.Invoke(this, EventArgs.Empty);
}
private IAssetPreview GetPreviewForAsset(AssetViewModel asset)
{
if (asset == null) throw new ArgumentNullException(nameof(asset));
var assetType = asset.Asset.GetType();
while (assetType != null)
{
AssetPreviewFactory factory;
if (assetPreviewFactories.TryGetValue(assetType, out factory))
{
var assetPreview = factory(this, PreviewGame, asset.AssetItem);
return assetPreview;
}
assetType = assetType.BaseType;
}
return null;
}
public object GetCurrentPreviewView()
{
return previewView;
}
public void RegisterAssetPreviewFactories(IReadOnlyDictionary<Type, AssetPreviewFactory> factories)
{
factories.ForEach(x => assetPreviewFactories.Add(x.Key, x.Value));
}
public void OnShowPreview()
{
PreviewGame.IsEditorHidden = false;
}
public void OnHidePreview()
{
PreviewGame.IsEditorHidden = true;
}
}
}