Skip to content

Commit

Permalink
#16 +ExceptionDataClass
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamarine5 committed Jul 29, 2023
1 parent 03b3bda commit 796e8c1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Aquc.Stackbricks.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>3.1.0.30728</VersionPrefix>
<VersionPrefix>3.1.2.30729</VersionPrefix>
<VersionSuffix>beta</VersionSuffix>
<Authors>aquamarine5</Authors>
<Company>aquamarine5; RenegadeCreation</Company>
Expand Down
38 changes: 37 additions & 1 deletion Aquc.Stackbricks.DataClass/DataClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public CheckDataClass(bool isProgram, bool needUpdate, string version)
IsProgram = isProgram;
}
}

[JsonConverter(typeof(ExceptionDCJsonConverter))]
public class ExceptionDataClass : IDataClass
{
public string DCID => ID;
public const string ID = "stbks.dc.exception";
public string type;
public string message;
public bool IsProgram => false;
public ExceptionDataClass(Exception exception)
{
type=exception.GetType().Name; message=exception.Message;
}
public ExceptionDataClass(string type, string message)
{
this.type = type;
this.message = message;
}
}

public class CheckDownloadDataClass : CheckDataClass, IDataClass
{

Expand Down Expand Up @@ -66,6 +86,20 @@ public CheckDownloadDataClass(bool isProgram, bool needUpdate, string version, s

}
}

public class InteropDataClass<T>
where T : IDataClass
{
public T? Value;
public ExceptionDataClass? Exception;
public bool IsSuccess=>Exception==default;
public InteropDataClass(T? value, ExceptionDataClass? exception)
{
Value = value;
Exception = exception;
}
}

public class InstallDataClass : IDataClass
{

Expand Down Expand Up @@ -117,13 +151,15 @@ public class DataClassManager
{typeof(CheckDataClass),CheckDataClass.ID },
{typeof(CheckDownloadDataClass),CheckDownloadDataClass.ID },
{typeof(InstallDataClass),InstallDataClass.ID },
{typeof(ExceptionDataClass),ExceptionDataClass.ID },
};
public readonly static Dictionary<string,Type> matchDictToType = new()
public readonly static Dictionary<string, Type> matchDictToType = new()
{
{UpdateDataClass.ID,typeof(UpdateDataClass) },
{CheckDataClass.ID , typeof(CheckDataClass) },
{CheckDownloadDataClass.ID , typeof(CheckDownloadDataClass) },
{InstallDataClass.ID , typeof(InstallDataClass) },
{ExceptionDataClass.ID,typeof(ExceptionDataClass) },
};
public static string ParseType<T>()
where T : IDataClass
Expand Down
20 changes: 20 additions & 0 deletions Aquc.Stackbricks.DataClass/JsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,25 @@ public override void Write(Utf8JsonWriter writer, CheckDataClass value, JsonSeri
writer.WriteString("DCID", value.DCID);
writer.WriteBoolean("isProgram", value.IsProgram);
writer.WriteBoolean("needUpdate", value.needUpdate);
writer.WriteEndObject();
}
}

public class ExceptionDCJsonConverter : JsonConverter<ExceptionDataClass>
{
public override ExceptionDataClass? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{

var jsonNode = JsonNode.Parse(ref reader)!;
return new ExceptionDataClass(jsonNode["type"]!.ToString(), jsonNode["message"]!.ToString());
}

public override void Write(Utf8JsonWriter writer, ExceptionDataClass value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString("type", value.type);
writer.WriteString("message", value.message);
writer.WriteString("DCID", value.DCID);
writer.WriteEndObject();
}
}
16 changes: 8 additions & 8 deletions Aquc.Stackbricks.Interop/Interop.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Aquc.Stackbricks.DataClass;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Aquc.Stackbricks.Interop;

Expand All @@ -11,11 +12,12 @@ public StackbricksInterop(FileInfo file)
{
processFile= file;
}
public async Task<UpdateDataClass> Update()
public async Task<InteropDataClass<UpdateDataClass>> Update()
{
return await Execute<UpdateDataClass>(new string[] { "self","update", "--json", "--no-log" });
}
public async Task<object?> Execute(string[] args)
public async Task<InteropDataClass<T>> Execute<T>(string[] args)
where T : IDataClass
{
var arg = string.Join(" ", args);
var process = new Process
Expand All @@ -35,11 +37,9 @@ public async Task<UpdateDataClass> Update()
var result=output.Split(DataClassManager.SPLIT_KEY);
Console.WriteLine(output);
var type = DataClassManager.ParseID(result[0]);
return JsonSerializer.Deserialize(result[1], type);
}
public async Task<T> Execute<T>(string[] args)
where T : IDataClass
{
return (T)(await Execute(args))!;
if (result[0] == ExceptionDataClass.ID)
return new InteropDataClass<T>(default, JsonSerializer.Deserialize<ExceptionDataClass>(result[1]));
else
return new InteropDataClass<T>((T)JsonSerializer.Deserialize(result[1], type)!,default);
}
}
1 change: 1 addition & 0 deletions Aquc.Stackbricks/Aquc.Stackbricks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Nullable>enable</Nullable>
<DebugType>embedded</DebugType>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>

<PublishSingleFile>true</PublishSingleFile>
<SatelliteResourceLanguages>en-US</SatelliteResourceLanguages>
<SupportedOSPlatformVersion>10.0.17763.0</SupportedOSPlatformVersion>
Expand Down
14 changes: 14 additions & 0 deletions Aquc.Stackbricks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ public class StackbricksProgram
}).Invoke();

public static async Task Main(string[] args)
{
try
{
await BuiltinMain(args);
}
catch(Exception ex)
{
if (args.Contains("--json"))
DataClassParser.ParseDataClassPrintin(new ExceptionDataClass(ex));
throw;
}
}

static async Task BuiltinMain(string[] args)
{
var jsonOption = new Option<bool>("--json", () => { return false; });
var uwpnofOption = new Option<bool>("--no-uwpnof", () => { return false; });
Expand Down

0 comments on commit 796e8c1

Please sign in to comment.