forked from Facepunch/Rust.Community
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunityEntity.UI.ServerImage.cs
More file actions
239 lines (197 loc) · 6.94 KB
/
CommunityEntity.UI.ServerImage.cs
File metadata and controls
239 lines (197 loc) · 6.94 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Facepunch.Extend;
using System.IO;
using MaskableGraphic = UnityEngine.UI.MaskableGraphic;
#if CLIENT
public partial class CommunityEntity
{
private static Dictionary<uint, ImageRequest> requestingTextureImages = new Dictionary<uint, ImageRequest>();
private static Dictionary<uint, CachedTexture> _textureCache = new Dictionary<uint, CachedTexture>();
private class ImageRequest
{
public List<Entry> Entries = new List<Entry>();
public struct Entry
{
public UnityEngine.UI.MaskableGraphic graphic;
public Vector4? slice;
}
}
private class CachedTexture
{
public Texture2D Texture;
// Default non-sliced sprite
public Sprite Sprite;
// Cache sprites created with slicing settings
public Dictionary<Vector4, Sprite> SlicedSprites;
public Sprite GetOrCreateSprite(Vector4? slice = null)
{
// Create sliced sprite if Vector4 settings provided
if(slice != null)
{
if (SlicedSprites != null && SlicedSprites.TryGetValue(slice.Value, out var slicedSprite))
return slicedSprite;
SlicedSprites ??= new Dictionary<Vector4, Sprite>();
// 100 pixels per unit is the same as the constructor below uses for non-sliced sprites
slicedSprite = Sprite.Create( Texture, new Rect( 0, 0, Texture.width, Texture.height ), new Vector2( 0.5f, 0.5f ), 100, 0, SpriteMeshType.FullRect, slice.Value);
SlicedSprites.Add(slice.Value, slicedSprite);
return slicedSprite;
}
// Otherwise create a normal sprite
if (Sprite == null)
{
Sprite = Sprite.Create(Texture, new Rect(0, 0, Texture.width, Texture.height), new Vector2(0.5f, 0.5f));
}
return Sprite;
}
public void Destroy()
{
if ( Texture != null )
{
UnityEngine.Object.Destroy( Texture );
Texture = null;
}
if ( Sprite != null )
{
UnityEngine.Object.Destroy( Sprite );
Sprite = null;
}
if (SlicedSprites != null)
{
foreach (var slicedSprite in SlicedSprites)
{
UnityEngine.Object.Destroy( slicedSprite.Value );
}
SlicedSprites = null;
}
}
}
[RPC_Client]
public void CL_ReceiveFilePng( BaseEntity.RPCMessage msg )
{
var textureID = msg.read.UInt32();
var bytes = msg.read.BytesWithSize();
if ( bytes == null ) return;
if ( FileStorage.client.Store( bytes, FileStorage.Type.png, net.ID ) != textureID )
{
Log( "Client/Server FileStorage CRC differs" );
}
var texture = StoreCachedTexture( textureID, bytes );
// Get existing image request
var request = RequestImage(textureID, false);
if (request != null)
{
foreach (var c in request.Entries)
{
ApplyCachedTextureToImage(c, texture);
}
// Remove request
requestingTextureImages.Remove(textureID);
}
}
private static CachedTexture GetCachedTexture( uint textureId )
{
_textureCache.TryGetValue( textureId, out var texture );
return texture;
}
private CachedTexture StoreCachedTexture( uint textureId, byte[] bytes )
{
var texture = new CachedTexture()
{
Texture = new Texture2D( 1, 1, TextureFormat.RGBA32, false ),
};
texture.Texture.LoadImage( bytes );
// Check for duplicate textureId and unload old one ( dont think they will conflict but safety first)
if ( _textureCache.TryGetValue( textureId, out var oldTexture ) )
{
oldTexture.Destroy();
}
_textureCache[ textureId ] = texture;
return texture;
}
// Request an image or get the existing request
private ImageRequest RequestImage(uint textureID, bool createRequest)
{
if (!requestingTextureImages.TryGetValue(textureID, out var request) && createRequest)
{
request = new ImageRequest();
requestingTextureImages[textureID] = request;
RequestFileFromServer(textureID, FileStorage.Type.png, "CL_ReceiveFilePng");
}
return request;
}
public Sprite GetOrRequestSprite(uint id, Vector4? slice = null)
{
var cachedImage = GetCachedTexture(id);
// Cached in memory
if (cachedImage != null)
{
return cachedImage.GetOrCreateSprite(slice);
}
var bytes = FileStorage.client.Get(id, FileStorage.Type.png, net.ID);
// Cached on disk
if (bytes != null)
{
cachedImage = StoreCachedTexture(id, bytes);
return cachedImage.GetOrCreateSprite(slice);
}
// Create request for texture
// NOTE: don't worry about callback since ItemIcon should be polling every frame
RequestImage(id, true);
return null;
}
public void ApplyTextureToImage( UnityEngine.UI.MaskableGraphic component, uint textureID, Vector4? slice = null )
{
var texture = GetCachedTexture( textureID );
if ( texture == null )
{
var bytes = FileStorage.client.Get( textureID, FileStorage.Type.png, net.ID );
if ( bytes != null )
{
texture = StoreCachedTexture( textureID, bytes );
}
else
{
var request = RequestImage(textureID, true);
request.Entries.Add( new ImageRequest.Entry()
{
graphic = component,
slice = slice
});
return;
}
}
ApplyCachedTextureToImage( new ImageRequest.Entry()
{
graphic = component,
slice = slice
}, texture );
}
private void ApplyCachedTextureToImage( ImageRequest.Entry entry, CachedTexture texture )
{
var image = entry.graphic as UnityEngine.UI.Image;
if ( image )
{
image.sprite = texture.GetOrCreateSprite(entry.slice);
return;
}
var rawImage = entry.graphic as UnityEngine.UI.RawImage;
if ( rawImage )
{
rawImage.texture = texture.Texture;
}
}
private static void UnloadTextureCache()
{
foreach( var texture in _textureCache.Values )
{
texture.Destroy();
}
_textureCache.Clear();
// Clear image requests as well
requestingTextureImages.Clear();
}
}
#endif