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

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

Update Chat stuff to 14.6 b26

File size: 3.9 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 static ItemIconHandler instance = null;
12 public static ItemIconHandler Instance {
13 get { return instance; }
14 }
15
16 private string staticPart;
17 private bool logMissingFiles;
18 private Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> ();
19 private bool loaded = false;
20
21 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base(moduleName) {
22 this.staticPart = staticPart;
23 this.logMissingFiles = logMissingFiles;
24 ItemIconHandler.instance = this;
25 }
26
27 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
28 if (!loaded) {
29 if (!LoadIcons ()) {
30 resp.StatusCode = (int)HttpStatusCode.NotFound;
31 Log.Out ("Web:IconHandler: Could not load icons");
32 return;
33 }
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.ToLower ().EndsWith (".png")) {
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 return;
52 }
53 }
54
55 public bool LoadIcons () {
56 lock (icons) {
57 if (loaded) {
58 return true;
59 }
60
61 MicroStopwatch microStopwatch = new MicroStopwatch ();
62
63 GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas");
64 if (atlasObj == null) {
65 Log.Error ("Web:IconHandler: Atlas object not found");
66 loaded = true;
67 return false;
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 Dictionary<string, List<Color>> tintedIcons = new Dictionary<string, List<Color>> ();
77 foreach (ItemClass ic in ItemClass.list) {
78 if (ic != null) {
79 Color tintColor = ic.GetIconTint ();
80 if (tintColor != Color.white) {
81 string name = ic.GetIconName ();
82 if (!tintedIcons.ContainsKey (name)) {
83 tintedIcons.Add (name, new List<Color> ());
84 }
85 List<Color> list = tintedIcons [name];
86 list.Add (tintColor);
87 }
88 }
89 }
90
91 Texture2D atlasTex = atlas.texture as Texture2D;
92
93 foreach (UISpriteData data in atlas.spriteList) {
94 string name = data.name;
95 Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
96 tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height));
97
98 icons.Add (name + "__FFFFFF", tex.EncodeToPNG ());
99
100 if (tintedIcons.ContainsKey (name)) {
101 foreach (Color c in tintedIcons [name]) {
102 string tintedName = name + "__" + AllocsUtils.ColorToHex (c);
103 if (!icons.ContainsKey (tintedName)) {
104 Texture2D tintedTex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
105
106 for (int x = 0; x < data.width; x++) {
107 for (int y = 0; y < data.height; y++) {
108 tintedTex.SetPixel (x, y, tex.GetPixel (x, y) * c);
109 }
110 }
111
112 icons.Add (tintedName, tintedTex.EncodeToPNG ());
113
114 UnityEngine.Object.Destroy (tintedTex);
115 }
116 }
117 }
118
119 UnityEngine.Object.Destroy (tex);
120 }
121
122 loaded = true;
123 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
124
125 return true;
126 }
127 }
128 }
129}
130
Note: See TracBrowser for help on using the repository browser.