Skip to content

Commit

Permalink
🔀 Merge pull request #120 from ShimakazeProject/feat/refactor
Browse files Browse the repository at this point in the history
♻️ 重构代码
  • Loading branch information
frg2089 authored Jul 14, 2023
2 parents 2f3c81c + 0f7cb9e commit a81bb69
Show file tree
Hide file tree
Showing 186 changed files with 2,397 additions and 3,207 deletions.
12 changes: 6 additions & 6 deletions app/Shimakaze.Sdk.Vpl.Editor/ColorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ public static class ColorExtensions
/// <summary>
/// 转 ANSI Color 字符串 用于在终端显示颜色
/// </summary>
/// <param name="color">颜色</param>
/// <param name="isBackground">是否是背景色</param>
/// <returns></returns>
/// <param name="color"> 颜色 </param>
/// <param name="isBackground"> 是否是背景色 </param>
/// <returns> </returns>
public static string GetANSIString(this in Color color, bool isBackground = false) => $"\x1B[{(isBackground ? 4 : 3)}8;2;{color.GetR5()};{color.GetG6()};{color.GetB5()}m";

/// <summary>
/// 反转颜色
/// </summary>
/// <param name="color">颜色</param>
/// <returns>被反转后的颜色</returns>
/// <param name="color"> 颜色 </param>
/// <returns> 被反转后的颜色 </returns>
public static Color GetReverse(this in Color color) => new(
(byte)~color.Red,
(byte)~color.Green,
(byte)~color.Blue
);
}
}
6 changes: 3 additions & 3 deletions app/Shimakaze.Sdk.Vpl.Editor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

await using (var vplStream = File.OpenRead(vplPath))
await using (VoxelPaletteReader vplReader = new(vplStream))
vpl = vplReader.Read();
vpl = await vplReader.ReadAsync();

await using (var palStream = File.OpenRead(palPath))
await using (PaletteReader palReader = new(palStream))
pal = palReader.Read();
pal = await palReader.ReadAsync();

VplEditor editor = new(vpl, pal, async (editor) =>
{
string path = Prompt.Input<string>("Where is your new VPL file save to?", vplPath);
await using var fs = File.Create(path);
await using VoxelPaletteWriter writer = new(fs);
writer.Write(editor.Vpl);
await writer.WriteAsync(editor.Vpl);
});

await editor.RunAsync();
24 changes: 19 additions & 5 deletions app/Shimakaze.Sdk.Vpl.Editor/VplEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Shimakaze.Sdk.Vpl.Editor;

