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
198 lines (161 loc) · 5.37 KB
/
CommunityEntity.UI.ServerImage.cs
File metadata and controls
198 lines (161 loc) · 5.37 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
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<UnityEngine.UI.MaskableGraphic> MaskableGraphics = new List<MaskableGraphic>();
}
private class CachedTexture
{
public Texture2D Texture;
public Sprite Sprite;
public Sprite GetOrCreateSprite()
{
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;
}
}
}
[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.MaskableGraphics)
{
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)
{
var cachedImage = GetCachedTexture(id);
// Cached in memory
if (cachedImage != null)
{
return cachedImage.GetOrCreateSprite();
}
var bytes = FileStorage.client.Get(id, FileStorage.Type.png, net.ID);
// Cached on disk
if (bytes != null)
{
cachedImage = StoreCachedTexture(id, bytes);
return cachedImage.GetOrCreateSprite();
}
// 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 )
{
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.MaskableGraphics.Add( component );
return;
}
}
ApplyCachedTextureToImage( component, texture );
}
private void ApplyCachedTextureToImage( UnityEngine.UI.MaskableGraphic component, CachedTexture texture )
{
var image = component as UnityEngine.UI.Image;
if ( image )
{
image.sprite = texture.GetOrCreateSprite();
return;
}
var rawImage = component 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