Skip to content

Commit 11583f4

Browse files
[Jobs] Add ActionJob;
[Editor] Update background tasks to job system;
1 parent 6b873fe commit 11583f4

File tree

15 files changed

+263
-307
lines changed

15 files changed

+263
-307
lines changed

Engine/Core/Jobs/ActionJob.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Staple.Jobs;
4+
5+
public class ActionJob(Action action) : IJob
6+
{
7+
public Action action = action;
8+
9+
public void Execute()
10+
{
11+
action();
12+
}
13+
}

Engine/Core/Jobs/JobHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ internal JobHandle(Task task)
1818
/// <summary>
1919
/// Checks whether the task was completed
2020
/// </summary>
21-
public bool Completed => task.IsCompleted;
21+
public bool Completed => task?.IsCompleted ?? true;
2222

2323
/// <summary>
2424
/// Waits for the handle to complete
2525
/// </summary>
2626
public readonly void Complete()
2727
{
28-
if(task.IsCompleted)
28+
if(task?.IsCompleted ?? true)
2929
{
3030
return;
3131
}

Engine/Core/Resources/ResourceManager.cs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ internal void RecreateResources()
293293
/// <returns>The byte array, or null</returns>
294294
public byte[] LoadFile(string path, string prefix = null)
295295
{
296+
if ((path?.Length ?? 0) == 0)
297+
{
298+
return null;
299+
}
300+
296301
var pakPath = path.Replace(Path.DirectorySeparatorChar, '/');
297302

298303
var guid = AssetDatabase.GetAssetGuid(pakPath);
@@ -370,6 +375,11 @@ public byte[] LoadFile(string path, string prefix = null)
370375
/// <returns>The string, or null</returns>
371376
public string LoadFileString(string path)
372377
{
378+
if ((path?.Length ?? 0) == 0)
379+
{
380+
return null;
381+
}
382+
373383
var data = LoadFile(path);
374384

375385
if(data == null)
@@ -427,6 +437,11 @@ public List<string> LoadSceneList()
427437
/// <returns>The scene, or null</returns>
428438
public Scene LoadRawSceneFromPath(string path)
429439
{
440+
if ((path?.Length ?? 0) == 0)
441+
{
442+
return null;
443+
}
444+
430445
World.Current = new();
431446

432447
Scene.current = null;
@@ -760,6 +775,11 @@ public Scene LoadSceneFromGuid(string guid)
760775
/// <returns>The scene, or null</returns>
761776
public Scene LoadScene(string name)
762777
{
778+
if ((name?.Length ?? 0) == 0)
779+
{
780+
return null;
781+
}
782+
763783
var guid = AssetDatabase.GetAssetGuid(name) ??
764784
name;
765785

@@ -789,6 +809,11 @@ public Scene LoadScene(string name)
789809
/// <returns>The shader data, or null</returns>
790810
public SerializableShader LoadShaderData(string path)
791811
{
812+
if ((path?.Length ?? 0) == 0)
813+
{
814+
return null;
815+
}
816+
792817
var prefix = ShaderPrefix;
793818
var guid = AssetDatabase.GetAssetGuid(path);
794819

@@ -854,6 +879,11 @@ public SerializableShader LoadShaderData(string path)
854879
/// <returns>The shader, or null</returns>
855880
public Shader LoadShader(string path, bool ignoreCache = false)
856881
{
882+
if ((path?.Length ?? 0) == 0)
883+
{
884+
return null;
885+
}
886+
857887
var prefix = ShaderPrefix;
858888
var guid = AssetDatabase.GetAssetGuid(path);
859889

@@ -971,7 +1001,12 @@ public Shader LoadShader(string path, bool ignoreCache = false)
9711001
/// <returns>The material, or null</returns>
9721002
public Material LoadMaterial(string path, bool ignoreCache = false)
9731003
{
974-
if(ignoreCache == false &&
1004+
if ((path?.Length ?? 0) == 0)
1005+
{
1006+
return null;
1007+
}
1008+
1009+
if (ignoreCache == false &&
9751010
cachedMaterials.TryGetValue(path, out var material) &&
9761011
material != null &&
9771012
material.Disposed == false)
@@ -1245,6 +1280,11 @@ public Texture LoadTexture(string path, TextureFlags flags = TextureFlags.None,
12451280
/// <returns>The audio clip, or null</returns>
12461281
public AudioClip LoadAudioClip(string path, bool ignoreCache = false)
12471282
{
1283+
if ((path?.Length ?? 0) == 0)
1284+
{
1285+
return null;
1286+
}
1287+
12481288
if (ignoreCache == false &&
12491289
cachedAudioClips.TryGetValue(path, out var audioClip) &&
12501290
audioClip != null)
@@ -1320,7 +1360,12 @@ public AudioClip LoadAudioClip(string path, bool ignoreCache = false)
13201360
/// <returns>The mesh, or null</returns>
13211361
public Mesh LoadMesh(string guid, bool ignoreCache = false)
13221362
{
1323-
if(guid.StartsWith("Internal/", StringComparison.InvariantCulture))
1363+
if ((guid?.Length ?? 0) == 0)
1364+
{
1365+
return null;
1366+
}
1367+
1368+
if (guid.StartsWith("Internal/", StringComparison.InvariantCulture))
13241369
{
13251370
return Mesh.GetDefaultMesh(guid);
13261371
}
@@ -1417,6 +1462,11 @@ public Mesh LoadMesh(string guid, bool ignoreCache = false)
14171462
/// <returns>The mesh asset, or null</returns>
14181463
public MeshAsset LoadMeshAsset(string path, bool ignoreCache = false)
14191464
{
1465+
if ((path?.Length ?? 0) == 0)
1466+
{
1467+
return null;
1468+
}
1469+
14201470
var guid = AssetDatabase.GetAssetGuid(path);
14211471

14221472
path = guid ?? path;
@@ -1718,6 +1768,11 @@ public MeshAsset LoadMeshAsset(string path, bool ignoreCache = false)
17181768
/// <returns>The asset, or null</returns>
17191769
public T LoadAsset<T>(string path, bool ignoreCache = false) where T: IStapleAsset
17201770
{
1771+
if ((path?.Length ?? 0) == 0)
1772+
{
1773+
return default;
1774+
}
1775+
17211776
var guid = AssetDatabase.GetAssetGuid(path);
17221777

17231778
path = guid ?? path;
@@ -1747,6 +1802,11 @@ public T LoadAsset<T>(string path, bool ignoreCache = false) where T: IStapleAss
17471802
/// <returns>The asset, or null</returns>
17481803
public IStapleAsset LoadAsset(string path, bool ignoreCache = false)
17491804
{
1805+
if ((path?.Length ?? 0) == 0)
1806+
{
1807+
return default;
1808+
}
1809+
17501810
var guid = AssetDatabase.GetAssetGuid(path);
17511811

17521812
path = guid ?? path;
@@ -1822,6 +1882,11 @@ public IStapleAsset LoadAsset(string path, bool ignoreCache = false)
18221882
/// <returns>The prefab, or null</returns>
18231883
public Prefab LoadPrefab(string path, bool ignoreCache = false)
18241884
{
1885+
if ((path?.Length ?? 0) == 0)
1886+
{
1887+
return null;
1888+
}
1889+
18251890
var guid = AssetDatabase.GetAssetGuid(path);
18261891

18271892
path = guid ?? path;
@@ -1893,6 +1958,11 @@ public Prefab LoadPrefab(string path, bool ignoreCache = false)
18931958
/// <returns>The font, or null</returns>
18941959
public FontAsset LoadFont(string path, bool ignoreCache = false)
18951960
{
1961+
if ((path?.Length ?? 0) == 0)
1962+
{
1963+
return null;
1964+
}
1965+
18961966
var guid = AssetDatabase.GetAssetGuid(path);
18971967

18981968
path = guid ?? path;

Engine/Core/StapleCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<Compile Include="Input\InputActionContext.cs" />
147147
<Compile Include="Input\InputActionType.cs" />
148148
<Compile Include="Input\InputDevice.cs" />
149+
<Compile Include="Jobs\ActionJob.cs" />
149150
<Compile Include="Math\NoiseGenerator.cs" />
150151
<Compile Include="Modules\ModuleInitializer.cs" />
151152
<Compile Include="Modules\ModuleType.cs" />

Engine/Editor/Editors/Assets/AudioClipEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override void Destroy()
4343

4444
lock (lockObject)
4545
{
46-
cancellation.Cancel();
46+
cancellation?.Cancel();
4747
audioSource?.Destroy();
4848
audioClip?.Destroy();
4949

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Threading;
1+
using Staple.Jobs;
42

53
namespace Staple.Editor;
64

@@ -9,76 +7,29 @@ internal partial class StapleEditor
97
/// <summary>
108
/// Executes a background task
119
/// </summary>
12-
/// <param name="callback">The background callback</param>
13-
public void StartBackgroundTask(IEnumerator<(bool, string, float)> callback)
10+
/// <param name="handle">The job handle</param>
11+
public void StartBackgroundTask(JobHandle handle)
1412
{
15-
Thread thread = null;
16-
17-
showingProgress = true;
18-
progressFraction = 0;
19-
20-
thread = new Thread(() =>
21-
{
22-
for (; ; )
23-
{
24-
var shouldQuit = false;
25-
float t = 0;
26-
string s = "";
27-
28-
lock(backgroundLock)
29-
{
30-
if(shouldTerminate)
31-
{
32-
return;
33-
}
34-
35-
t = progressFraction;
36-
s = progressMessage;
37-
}
38-
39-
try
40-
{
41-
shouldQuit = callback.Current.Item1;
42-
43-
s = callback.Current.Item2;
44-
45-
t = callback.Current.Item3;
46-
}
47-
catch(Exception e)
48-
{
49-
Log.Error($"Background Task Error: {e}");
50-
51-
shouldQuit = true;
52-
}
53-
54-
if (callback.MoveNext() == false)
55-
{
56-
shouldQuit = true;
57-
}
58-
59-
if(shouldQuit)
60-
{
61-
lock(backgroundLock)
62-
{
63-
backgroundThreads.Remove(thread);
64-
}
65-
66-
return;
67-
}
68-
69-
lock(backgroundLock)
70-
{
71-
progressFraction = t;
72-
progressMessage = s;
73-
}
74-
}
75-
});
76-
7713
lock(backgroundLock)
7814
{
79-
backgroundThreads.Add(thread);
15+
backgroundHandles.Add(handle);
16+
17+
showingProgress = true;
18+
progressFraction = 0;
8019
}
20+
}
8121

82-
thread.Start();
22+
/// <summary>
23+
/// Sets the current background progress and message
24+
/// </summary>
25+
/// <param name="progress">The progress percentage (0-1)</param>
26+
/// <param name="message">The message</param>
27+
public void SetBackgroundProgress(float progress, string message)
28+
{
29+
ThreadHelper.Dispatch(() =>
30+
{
31+
progressFraction = progress;
32+
progressMessage = message;
33+
});
8334
}
8435
}

0 commit comments

Comments
 (0)