Skip to content

Commit

Permalink
✨ Csf 格式转换器
Browse files Browse the repository at this point in the history
Signed-off-by: 舰队的偶像-岛风酱! <frg2089@outlook.com>
  • Loading branch information
frg2089 committed Aug 26, 2023
1 parent 77d4767 commit 901fe7c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
107 changes: 107 additions & 0 deletions app/Shimakaze.Sdk.Csf.Converter/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;

using Microsoft.Extensions.DependencyInjection;

using Sharprompt;

using Shimakaze.Sdk.Common;
using Shimakaze.Sdk.Csf;
using Shimakaze.Sdk.Csf.Converter;
using Shimakaze.Sdk.IO.Csf;
using Shimakaze.Sdk.IO.Csf.Json;
using Shimakaze.Sdk.IO.Csf.Xml;
using Shimakaze.Sdk.IO.Csf.Yaml;

if (args is { Length: < 1 })
throw new ArgumentException("参数太少");

string input = args[0];

await using var ifs = File.OpenRead(input);

ServiceCollection services = new();

var formats = new[]{
"Yaml",
"JsonV2",
"JsonV1",
"Xml",
"Csf"
};
string current;
string defaultValue;
if (input.EndsWith(".csf", StringComparison.OrdinalIgnoreCase))
{
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfReader(ifs));
current = "Csf";
defaultValue = "Yaml";
}
else if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase))
{
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfYamlV1Reader(ifs));
current = "Yaml";
defaultValue = "Csf";
}
else if (input.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
var protocol = await JsonSerializer.DeserializeAsync<ProtocolObject>(ifs);
ifs.Seek(0, SeekOrigin.Begin);
JsonSerializerOptions options = new()
{
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
};
switch (protocol?.Protocol)
{
case 2:
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfJsonV2Reader(ifs, options));
current = "JsonV2";
break;
case 1:
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfJsonV1Reader(ifs, options));
current = "JsonV1";
break;
default:
Console.WriteLine(protocol?.Protocol);
throw new NotSupportedException();
}
defaultValue = "Csf";
}
else if (input.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
{
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfXmlV1Reader(ifs));
current = "Xml";
defaultValue = "Csf";
}
else
{
throw new NotSupportedException();
}

var selected = Prompt.Select("请选择要转换的格式", formats.Where(i => i != current), defaultValue: defaultValue);
var output = args.Length > 1
? args[1]
: Prompt.Input<string>("请输入生成的文件的路径", selected switch
{
"Yaml" => $"{input}.yaml",
"JsonV2" => $"{input}.json",
"JsonV1" => $"{input}.json",
"Xml" => $"{input}.xml",
"Csf" => $"{input}.csf",
_ => throw new NotSupportedException()
});
await using var ofs = File.Create(output);
services.AddSingleton<AsyncWriter<CsfDocument>>(selected switch
{
"Yaml" => new CsfYamlV1Writer(ofs),
"JsonV2" => new CsfJsonV2Writer(ofs, new() { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }),
"JsonV1" => new CsfJsonV1Writer(ofs, new() { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }),
"Xml" => new CsfXmlV1Writer(ofs, new() { Indent = true }),
"Csf" => new CsfWriter(ofs),
_ => throw new NotSupportedException()
});
await using var provider = services.BuildServiceProvider();
var csf = await provider.GetRequiredService<AsyncReader<CsfDocument>>().ReadAsync();
await provider.GetRequiredService<AsyncWriter<CsfDocument>>().WriteAsync(csf);
9 changes: 9 additions & 0 deletions app/Shimakaze.Sdk.Csf.Converter/ProtocolObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace Shimakaze.Sdk.Csf.Converter;

class ProtocolObject
{
[JsonPropertyName("protocol")]
public int Protocol { get; set; }
}
15 changes: 15 additions & 0 deletions app/Shimakaze.Sdk.Csf.Converter/Shimakaze.Sdk.Csf.Converter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Shimakaze.Sdk\Shimakaze.Sdk.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Sharprompt" Version="3.0.0-preview2" />
</ItemGroup>

</Project>

0 comments on commit 901fe7c

Please sign in to comment.