Skip to content

Commit 626ebed

Browse files
committed
feat: Improved notification parameter parsing
1 parent be6e80e commit 626ebed

File tree

2 files changed

+58
-64
lines changed

2 files changed

+58
-64
lines changed

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void CheckNotifications()
133133
}
134134
}
135135

136-
public Notifications GetNotifications()
136+
Notifications GetNotifications()
137137
{
138138
string notificationsSerial;
139139

@@ -437,68 +437,37 @@ private string GetUrl(Link link)
437437
return url;
438438
}
439439

440-
public void ReplaceParameters()
440+
void ReplaceParameters()
441441
{
442-
foreach (var n in Notifications.NotificationList)
443-
{
444-
n.Title = ReplaceParameter(n.Title);
445-
n.Date = ReplaceParameter(n.Date);
446-
n.ItemList?.ForEach(item => ReplaceItemParameter(item));
447-
}
448-
foreach (var list in Notifications.CheckList)
449-
{
450-
foreach(var c in list?.AnyOfList)
451-
{
452-
c?.AllOfList.ForEach(criteria => ReplaceCriteriaPropertyParameter(criteria));
453-
c?.AllOfList.ForEach(criteria => ReplaceCriteriaValueParameter(criteria));
454-
}
455-
}
442+
Notifications.ReplaceParameters(ReplaceParameterValues);
456443
}
457444

458-
private void ReplaceItemParameter(Item item)
459-
{
460-
if (item is Record record)
461-
record.Value = ReplaceParameter(record.Value);
462-
if (item is Link link)
463-
link.Value = ReplaceParameter(link.Value);
464-
if (item is Update update)
465-
update.Value = ReplaceParameter(update.Value);
466-
}
467-
468-
/// <summary>
469-
/// If Property is a parameter, remove {{..}} and add it and its replacement to the dictionary.
470-
/// </summary>
471-
/// <param name="criteria"></param>
472-
private void ReplaceCriteriaPropertyParameter(Criteria criteria)
445+
string ReplaceParameterValues(string value)
473446
{
474-
if (ContainsParameter(criteria.Property))
447+
if (value == null) return value;
448+
var start = 0;
449+
while ((start = value.IndexOf("{{", start)) >= 0)
475450
{
476-
criteria.Property = ReplaceParameter(criteria.Property);
451+
var end = value.IndexOf("}}", start);
452+
if (end == -1) break;
453+
var variable = value.Substring(start + 2, end - start - 2);
454+
var replacement = GetParameterValue(variable);
455+
value = value.Substring(0, start) + replacement + value.Substring(end + 2);
456+
start += replacement.Length;
477457
}
458+
return value;
478459
}
479460

480-
private void ReplaceCriteriaValueParameter(Criteria criteria)
481-
{
482-
criteria.Value = ReplaceParameter(criteria.Value);
483-
}
484-
485-
private string ReplaceParameter(string field)
461+
string GetParameterValue(string parameter)
486462
{
487-
if (ContainsParameter(field) == false) return field;
488-
489-
var parameterArray = field.Split('{', '}'); // 5 elements: prefix, "", target, "", suffix
490-
var target = parameterArray[2];
491-
var lowerCaseTarget = parameterArray[2].ToLower();
492463
string replacement;
493-
494-
// If found in dictionary, then use that else extract it from program
495-
if (ParameterDictionary.ContainsKey(lowerCaseTarget))
464+
if (ParameterDictionary.ContainsKey(parameter))
496465
{
497-
replacement = ParameterDictionary[lowerCaseTarget];
466+
replacement = ParameterDictionary[parameter];
498467
}
499468
else
500469
{
501-
switch (lowerCaseTarget)
470+
switch (parameter)
502471
{
503472
// Update parameters
504473
// Using "none" instead of "" so that records are readable.
@@ -555,23 +524,15 @@ private string ReplaceParameter(string field)
555524
replacement = GetInstalledRoutes();
556525
break;
557526
default:
558-
var propertyValue = GetSetting(target);
527+
var propertyValue = GetSetting(parameter);
559528
replacement = (propertyValue == "")
560-
? field // strings that are not recognised are not replaced.
529+
? "{{" + parameter + "}}" // strings that are not recognised are not replaced.
561530
: propertyValue.ToLower().Replace("false", "off").Replace("true", "on");
562531
break;
563532
}
564-
ParameterDictionary.Add(lowerCaseTarget, replacement);
533+
ParameterDictionary.Add(parameter, replacement);
565534
}
566-
567-
return parameterArray[0] + replacement + parameterArray[4];
568-
}
569-
570-
private bool ContainsParameter(string field)
571-
{
572-
if (field.Contains("{{") == false) return false;
573-
if (field.Contains("}}") == false) return false;
574-
return true;
535+
return replacement;
575536
}
576537

