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