| [238] | 1 | using System;
 | 
|---|
 | 2 | using System.Collections.Generic;
 | 
|---|
 | 3 | using System.IO;
 | 
|---|
 | 4 | using System.Net;
 | 
|---|
 | 5 | using System.Threading;
 | 
|---|
 | 6 | 
 | 
|---|
 | 7 | using UnityEngine;
 | 
|---|
 | 8 | 
 | 
|---|
| [244] | 9 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers
 | 
|---|
| [238] | 10 | {
 | 
|---|
 | 11 |         public class ItemIconHandler : PathHandler
 | 
|---|
 | 12 |         {
 | 
|---|
 | 13 |                 private string staticPart;
 | 
|---|
 | 14 |                 private bool logMissingFiles;
 | 
|---|
 | 15 |                 private Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> ();
 | 
|---|
 | 16 |                 private bool loaded = false;
 | 
|---|
 | 17 | 
 | 
|---|
| [244] | 18 |                 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base(moduleName) {
 | 
|---|
| [238] | 19 |                         this.staticPart = staticPart;
 | 
|---|
 | 20 |                         this.logMissingFiles = logMissingFiles;
 | 
|---|
 | 21 |                 }
 | 
|---|
 | 22 | 
 | 
|---|
| [244] | 23 |                 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
 | 
|---|
| [238] | 24 |                         if (!loaded) {
 | 
|---|
 | 25 |                                 if (!LoadIcons ()) {
 | 
|---|
 | 26 |                                         resp.StatusCode = (int)HttpStatusCode.NotFound;
 | 
|---|
 | 27 |                                         Log.Out ("Web:IconHandler: Could not load icons");
 | 
|---|
 | 28 |                                         return;
 | 
|---|
 | 29 |                                 }
 | 
|---|
 | 30 |                         }
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 |                         string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);
 | 
|---|
 | 33 |                         fn = fn.Remove (fn.LastIndexOf ('.'));
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |                         if (icons.ContainsKey (fn)) {
 | 
|---|
 | 36 |                                 resp.ContentType = MimeType.GetMimeType (".png");
 | 
|---|
 | 37 |                                 resp.ContentLength64 = icons [fn].Length;
 | 
|---|
 | 38 |                                 resp.OutputStream.Write (icons [fn], 0, icons [fn].Length);
 | 
|---|
 | 39 |                         } else {
 | 
|---|
 | 40 |                                 resp.StatusCode = (int)HttpStatusCode.NotFound;
 | 
|---|
 | 41 |                                 if (logMissingFiles)
 | 
|---|
 | 42 |                                         Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" ");
 | 
|---|
 | 43 |                                 return;
 | 
|---|
 | 44 |                         }
 | 
|---|
 | 45 |                 }
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |                 private bool LoadIcons () {
 | 
|---|
| [242] | 48 |                         lock (icons) {
 | 
|---|
 | 49 |                                 if (loaded) {
 | 
|---|
 | 50 |                                         return true;
 | 
|---|
 | 51 |                                 }
 | 
|---|
| [238] | 52 | 
 | 
|---|
| [242] | 53 |                                 GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas");
 | 
|---|
 | 54 |                                 if (atlasObj == null) {
 | 
|---|
 | 55 |                                         Log.Error ("Web:IconHandler: Atlas object not found");
 | 
|---|
 | 56 |                                         loaded = true;
 | 
|---|
 | 57 |                                         return false;
 | 
|---|
 | 58 |                                 }
 | 
|---|
 | 59 |                                 DynamicUIAtlas atlas = atlasObj.GetComponent<DynamicUIAtlas> ();
 | 
|---|
 | 60 |                                 if (atlas == null) {
 | 
|---|
 | 61 |                                         Log.Error ("Web:IconHandler: Atlas component not found");
 | 
|---|
 | 62 |                                         loaded = true;
 | 
|---|
 | 63 |                                         return false;
 | 
|---|
 | 64 |                                 }
 | 
|---|
| [238] | 65 | 
 | 
|---|
| [242] | 66 |                                 Texture2D atlasTex = atlas.texture as Texture2D;
 | 
|---|
| [238] | 67 | 
 | 
|---|
| [242] | 68 |                                 foreach (UISpriteData data in atlas.spriteList) {
 | 
|---|
 | 69 |                                         string name = data.name;
 | 
|---|
 | 70 |                                         Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
 | 
|---|
 | 71 |                                         tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height));
 | 
|---|
 | 72 |                                         byte[] pixData = tex.EncodeToPNG ();
 | 
|---|
| [238] | 73 | 
 | 
|---|
| [242] | 74 |                                         icons.Add (name, pixData);
 | 
|---|
 | 75 |                                         UnityEngine.Object.Destroy (tex);
 | 
|---|
 | 76 |                                 }
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 |                                 loaded = true;
 | 
|---|
 | 79 |                                 Log.Out ("Web:IconHandler: Icons loaded");
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |                                 return true;
 | 
|---|
| [238] | 82 |                         }
 | 
|---|
 | 83 |                 }
 | 
|---|
 | 84 |         }
 | 
|---|
 | 85 | }
 | 
|---|
 | 86 | 
 | 
|---|