source: binary-improvements2/WebServer/src/UrlHandlers/ItemIconHandler.cs@ 399

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

Updated logging strings

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