Repository navigation
Add input recording and replay #2586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace Spice86.Core.CLI; | ||
|
|
||
| /// <summary> | ||
| /// Selects which key schedules replayed input events. | ||
| /// </summary> | ||
| public enum ReplayInputKey { | ||
| /// <summary>Schedule each replayed event at its recorded CPU cycle count.</summary> | ||
| Cycle, | ||
|
|
||
| /// <summary>Schedule each replayed event at its recorded emulated elapsed time in milliseconds.</summary> | ||
| Time | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Spice86.Core/Emulator/VM/Input/Recording/InputEventSerialization.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| namespace Spice86.Core.Emulator.VM.Input.Recording; | ||
|
|
||
| using Spice86.Core.Emulator.VM.Input.Events; | ||
| using Spice86.Shared.Emulator.Keyboard; | ||
| using Spice86.Shared.Emulator.Mouse; | ||
| using Spice86.Shared.Interfaces; | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using System.Text.Json.Serialization.Metadata; | ||
|
|
||
| /// <summary>Shared JSON serializer options for input event recordings.</summary> | ||
| public static class InputEventSerialization { | ||
| /// <summary>JSON options using web defaults, string enum names and a "kind" discriminator for input events.</summary> | ||
| public static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web) { | ||
| Converters = { new JsonStringEnumConverter() }, | ||
| TypeInfoResolver = new DefaultJsonTypeInfoResolver { Modifiers = { DescribeInputEventKinds } } | ||
| }; | ||
|
|
||
| /// <summary>Registers the queued input kinds on <see cref="IInputEvent"/>, which lives in | ||
| /// Spice86.Shared and cannot name the Core command records in attributes.</summary> | ||
| private static void DescribeInputEventKinds(JsonTypeInfo typeInfo) { | ||
| if (typeInfo.Type != typeof(IInputEvent)) { | ||
| return; | ||
| } | ||
| typeInfo.PolymorphismOptions = new JsonPolymorphismOptions { | ||
| TypeDiscriminatorPropertyName = "kind", | ||
| DerivedTypes = { | ||
| new JsonDerivedType(typeof(KeyboardEventArgs), "key"), | ||
| new JsonDerivedType(typeof(MouseMoveEventArgs), "mouseMove"), | ||
| new JsonDerivedType(typeof(MouseButtonEventArgs), "mouseButton"), | ||
| new JsonDerivedType(typeof(ReleaseAllKeysInput), "releaseAllKeys"), | ||
| new JsonDerivedType(typeof(TimeMultiplierInput), "timeMultiplier") | ||
|
kevinferrare marked this conversation as resolved.
Resolved
|
||
| } | ||
| }; | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
src/Spice86.Core/Emulator/VM/Input/Recording/InputRecording.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| namespace Spice86.Core.Emulator.VM.Input.Recording; | ||
|
|
||
| using System.Linq; | ||
|
|
||
| /// <summary> | ||
| /// Collects the host input events applied during a reproducible run, for the dump to write. | ||
| /// </summary> | ||
| public sealed class InputRecording { | ||
| private readonly List<RecordedInputEvent> _events = []; | ||
| private readonly Lock _lock = new(); | ||
|
|
||
| /// <summary> | ||
| /// Creates a recording; a disabled one ignores every event and is not dumped. | ||
| /// </summary> | ||
| public InputRecording(bool isEnabled) { | ||
| IsEnabled = isEnabled; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Whether events are collected and dumped (set by --ReproducibleRun). | ||
| /// </summary> | ||
| public bool IsEnabled { get; } | ||
|
|
||
| /// <summary> | ||
| /// Adds an applied event; does nothing when the recording is disabled. | ||
| /// </summary> | ||
| public void Add(RecordedInputEvent recordedEvent) { | ||
| if (!IsEnabled) { | ||
| return; | ||
| } | ||
| lock (_lock) { | ||
| _events.Add(recordedEvent); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Copy of the events recorded so far, safe to take while emulation runs. | ||
| /// </summary> | ||
| public IReadOnlyList<RecordedInputEvent> Snapshot() { | ||
| lock (_lock) { | ||
| return _events.ToList(); | ||
| } | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/Spice86.Core/Emulator/VM/Input/Recording/InputRecordingReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace Spice86.Core.Emulator.VM.Input.Recording; | ||
|
|
||
| using Spice86.Shared.Emulator.Errors; | ||
|
|
||
| using System.IO; | ||
| using System.Text.Json; | ||
|
|
||
| internal static class InputRecordingReader { | ||
| internal static IReadOnlyList<RecordedInputEvent> Read(string path) { | ||
| List<RecordedInputEvent> events = []; | ||
| int lineNumber = 0; | ||
| foreach (string line in File.ReadLines(path)) { | ||
| lineNumber++; | ||
| if (!string.IsNullOrWhiteSpace(line)) { | ||
| events.Add(Parse(path, lineNumber, line)); | ||
| } | ||
| } | ||
| return events; | ||
| } | ||
|
|
||
| private static RecordedInputEvent Parse(string path, int lineNumber, string line) { | ||
| try { | ||
| RecordedInputEvent? recorded = JsonSerializer.Deserialize<RecordedInputEvent>(line, InputEventSerialization.Options); | ||
| if (recorded?.Event is null) { | ||
| throw new JsonException("The line holds no event."); | ||
| } | ||
| return recorded; | ||
| } catch (JsonException exception) { | ||
| throw new UnrecoverableException($"Invalid input recording {path} at line {lineNumber}: {exception.Message}", exception); | ||
| } | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Spice86.Core/Emulator/VM/Input/Recording/InputRecordingWriter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| namespace Spice86.Core.Emulator.VM.Input.Recording; | ||
|
|
||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
|
|
||
| /// <summary> | ||
| /// Writes recorded host input events as JSON Lines, the counterpart of <see cref="InputRecordingReader"/>. | ||
| /// </summary> | ||
| internal static class InputRecordingWriter { | ||
| internal static void Write(string path, IReadOnlyList<RecordedInputEvent> events) { | ||
| File.WriteAllLines(path, events.Select(e => JsonSerializer.Serialize(e, InputEventSerialization.Options))); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/Spice86.Core/Emulator/VM/Input/Recording/InputReplay.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| namespace Spice86.Core.Emulator.VM.Input.Recording; | ||
|
|
||
| /// <summary> | ||
| /// Holds the ordered stream of recorded input events being replayed. | ||
| /// </summary> | ||
| public sealed class InputReplay { | ||
| private readonly IReadOnlyList<RecordedInputEvent> _events; | ||
| private int _index; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="InputReplay"/> class. | ||
| /// </summary> | ||
| public InputReplay(IReadOnlyList<RecordedInputEvent> events) { | ||
| _events = events; | ||
| } | ||
|
|
||
| /// <summary>Gets a value indicating whether recorded events remain to be replayed.</summary> | ||
| public bool IsReplaying => _index < _events.Count; | ||
|
|
||
| /// <summary>Gets the next recorded event to be applied.</summary> | ||
| public RecordedInputEvent Next => _events[_index]; | ||
|
|
||
| /// <summary>Consumes and returns the next recorded event.</summary> | ||
| public RecordedInputEvent TakeNext() => _events[_index++]; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.