[251] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Net;
|
---|
| 4 | using System.Threading;
|
---|
| 5 |
|
---|
| 6 | using UnityEngine;
|
---|
| 7 |
|
---|
| 8 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers
|
---|
| 9 | {
|
---|
| 10 | public class ItemIconHandler : PathHandler
|
---|
| 11 | {
|
---|
| 12 | private string staticPart;
|
---|
| 13 | private bool logMissingFiles;
|
---|
| 14 | private Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> ();
|
---|
| 15 | private bool loaded = false;
|
---|
| 16 |
|
---|
| 17 | public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base(moduleName) {
|
---|
| 18 | this.staticPart = staticPart;
|
---|
| 19 | this.logMissingFiles = logMissingFiles;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
|
---|
| 23 | if (!loaded) {
|
---|
| 24 | if (!LoadIcons ()) {
|
---|
| 25 | resp.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
| 26 | Log.Out ("Web:IconHandler: Could not load icons");
|
---|
| 27 | return;
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | // BEGIN CHANGED BY PSOUZA4
|
---|
| 32 | string requestFileName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
| 33 | requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.'));
|
---|
| 34 |
|
---|
| 35 | string requestColorTintHex = string.Empty;
|
---|
| 36 |
|
---|
| 37 | // Chose a split instead of using a querystring variable in the URI, but that may arguably be cleaner
|
---|
| 38 | if (requestFileName.Contains("@@")) {
|
---|
| 39 | try {
|
---|
| 40 | string[] tempTintingOptions = requestFileName.Split (new string[] { "@@" }, StringSplitOptions.RemoveEmptyEntries);
|
---|
| 41 | requestFileName = tempTintingOptions [0];
|
---|
| 42 | requestColorTintHex = tempTintingOptions [1].Trim ().ToUpper ();
|
---|
| 43 | }
|
---|
| 44 | catch { }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | if (icons.ContainsKey (requestFileName) && req.Url.AbsolutePath.ToLower ().EndsWith(".png")) {
|
---|
| 48 | resp.ContentType = MimeType.GetMimeType (".png");
|
---|
| 49 |
|
---|
| 50 | byte[] itemIconData = icons [requestFileName];
|
---|
| 51 |
|
---|
| 52 | // Note: optionally split this code into a ImageMultiplyBlend method
|
---|
| 53 | if (!string.IsNullOrEmpty (requestColorTintHex) && (requestColorTintHex != "FFFFFF")) {
|
---|
| 54 | try {
|
---|
| 55 | System.Drawing.Color colorTint = System.Drawing.ColorTranslator.FromHtml ("#" + requestColorTintHex);
|
---|
| 56 | System.Drawing.Bitmap image = (System.Drawing.Bitmap)PetesUtils.GetImageFromBytes (itemIconData);
|
---|
| 57 |
|
---|
| 58 | for (var x = 0; x < image.Width; x++) {
|
---|
| 59 | for (int y = 0; y < image.Height; y++) {
|
---|
| 60 | System.Drawing.Color originalColor = image.GetPixel (x, y);
|
---|
| 61 | System.Drawing.Color changedColor = originalColor;
|
---|
| 62 |
|
---|
| 63 | // Only change the icon tinting if the alpha channel is fully opaque
|
---|
| 64 | if (originalColor.A == 255) {
|
---|
| 65 | // based on http://stackoverflow.com/questions/3837757/multiply-two-images-in-c-sharp-as-multiply-two-layers-in-photoshop
|
---|
| 66 |
|
---|
| 67 | double component_R = (((double)originalColor.R) * ((double)colorTint.R)) / 255.0;
|
---|
| 68 | double component_G = (((double)originalColor.G) * ((double)colorTint.G)) / 255.0;
|
---|
| 69 | double component_B = (((double)originalColor.B) * ((double)colorTint.B)) / 255.0;
|
---|
| 70 |
|
---|
| 71 | if (component_R > 255.0) component_R = 255.0;
|
---|
| 72 | if (component_G > 255.0) component_G = 255.0;
|
---|
| 73 | if (component_B > 255.0) component_B = 255.0;
|
---|
| 74 |
|
---|
| 75 | // multiply blend shouldn't ever calculate below 0, but for completeness let's leave in this logic
|
---|
| 76 | if (component_R < 0.0) component_R = 0.0;
|
---|
| 77 | if (component_G < 0.0) component_G = 0.0;
|
---|
| 78 | if (component_B < 0.0) component_B = 0.0;
|
---|
| 79 |
|
---|
| 80 | changedColor = System.Drawing.Color.FromArgb (originalColor.A, (int)component_R, (int)component_G, (int)component_B);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | image.SetPixel (x, y, changedColor);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | itemIconData = PetesUtils.SaveImage_ToBytes (image, true);
|
---|
| 88 | }
|
---|
| 89 | catch { }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | resp.ContentLength64 = itemIconData.Length;
|
---|
| 93 | resp.OutputStream.Write (itemIconData, 0, itemIconData.Length);
|
---|
| 94 | // END CHANGED BY PSOUZA4
|
---|
| 95 | } else {
|
---|
| 96 | resp.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
| 97 | if (logMissingFiles)
|
---|
| 98 | Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" ");
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | private bool LoadIcons () {
|
---|
| 104 | lock (icons) {
|
---|
| 105 | if (loaded) {
|
---|
| 106 | return true;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas");
|
---|
| 110 | if (atlasObj == null) {
|
---|
| 111 | Log.Error ("Web:IconHandler: Atlas object not found");
|
---|
| 112 | loaded = true;
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
| 115 | DynamicUIAtlas atlas = atlasObj.GetComponent<DynamicUIAtlas> ();
|
---|
| 116 | if (atlas == null) {
|
---|
| 117 | Log.Error ("Web:IconHandler: Atlas component not found");
|
---|
| 118 | loaded = true;
|
---|
| 119 | return false;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | Texture2D atlasTex = atlas.texture as Texture2D;
|
---|
| 123 |
|
---|
| 124 | foreach (UISpriteData data in atlas.spriteList) {
|
---|
| 125 | string name = data.name;
|
---|
| 126 | Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false);
|
---|
| 127 | tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height));
|
---|
| 128 | byte[] pixData = tex.EncodeToPNG ();
|
---|
| 129 |
|
---|
| 130 | icons.Add (name, pixData);
|
---|
| 131 | UnityEngine.Object.Destroy (tex);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | loaded = true;
|
---|
| 135 | Log.Out ("Web:IconHandler: Icons loaded");
|
---|
| 136 |
|
---|
| 137 | return true;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|