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

Last change on this file since 387 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
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 {
[382]9 public class ItemIconHandler : AbsHandler {
[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
[387]26 public override void HandleRequest (RequestContext _context) {
[253]27 if (!loaded) {
[387]28 _context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
[291]29 Log.Out ("Web:IconHandler: Icons not loaded");
30 return;
[253]31 }
32
[387]33 string requestFileName = _context.RequestPath.Remove (0, urlBasePath.Length);
[253]34 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
35
[387]36 if (icons.ContainsKey (requestFileName) && _context.RequestPath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {
37 _context.Response.ContentType = MimeType.GetMimeType (".png");
[253]38
39 byte[] itemIconData = icons [requestFileName];
40
[387]41 _context.Response.ContentLength64 = itemIconData.Length;
42 _context.Response.OutputStream.Write (itemIconData, 0, itemIconData.Length);
[253]43 } else {
[387]44 _context.Response.StatusCode = (int) HttpStatusCode.NotFound;
[253]45 if (logMissingFiles) {
[387]46 Log.Out ("Web:IconHandler:FileNotFound: \"" + _context.RequestPath + "\" ");
[253]47 }
48 }
49 }
50
[267]51 public bool LoadIcons () {
[354]52
[253]53 lock (icons) {
54 if (loaded) {
55 return true;
56 }
57
58 MicroStopwatch microStopwatch = new MicroStopwatch ();
59
[292]60 // Get list of used tints for all items
[253]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 }
[325]70
[253]71 List<Color> list = tintedIcons [name];
72 list.Add (tintColor);
73 }
74 }
75 }
76
[354]77 try {
[369]78 loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons);
[354]79 } catch (Exception e) {
80 Log.Error ("Failed loading icons from base game");
81 Log.Exception (e);
[292]82 }
[253]83
[292]84 // Load icons from mods
85 foreach (Mod mod in ModManager.GetLoadedMods ()) {
86 try {
87 string modIconsPath = mod.Path + "/ItemIcons";
[354]88 loadIconsFromFolder (modIconsPath, tintedIcons);
[292]89 } catch (Exception e) {
[354]90 Log.Error ("Failed loading icons from mod " + mod.ModInfo.Name.Value);
[292]91 Log.Exception (e);
[253]92 }
93 }
94
95 loaded = true;
96 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds);
97
98 return true;
99 }
100 }
[292]101
[354]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
[292]122 private void AddIcon (string _name, Texture2D _tex, Dictionary<string, List<Color>> _tintedIcons) {
[306]123 icons [_name + "__FFFFFF"] = _tex.EncodeToPNG ();
[292]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
[306]137 icons [tintedName] = tintedTex.EncodeToPNG ();
[292]138
[325]139 Object.Destroy (tintedTex);
[292]140 }
141 }
142 }
143 }
[253]144 }
[325]145}
Note: See TracBrowser for help on using the repository browser.