-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundLoading.cs
More file actions
67 lines (52 loc) · 1.97 KB
/
BackgroundLoading.cs
File metadata and controls
67 lines (52 loc) · 1.97 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
using Godot;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace AlexOtsuka.Loading;
public static class BackgroundLoading
{
private static HashSet<string> _requested = [];
public static Error Request(string path, string typeHint = "", bool useSubThreads = false, ResourceLoader.CacheMode cacheMode = ResourceLoader.CacheMode.Reuse)
{
if (_requested.Contains(path))
return Error.AlreadyExists;
var error = ResourceLoader.LoadThreadedRequest(path, typeHint, useSubThreads, cacheMode);
if (error != Error.Ok) { return error; }
_requested.Add(path);
return Error.Ok;
}
public static string[] Requested() => _requested.ToArray();
public static Resource GetResource(string path)
{
if (ResourceLoader.LoadThreadedGetStatus(path) == ResourceLoader.ThreadLoadStatus.InvalidResource)
return new Resource();
_requested.Remove(path);
return ResourceLoader.LoadThreadedGet(path);
}
public static T GetResource<T>(string path) where T : Resource => GetResource(path) as T;
public static ResourceLoader.ThreadLoadStatus GetStatus(string path)
{
if (_requested.Contains(path))
return ResourceLoader.LoadThreadedGetStatus(path);
return ResourceLoader.ThreadLoadStatus.InvalidResource;
}
public static void Cancel(string path)
{
_requested.Remove(path);
if (ResourceLoader.LoadThreadedGetStatus(path) == ResourceLoader.ThreadLoadStatus.Loaded)
{
ResourceLoader.LoadThreadedGet(path);
}
else if (ResourceLoader.LoadThreadedGetStatus(path) == ResourceLoader.ThreadLoadStatus.InProgress)
{
var thread = new Thread(() => ResourceLoader.LoadThreadedGet(path));
thread.Start();
}
}
public static void Clear()
{
foreach(string path in _requested)
Cancel(path);
_requested.Clear();
}
}