Skip to content

Commit

Permalink
SubtitleParse/AssParse: AOT-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
MIRIMIRIM committed Mar 7, 2024
1 parent 932e773 commit 2cbed51
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
9 changes: 4 additions & 5 deletions SubtitleParse/AssTypes/AssColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ public StringBuilder ToStringBuilder()

public class AssRGB8
{
public byte R = 0;
public byte G = 0;
public byte B = 0;
public byte A = 0;
public byte R { get; set; } = 0;
public byte G { get; set; } = 0;
public byte B { get; set; } = 0;
public byte A { get; set; } = 0;

public static AssRGB8 Parse(ReadOnlySpan<char> sp)
{

var sign = (sp[^1] == '&') ? 3 : 2;

if ((sp[0] != '&') || (sp[1] != 'H') || ((sp.Length - sign) % 2 != 0))
Expand Down
2 changes: 1 addition & 1 deletion SubtitleParse/AssTypes/AssEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void ReadWithoutHeader(ReadOnlySpan<char> sp, string[] fmts)
case "Marked":
break;
default:
SetProperty(this, fmts[segCount], v);
SetProperty(this, typeof(AssEvent), fmts[segCount], v);
break;
}

Expand Down
4 changes: 2 additions & 2 deletions SubtitleParse/AssTypes/AssScriptInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void Read(ReadOnlySpan<char> sp)
default:
if (TrySplitKeyValue(sp, out string k, out string v))
{
if (IsStringInFields(new AssConstants.ScriptInfo(), k))
if (IsStringInFields(new AssConstants.ScriptInfo(), typeof(AssConstants.ScriptInfo), k))
{
if (k.AsSpan().SequenceEqual(AssConstants.ScriptInfo.YCbCrMatrix.AsSpan()))
{
Expand All @@ -100,7 +100,7 @@ public void Read(ReadOnlySpan<char> sp)
}
else
{
SetProperty(this, k.Contains(' ') ? k.Replace(" ", "") : k, v);
SetProperty(this, typeof(AssScriptInfo), k.Contains(' ') ? k.Replace(" ", "") : k, v);
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion SubtitleParse/AssTypes/AssStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Read(ReadOnlySpan<char> sp)

for (var i = 0; i < va.Length; i++)
{
SetProperty(syl, Formats[i], va[i]);
SetProperty(syl, typeof(AssStyle), Formats[i], va[i]);
}
Collection.Add(syl);
if (!Names.Add(syl.Name))
Expand Down
9 changes: 5 additions & 4 deletions SubtitleParse/Utils/ParseHelper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Mobsub.AssTypes;
using System.Diagnostics.CodeAnalysis;

namespace Mobsub.Utils;

public class ParseHelper
{
internal static void SetProperty(object obj, string propertyName, string value)
public static void SetProperty(object obj, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type T, string propertyName, string value)
{
var property = obj.GetType().GetProperty(propertyName);
var property = T.GetProperty(propertyName);
if (property == null)
{
return;
Expand Down Expand Up @@ -50,13 +51,13 @@ internal static void SetProperty(object obj, string propertyName, string value)
}
}

public static bool IsStringInFields(object obj, string searchString)
public static bool IsStringInFields(object obj, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type T, string searchString)
{
if (obj == null || string.IsNullOrEmpty(searchString))
{
return false;
}
foreach (var field in obj.GetType().GetFields())
foreach (var field in T.GetFields())
{
if (field.FieldType == typeof(string))
{
Expand Down

0 comments on commit 2cbed51

Please sign in to comment.