internal sealed class VplEditor
{
public readonly VoxelPalette Vpl;
private const int SIZE_OF_CELL = 3;
private const int X_SECTION_OFFSET = 3 * 16 + 4;
private const int Y_OFFSET = 1;
private const int SIZE_OF_CELL = 3;
public readonly VoxelPalette Vpl;
private readonly Palette _pal;
private readonly Func<VplEditor, Task> _saver;
private bool _isEditing;
private (int X, int Y, int Section) _current;
private (int X, int Y) _editing;
private bool _isEditing;

public VplEditor(VoxelPalette vpl, Palette pal, Func<VplEditor, Task> saver)
{
Expand All @@ -41,43 +41,53 @@ public async Task RunAsync()
_current.Section--;
PrintColor();
break;

case ConsoleKey.Tab when !_isEditing && key.Modifiers is 0 && _current.Section + 1 < Vpl.Header.SectionCount:
case ConsoleKey.M when !_isEditing && _current.Section + 1 < Vpl.Header.SectionCount:
_current.Section++;
PrintColor();
break;

case ConsoleKey.LeftArrow when !_isEditing && _current.X > 0:
case ConsoleKey.H when !_isEditing && _current.X > 0:
_current.X--;
break;

case ConsoleKey.RightArrow when !_isEditing && _current.X < 15:
case ConsoleKey.L when !_isEditing && _current.X < 15:
_current.X++;
break;

case ConsoleKey.UpArrow when !_isEditing && _current.Y > 0:
case ConsoleKey.K when !_isEditing && _current.Y > 0:
_current.Y--;
break;

case ConsoleKey.DownArrow when !_isEditing && _current.Y < 15:
case ConsoleKey.J when !_isEditing && _current.Y < 15:
_current.Y++;
break;

case ConsoleKey.LeftArrow when _isEditing && _editing.X > 0:
case ConsoleKey.H when _isEditing && _editing.X > 0:
_editing.X--;
break;

case ConsoleKey.RightArrow when _isEditing && _editing.X < 15:
case ConsoleKey.L when _isEditing && _editing.X < 15:
_editing.X++;
break;

case ConsoleKey.UpArrow when _isEditing && _editing.Y > 0:
case ConsoleKey.K when _isEditing && _editing.Y > 0:
_editing.Y--;
break;

case ConsoleKey.DownArrow when _isEditing && _editing.Y < 15:
case ConsoleKey.J when _isEditing && _editing.Y < 15:
_editing.Y++;
break;

case ConsoleKey.Enter when !_isEditing:
case ConsoleKey.Spacebar when !_isEditing:
byte index = Vpl[_current.Section][(_current.Y * 16) + _current.X];
Expand All @@ -86,6 +96,7 @@ public async Task RunAsync()
_isEditing = true;
PrintColor();
break;

case ConsoleKey.Enter when _isEditing:
case ConsoleKey.Spacebar when _isEditing:
index = (byte)((_editing.Y * 16) + _editing.X);
Expand All @@ -97,11 +108,13 @@ public async Task RunAsync()
_isEditing = false;
PrintColor();
break;

case ConsoleKey.Escape when _isEditing:
case ConsoleKey.Q when _isEditing:
_isEditing = false;
PrintColor();
break;

case ConsoleKey.Escape when !_isEditing:
case ConsoleKey.Q when !_isEditing:
Console.SetCursorPosition(0, Y_OFFSET + 16);
Expand All @@ -111,6 +124,7 @@ public async Task RunAsync()

PrintColor();
break;

case ConsoleKey.S:
Console.SetCursorPosition(0, Y_OFFSET + 16);
await _saver(this);
Expand All @@ -120,7 +134,7 @@ public async Task RunAsync()
}
}

void PrintColor()
private void PrintColor()
{
Console.CursorVisible = false;
Console.Clear();
Expand Down Expand Up @@ -156,4 +170,4 @@ void PrintColor()
}
Console.CursorVisible = true;
}
}
}
6 changes: 5 additions & 1 deletion build/CSharp.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(MSBuildProjectNameWithoutTests)' != 'Shimakaze.Sdk.Common' ">
<Using Include="Shimakaze.Sdk.Common" />
<ProjectReference Include="$(ShimakazeSdkSourceFolder)Shimakaze.Sdk.Common\Shimakaze.Sdk.Common.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="$(MSBuildProjectName).Tests" />
<Using Condition=" '$(AllowUnsafeBlocks)' == 'True' " Include="Shimakaze.Sdk.IO.Extensions" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion build/CSharpTests.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<CollectCoverage>True</CollectCoverage>
<CoverletOutputFormat>lcov</CoverletOutputFormat>
<ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute</ExcludeByAttribute>
<ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,ExcludeFromCodeCoverageAttribute</ExcludeByAttribute>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions src/Shimakaze.Sdk.Build/CommonUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace Shimakaze.Sdk.Build;

