Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SeString ToString overrides #76

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Lumina/Text/Expressions/BinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public override string ToString()
{
return ExpressionType switch
{
ExpressionType.GreaterThanOrEqualTo => $"BinExpr{{{Operand1} >= {Operand2}}}",
ExpressionType.GreaterThan => $"BinExpr{{{Operand1} > {Operand2}}}",
ExpressionType.LessThanOrEqualTo => $"BinExpr{{{Operand1} <= {Operand2}}}",
ExpressionType.LessThan => $"BinExpr{{{Operand1} < {Operand2}}}",
ExpressionType.Equal => $"BinExpr{{{Operand1} == {Operand2}}}",
ExpressionType.NotEqual => $"BinExpr{{{Operand1} != {Operand2}}}",
ExpressionType.GreaterThanOrEqualTo => $"[{Operand1}>={Operand2}]",
ExpressionType.GreaterThan => $"[{Operand1}>{Operand2}]",
ExpressionType.LessThanOrEqualTo => $"[{Operand1}<={Operand2}]",
ExpressionType.LessThan => $"[{Operand1}<{Operand2}]",
ExpressionType.Equal => $"[{Operand1}=={Operand2}]",
ExpressionType.NotEqual => $"[{Operand1}!={Operand2}]",
_ => throw new NotImplementedException() // cannot reach, as this instance is immutable and this field is filtered from constructor
};
}
Expand Down
33 changes: 20 additions & 13 deletions src/Lumina/Text/Expressions/ExpressionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@ namespace Lumina.Text.Expressions;

public enum ExpressionType : byte
{
// Placeholder expressions (Zero arity): 0xD0 ~ 0xDF
Hour = 0xDB, // 0 ~ 23
Weekday = 0xDD, // 1(sunday) ~ 7(saturday)
// Placeholder expressions (Zero arity): 0xD8 ~ 0xDF
Millisecond = 0xD8, // t_msec
Second = 0xD9, // t_sec
Minute = 0xDA, // t_min
Hour = 0xDB, // t_hour 0 ~ 23
Day = 0xDC, // t_day
Weekday = 0xDD, // t_wday 1(sunday) ~ 7(saturday)
Month = 0xDE, // t_mon
Year = 0xDF, // t_year

// Binary expressions
GreaterThanOrEqualTo = 0xE0,
GreaterThan = 0xE1,
LessThanOrEqualTo = 0xE2,
LessThan = 0xE3,
Equal = 0xE4,
NotEqual = 0xE5,
GreaterThanOrEqualTo = 0xE0, // [gteq]
GreaterThan = 0xE1, // [gt]
LessThanOrEqualTo = 0xE2, // [lteq]
LessThan = 0xE3, // [lt]
Equal = 0xE4, // [eq]
NotEqual = 0xE5, // [neq]

// Parameter (unary expressions)
IntegerParameter = 0xE8,
PlayerParameter = 0xE9,
StringParameter = 0xEA,
ObjectParameter = 0xEB,
IntegerParameter = 0xE8, // lnum
PlayerParameter = 0xE9, // gnum
StringParameter = 0xEA, // lstr
ObjectParameter = 0xEB, // gstr

// Placeholder expressions also exist between 0xEC ~ 0xEF, where 0xEC is at least checked existing.
StackColor = 0xEC, // stackcolor

// Embedded SeString
SeString = 0xff, // Followed by length (including length) and data
Expand Down
2 changes: 1 addition & 1 deletion src/Lumina/Text/Expressions/IntegerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static int CalculateSize( uint value )
}

/// <inheritdoc />
public override string ToString() => Value.ToString();
public override string ToString() => ( (int)Value ).ToString();

/// <summary>
/// Parse given Stream into an IntegerExpression.
Expand Down
8 changes: 4 additions & 4 deletions src/Lumina/Text/Expressions/ParameterExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public override string ToString()
{
return ExpressionType switch
{
ExpressionType.IntegerParameter => $"IntegerParam{{{Operand}}}",
ExpressionType.PlayerParameter => $"PlayerParam{{{Operand}}}",
ExpressionType.StringParameter => $"StringParam{{{Operand}}}",
ExpressionType.ObjectParameter => $"ObjectParam{{{Operand}}}",
ExpressionType.IntegerParameter => $"lnum{Operand}",
ExpressionType.PlayerParameter => $"gnum{Operand}",
ExpressionType.StringParameter => $"lstr{Operand}",
ExpressionType.ObjectParameter => $"gstr{Operand}",
_ => throw new NotImplementedException() // cannot reach, as this instance is immutable and this field is filtered from constructor
};
}
Expand Down
17 changes: 16 additions & 1 deletion src/Lumina/Text/Expressions/PlaceholderExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ public PlaceholderExpression( ExpressionType expressionType )
public override void Encode( Stream stream ) => stream.WriteByte( (byte)ExpressionType );

