using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Windows; using System.Windows.Media.Imaging; namespace Crysome.Server.Model; public static class FlagCache { private static readonly Dictionary _cache = new Dictionary(StringComparer.OrdinalIgnoreCase); private static bool _loaded; public static int Count => _cache.Count; public static string DebugInfo { get; private set; } = ""; public static BitmapImage Get(string code) { if (!_loaded) { Load(); } if (_cache.TryGetValue(code, out var value)) { return value; } return null; } private static void Load() { _loaded = true; string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine("BaseDir: " + baseDirectory); string[] array = new string[4] { Path.Combine(baseDirectory, "flags"), baseDirectory, Path.Combine(baseDirectory, "icons"), Path.Combine(baseDirectory, "icons", "flags") }; foreach (string text in array) { bool flag = Directory.Exists(text); stringBuilder.AppendLine("Search: " + text + " -> " + (flag ? "EXISTS" : "NOT FOUND")); if (!flag) { continue; } string[] files = Directory.GetFiles(text, "*.png"); int num = 0; string[] array2 = files; foreach (string text2 in array2) { string text3 = Path.GetFileNameWithoutExtension(text2).ToUpperInvariant(); if (!text3.Contains("@") && text3.Length == 2 && !_cache.ContainsKey(text3)) { try { byte[] buffer = File.ReadAllBytes(text2); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(buffer); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); ((Freezable)bitmapImage).Freeze(); _cache[text3] = bitmapImage; num++; } catch (Exception ex) { stringBuilder.AppendLine("FAIL: " + text2 + " -> " + ex.Message); } } } stringBuilder.AppendLine("Loaded " + num + " flags from " + text); if (num > 0) { break; } } stringBuilder.AppendLine("Total flags: " + _cache.Count); DebugInfo = stringBuilder.ToString(); try { File.WriteAllText(Path.Combine(baseDirectory, "flag_debug.log"), DebugInfo); } catch { } } }