[ExcludeFromCodeCoverage]
internal static class CommonUtil
{
/// <summary>
/// 根据文件路径创建上级目录
/// </summary>
/// <param name="file">文件</param>
/// <param name="log">日志帮助程序</param>
/// <returns></returns>
/// <param name="file"> 文件 </param>
/// <param name="log"> 日志帮助程序 </param>
/// <returns> </returns>
public static bool CreateParentDirectory([NotNullWhen(true)] this string? file, TaskLoggingHelper? log = null)
{
if (string.IsNullOrEmpty(file))
Expand Down
51 changes: 28 additions & 23 deletions src/Shimakaze.Sdk.Build/CsfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
using Microsoft.Extensions.DependencyInjection;

using Shimakaze.Sdk.Csf;
using Shimakaze.Sdk.Csf.Json.Serialization;
using Shimakaze.Sdk.Csf.Xml.Serialization;
using Shimakaze.Sdk.Csf.Yaml.Serialization;
using Shimakaze.Sdk.IO;
using Shimakaze.Sdk.IO.Csf;
using Shimakaze.Sdk.IO.Serialization;
using Shimakaze.Sdk.IO.Csf.Json;
using Shimakaze.Sdk.IO.Csf.Xml;
using Shimakaze.Sdk.IO.Csf.Yaml;

using MSTask = Microsoft.Build.Utilities.Task;

Expand All @@ -20,26 +18,28 @@ namespace Shimakaze.Sdk.Build;
public sealed class CsfBuilder : MSTask
{
/// <summary>
/// 将要被处理的文件
/// Destination
/// </summary>
[Required]
public required ITaskItem[] SourceFiles { get; set; }
public const string Metadata_Destination = "Destination";

/// <summary>
/// Type
/// </summary>
public const string Metadata_Type = "Type";

/// <summary>
/// 生成的目标文件
/// </summary>
[Output]
public ITaskItem[] OutputFiles { get; set; } = Array.Empty<ITaskItem>();

/// <summary>
/// Destination
/// </summary>
public const string Metadata_Destination = "Destination";
/// <summary>
/// Type
/// 将要被处理的文件
/// </summary>
public const string Metadata_Type = "Type";
[Required]
public required ITaskItem[] SourceFiles { get; set; }

/// <inheritdoc/>
/// <inheritdoc />
public override bool Execute()
{
Log.LogMessage("Generating CSF File...");
Expand All @@ -56,7 +56,7 @@ public override bool Execute()
services.Clear();
using Stream stream = File.OpenRead(file.ItemSpec);
using Stream output = File.Create(dest);
services.AddSingleton<IWriter<CsfDocument>>(new CsfWriter(output));
services.AddSingleton<AsyncWriter<CsfDocument>>(new CsfWriter(output));

switch (tag.ToLowerInvariant())
{
Expand All @@ -71,34 +71,39 @@ public override bool Execute()
0,
0,
"You shouldn't use the \"CSF Json version 1\". Please port your file to \"version 2\" or use \"Csf Yaml version 1\" to replace that.");
services.AddSingleton<IDeserializer<CsfDocument>>(new CsfJsonV1Deserializer(stream));
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfJsonV1Reader(stream));
break;

case "json":
case "jsonv2":
services.AddSingleton<IDeserializer<CsfDocument>>(new CsfJsonV2Deserializer(stream));
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfJsonV2Reader(stream));
break;

case "xml":
case "xmlv1":
services.AddSingleton<IDeserializer<CsfDocument>>(new CsfXmlV1Deserializer(stream));
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfXmlV1Reader(stream));
break;

case "yml":
case "yaml":
case "ymlv1":
case "yamlv1":
services.AddSingleton<IDeserializer<CsfDocument>>(new CsfYamlV1Deserializer(stream));
services.AddSingleton<AsyncReader<CsfDocument>>(new CsfYamlV1Reader(stream));
break;

case "csf":
Log.LogWarning(
"Shimakaze.Sdk.Csf",
"CSF0001",
"No Json V1",
"No CSF",
file.ItemSpec,
0,
0,
0,
0,
"You shouldn't use the \"CSF File\" direct in your project. Please port your file to \"version 2\" or use \"Csf Yaml version 1\" to replace that.");
break;

default:
Log.LogError(
"Shimakaze.Sdk.Csf",
Expand All @@ -117,7 +122,7 @@ public override bool Execute()
CsfDocument csf;
try
{
csf = provider.GetRequiredService<IDeserializer<CsfDocument>>().Deserialize();
csf = provider.GetRequiredService<AsyncReader<CsfDocument>>().ReadAsync().Result;
}
catch (Exception e)
{
Expand All @@ -139,7 +144,7 @@ public override bool Execute()
}
return false;
}
provider.GetRequiredService<IWriter<CsfDocument>>().Write(csf);
provider.GetRequiredService<AsyncWriter<CsfDocument>>().WriteAsync(csf).Wait();
TaskItem item = new(dest);
file.CopyMetadataTo(item);
item.RemoveMetadata(Metadata_Destination);
Expand Down
Loading

0 comments on commit a81bb69

Please sign in to comment.