/// <inheritdoc />
public override string ToString() => $"Placeholder#{ExpressionType:X02}";
public override string ToString()
{
return ExpressionType switch
{
ExpressionType.Millisecond => "t_msec",
ExpressionType.Second => "t_sec",
ExpressionType.Minute => "t_min",
ExpressionType.Hour => "t_hour",
ExpressionType.Day => "t_day",
ExpressionType.Weekday => "t_wday",
ExpressionType.Month => "t_mon",
ExpressionType.Year => "t_year",
ExpressionType.StackColor => "stackcolor",
_ => $"Placeholder#{ExpressionType:X02}"
};
}

/// <summary>
/// Identify whether the given type byte indicates a PlaceholderExpression.
Expand Down
2 changes: 1 addition & 1 deletion src/Lumina/Text/Expressions/StringExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override void Encode( Stream stream )
}

/// <inheritdoc />
public override string ToString() => $"Str{{{Value?.ToString() ?? string.Empty}}}";
public override string ToString() => Value?.ToString() ?? string.Empty;

/// <summary>
/// Parse given Stream into a StringExpression.
Expand Down
27 changes: 27 additions & 0 deletions src/Lumina/Text/Payloads/BasePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,32 @@ public BasePayload ToTypedPayload()
/// Extracts and concatenates the text payloads and integer constants.
/// </summary>
public string RawString => _rawString.Value;

/// <inheritdoc />
public override string ToString()
{
if( PayloadType == PayloadType.Text )
return RawString.Replace( "<", "\\<" );

var code = (MacroCodes)PayloadType;
switch( code )
{
case MacroCodes.NewLine:
return "<br>";
case MacroCodes.NonBreakingSpace:
return "<nbsp>";
case MacroCodes.SoftHyphen:
return "-";
case MacroCodes.Hyphen:
return "--";
default:
{
if( Expressions.Count == 0 )
return $"<{code.ToString().ToLower()}>";

return $"<{code.ToString().ToLower()}({string.Join( ',', Expressions )})>";
}
}
}
}
}
59 changes: 59 additions & 0 deletions src/Lumina/Text/Payloads/MacroCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace Lumina.Text.Payloads;

internal enum MacroCodes : byte
{
SetResetTime = 0x06, // n N x
SetTime = 0x07, // n x
If = 0x08, // . . * x
Switch = 0x09, // . . .
PcName = 0x0A, // n x
IfPcGender = 0x0B, // n . . x
IfPcName = 0x0C, // n . . . x
Josa = 0x0D, // s s s x
Josaro = 0x0E, // s s s x
IfSelf = 0x0F, // n . . x
NewLine = 0x10, // <br>
Wait = 0x11, // n x
Icon = 0x12, // n x
Color = 0x13, // n N x
EdgeColor = 0x14, // n N x
ShadowColor = 0x15, // n N x
SoftHyphen = 0x16, // -
Key = 0x17, //
Scale = 0x18, // n
Bold = 0x19, // n
Italic = 0x1A, // n
Edge = 0x1B, // n n
Shadow = 0x1C, // n n
NonBreakingSpace = 0x1D, // <nbsp>
Icon2 = 0x1E, // n N x
Hyphen = 0x1F, // --
Num = 0x20, // n x
Hex = 0x21, // n x
Kilo = 0x22, // . s x
Byte = 0x23, // n x
Sec = 0x24, // n x
Time = 0x25, // n x
Float = 0x26, // n n s x
Link = 0x27, // n n n n s
Sheet = 0x28, // s . . .
String = 0x29, // s x
Caps = 0x2A, // s x
Head = 0x2B, // s x
Split = 0x2C, // s s n x
HeadAll = 0x2D, // s x
Fixed = 0x2E, // n n . . .
Lower = 0x2F, // s x
JaNoun = 0x30, // s . .
EnNoun = 0x31, // s . .
DeNoun = 0x32, // s . .
FrNoun = 0x33, // s . .
ChNoun = 0x34, // s . .
LowerHead = 0x40, // s x
ColorType = 0x48, // n x
EdgeColorType = 0x49, // n x
Digit = 0x50, // n n x
Ordinal = 0x51, // n x
Sound = 0x60, // n n
LevelPos = 0x61 // n x
}
2 changes: 1 addition & 1 deletion src/Lumina/Text/SeString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private ImmutableList< BasePayload > BuildPayloads()

public override string ToString()
{
return RawString;
return string.Concat( Payloads );
}
}
}
Loading