Nuke.Unreal
Build Unreal apps in Style.
Loading...
Searching...
No Matches
ConfigCommand.cs
1using System.Collections.Generic;
2using System.Linq;
3using System.Text.RegularExpressions;
4
6
7/// <summary>
8/// Unreal INI config items are read top to bottom, reading like individual commands, especially when
9/// manipulating Unreal containers. This is why Nuke.Unreal.Ini refers to config lines as 'Commands'.
10/// </summary>
11public enum CommandType
12{
13 Set = 0,
14 Add = '+',
15 Remove = '-',
16 Clear = '!',
17 Comment = ';'
18}
19
20/// <summary>
21/// Structural representation of a config line in Unreal.
22/// </summary>
23public struct ConfigCommand
24{
25 private const string AllCommands = "+-!;";
26
27 /// <summary>
28 /// Maintain the original position for this section as they were in the source file
29 /// so serialization doesn't introduce that much unnecessary changes.
30 /// </summary>
31 public int Order;
32
33 /// <summary>
34 /// Config item name
35 /// </summary>
36 public string Name;
37
38 /// <summary>
39 /// Everything on the right side of the `=` symbol on a config,
40 /// </summary>
41 /// <remarks>
42 /// For comments the actual text of the comment is contained in Name, however if that comment
43 /// has `=` character everything following that will be stored in Value. Use CommentText
44 /// when dealing with comments.
45 /// </remarks>
46 public string Value;
47
48 /// <summary>
49 /// Was the value quoted originally in the source config file.
50 /// </summary>
51 public bool IsQuotedString;
52
53 /// <summary>
54 /// How this config item should apply its value
55 /// </summary>
57
58 /// <summary>
59 /// Get the full text of a comment including `=` character and its right side
60 /// </summary>
61 public string CommentText => Name + (string.IsNullOrWhiteSpace(GetSerializedValue()) ? "" : "=" + Value);
62
63 public static bool operator ==(ConfigCommand a, ConfigCommand b)
64 {
65 return a.Equals(b);
66 }
67
68 public static bool operator !=(ConfigCommand a, ConfigCommand b)
69 {
70 return !a.Equals(b);
71 }
72
73 public ConfigCommand(string line, int order)
74 {
75 Order = order;
76
77 string command = "", name = "", value = "";
78
79 line.MatchGroup(@"^(?<COMMAND>[\+\-!;]?)(?<NAME>.*?)(=(?<VALUE>.*))?$", RegexOptions.CultureInvariant)
80 ?.Fetch("COMMAND", out command)
81 ?.Fetch("NAME", out name)
82 ?.Fetch("VALUE", out value);
83
84
85 Type = string.IsNullOrWhiteSpace(command) || AllCommands.All(c => c != command[0])
86 ? CommandType.Set
87 : (CommandType) command[0];
88
89 value = value.Trim();
90 Name = Type != CommandType.Comment ? name.Trim() : name;
91 IsQuotedString = value.StartsWith("\"") && value.EndsWith("\"");
92 Value = IsQuotedString ? value.Trim('"') : value;
93 }
94
95 private string GetSerializedValue() => IsQuotedString ? $"\"{Value}\"" : Value;
96
97 public string Serialize() => Type switch
98 {
99 CommandType.Set => $"{Name}={GetSerializedValue()}",
100 CommandType.Add => $"+{Name}={GetSerializedValue()}",
101 CommandType.Remove => $"-{Name}={GetSerializedValue()}",
102 CommandType.Clear => "!" + Name,
103 CommandType.Comment => ";" + Name + (string.IsNullOrWhiteSpace(GetSerializedValue()) ? "" : "=" + Value),
104 _ => ""
105 };
106
107 public override bool Equals(object? obj)
108 {
109 if (obj is ConfigCommand b)
110 {
111 return Type == b.Type && Name == b.Name && Value == b.Value;
112 }
113 return false;
114 }
115
116 public override int GetHashCode() => (int)Type ^ (Name?.GetHashCode() ^ Value?.GetHashCode() ?? 0);
117}
CommandType
Unreal INI config items are read top to bottom, reading like individual commands, especially when man...
Structural representation of a config line in Unreal.
string Name
Config item name.
int Order
Maintain the original position for this section as they were in the source file so serialization does...
string Value
Everything on the right side of the = symbol on a config,.
CommandType Type
How this config item should apply its value.
string CommentText
Get the full text of a comment including = character and its right side.
bool IsQuotedString
Was the value quoted originally in the source config file.