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

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

A21 preparations.
NOT COMPATIBLE WITH A20 ANYMORE!

File size: 4.3 KB
RevLine 
[253]1using System;
2using System.Collections.Generic;
[325]3using System.IO;
[253]4using System.Net;
5using UnityEngine;
[325]6using Object = UnityEngine.Object;
[253]7
[325]8namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
[253]9 public class ItemIconHandler : PathHandler {
[325]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;
[267]17 }
18
[367]19 public ItemIconHandler (bool _logMissingFiles, string _moduleName = null) : base (_moduleName) {
[351]20 logMissingFiles = _logMissingFiles;
[325]21 Instance = this;
[253]22 }
23
[325]24 public static ItemIconHandler Instance { get; private set; }
25
[351]26 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
27 int _permissionLevel) {
[253]28 if (!loaded) {
[351]29 _resp.StatusCode = (int) HttpStatusCode.InternalServerError;
[291]30 Log.Out ("Web:IconHandler: Icons not loaded");
31 return;
[253]32 }
33
[367]34 string requestFileName = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length);
[253]35 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
36
[351]37 if (icons.ContainsKey (requestFileName) && _req.Url.AbsolutePath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
38 _resp.ContentType = MimeType.GetMimeType (".png");
[253]39
40 byte[] itemIconData = icons [requestFileName];
41
[351]42 _resp.ContentLength64 = itemIconData.Length;
43 _resp.OutputStream.Write (itemIconData, 0, itemIconData.Length);
[253]44 } else {
[351]45 _resp.StatusCode = (int) HttpStatusCode.NotFound;
[253]46 if (logMissingFiles) {
[351]47 Log.Out ("Web:IconHandler:FileNotFound: \"" + _req.Url.AbsolutePath + "\" ");
[253]48 }
49 }
50 }
51
[267]52 public bool LoadIcons () {
[354]53
[253]54 lock (icons) {
55 if (loaded) {
56 return true;
57 }
58
59 MicroStopwatch microStopwatch = new MicroStopwatch ();
60
[292]61 // Get list of used tints for all items
[253]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 }
[325]71
[253]72 List<Color> list = tintedIcons [name];
73 list.Add (tintColor);
74 }
75 }
76 }
77
[354]78 try {
[369]79 loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons);
[354]80 } catch (Exception e) {
81 Log.Error ("Failed loading icons from base game");
82 Log.Exception (e);
[292]83 }
[253]84
[292]85 // Load icons from mods
86 foreach (Mod mod in ModManager.GetLoadedMods ()) {
87 try {
88 string modIconsPath = mod.Path + "/ItemIcons";
[354]89 loadIconsFromFolder (modIconsPath, tintedIcons);
[292]90 } catch (Exception e) {
[420]91 Log.Error ("Failed loading icons from mod " + mod.Name);
[292]92 Log.Exception (e);
[253]93 }
94 }
95
96 loaded = true;
97 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
98
99 return true;
100 }
101 }
[292]102
[354]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
[292]123 private void AddIcon (string _name, Texture2D _tex, Dictionary<string, List<Color>> _tintedIcons) {
[306]124 icons [_name + "__FFFFFF"] = _tex.EncodeToPNG ();
[292]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
[306]138 icons [tintedName] = tintedTex.EncodeToPNG ();
[292]139
[325]140 Object.Destroy (tintedTex);
[292]141 }
142 }
143 }
144 }
[253]145 }
[325]146}
Note: See TracBrowser for help on using the repository browser.