source: binary-improvements2/MapRendering/Web/Handlers/ItemIconHandler.cs@ 386

Last change on this file since 386 was 382, checked in by alloc, 2 years ago

Switched to use SpaceWizards.HttpListener

File size: 4.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using UnityEngine;
6using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
7using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
8using Object = UnityEngine.Object;
9
10namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
11 public class ItemIconHandler : AbsHandler {
12 private readonly Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> ();
13 private readonly bool logMissingFiles;
14
15 private bool loaded;
16
17 static ItemIconHandler () {
18 Instance = null;
19 }
20
21 public ItemIconHandler (bool _logMissingFiles, string _moduleName = null) : base (_moduleName) {
22 logMissingFiles = _logMissingFiles;
23 Instance = this;
24 }
25
26 public static ItemIconHandler Instance { get; private set; }
27
28 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con,
29 int _permissionLevel) {
30 if (!loaded) {
31 _resp.StatusCode = (int) HttpStatusCode.InternalServerError;
32 Log.Out ("Web:IconHandler: Icons not loaded");
33 return;
34 }
35
36 string requestFileName = _requestPath.Remove (0, urlBasePath.Length);
37 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
38
39 if (icons.ContainsKey (requestFileName) && _requestPath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
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: \"" + _requestPath + "\" ");
50 }
51 }
52 }
53
54 public bool LoadIcons () {
55
56 lock (icons) {
57 if (loaded) {
58 return true;
59 }
60
61 MicroStopwatch microStopwatch = new MicroStopwatch ();
62
63 // Get list of used tints for all items
64 Dictionary<string, List<Color>> tintedIcons = new Dictionary<string, List<Color>> ();
65 foreach (ItemClass ic in ItemClass.list) {
66 if (ic != null) {
67 Color tintColor = ic.GetIconTint ();
68 if (tintColor != Color.white) {
69 string name = ic.GetIconName ();
70 if (!tintedIcons.ContainsKey (name)) {
71 tintedIcons.Add (name, new List<Color> ());
72 }
73
74 List<Color> list = tintedIcons [name];
75 list.Add (tintColor);
76 }
77 }
78 }
79
80 try {
81 loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons);
82 } catch (Exception e) {
83 Log.Error ("Failed loading icons from base game");
84 Log.Exception (e);
85 }
86
87 // Load icons from mods
88 foreach (Mod mod in ModManager.GetLoadedMods ()) {
89 try {
90 string modIconsPath = mod.Path + "/ItemIcons";
91 loadIconsFromFolder (modIconsPath, tintedIcons);
92 } catch (Exception e) {
93 Log.Error ("Failed loading icons from mod " + mod.ModInfo.Name.Value);
94 Log.Exception (e);
95 }
96 }
97
98 loaded = true;
99 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
100
101 return true;
102 }
103 }
104
105 private void loadIconsFromFolder (string _path, Dictionary<string, List<Color>> _tintedIcons) {
106 if (Directory.Exists (_path)) {
107 foreach (string file in Directory.GetFiles (_path)) {
108 try {
109 if (file.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
110 string name = Path.GetFileNameWithoutExtension (file);
111 Texture2D tex = new Texture2D (1, 1, TextureFormat.ARGB32, false);
112 if (tex.LoadImage (File.ReadAllBytes (file))) {
113 AddIcon (name, tex, _tintedIcons);
114
115 Object.Destroy (tex);
116 }
117 }
118 } catch (Exception e) {
119 Log.Exception (e);
120 }
121 }
122 }
123 }
124
125 private void AddIcon (string _name, Texture2D _tex, Dictionary<string, List<Color>> _tintedIcons) {
126 icons [_name + "__FFFFFF"] = _tex.EncodeToPNG ();
127
128 if (_tintedIcons.ContainsKey (_name)) {
129 foreach (Color c in _tintedIcons [_name]) {
130 string tintedName = _name + "__" + AllocsUtils.ColorToHex (c);
131 if (!icons.ContainsKey (tintedName)) {
132 Texture2D tintedTex = new Texture2D (_tex.width, _tex.height, TextureFormat.ARGB32, false);
133
134 for (int x = 0; x < _tex.width; x++) {
135 for (int y = 0; y < _tex.height; y++) {
136 tintedTex.SetPixel (x, y, _tex.GetPixel (x, y) * c);
137 }
138 }
139
140 icons [tintedName] = tintedTex.EncodeToPNG ();
141
142 Object.Destroy (tintedTex);
143 }
144 }
145 }
146 }
147 }
148}
Note: See TracBrowser for help on using the repository browser.