From 1f5c38b300eb4dd2e60929e1124f24bd08ed0464 Mon Sep 17 00:00:00 2001 From: Dale McCoy <21223975+DaleStan@users.noreply.github.com> Date: Mon, 9 Jan 2023 21:44:49 -0500 Subject: [PATCH] Implement a variant of #181, to allow XXb for a number of belts of production. Also look for /s, /m, or /h suffixes. If present, use that suffix regardless of the current display settings. --- YAFC/Data/Tips.txt | 5 ++- YAFCmodel/Data/DataUtils.cs | 77 +++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/YAFC/Data/Tips.txt b/YAFC/Data/Tips.txt index 70ace22a..882933b6 100644 --- a/YAFC/Data/Tips.txt +++ b/YAFC/Data/Tips.txt @@ -7,4 +7,7 @@ Tip: You can open recipe explorer using Ctrl+N or with middle mouse click on any Tip: If you close a page, it doesn't get deleted. Tip: You can undo with Ctrl+Z and redo with Ctrl+Y or Ctrl+Shift+Z Tip: Use desired products to subtract desired demand from actual production -Tip: Ctrl+Click on a tab to open it on half screen in addition to currently opened tab \ No newline at end of file +Tip: Ctrl+Click on a tab to open it on half screen in addition to currently opened tab +Tip: Specify /m to produce that many items (or fluid units) per minute, regardless of the current display mode +Tip: Specify b to produce that many belts worth of product, for the belt you've selected in the preferences +Tip: Specify k to multiply the production by 1,000. Other SI suffixes work too! (put the suffix before 'b' or '/h') diff --git a/YAFCmodel/Data/DataUtils.cs b/YAFCmodel/Data/DataUtils.cs index 016b3de0..066442c1 100644 --- a/YAFCmodel/Data/DataUtils.cs +++ b/YAFCmodel/Data/DataUtils.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Runtime.CompilerServices; using System.Text; +using System.Text.RegularExpressions; using System.Text.Unicode; using Google.OrTools.LinearSolver; using SDL2; @@ -462,47 +463,57 @@ public static string FormatAmountRaw(float amount, float unitMultipler, string u public static bool TryParseAmount(string str, out float amount, UnitOfMeasure unit) { var (mul, _) = Project.current.ResolveUnitOfMeasure(unit); - var lastValidChar = 0; var multiplier = unit == UnitOfMeasure.Megawatt ? 1e6f : 1f; + + var groups = Regex.Match(str, "([-0-9.e]+)\\s*([μukmgt])?\\s*(/\\s*[hms]|b)?", RegexOptions.IgnoreCase).Groups; amount = 0; - foreach (var c in str) + if (groups.Count < 4 || !float.TryParse(groups[1].Value, out amount)) + return false; + + switch (groups[2].Value) // μukmgt { - if (c >= '0' && c <= '9' || c == '.' || c == '-' || c == 'e') - ++lastValidChar; - else - { - if (lastValidChar == 0) - return false; - switch (c) - { - case 'k': case 'K': - multiplier = 1e3f; - break; - case 'm': case 'M': - multiplier = 1e6f; - break; - case 'g': case 'G': - multiplier = 1e9f; - break; - case 't': case 'T': - multiplier = 1e12f; - break; - case 'μ': case 'u': - multiplier = 1e-6f; - break; - } + case "μ": case "u": + multiplier = 1e-6f; + break; + case "k": case "K": + multiplier = 1e3f; + break; + case "m": case "M": + multiplier = 1e6f; + break; + case "g": case "G": + multiplier = 1e9f; + break; + case "t": case "T": + multiplier = 1e12f; + break; + case "U": case "Μ": // capital uμ; false positive in the regex + return false; + } + + switch (groups[3].Value.LastOrDefault()) // b/hms + { + case 'b': case 'B': + if (Project.current.preferences.itemUnit > 0) + mul = 1 / Project.current.preferences.itemUnit; + else + mul = 1 / Project.current.preferences.defaultBelt.beltItemsPerSecond; + break; + case 's': case 'S': + mul = 1; + break; + case 'm': case 'M': + mul = 60; + break; + case 'h': case 'H': + mul = 3600; break; - } } multiplier /= mul; - var substr = str.Substring(0, lastValidChar); - if (!float.TryParse(substr, out amount)) return false; amount *= multiplier; - if (amount > 1e15) - return false; - return true; + return amount <= 1e15; } - + public static void WriteException(this TextWriter writer, Exception ex) { writer.WriteLine("Exception: "+ex.Message);