using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace ScreenConnect; public abstract class ResourceManagerExBase { protected class State { public List MappedCultures; public List SpecifiedCultures; public HashSet AllKeys; public Dictionary, object> NamePlusKeyMap; } private class ResourceFormatter : FormatterBase { public static readonly ResourceFormatter Instance = new ResourceFormatter(); private ResourceFormatter() { } protected override string Format(string format, object arg, IFormatProvider formatProvider) { if (!(format == "url")) { if (format == "htmlencode") { return arg?.ToString().Pipe(Extensions.HtmlEncode) ?? string.Empty; } return base.Format(format, arg, formatProvider); } return arg?.ToString().TryPipe((string it) => new Uri(it).AbsoluteUri) ?? string.Empty; } } protected virtual void OnStateRefreshed() { } protected State CreateState(ICollection resourcePacks) { HashSet hashSet = new HashSet(); Dictionary, object> dictionary = new Dictionary, object>(); Func> getSelfAndAncestorsFunc = (CultureInfo culture) => culture.GetAncestors((CultureInfo c) => (c.Name.Length != 0) ? c.Parent : null, includeSelf: true); ResourcePack[] array = resourcePacks.OrderBy((ResourcePack rp) => rp.Precedence).ToArray(resourcePacks.Count); List list = array.Select((ResourcePack rp) => rp.Culture).Distinct().ToList(); Dictionary cultureToCountMap = list.SelectMany((CultureInfo c) => getSelfAndAncestorsFunc(c)).GroupBy((CultureInfo it) => it, (CultureInfo culture, IEnumerable occurrences) => Extensions.CreateKeyValuePair(culture, occurrences.Count())).ToDictionary(); List mappedCultures = (from kvp in cultureToCountMap select kvp.Key into c where c.Name.Length == 0 || cultureToCountMap[c.Parent] > 1 select c).ToList(); ResourcePack[] array2 = array; foreach (ResourcePack resourcePack in array2) { List list2 = (from c in resourcePack.Culture.SafeNav((CultureInfo c) => getSelfAndAncestorsFunc(c)) where mappedCultures.Contains(c) select c).ToList(); foreach (KeyValuePair resource in resourcePack.ResourceList) { bool flag = true; if (resourcePack.Precedence == ResourcePackPrecedence.Override) { flag = hashSet.Contains(resource.Key); } else { hashSet.Add(resource.Key); } if (!flag) { continue; } foreach (CultureInfo item in list2) { dictionary[Tuple.Create(item.Name, resource.Key)] = resource.Value; } } } return new State { MappedCultures = mappedCultures, SpecifiedCultures = list, AllKeys = hashSet, NamePlusKeyMap = dictionary }; } protected ResourcePack LoadResourcePack(string filePathForMetadata, IList> resourceList) { string path = filePathForMetadata; CultureInfo culture = CultureInfo.InvariantCulture; ResourcePackPrecedence precedence = ResourcePackPrecedence.Default; while (true) { string text = Path.GetExtension(path).Trim(new char[1] { '.' }); if (text == string.Empty) { break; } if (Extensions.TryParseEnum(text, out var value)) { precedence = value; } else { Regex.Match(text, "^([a-z]{2,3}((-[A-Za-z]{4})?-[A-Z]{2,3})?)$").If((Match _) => _.Success).TrySafePass(delegate(Match _) { culture = Singleton.Instance.GetCultureInfo(_.Groups[1].Value); }); } path = Path.GetFileNameWithoutExtension(path); } return new ResourcePack(culture, resourceList, precedence); } protected IEnumerable> GetAllEntries(State state) { CultureInfo cultureInfo = CultureInfo.CurrentUICulture; while (cultureInfo != null && cultureInfo.Name.Length > 0 && !state.MappedCultures.Contains(cultureInfo)) { cultureInfo = cultureInfo.Parent; } return state.AllKeys.Select((string k) => Extensions.CreateKeyValuePair(k, GetObject(state, k, cultureInfo))); } protected IEnumerable>>> GetMappedEntries(State state) { return state.MappedCultures.Select((CultureInfo c) => Extensions.CreateKeyValuePair(c, from k in state.AllKeys select Extensions.CreateKeyValuePair(k, GetObjectWithoutFallback(state, k, c)) into kvp where kvp.Value != null select kvp)); } protected string GetString(State state, string key, bool returnKeyAsStringIfNonExistent = true) { object obj = GetObject(state, key) as string; if (obj == null) { if (!returnKeyAsStringIfNonExistent) { return null; } obj = key; } return (string)obj; } protected string TryFormatString(State state, string key, params object[] parameters) { string value = GetString(state, key); return Extensions.TryGet(() => string.Format(ResourceFormatter.Instance, value, parameters), value); } protected string TryFormatStringWithFallback(State state, string keyFormat, object[] keyFormatArgs, bool fallbackToKeyOrEmpty, params object[] resourceParameters) { int num = 1 << Math.Min(keyFormatArgs.Length, 5); for (int bitSet = 0; bitSet < num; bitSet++) { object[] candidateKeyFormatArgs = keyFormatArgs.Select((object arg, int argIndex) => (!bitSet.AreFlagsSet(1 << argIndex)) ? arg.SafeToString() : string.Empty).Cast().ToArray(); string key = Extensions.TryGet(() => string.Format(keyFormat, candidateKeyFormatArgs)); string resourceFormat = GetString(state, key, returnKeyAsStringIfNonExistent: false); if (resourceFormat != null) { return Extensions.TryGet(() => string.Format(ResourceFormatter.Instance, resourceFormat, resourceParameters), resourceFormat); } } if (!fallbackToKeyOrEmpty) { return string.Empty; } return Extensions.TryGet(() => string.Format(ResourceFormatter.Instance, keyFormat, keyFormatArgs), keyFormat); } protected byte[] GetBytes(State state, string key) { return GetObject(state, key) as byte[]; } protected bool GetBoolean(State state, string key) { return GetString(state, key).TryParseBool(); } protected double GetDouble(State state, string key) { return GetString(state, key).TryParseDouble(); } protected long GetInt64(State state, string key) { return GetString(state, key).TryParseInt64(0L); } protected object GetObject(State state, string key) { return GetObject(state, key, CultureInfo.CurrentUICulture); } protected object GetObject(State state, string key, CultureInfo cultureInfo) { while (true) { object objectWithoutFallback = GetObjectWithoutFallback(state, key, cultureInfo); if (objectWithoutFallback != null) { return objectWithoutFallback; } if (cultureInfo.Name.Length == 0) { break; } cultureInfo = cultureInfo.Parent; } return null; } private object GetObjectWithoutFallback(State state, string key, CultureInfo mappedCultureInfo) { Tuple key2 = Tuple.Create(mappedCultureInfo.Name, key); return state.NamePlusKeyMap.TryGetValue(key2); } }