-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSeStringConverter.cs
More file actions
186 lines (157 loc) · 8.38 KB
/
Copy pathSeStringConverter.cs
File metadata and controls
186 lines (157 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Lumina.Excel;
using Lumina.Excel.Sheets;
using Lumina.Text.Expressions;
using Lumina.Text.Payloads;
using Lumina.Text.ReadOnly;
public class SeStringConverter : JsonConverter<ReadOnlySeString>
{
private readonly ExcelSheet<UIColor> _uiColors;
public SeStringConverter(ExcelSheet<UIColor> uiColors)
{
_uiColors = uiColors;
}
public override ReadOnlySeString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// For deserialization, you might want to convert the JSON string back to SeString.
// This example assumes you're not deserializing SeString objects.
throw new NotImplementedException("Deserialization is not supported.");
}
public override void Write(Utf8JsonWriter writer, ReadOnlySeString value, JsonSerializerOptions options)
{
// Serialize the SeString object as a JSON string.
try { writer.WriteStringValue(ConvertSeString(value)); } catch (Exception) {
writer.WriteNullValue();
}
}
public String ConvertSeString(ReadOnlySeString value)
{
return string.Join("", value.Select(ConvertPayload));
}
public string ConvertPayloadTag(String type)
{
// Convert italics to HTML <i> and </i> tags.
var lowerType = type.ToLower();
if (lowerType == "<italic>1</italic>")
return "<i>";
if (lowerType == "<italic>0</italic>")
return "</i>";
// Eliminate uicolorborder ("UIGlow") as we don't bother representing it in any way.
if (lowerType.StartsWith("<edgecolortype")) {
return "";
}
// Convert ui color fill to a span with a foreground color.
if (lowerType.StartsWith("<colortype")) {
if (lowerType == "<colortype>0</colortype>")
return "</span>";
string numPattern = @"<colortype>(\d+)</colortype>";
Match match = Regex.Match(lowerType, numPattern);
string colorAttr = "";
if (match.Success) {
string numStr = match.Groups[1].Value; // Extract the number as a string
uint num = uint.Parse(numStr); // Convert the string to an integer
var row = _uiColors.GetRow(num);
var colorProperty = typeof(UIColor).GetProperty("Dark");
if (colorProperty != null && colorProperty.PropertyType == typeof(uint)) {
uint colorValue = (uint)(colorProperty.GetValue(row) ?? 0U);
string hexValue = colorValue.ToString("x");
string paddedHexValue = hexValue.PadLeft(8, '0');
string firstSixChars = paddedHexValue.Substring(0, 6);
colorAttr = " style=\"color: #" + firstSixChars + ";\"";
}
}
return $"<span{colorAttr}>";
}
return type;
}
public String ConvertExpression(Lumina.Text.ReadOnly.ReadOnlySeExpression expression)
{
if (expression.TryGetParameterExpression(out byte paramType, out ReadOnlySeExpression paramExpression))
return ConvertParameterExpression(paramExpression, (ExpressionType)paramType);
if (expression.TryGetBinaryExpression(out byte type, out ReadOnlySeExpression one, out ReadOnlySeExpression two))
return ConvertBinaryExpression(one, two, (ExpressionType)type);
if (expression.TryGetString(out ReadOnlySeString seString))
return ConvertSeString(seString);
return expression.AsSpan().ToString();
}
public String ConvertParameterExpression(ReadOnlySeExpression expression, ExpressionType type)
{
return type switch
{
Lumina.Text.Expressions.ExpressionType.LocalNumber => $"IntegerParameter({ConvertExpression(expression)})",
Lumina.Text.Expressions.ExpressionType.GlobalNumber => $"PlayerParameter({ConvertExpression(expression)})",
Lumina.Text.Expressions.ExpressionType.LocalString => $"StringParameter({ConvertExpression(expression)})",
Lumina.Text.Expressions.ExpressionType.GlobalString => $"ObjectParameter({ConvertExpression(expression)})",
_ => throw new NotImplementedException() // cannot reach, as this instance is immutable and this field is filtered from constructor
};
}
public String ConvertBinaryExpression(ReadOnlySeExpression firstExpression, ReadOnlySeExpression secondExpression, ExpressionType type)
{
string first = ConvertExpression(firstExpression);
string second = ConvertExpression(secondExpression);
var result = type switch
{
Lumina.Text.Expressions.ExpressionType.GreaterThanOrEqualTo => $"GreaterThanOrEqualTo({first},{second})",
Lumina.Text.Expressions.ExpressionType.GreaterThan => $"GreaterThan({first},{second})",
Lumina.Text.Expressions.ExpressionType.LessThanOrEqualTo => $"LessThanOrEqualTo({first},{second})",
Lumina.Text.Expressions.ExpressionType.LessThan => $"LessThan({first},{second})",
Lumina.Text.Expressions.ExpressionType.Equal => $"Equal({first},{second})",
Lumina.Text.Expressions.ExpressionType.NotEqual => $"NotEqual({first},{second})",
_ => throw new NotImplementedException() // cannot reach, as this instance is immutable and this field is filtered from constructor
};
// Define the regular expression pattern
// \b matches a word boundary, gnum matches the literal string "gnum",
// \d+ matches one or more digits, and (?=\D) is a positive lookahead that asserts the next character is not a digit
string pattern = @"\bgnum(\d+)(?=\D)";
// Define the replacement string using a MatchEvaluator delegate
// This allows us to dynamically construct the replacement string based on the matched digits
return Regex.Replace(result, pattern, match => $"PlayerParameter({match.Groups[1].Value})");
}
public String ConvertPayload(ReadOnlySePayload payload)
{
if (payload.Type == ReadOnlySePayloadType.Invalid)
return "";
if (payload.Type == ReadOnlySePayloadType.Text)
return payload.AsSpan().ToString().Replace( "<", "\\<" );
switch( payload.MacroCode )
{
case MacroCode.NewLine:
return "\n";
case MacroCode.SoftHyphen:
return "";
case MacroCode.Hyphen:
return "–";
case MacroCode.NonBreakingSpace:
return "";
default:
{
//return "\nExpression: " + payload.AsSpan().ToString() + "\n";
var expressions = (payload as ICollection<ReadOnlySeExpression>);
var expressionCount = expressions.Count;
if (expressionCount == 0)
return ConvertPayloadTag($"<{payload.AsSpan().ToString().ToLower()}>");
var originalPayloadTag = payload.MacroCode.ToString();
var payloadTag = originalPayloadTag.ToLower();
if (payloadTag == "if" && expressionCount == 3) {
string expression1 = ConvertExpression(expressions.ElementAt(0));
string expression2 = ConvertExpression(expressions.ElementAt(1));
if (expression2.Length == 0)
expression2 = " ";
string expression3 = ConvertExpression(expressions.ElementAt(2));
if (expression3.Length == 0)
expression3 = " ";
return $"<If({expression1})>{expression2}<Else/>{expression3}</If>";
}
if (expressionCount == 1) {
string expression1 = ConvertExpression(expressions.ElementAt(0));
return ConvertPayloadTag($"<{originalPayloadTag}>{expression1}</{originalPayloadTag}>");
}
return ConvertPayloadTag($"<{payloadTag}({string.Join( ',', expressions.Select(
ex => ConvertExpression(ex)
) )})>");
}
}
}
}