This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-nitride-cil/src/MfGames.Nitride/Contents/TextContentExtensions.cs

51 lines
1.2 KiB
C#
Raw Permalink Normal View History

2021-09-07 05:15:45 +00:00
using System.IO;
2022-09-06 05:53:22 +00:00
using MfGames.Gallium;
2022-09-06 05:53:22 +00:00
namespace MfGames.Nitride.Contents;
public static class TextContentExtensions
2021-09-07 05:15:45 +00:00
{
public static string GetTextContentString(this ITextContent content)
2021-09-07 05:15:45 +00:00
{
var writer = new StringWriter();
using TextReader reader = content.GetReader();
string? line = reader.ReadLine();
2021-09-07 05:15:45 +00:00
while (line != null)
{
writer.WriteLine(line);
line = reader.ReadLine();
2021-09-07 05:15:45 +00:00
}
return writer.ToString();
2021-09-07 05:15:45 +00:00
}
public static string? GetTextContentString(this IBinaryContent content)
{
if (content is ITextContentConvertable convertableContent)
{
return convertableContent
.ToTextContent()
.GetTextContentString();
}
return null;
}
public static string? GetTextContentString(this Entity entity)
{
if (entity.TryGet(out ITextContent textContent))
{
return textContent.GetTextContentString();
}
if (entity.TryGet(out IBinaryContent binaryContent))
{
return binaryContent.GetTextContentString();
}
return null;
}
2021-09-07 05:15:45 +00:00
}