- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/UrlHandlers/ItemIconHandler.cs
r399 r402 3 3 using System.IO; 4 4 using System.Net; 5 using AllocsFixes;6 5 using UnityEngine; 7 6 using Object = UnityEngine.Object; … … 45 44 _context.Response.StatusCode = (int) HttpStatusCode.NotFound; 46 45 if (logMissingFiles) { 47 Log.Out ( "[Web] IconHandler: FileNotFound: \"" + _context.RequestPath + "\" ");46 Log.Out ($"[Web] IconHandler: FileNotFound: \"{_context.RequestPath}\" "); 48 47 } 49 48 } 49 } 50 51 private class LoadingStats { 52 public int Files; 53 public int Tints; 54 public readonly MicroStopwatch MswTotal = new MicroStopwatch (false); 55 public readonly MicroStopwatch MswLoading = new MicroStopwatch (false); 56 public readonly MicroStopwatch MswEncoding = new MicroStopwatch (false); 57 public readonly MicroStopwatch MswTinting = new MicroStopwatch (false); 50 58 } 51 59 … … 57 65 } 58 66 59 MicroStopwatch microStopwatch = new MicroStopwatch (); 67 LoadingStats stats = new LoadingStats (); 68 stats?.MswTotal.Start (); 60 69 61 70 // Get list of used tints for all items … … 72 81 73 82 string name = ic.GetIconName (); 74 if (!tintedIcons.ContainsKey (name)) { 75 tintedIcons.Add (name, new List<Color> ()); 83 if (!tintedIcons.TryGetValue (name, out List<Color> tintsList)) { 84 tintsList = new List<Color> (); 85 tintedIcons.Add (name, tintsList); 76 86 } 77 87 78 List<Color> list = tintedIcons [name]; 79 list.Add (tintColor); 88 tintsList.Add (tintColor); 80 89 } 81 90 82 91 try { 83 loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons );92 loadIconsFromFolder (GameIO.GetGameDir ("Data/ItemIcons"), tintedIcons, stats); 84 93 } catch (Exception e) { 85 94 Log.Error ("[Web] Failed loading icons from base game"); … … 90 99 foreach (Mod mod in ModManager.GetLoadedMods ()) { 91 100 try { 92 string modIconsPath = mod.Path + "/ItemIcons";93 loadIconsFromFolder (modIconsPath, tintedIcons );101 string modIconsPath = $"{mod.Path}/ItemIcons"; 102 loadIconsFromFolder (modIconsPath, tintedIcons, stats); 94 103 } catch (Exception e) { 95 Log.Error ( "[Web] Failed loading icons from mod " + mod.ModInfo.Name.Value);104 Log.Error ($"[Web] Failed loading icons from mod {mod.Name}"); 96 105 Log.Exception (e); 97 106 } 98 107 } 108 109 loaded = true; 99 110 100 loaded = true; 101 Log.Out ("[Web] IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds); 111 if (stats == null) { 112 Log.Out ($"[Web] IconHandler: Loaded {icons.Count} icons"); 113 } else { 114 stats?.MswTotal.Stop (); 115 Log.Out ($"[Web] IconHandler: Loaded {icons.Count} icons ({stats.Files} source images with {stats.Tints} tints applied)"); 116 Log.Out ($"[Web] IconHandler: Total time {stats.MswTotal.ElapsedMilliseconds} ms, loading files {stats.MswLoading.ElapsedMilliseconds} ms, tinting files {stats.MswTinting.ElapsedMilliseconds} ms, encoding files {stats.MswEncoding.ElapsedMilliseconds} ms"); 117 118 int totalSize = 0; 119 foreach ((string _, byte[] iconData) in icons) { 120 totalSize += iconData.Length; 121 } 122 123 Log.Out ($"[Web] IconHandler: Cached {totalSize / 1024} KiB"); 124 } 102 125 103 126 return true; … … 105 128 } 106 129 107 private void loadIconsFromFolder (string _path, Dictionary<string, List<Color>> _tintedIcons ) {130 private void loadIconsFromFolder (string _path, Dictionary<string, List<Color>> _tintedIcons, LoadingStats _stats) { 108 131 if (!Directory.Exists (_path)) { 109 132 return; … … 118 141 string name = Path.GetFileNameWithoutExtension (file); 119 142 Texture2D tex = new Texture2D (1, 1, TextureFormat.ARGB32, false); 120 if (!tex.LoadImage (File.ReadAllBytes (file))) { 143 144 _stats?.MswLoading.Start (); 145 byte[] sourceBytes = File.ReadAllBytes (file); 146 if (!tex.LoadImage (sourceBytes)) { 147 _stats?.MswLoading.Stop (); 121 148 continue; 122 149 } 150 _stats?.MswLoading.Stop (); 123 151 124 AddIcon (name, tex, _tintedIcons);152 AddIcon (name, sourceBytes, tex, _tintedIcons, _stats); 125 153 126 154 Object.Destroy (tex); … … 131 159 } 132 160 133 private void AddIcon (string _name, Texture2D _tex, Dictionary<string, List<Color>> _tintedIcons) { 134 icons [_name + "__FFFFFF"] = _tex.EncodeToPNG (); 161 private void AddIcon (string _name, byte[] _sourceBytes, Texture2D _tex, Dictionary<string, List<Color>> _tintedIcons, LoadingStats _stats) { 162 _stats?.MswEncoding.Start (); 163 icons [$"{_name}__FFFFFF"] = _sourceBytes; 164 _stats?.MswEncoding.Stop (); 135 165 136 if (!_tintedIcons.ContainsKey (_name)) { 166 if (_stats != null) { 167 _stats.Files++; 168 } 169 170 if (!_tintedIcons.TryGetValue (_name, out List<Color> tintsList)) { 137 171 return; 138 172 } 139 173 140 foreach (Color c in _tintedIcons [_name]) {141 string tintedName = _name + "__" + AllocsUtils.ColorToHex (c);174 foreach (Color c in tintsList) { 175 string tintedName = $"{_name}__{c.ToHexCode ()}"; 142 176 if (icons.ContainsKey (tintedName)) { 143 177 continue; … … 146 180 Texture2D tintedTex = new Texture2D (_tex.width, _tex.height, TextureFormat.ARGB32, false); 147 181 148 for (int x = 0; x < _tex.width; x++) { 149 for (int y = 0; y < _tex.height; y++) { 150 tintedTex.SetPixel (x, y, _tex.GetPixel (x, y) * c); 151 } 152 } 182 _stats?.MswTinting.Start (); 183 TextureUtils.ApplyTint (_tex, tintedTex, c); 184 _stats?.MswTinting.Stop (); 153 185 186 _stats?.MswEncoding.Start (); 154 187 icons [tintedName] = tintedTex.EncodeToPNG (); 188 _stats?.MswEncoding.Stop (); 155 189 156 190 Object.Destroy (tintedTex); 191 192 if (_stats != null) { 193 _stats.Tints++; 194 } 157 195 } 158 196 } 197 159 198 } 160 199 }
Note:
See TracChangeset
for help on using the changeset viewer.