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 |
|
---|
9 | namespace AllocsFixes.NetConnections.Servers.Web
|
---|
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 |
|
---|
18 | public ItemIconHandler (string staticPart, bool logMissingFiles) {
|
---|
19 | this.staticPart = staticPart;
|
---|
20 | this.logMissingFiles = logMissingFiles;
|
---|
21 | }
|
---|
22 |
|
---|
23 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user) {
|
---|
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 () {
|
---|
48 | loaded = true;
|
---|
49 |
|
---|
50 | GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas");
|
---|
51 | if (atlasObj == null) {
|
---|
52 | Log.Error ("Web:IconHandler: Atlas object not found");
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 | DynamicUIAtlas atlas = atlasObj.GetComponent<DynamicUIAtlas> ();
|
---|
56 | if (atlas == null) {
|
---|
57 | Log.Error ("Web:IconHandler: Atlas component not found");
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | Texture2D atlasTex = atlas.texture as Texture2D;
|
---|
62 |
|
---|
63 | foreach (UISpriteData data in atlas.spriteList) {
|
---|
64 | string name = data.name;
|
---|
65 | Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
|
---|
66 | tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height));
|
---|
67 | byte[] pixData = tex.EncodeToPNG ();
|
---|
68 |
|
---|
69 | icons.Add (name, pixData);
|
---|
70 | UnityEngine.Object.Destroy (tex);
|
---|
71 | }
|
---|
72 |
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|