source: binary-improvements/MapRendering/Web/Handlers/ItemIconHandler.cs@ 345

Last change on this file since 345 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 5.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using UnityEngine;
6using Object = UnityEngine.Object;
7
8namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
9 public class ItemIconHandler : PathHandler {
10 private readonly Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> ();
11 private readonly bool logMissingFiles;
12
13 private readonly string staticPart;
14 private bool loaded;
15
16 static ItemIconHandler () {
17 Instance = null;
18 }
19
20 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base (moduleName) {
21 this.staticPart = staticPart;
22 this.logMissingFiles = logMissingFiles;
23 Instance = this;
24 }
25
26 public static ItemIconHandler Instance { get; private set; }
27
28 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
29 int permissionLevel) {
30 if (!loaded) {
31 resp.StatusCode = (int) HttpStatusCode.InternalServerError;
32 Log.Out ("Web:IconHandler: Icons not loaded");
33 return;
34 }
35
36 string requestFileName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
37 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
38
39 if (icons.ContainsKey (requestFileName) && req.Url.AbsolutePath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
40 resp.ContentType = MimeType.GetMimeType (".png");
41
42 byte[] itemIconData = icons [requestFileName];
43
44 resp.ContentLength64 = itemIconData.Length;
45 resp.OutputStream.Write (itemIconData, 0, itemIconData.Length);
46 } else {
47 resp.StatusCode = (int) HttpStatusCode.NotFound;
48 if (logMissingFiles) {
49 Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" ");
50 }
51 }
52 }
53
54 public bool LoadIcons () {
55 lock (icons) {
56 if (loaded) {
57 return true;
58 }
59
60 MicroStopwatch microStopwatch = new MicroStopwatch ();
61
62 GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas");
63 if (atlasObj == null) {
64 Log.Error ("Web:IconHandler: Atlas object not found");
65 loaded = true;
66 return false;
67 }
68
69 DynamicUIAtlas atlas = atlasObj.GetComponent<DynamicUIAtlas> ();
70 if (atlas == null) {
71 Log.Error ("Web:IconHandler: Atlas component not found");
72 loaded = true;
73 return false;
74 }
75
76 string textureResourceName = atlas.PrebakedAtlas;
77 List<UISpriteData> sprites;
78 int elementWidth, elementHeight;
79 Texture2D atlasTex;
80
81 if (!DynamicUIAtlasTools.ReadPrebakedAtlasDescriptor (textureResourceName, out sprites,
82 out elementWidth, out elementHeight)) {
83 SdtdConsole.Instance.Output ("Web:IconHandler: Could not read dynamic atlas descriptor");
84 return false;
85 }
86
87 if (!DynamicUIAtlasTools.ReadPrebakedAtlasTexture (textureResourceName, out atlasTex)) {
88 SdtdConsole.Instance.Output ("Web:IconHandler: Could not read dynamic atlas texture");
89 return false;
90 }
91
92 // Get list of used tints for all items
93 Dictionary<string, List<Color>> tintedIcons = new Dictionary<string, List<Color>> ();
94 foreach (ItemClass ic in ItemClass.list) {
95 if (ic != null) {
96 Color tintColor = ic.GetIconTint ();
97 if (tintColor != Color.white) {
98 string name = ic.GetIconName ();
99 if (!tintedIcons.ContainsKey (name)) {
100 tintedIcons.Add (name, new List<Color> ());
101 }
102
103 List<Color> list = tintedIcons [name];
104 list.Add (tintColor);
105 }
106 }
107 }
108
109 // Load icons from vanilla
110 foreach (UISpriteData data in sprites) {
111 string name = data.name;
112 Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
113 tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width,
114 data.height));
115
116 AddIcon (name, tex, tintedIcons);
117
118 Object.Destroy (tex);
119 }
120
121 Resources.UnloadAsset (atlasTex);
122
123 // Load icons from mods
124 foreach (Mod mod in ModManager.GetLoadedMods ()) {
125 try {
126 string modIconsPath = mod.Path + "/ItemIcons";
127 if (Directory.Exists (modIconsPath)) {
128 foreach (string file in Directory.GetFiles (modIconsPath)) {
129 try {
130 if (file.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
131 string name = Path.GetFileNameWithoutExtension (file);
132 Texture2D tex = new Texture2D (1, 1, TextureFormat.ARGB32, false);
133 if (tex.LoadImage (File.ReadAllBytes (file))) {
134 if (tex.width == elementWidth && tex.height == elementHeight) {
135 AddIcon (name, tex, tintedIcons);
136 }
137
138 Object.Destroy (tex);
139 }
140 }
141 } catch (Exception e) {
142 Log.Exception (e);
143 }
144 }
145 }
146 } catch (Exception e) {
147 Log.Exception (e);
148 }
149 }
150
151 loaded = true;
152 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
153
154 return true;
155 }
156 }
157
158 private void AddIcon (string _name, Texture2D _tex, Dictionary<string, List<Color>> _tintedIcons) {
159 icons [_name + "__FFFFFF"] = _tex.EncodeToPNG ();
160
161 if (_tintedIcons.ContainsKey (_name)) {
162 foreach (Color c in _tintedIcons [_name]) {
163 string tintedName = _name + "__" + AllocsUtils.ColorToHex (c);
164 if (!icons.ContainsKey (tintedName)) {
165 Texture2D tintedTex = new Texture2D (_tex.width, _tex.height, TextureFormat.ARGB32, false);
166
167 for (int x = 0; x < _tex.width; x++) {
168 for (int y = 0; y < _tex.height; y++) {
169 tintedTex.SetPixel (x, y, _tex.GetPixel (x, y) * c);
170 }
171 }
172
173 icons [tintedName] = tintedTex.EncodeToPNG ();
174
175 Object.Destroy (tintedTex);
176 }
177 }
178 }
179 }
180 }
181}
Note: See TracBrowser for help on using the repository browser.