Skip to content

Commit

Permalink
Refactor Run, extract SplitsController, use source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimMaple committed Sep 18, 2024
1 parent f976414 commit 864c33b
Show file tree
Hide file tree
Showing 16 changed files with 511 additions and 489 deletions.
5 changes: 5 additions & 0 deletions SpeedTool/JSON/SourceGeneratorContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json.Serialization;
using SpeedTool.Global.Definitions;
using SpeedTool.Splits;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(ClassicUISettings))]
Expand All @@ -8,4 +9,8 @@
[JsonSerializable(typeof(HotkeySettings))]
[JsonSerializable(typeof(SpeedToolUISettings))]
[JsonSerializable(typeof(SpeedToolUITheme))]
[JsonSerializable(typeof(SplitInfo))]
[JsonSerializable(typeof(SplitDisplayInfo))]
[JsonSerializable(typeof(Category))]
[JsonSerializable(typeof(RunInfo))]
internal partial class SourceGeneratorContext : JsonSerializerContext {}
45 changes: 45 additions & 0 deletions SpeedTool/JSON/TimeCollectionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using SpeedTool.Splits;
using SpeedTool.Timer;

namespace SpeedTool.JSON;

public sealed class TimeCollectionConverter : JsonConverter<TimeCollection>
{
public override TimeCollection Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
{
throw new JsonException($"Invalid object");
}

TimeCollection tc = new();

for(int i = 0; i < (int)TimingMethod.Last; i++)
{
reader.Read();
if(reader.TokenType != JsonTokenType.Number)
throw new JsonException("Expected number");

tc[(TimingMethod)i] = new TimeSpan(reader.GetInt64());
}

while (reader.TokenType != JsonTokenType.EndArray)
{
reader.Read();
}

return tc;
}

public override void Write(Utf8JsonWriter writer, TimeCollection value, JsonSerializerOptions options)
{
writer.WriteStartArray();

for(int i = 0; i < (int)TimingMethod.Last; i++)
writer.WriteNumberValue(value[(TimingMethod)i].Ticks);

writer.WriteEndArray();
}
}
28 changes: 24 additions & 4 deletions SpeedTool/Platform/Platform.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Json.Nodes;
using System.Text.Json;
using Silk.NET.Input.Glfw;
using Silk.NET.Windowing.Glfw;
using SpeedTool.Injector;
Expand Down Expand Up @@ -134,6 +134,15 @@ public void PreviousCategory()
ReloadRun();
}

public TimeCollection GetCurrentTimes()
{
TimeCollection tc = new();
for(int i = 0; i < (int)TimingMethod.Last; i++)
tc[(TimingMethod)i] = GetTimerFor((TimingMethod)i).CurrentTime;

return tc;
}

public ITimerSource GetTimerFor(TimingMethod method)
{
return sources[(int)method];
Expand Down Expand Up @@ -185,7 +194,8 @@ public void SaveRunAsPB(RunInfo run)
var fileName = game!.Name + "." + CurrentCategory!.Name;
fileName = Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c, '_'));
var dst = ENV.LocalFilesPath + "pbs/" + fileName + ".json";
File.WriteAllText(dst, run.ToJson().ToString());
var json = JsonSerializer.Serialize(run, typeof(RunInfo), SourceGeneratorContext.Default);
File.WriteAllText(dst, json);
}

public RunInfo? GetPBRun(Game g, Category c)
Expand All @@ -195,7 +205,17 @@ public void SaveRunAsPB(RunInfo run)

var dst = ENV.LocalFilesPath + "pbs/" + fileName + ".json";
if(File.Exists(dst))
return RunInfo.FromJson(JsonNode.Parse(File.ReadAllText(dst))!.AsObject());
{
try
{
return JsonSerializer.Deserialize(File.ReadAllText(dst), typeof(RunInfo), SourceGeneratorContext.Default) as RunInfo;
}
catch
{
File.Delete(dst);
return null;
}
}
return null;
}

Expand Down Expand Up @@ -244,7 +264,7 @@ public void ReloadRun()
if(injector != null)
injector.Reset();

run = new Run(game, game.GetCategories()[activeCategory].Splits, GetPBRun(game, CurrentCategory!));
run = new Run(game, game.GetCategories()[activeCategory], GetPBRun(game, CurrentCategory!));
sources[(int)TimingMethod.RealTime] = run.Timer;
}

Expand Down
Loading

0 comments on commit 864c33b

Please sign in to comment.