Skip to content

Commit

Permalink
Fix custom content and importers blocking scripting reload
Browse files Browse the repository at this point in the history
  • Loading branch information
GoaLitiuM committed Jun 1, 2024
1 parent ed1ac9a commit 31b126e
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 8 deletions.
1 change: 1 addition & 0 deletions Source/Editor/CustomEditors/Editors/GenericEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ public override void Initialize(LayoutElementsContainer layout)
OnGroupsEnd();
}

/// <inheritdoc />
protected override void Deinitialize()
{
_visibleIfCaches = null;
Expand Down
44 changes: 44 additions & 0 deletions Source/Editor/Modules/ContentDatabaseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ internal ContentDatabaseModule(Editor editor)

// Register AssetItems serialization helper (serialize ref ID only)
FlaxEngine.Json.JsonSerializer.Settings.Converters.Add(new AssetItemConverter());

ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin;
}

private void OnContentAssetDisposing(Asset asset)
Expand Down Expand Up @@ -1260,6 +1262,47 @@ internal void OnDirectoryEvent(MainContentTreeNode node, FileSystemEventArgs e)
}
}

private void OnScriptsReloadBegin()
{
var enabledEvents = _enableEvents;
_enableEvents = false;
_isDuringFastSetup = true;
var startItems = _itemsCreated;
foreach (var project in Projects)
{
if (project.Content != null)
{
//Dispose(project.Content.Folder);
for (int i = 0; i < project.Content.Folder.Children.Count; i++)
{
Dispose(project.Content.Folder.Children[i]);
i--;
}
}
if (project.Source != null)
{
//Dispose(project.Source.Folder);
for (int i = 0; i < project.Source.Folder.Children.Count; i++)
{
Dispose(project.Source.Folder.Children[i]);
i--;
}
}
}

List<ContentProxy> removeProxies = new List<ContentProxy>();
foreach (var proxy in Editor.Instance.ContentDatabase.Proxy)
{
if (proxy.GetType().IsCollectible)
removeProxies.Add(proxy);
}
foreach (var proxy in removeProxies)
RemoveProxy(proxy, false);

_isDuringFastSetup = false;
_enableEvents = enabledEvents;
}

/// <inheritdoc />
public override void OnUpdate()
{
Expand All @@ -1285,6 +1328,7 @@ public override void OnUpdate()
public override void OnExit()
{
FlaxEngine.Content.AssetDisposing -= OnContentAssetDisposing;
ScriptsBuilder.ScriptsReloadBegin -= OnScriptsReloadBegin;

// Disable events
_enableEvents = false;
Expand Down
15 changes: 15 additions & 0 deletions Source/Editor/Modules/ContentImportingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,20 @@ private void EndWorker()
public override void OnInit()
{
ImportFileEntry.RegisterDefaultTypes();
ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin;
}

private void OnScriptsReloadBegin()
{
// Remove import file types from scripting assemblies
List<string> removeFileTypes = new List<string>();
foreach (var pair in ImportFileEntry.FileTypes)
{
if (pair.Value.Method.IsCollectible || (pair.Value.Target != null && pair.Value.Target.GetType().IsCollectible))
removeFileTypes.Add(pair.Key);
}
foreach (var fileType in removeFileTypes)
ImportFileEntry.FileTypes.Remove(fileType);
}

/// <inheritdoc />
Expand Down Expand Up @@ -451,6 +465,7 @@ public override void OnUpdate()
/// <inheritdoc />
public override void OnExit()
{
ScriptsBuilder.ScriptsReloadBegin -= OnScriptsReloadBegin;
EndWorker();
}
}
Expand Down
1 change: 0 additions & 1 deletion Source/Editor/Modules/WindowsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ private struct WindowRestoreData
public Float2 FloatPosition;

public AssetItem Item;
public AssetItem Item2;

// Constructor, to allow for default values
public WindowRestoreData()
Expand Down
45 changes: 38 additions & 7 deletions Source/Editor/Windows/ContentWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public sealed partial class ContentWindow : EditorWindow
private const string ProjectDataLastViewedFolder = "LastViewedFolder";
private bool _isWorkspaceDirty;
private string _workspaceRebuildLocation;
private string _lastViewedFolderBeforeReload;
private SplitPanel _split;
private Panel _contentViewPanel;
private Panel _contentTreePanel;
Expand Down Expand Up @@ -1031,6 +1032,41 @@ public override void OnInit()
ShowRoot();
};

