1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Net;
|
---|
4 | using System.Threading;
|
---|
5 |
|
---|
6 | using UnityEngine;
|
---|
7 |
|
---|
8 | namespace 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 | resp.StatusCode = (int)HttpStatusCode.InternalServerError;
|
---|
30 | Log.Out ("Web:IconHandler: Icons not loaded");
|
---|
31 | return;
|
---|
32 | }
|
---|
33 |
|
---|
34 | string requestFileName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
35 | requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
|
---|
36 |
|
---|
37 | if (icons.ContainsKey (requestFileName) && req.Url.AbsolutePath.ToLower ().EndsWith (".png")) {
|
---|
38 | resp.ContentType = MimeType.GetMimeType (".png");
|
---|
39 |
|
---|
40 | byte[] itemIconData = icons [requestFileName];
|
---|
41 |
|
---|
42 | resp.ContentLength64 = itemIconData.Length;
|
---|
43 | resp.OutputStream.Write (itemIconData, 0, itemIconData.Length);
|
---|
44 | } else {
|
---|
45 | resp.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
46 | if (logMissingFiles) {
|
---|
47 | Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" ");
|
---|
48 | }
|
---|
49 | return;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public bool LoadIcons () {
|
---|
54 | lock (icons) {
|
---|
55 | if (loaded) {
|
---|
56 | return true;
|
---|
57 | }
|
---|
58 |
|
---|
59 | MicroStopwatch microStopwatch = new MicroStopwatch ();
|
---|
60 |
|
---|
61 | GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas");
|
---|
62 | if (atlasObj == null) {
|
---|
63 | Log.Error ("Web:IconHandler: Atlas object not found");
|
---|
64 | loaded = true;
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | DynamicUIAtlas atlas = atlasObj.GetComponent<DynamicUIAtlas> ();
|
---|
68 | if (atlas == null) {
|
---|
69 | Log.Error ("Web:IconHandler: Atlas component not found");
|
---|
70 | loaded = true;
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | string textureResourceName = atlas.PrebakedAtlas;
|
---|
75 | List<UISpriteData> sprites;
|
---|
76 | int elementWidth, elementHeight;
|
---|
77 | Texture2D atlasTex;
|
---|
78 |
|
---|
79 | if (!DynamicUIAtlasTools.ReadPrebakedAtlasDescriptor (textureResourceName, out sprites, out elementWidth, out elementHeight)) {
|
---|
80 | SdtdConsole.Instance.Output ("Web:IconHandler: Could not read dynamic atlas descriptor");
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (!DynamicUIAtlasTools.ReadPrebakedAtlasTexture (textureResourceName, out atlasTex)) {
|
---|
85 | SdtdConsole.Instance.Output ("Web:IconHandler: Could not read dynamic atlas texture");
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | Dictionary<string, List<Color>> tintedIcons = new Dictionary<string, List<Color>> ();
|
---|
90 | foreach (ItemClass ic in ItemClass.list) {
|
---|
91 | if (ic != null) {
|
---|
92 | Color tintColor = ic.GetIconTint ();
|
---|
93 | if (tintColor != Color.white) {
|
---|
94 | string name = ic.GetIconName ();
|
---|
95 | if (!tintedIcons.ContainsKey (name)) {
|
---|
96 | tintedIcons.Add (name, new List<Color> ());
|
---|
97 | }
|
---|
98 | List<Color> list = tintedIcons [name];
|
---|
99 | list.Add (tintColor);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | foreach (UISpriteData data in sprites) {
|
---|
105 | string name = data.name;
|
---|
106 | Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
|
---|
107 | tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height));
|
---|
108 |
|
---|
109 | icons.Add (name + "__FFFFFF", tex.EncodeToPNG ());
|
---|
110 |
|
---|
111 | if (tintedIcons.ContainsKey (name)) {
|
---|
112 | foreach (Color c in tintedIcons [name]) {
|
---|
113 | string tintedName = name + "__" + AllocsUtils.ColorToHex (c);
|
---|
114 | if (!icons.ContainsKey (tintedName)) {
|
---|
115 | Texture2D tintedTex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
|
---|
116 |
|
---|
117 | for (int x = 0; x < data.width; x++) {
|
---|
118 | for (int y = 0; y < data.height; y++) {
|
---|
119 | tintedTex.SetPixel (x, y, tex.GetPixel (x, y) * c);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | icons.Add (tintedName, tintedTex.EncodeToPNG ());
|
---|
124 |
|
---|
125 | UnityEngine.Object.Destroy (tintedTex);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | UnityEngine.Object.Destroy (tex);
|
---|
131 | }
|
---|
132 |
|
---|
133 | Resources.UnloadAsset(atlasTex);
|
---|
134 |
|
---|
135 | loaded = true;
|
---|
136 | Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
|
---|
137 |
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|