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

Last change on this file since 253 was 253, checked in by alloc, 9 years ago

Fixes 6_8_10

File size: 3.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Threading;
5
6using UnityEngine;
7
8namespace AllocsFixes.NetConnections.Servers.Web.Handlers
9{
10 public class ItemIconHandler : PathHandler {
11 private string staticPart;
12 private bool logMissingFiles;
13 private Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> ();
14 private bool loaded = false;
15
16 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base(moduleName) {
17 this.staticPart = staticPart;
18 this.logMissingFiles = logMissingFiles;
19 }
20
21 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
22 if (!loaded) {
23 if (!LoadIcons ()) {
24 resp.StatusCode = (int)HttpStatusCode.NotFound;
25 Log.Out ("Web:IconHandler: Could not load icons");
26 return;
27 }
28 }
29
30 string requestFileName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
31 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
32
33 if (icons.ContainsKey (requestFileName) && req.Url.AbsolutePath.ToLower ().EndsWith (".png")) {
34 resp.ContentType = MimeType.GetMimeType (".png");
35
36 byte[] itemIconData = icons [requestFileName];
37
38 resp.ContentLength64 = itemIconData.Length;
39 resp.OutputStream.Write (itemIconData, 0, itemIconData.Length);
40 } else {
41 resp.StatusCode = (int)HttpStatusCode.NotFound;
42 if (logMissingFiles) {
43 Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" ");
44 }
45 return;
46 }
47 }
48
49 private bool LoadIcons () {
50 lock (icons) {
51 if (loaded) {
52 return true;
53 }
54
55 MicroStopwatch microStopwatch = new MicroStopwatch ();
56
57 GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas");
58 if (atlasObj == null) {
59 Log.Error ("Web:IconHandler: Atlas object not found");
60 loaded = true;
61 return false;
62 }
63 DynamicUIAtlas atlas = atlasObj.GetComponent<DynamicUIAtlas> ();
64 if (atlas == null) {
65 Log.Error ("Web:IconHandler: Atlas component not found");
66 loaded = true;
67 return false;
68 }
69
70 Dictionary<string, List<Color>> tintedIcons = new Dictionary<string, List<Color>> ();
71 foreach (ItemClass ic in ItemClass.list) {
72 if (ic != null) {
73 Color tintColor = ic.GetIconTint ();
74 if (tintColor != Color.white) {
75 string name = ic.GetIconName ();
76 if (!tintedIcons.ContainsKey (name)) {
77 tintedIcons.Add (name, new List<Color> ());
78 }
79 List<Color> list = tintedIcons [name];
80 list.Add (tintColor);
81 }
82 }
83 }
84
85 Texture2D atlasTex = atlas.texture as Texture2D;
86
87 foreach (UISpriteData data in atlas.spriteList) {
88 string name = data.name;
89 Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
90 tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height));
91
92 icons.Add (name + "__FFFFFF", tex.EncodeToPNG ());
93
94 if (tintedIcons.ContainsKey (name)) {
95 foreach (Color c in tintedIcons [name]) {
96 Texture2D tintedTex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
97
98 for (int x = 0; x < data.width; x++) {
99 for (int y = 0; y < data.height; y++) {
100 tintedTex.SetPixel (x, y, tex.GetPixel (x, y) * c);
101 }
102 }
103
104 icons.Add (name + "__" + AllocsUtils.ColorToHex (c), tintedTex.EncodeToPNG ());
105
106 UnityEngine.Object.Destroy (tintedTex);
107 }
108 }
109
110 UnityEngine.Object.Destroy (tex);
111 }
112
113 loaded = true;
114 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
115
116 return true;
117 }
118 }
119 }
120}
121
Note: See TracBrowser for help on using the repository browser.