Refresh();

// Load last viewed folder
if (Editor.ProjectCache.TryGetCustomData(ProjectDataLastViewedFolder, out string lastViewedFolder))
{
if (Editor.ContentDatabase.Find(lastViewedFolder) is ContentFolder folder)
_tree.Select(folder.Node);
}

ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin;
ScriptsBuilder.ScriptsReloadEnd += OnScriptsReloadEnd;
}

private void OnScriptsReloadBegin()
{
var lastViewedFolder = _tree.Selection.Count == 1 ? _tree.SelectedNode as ContentTreeNode : null;
_lastViewedFolderBeforeReload = lastViewedFolder?.Path ?? string.Empty;

_tree.RemoveChild(_root);
_root = null;
}

private void OnScriptsReloadEnd()
{
Refresh();

if (!string.IsNullOrEmpty(_lastViewedFolderBeforeReload))
{
if (Editor.ContentDatabase.Find(_lastViewedFolderBeforeReload) is ContentFolder folder)
_tree.Select(folder.Node);
}
}

private void Refresh()
{
// Setup content root node
_root = new RootContentTreeNode
{
Expand Down Expand Up @@ -1066,13 +1102,6 @@ public override void OnInit()
// Update UI layout
_isLayoutLocked = false;
PerformLayout();

// Load last viewed folder
if (Editor.ProjectCache.TryGetCustomData(ProjectDataLastViewedFolder, out string lastViewedFolder))
{
if (Editor.ContentDatabase.Find(lastViewedFolder) is ContentFolder folder)
_tree.Select(folder.Node);
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -1220,6 +1249,8 @@ public override void OnDestroy()
_viewDropdown = null;

Editor.Options.OptionsChanged -= OnOptionsChanged;
ScriptsBuilder.ScriptsReloadBegin -= OnScriptsReloadBegin;
ScriptsBuilder.ScriptsReloadEnd -= OnScriptsReloadEnd;

base.OnDestroy();
}
Expand Down
16 changes: 16 additions & 0 deletions Source/Editor/Windows/ToolboxWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ private void OnScriptsReload()
_searchBox.Clear();
_groupSearch.DisposeChildren();
_groupSearch.PerformLayout();

// Remove tabs
var tabs = new List<Tab>();
foreach (var child in _actorGroups.Children)
{
if (child is Tab tab)
{
if (tab.Text != "Search")
tabs.Add(tab);
}
}
foreach (var tab in tabs)
{
var group = _actorGroups.Children.Find(T => T == tab);
group.Dispose();
}
}

private void OnScriptsReloadEnd()
Expand Down
21 changes: 21 additions & 0 deletions Source/Engine/Scripting/Scripting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ internal static void Init()
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
Localization.LocalizationChanged += OnLocalizationChanged;
#if FLAX_EDITOR
FlaxEditor.ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin;
FlaxEditor.ScriptsBuilder.ScriptsReloadEnd += OnScriptsReloadEnd;
#endif

OnLocalizationChanged();
if (!Engine.IsEditor)
Expand All @@ -178,6 +182,19 @@ internal static void Init()
}
}

#if FLAX_EDITOR
private static void OnScriptsReloadBegin()
{
// Tooltip might hold references to scripting assemblies
Style.Current.SharedTooltip = null;
}

private static void OnScriptsReloadEnd()
{
Style.Current.SharedTooltip = new Tooltip();
}
#endif

private static void OnLocalizationChanged()
{
// Invariant-globalization only (see InitHostfxr with Mono)
Expand Down Expand Up @@ -359,6 +376,10 @@ internal static void Internal_Exit()

MainThreadTaskScheduler.Dispose();
Json.JsonSerializer.Dispose();
#if FLAX_EDITOR
FlaxEditor.ScriptsBuilder.ScriptsReloadBegin -= OnScriptsReloadBegin;
FlaxEditor.ScriptsBuilder.ScriptsReloadEnd -= OnScriptsReloadEnd;
#endif
}

/// <summary>
Expand Down

0 comments on commit 31b126e

Please sign in to comment.