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

Last change on this file since 364 was 354, checked in by alloc, 5 years ago

A18 preps

File size: 4.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using UnityEngine;
6using Object = UnityEngine.Object;
7
8namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
9 public class ItemIconHandler : PathHandler {
10 private readonly Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> ();
11 private readonly bool logMissingFiles;
12
13 private readonly string staticPart;
14 private bool loaded;
15
16 static ItemIconHandler () {
17 Instance = null;
18 }
19
20 public ItemIconHandler (string _staticPart, bool _logMissingFiles, string _moduleName = null) : base (_moduleName) {
21 staticPart = _staticPart;
22 logMissingFiles = _logMissingFiles;
23 Instance = this;
24 }
25
26 public static ItemIconHandler Instance { get; private set; }
27
28 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
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 = _req.Url.AbsolutePath.Remove (0, staticPart.Length);
37 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
38
39 if (icons.ContainsKey (requestFileName) && _req.Url.AbsolutePath.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: \"" + _req.Url.AbsolutePath + "\" ");
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 (Utils.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.