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

Last change on this file since 434 was 420, checked in by alloc, 20 months ago

A21 preparations.
NOT COMPATIBLE WITH A20 ANYMORE!

File size: 4.3 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 bool loaded;
14
15 static ItemIconHandler () {
16 Instance = null;
17 }
18
19 public ItemIconHandler (bool _logMissingFiles, string _moduleName = null) : base (_moduleName) {
20 logMissingFiles = _logMissingFiles;
21 Instance = this;
22 }
23
24 public static ItemIconHandler Instance { get; private set; }
25
26 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
27 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, urlBasePath.Length);
35 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
36
37 if (icons.ContainsKey (requestFileName) && _req.Url.AbsolutePath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
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 }
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 Color tintColor = ic.GetIconTint ();
66 if (tintColor != Color.white) {
67 string name = ic.GetIconName ();
68 if (!tintedIcons.ContainsKey (name)) {
69 tintedIcons.Add (name, new List<Color> ());
70 }
71
72 List<Color> list = tintedIcons [name];
73 list.Add (tintColor);
74 }
75 }
76 }
77
78 try {
79 loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons);
80 } catch (Exception e) {
81 Log.Error ("Failed loading icons from base game");
82 Log.Exception (e);
83 }
84
85 // Load icons from mods
86 foreach (Mod mod in ModManager.GetLoadedMods ()) {
87 try {
88 string modIconsPath = mod.Path + "/ItemIcons";
89 loadIconsFromFolder (modIconsPath, tintedIcons);
90 } catch (Exception e) {
91 Log.Error ("Failed loading icons from mod " + mod.Name);
92 Log.Exception (e);
93 }
94 }
95
96 loaded = true;
97 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
98
99 return true;
100 }
101 }
102
103 private void loadIconsFromFolder (string _path, Dictionary<string, List<Color>> _tintedIcons) {
104 if (Directory.Exists (_path)) {
105 foreach (string file in Directory.GetFiles (_path)) {
106 try {
107 if (file.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
108 string name = Path.GetFileNameWithoutExtension (file);
109 Texture2D tex = new Texture2D (1, 1, TextureFormat.ARGB32, false);
110 if (tex.LoadImage (File.ReadAllBytes (file))) {
111 AddIcon (name, tex, _tintedIcons);
112
113 Object.Destroy (tex);
114 }
115 }
116 } catch (Exception e) {
117 Log.Exception (e);
118 }
119 }
120 }
121 }
122
123 private void AddIcon (string _name, Texture2D _tex, Dictionary<string, List<Color>> _tintedIcons) {
124 icons [_name + "__FFFFFF"] = _tex.EncodeToPNG ();
125
126 if (_tintedIcons.ContainsKey (_name)) {
127 foreach (Color c in _tintedIcons [_name]) {
128 string tintedName = _name + "__" + AllocsUtils.ColorToHex (c);
129 if (!icons.ContainsKey (tintedName)) {
130 Texture2D tintedTex = new Texture2D (_tex.width, _tex.height, TextureFormat.ARGB32, false);
131
132 for (int x = 0; x < _tex.width; x++) {
133 for (int y = 0; y < _tex.height; y++) {
134 tintedTex.SetPixel (x, y, _tex.GetPixel (x, y) * c);
135 }
136 }
137
138 icons [tintedName] = tintedTex.EncodeToPNG ();
139
140 Object.Destroy (tintedTex);
141 }
142 }
143 }
144 }
145 }
146}
Note: See TracBrowser for help on using the repository browser.