-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Input listener and replay session (#38)
- Loading branch information
1 parent
e150116
commit 516ceb6
Showing
28 changed files
with
452 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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,20 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="SpaceWar (P1) (save)" type="DotNetProject" factoryName=".NET Project"> | ||
<option name="EXE_PATH" value="$PROJECT_DIR$/samples/SpaceWar/bin/Debug/net8.0/SpaceWar.exe" /> | ||
<option name="PROGRAM_PARAMETERS" value="9000 2 --save-to replay.inputs local 127.0.0.1:9001 " /> | ||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/samples/SpaceWar/bin/Debug/net8.0" /> | ||
<option name="PASS_PARENT_ENVS" value="1" /> | ||
<option name="USE_EXTERNAL_CONSOLE" value="0" /> | ||
<option name="USE_MONO" value="0" /> | ||
<option name="RUNTIME_ARGUMENTS" value="" /> | ||
<option name="PROJECT_PATH" value="$PROJECT_DIR$/samples/SpaceWar/SpaceWar.csproj" /> | ||
<option name="PROJECT_EXE_PATH_TRACKING" value="1" /> | ||
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" /> | ||
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" /> | ||
<option name="PROJECT_KIND" value="DotNetCore" /> | ||
<option name="PROJECT_TFM" value="net8.0" /> | ||
<method v="2"> | ||
<option name="Build" /> | ||
</method> | ||
</configuration> | ||
</component> |
This file contains 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,20 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="SpaceWar (replay)" type="DotNetProject" factoryName=".NET Project"> | ||
<option name="EXE_PATH" value="$PROJECT_DIR$/samples/SpaceWar/bin/Debug/net8.0/SpaceWar.exe" /> | ||
<option name="PROGRAM_PARAMETERS" value="9000 2 replay replay.inputs" /> | ||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/samples/SpaceWar/bin/Debug/net8.0" /> | ||
<option name="PASS_PARENT_ENVS" value="1" /> | ||
<option name="USE_EXTERNAL_CONSOLE" value="0" /> | ||
<option name="USE_MONO" value="0" /> | ||
<option name="RUNTIME_ARGUMENTS" value="" /> | ||
<option name="PROJECT_PATH" value="$PROJECT_DIR$/samples/SpaceWar/SpaceWar.csproj" /> | ||
<option name="PROJECT_EXE_PATH_TRACKING" value="1" /> | ||
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" /> | ||
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" /> | ||
<option name="PROJECT_KIND" value="DotNetCore" /> | ||
<option name="PROJECT_TFM" value="net8.0" /> | ||
<method v="2"> | ||
<option name="Build" /> | ||
</method> | ||
</configuration> | ||
</component> |
This file contains 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 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,51 @@ | ||
using Backdash.Data; | ||
using Backdash.Sync.Input.Confirmed; | ||
using SpaceWar.Logic; | ||
|
||
namespace SpaceWar; | ||
|
||
sealed class SaveInputsToFileListener(string filename) : IInputListener<PlayerInputs> | ||
{ | ||
const int InputSize = sizeof(PlayerInputs); | ||
readonly FileStream fileStream = File.Create(filename); | ||
readonly byte[] inputBuffer = new byte[InputSize]; | ||
|
||
public void OnConfirmed(in Frame frame, in ConfirmedInputs<PlayerInputs> inputs) | ||
{ | ||
for (var i = 0; i < inputs.Count; i++) | ||
{ | ||
var input = (ushort)inputs.Inputs[i]; | ||
Array.Clear(inputBuffer); | ||
if (!input.TryFormat(inputBuffer, out _)) | ||
throw new InvalidOperationException("unable to save input"); | ||
|
||
fileStream.Write(inputBuffer); | ||
} | ||
|
||
fileStream.Write("\n"u8); | ||
} | ||
|
||
public void Dispose() => fileStream.Dispose(); | ||
|
||
public static IEnumerable<ConfirmedInputs<PlayerInputs>> GetInputs(int players, string file) | ||
{ | ||
using var replayStream = File.OpenRead(file); | ||
var buffer = new byte[InputSize * players]; | ||
var inputsBuffer = new PlayerInputs[players]; | ||
var lineBreak = new byte[1]; | ||
|
||
while (replayStream.Read(buffer) > 0) | ||
{ | ||
for (var i = 0; i < players; i++) | ||
{ | ||
if (ushort.TryParse(buffer.AsSpan().Slice(i * InputSize, InputSize), out var value)) | ||
inputsBuffer[i] = (PlayerInputs)value; | ||
} | ||
|
||
yield return new ConfirmedInputs<PlayerInputs>(inputsBuffer.AsSpan()[..players]); | ||
|
||
if (replayStream.Read(lineBreak) is 0 || lineBreak[0] != '\n') | ||
throw new InvalidOperationException("invalid replay file content"); | ||
} | ||
} | ||
} |
This file contains 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
Binary file not shown.
This file contains 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,7 @@ | ||
#!/bin/bash | ||
dotnet build -c Release "$(dirname "$0")/../.." | ||
pushd "$(dirname "$0")/../../bin/Release/net8.0" || exit | ||
rm ./*.log | ||
dotnet SpaceWar.dll 9000 2 --save-to replay.inputs local 127.0.0.1:9001 & | ||
dotnet SpaceWar.dll 9001 2 127.0.0.1:9000 local & | ||
popd || exit |
This file contains 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,6 @@ | ||
#!/bin/bash | ||
dotnet build -c Release "$(dirname "$0")/../.." | ||
pushd "$(dirname "$0")/../../bin/Release/net8.0" || exit | ||
rm ./*.log | ||
dotnet SpaceWar.dll 9000 2 replay replay.inputs & | ||
popd || exit |
This file contains 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,6 @@ | ||
dotnet build -c Release %~dp0\..\.. | ||
pushd %~dp0\..\..\bin\Release\net8.0 | ||
del *.log | ||
start SpaceWar 9000 2 --save-to replay.inputs local 127.0.0.1:9001 | ||
start SpaceWar 9001 2 127.0.0.1:9000 local | ||
popd |
This file contains 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,5 @@ | ||
dotnet build -c Release %~dp0\..\.. | ||
pushd %~dp0\..\..\bin\Release\net8.0 | ||
del *.log | ||
start SpaceWar 9000 2 replay replay.inputs | ||
popd |
This file contains 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 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
Oops, something went wrong.