577538
/// <summary>
@@ -617,7 +578,7 @@ private string GetInstalledRoutes()
617578
return installedRouteList;
618579
}
619580

620-
public OverrideParameterList GetOverrideParameters()
581+
OverrideParameterList GetOverrideParameters()
621582
{
622583
// To support testing of a new remote notifications.json file before it is published,
623584
// GetNotifications tests first for a local file notifications_override_values.json

Source/Menu/Notifications/Notifications.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2009 - 2024 by the Open Rails project.
1+
// COPYRIGHT 2009 - 2024 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -15,15 +15,20 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18+
using System;
1819
using System.Collections.Generic;
1920

20-
2121
namespace Menu.Notifications
2222
{
2323
public class Notifications
2424
{
2525
public List<Notification> NotificationList = new List<Notification>();
2626
public List<Check> CheckList = new List<Check>();
27+
internal void ReplaceParameters(Func<string, string> replaceFunc)
28+
{
29+
NotificationList?.ForEach(item => item.ReplaceParameters(replaceFunc));
30+
CheckList?.ForEach(item => item.ReplaceParameters(replaceFunc));
31+
}
2732
}
2833

2934
public class Notification
@@ -33,6 +38,12 @@ public class Notification
3338
public List<string> IncludeIf { get; set; }
3439
public List<string> IncludeIfNot { get; set; }
3540
public List<Item> ItemList { get; set; }
41+
internal void ReplaceParameters(Func<string, string> replaceFunc)
42+
{
43+
Date = replaceFunc(Date);
44+
Title = replaceFunc(Title);
45+
ItemList?.ForEach(item => item.ReplaceParameters(replaceFunc));
46+
}
3647
}
3748
class Record : ValueItem
3849
{
@@ -61,6 +72,11 @@ class Update : ValueItem
6172
abstract class ValueItem : Item
6273
{
6374
public string Value { get; set; }
75+
internal override void ReplaceParameters(Func<string, string> replaceFunc)
76+
{
77+
base.ReplaceParameters(replaceFunc);
78+
Value = replaceFunc(Value);
79+
}
6480
}
6581
public abstract class Item
6682
{
@@ -69,17 +85,29 @@ public abstract class Item
6985
public string Label { get; set; }
7086
public string Color { get; set; } = "black";
7187
public int Indent { get; set; } = 140;
88+
internal virtual void ReplaceParameters(Func<string, string> replaceFunc)
89+
{
90+
Label = replaceFunc(Label);
91+
}
7292
}
7393

7494
public class Check
7595
{
7696
public string Id { get; set; }
7797
public List<AnyOf> AnyOfList { get; set; }
98+
internal void ReplaceParameters(Func<string, string> replaceFunc)
99+
{
100+
AnyOfList?.ForEach(item => item.ReplaceParameters(replaceFunc));
101+
}
78102
}
79103

80104
public class AnyOf
81105
{
82106
public List<Criteria> AllOfList { get; set; }
107+
internal void ReplaceParameters(Func<string, string> replaceFunc)
108+
{
109+
AllOfList?.ForEach(item => item.ReplaceParameters(replaceFunc));
110+
}
83111
}
84112

85113
// These criteria are all doing an actual comparison
@@ -106,6 +134,11 @@ public abstract class Criteria
106134
public string Property { get; set; } // installed_version, direct3d, runtime, system, memory, cpu, gpu
107135
public string Value { get; set; } // {{new_version}}, {{10_0}}
108136
public abstract bool IsMatch();
137+
internal void ReplaceParameters(Func<string, string> replaceFunc)
138+
{
139+
Property = replaceFunc(Property);
140+
Value = replaceFunc(Value);
141+
}
109142
}
110143

111144
class ParameterValue

0 commit comments

Comments
 (0)