using CommonHelpers; using System; using System.Collections.Generic; using System.Linq; using System.Security.Policy; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; namespace PerformanceOverlay { internal class Overlays { public class Entry { public String? Text { get; set; } public IList Include { get; set; } = new List(); public IList Exclude { get; set; } = new List(); public IList Nested { get; set; } = new List(); public String Separator { get; set; } = ""; public bool IgnoreMissing { get; set; } public static readonly Regex attributeRegex = new Regex("{([^}]+)}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); public Entry() { } public Entry(String text) { this.Text = text; } public IEnumerable AllAttributes { get { return attributeRegex.Matches(Text ?? ""); } } private String EvaluateText(Sensors sensors) { String output = Text ?? ""; foreach (var attribute in AllAttributes) { String attributeName = attribute.Groups[1].Value; String? value = sensors.GetValue(attributeName); if (value is null && IgnoreMissing) return ""; output = output.Replace(attribute.Value, value ?? "-"); } return output; } public String? GetValue(OverlayMode mode, Sensors sensors) { if (Exclude.Count > 0 && Exclude.Contains(mode)) return null; if (Include.Count > 0 && !Include.Contains(mode)) return null; String output = EvaluateText(sensors); if (Nested.Count > 0) { var outputs = Nested.Select(entry => entry.GetValue(mode, sensors)).Where(output => output != null); if (outputs.Count() == 0) return null; output += String.Join(Separator, outputs); } if (output == String.Empty) return null; return output; } } public static readonly String[] Helpers = { "", "", }; public static readonly Entry OSD = new Entry { Nested = { // Simple just FPS new Entry { Nested = { new Entry(" FPS"), new Entry("{BATT_%} %") { Include = { OverlayMode.FPSWithBattery }, IgnoreMissing = true }, new Entry("{BATT_W} W") { Include = { OverlayMode.FPSWithBattery }, IgnoreMissing = true }, new Entry("{BATT_MIN} min") { Include = { OverlayMode.FPSWithBattery }, IgnoreMissing = true } }, Include = { OverlayMode.FPS, OverlayMode.FPSWithBattery } }, // Minimal and Detail new Entry { Nested = { new Entry { Text = "BAT", Nested = { new Entry("{BATT_%} %"), new Entry("{BATT_W} W") { IgnoreMissing = true }, new Entry("{BATT_MIN} min") { IgnoreMissing = true }, new Entry("C{BATT_CHARGE_W} W") { IgnoreMissing = true, Include = { OverlayMode.Detail } } } }, new Entry { Text = "GPU", Nested = { new Entry("{GPU_%} %"), new Entry("{GPU_W} W"), new Entry("{GPU_T} C") { IgnoreMissing = true, Include = { OverlayMode.Detail } } } }, new Entry { Text = "CPU", Nested = { new Entry("{CPU_%} %"), new Entry("{CPU_W} W"), new Entry("{CPU_T} C") { IgnoreMissing = true, Include = { OverlayMode.Detail } } } }, new Entry { Text = "RAM", Nested = { new Entry("{MEM_GB} GiB") } }, new Entry { Text = "FAN", Nested = { new Entry("{FAN_RPM} RPM") }, Include = { OverlayMode.Detail } }, new Entry { Text = "", Nested = { new Entry(" FPS") } }, new Entry { Text = "[OBJ_FT_SMALL] ms", Include = { OverlayMode.Detail } } }, Separator = "| ", Include = { OverlayMode.Minimal, OverlayMode.Detail } }, new Entry { Nested = { new Entry("CPU\t ") { Nested = { new Entry("{CPU_%} %"), new Entry("{CPU_W} W"), new Entry("{CPU_T} C") { IgnoreMissing = true }, } }, new Entry("\t ") { Nested = { new Entry("{MEM_MB} MB"), new Entry("{CPU_MHZ} MHz") } }, new Entry("GPU\t ") { Nested = { new Entry("{GPU_%} %"), new Entry("{GPU_W} W"), new Entry("{GPU_T} C") { IgnoreMissing = true }, } }, new Entry("\t ") { Nested = { new Entry("{GPU_MB} MB"), new Entry("{GPU_MHZ} MHz") { IgnoreMissing = true } } }, new Entry("FAN\t ") { Nested = { new Entry("{FAN_RPM} RPM"), } }, new Entry("\t ") { Nested = { new Entry(" FPS"), new Entry(" ms"), } }, new Entry("BAT\t ") { Nested = { new Entry("{BATT_%} %"), new Entry("{BATT_W} W") { IgnoreMissing = true }, new Entry("{BATT_MIN} min") { IgnoreMissing = true }, new Entry("C{BATT_CHARGE_W} W") { IgnoreMissing = true } } }, new Entry("Frametime"), new Entry("[OBJ_FT_LARGE] ms"), }, Separator = "\r\n", Include = { OverlayMode.Full } } } }; public static String GetOSD(OverlayMode mode, Sensors sensors) { var sb = new StringBuilder(); sb.AppendJoin("", Helpers); sb.Append(OSD.GetValue(mode, sensors) ?? ""); return sb.ToString(); } } }