Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Asset.cs
Go to the documentation of this file.
1using System;
4
5namespace ReLogic.Content;
6
7public sealed class Asset<T> : IAsset, IDisposable where T : class
8{
9 public static readonly Asset<T> Empty = new Asset<T>("");
10
11 private T ownValue;
12
13 public static T DefaultValue { get; set; }
14
15 public string Name { get; private set; }
16
17 public bool IsLoaded => State == AssetState.Loaded;
18
19 public AssetState State { get; private set; }
20
21 public bool IsDisposed { get; private set; }
22
23 public IContentSource Source { get; private set; }
24
25 public T Value
26 {
27 get
28 {
29 if (!IsLoaded)
30 {
31 return DefaultValue;
32 }
33 return ownValue;
34 }
35 }
36
37 internal Action Continuation { get; set; }
38
39 Action IAsset.Continuation
40 {
41 set
42 {
44 }
45 }
46
47 public Action Wait { get; internal set; }
48
49 internal Asset(string name)
50 {
51 State = AssetState.NotLoaded;
52 Name = name;
53 }
54
55 public static explicit operator T(Asset<T> asset)
56 {
57 return asset.Value;
58 }
59
60 internal void Unload()
61 {
63 State = AssetState.NotLoaded;
64 ownValue = null;
65 Source = null;
66 }
67
69 {
70 if (value == null)
71 {
72 throw new ArgumentNullException("value");
73 }
76 Source = source;
78 State = AssetState.Loaded;
79 }
80
81 internal void SetToLoadingState()
82 {
83 State = AssetState.Loading;
84 }
85
86 private void Dispose(bool disposing)
87 {
88 if (IsDisposed)
89 {
90 return;
91 }
92 if (disposing && ownValue != null)
93 {
94 IDisposable disposable = ownValue as IDisposable;
95 if (IsLoaded)
96 {
97 disposable?.Dispose();
98 }
99 ownValue = null;
100 }
101 IsDisposed = true;
102 }
103
104 public void Dispose()
105 {
106 Dispose(disposing: true);
107 }
108}
void SubmitLoadedContent(T value, IContentSource source)
Definition Asset.cs:68
static T DefaultValue
Definition Asset.cs:13
IContentSource Source
Definition Asset.cs:23
Asset(string name)
Definition Asset.cs:49
void Dispose(bool disposing)
Definition Asset.cs:86
Action Continuation
Definition Asset.cs:37
void SetToLoadingState()
Definition Asset.cs:81
static void MemoryBarrier()
Definition Thread.cs:827