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

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

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

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