-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetLoader.cs
More file actions
185 lines (165 loc) · 4.42 KB
/
AssetLoader.cs
File metadata and controls
185 lines (165 loc) · 4.42 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace Core.AssetSystem
{
public enum LoadPriority
{
Medium,
Low,
High
}
internal static class AssetLoader
{
internal struct LoadJob
{
public int assetId;
public string assetName;
public LoadPriority priority;
public IAsset asset;
}
private static BlockingCollection<LoadJob> highPriorityQueue = new BlockingCollection<LoadJob>(new ConcurrentQueue<LoadJob>());
private static BlockingCollection<LoadJob> mediumPriorityQueue = new BlockingCollection<LoadJob>(new ConcurrentQueue<LoadJob>());
private static BlockingCollection<LoadJob> lowPriorityQueue = new BlockingCollection<LoadJob>(new ConcurrentQueue<LoadJob>());
private static BlockingCollection<LoadJob>[] queues = new[]
{highPriorityQueue, mediumPriorityQueue, lowPriorityQueue};
private static bool isSetup = false;
private static readonly object SetupLock = new object();
private static AssetLoaderThread[] normalLoaderThreads;
private static AssetLoaderThread highPriorityLoaderThread;
private static int numNormalLoaderThreads = 2;
private static void Setup()
{
lock (SetupLock) //lock while setup incase multiple threads try to access setup
{
if (isSetup) return;
normalLoaderThreads = new AssetLoaderThread[numNormalLoaderThreads];
for (int i = 0; i < numNormalLoaderThreads; i++)
{
var loaderThread = new AssetLoaderThread(false);
var t = new Thread(loaderThread.Work);
loaderThread.thread = t;
t.Name = "AssetLoaderThread";
t.IsBackground = true;
t.Priority = ThreadPriority.BelowNormal;
t.Start();
normalLoaderThreads[i] = loaderThread;
}
highPriorityLoaderThread = new AssetLoaderThread(true);
var ht = new Thread(highPriorityLoaderThread.Work);
highPriorityLoaderThread.thread = ht;
ht.Name = "AssetLoaderThread_HighPriority";
ht.IsBackground = true;
ht.Priority = ThreadPriority.Normal;
ht.Start();
isSetup = true;
}
}
internal static void QueueAssetLoad<T>(AssetReference<T> asset, LoadPriority priority) where T : class, IAsset
{
AssetManager.SetState(asset.assetIndex, AssetState.Loading);
if (!isSetup)
{
Setup();
}
LoadJob job = new LoadJob()
{
asset = asset.Get(),
assetId = asset.assetIndex,
assetName = asset.GetAssetName(),
priority = priority
};
switch (priority)
{
case LoadPriority.Medium:
mediumPriorityQueue.Add(job);
break;
case LoadPriority.Low:
lowPriorityQueue.Add(job);
break;
case LoadPriority.High:
highPriorityQueue.Add(job);
break;
default:
throw new ArgumentOutOfRangeException(nameof(priority), priority, null);
}
}
internal static void WaitForAssetToLoad(int assetId) {
while (AssetManager.GetState(assetId) == AssetState.Loading) {
Thread.Sleep(10);
}
}
private class AssetLoaderThread
{
public volatile bool loading = false;
public Thread thread;
private readonly bool isHighPriorityOnly;
public AssetLoaderThread(bool isHighPriorityOnly)
{
this.isHighPriorityOnly = isHighPriorityOnly;
}
private void CompleteJob(LoadJob job)
{
try
{
loading = true;
var assetPackage = AssetManager.GetAssetPackageForAsset(job.assetName);
using Stream s = assetPackage.GetUncompressedFileStream(job.assetName);
IAsset asset = job.asset;
asset.LoadFromStream(s);
AssetManager.SetState(job.assetId, AssetState.Loaded);
}
catch (Exception e)
{
Console.WriteLine(e);
AssetManager.SetState(job.assetId, AssetState.FailedToLoad);
}
finally
{
loading = false;
}
}
public void Work()
{
while (true)
{
try
{
if (isHighPriorityOnly)
{
var job = highPriorityQueue.Take();
CompleteJob(job);
}
else
{
if (highPriorityQueue.TryTake(out var hj))
{
CompleteJob(hj);
}
else if (mediumPriorityQueue.TryTake(out var mj))
{
CompleteJob(mj);
}
else if (lowPriorityQueue.TryTake(out var lj))
{
CompleteJob(lj);
}
else
{
BlockingCollection<LoadJob>.TakeFromAny(queues, out var job);
CompleteJob(job);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
}
}