-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
48 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,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace IntelOrca.Biohazard.Room | ||
{ | ||
public readonly struct CvScript | ||
{ | ||
public ReadOnlyMemory<byte> Data { get; } | ||
|
||
public CvScript(ReadOnlyMemory<byte> data) | ||
{ | ||
Data = data; | ||
} | ||
|
||
public int Count | ||
{ | ||
get | ||
{ | ||
if (Data.Length < 4) | ||
return 0; | ||
|
||
return GetOffset(0) / 4; | ||
} | ||
} | ||
|
||
public CvScriptRoutine this[int index] | ||
{ | ||
get | ||
{ | ||
var count = Count; | ||
if (index < 0 || index >= count) | ||
throw new ArgumentOutOfRangeException(nameof(index)); | ||
|
||
var offset = GetOffset(index); | ||
var length = index < count - 1 ? GetOffset(index + 1) - offset : Data.Length - offset; | ||
return new CvScriptRoutine(Data.Slice(offset, length)); | ||
} | ||
} | ||
|
||
public int GetOffset(int index) | ||
{ | ||
var offsets = MemoryMarshal.Cast<byte, int>(Data.Span); | ||
return offsets[index]; | ||
} | ||
|
||
public Builder ToBuilder() | ||
{ | ||
var builder = new Builder(); | ||
for (var i = 0; i < Count; i++) | ||
builder.Routines.Add(this[i]); | ||
return builder; | ||
} | ||
|
||
public class Builder | ||
{ | ||
public List<CvScriptRoutine> Routines { get; } = new List<CvScriptRoutine>(); | ||
|
||
public CvScript ToScript() | ||
{ | ||
var ms = new MemoryStream(); | ||
var bw = new BinaryWriter(ms); | ||
|
||
var offset = Routines.Count * 4; | ||
for (var i = 0; i < Routines.Count; i++) | ||
{ | ||
bw.Write(offset); | ||
offset += Routines[i].Data.Length; | ||
} | ||
|
||
foreach (var routine in Routines) | ||
{ | ||
bw.Write(routine.Data); | ||
} | ||
|
||
var bytes = ms.ToArray(); | ||
return new CvScript(bytes); | ||
} | ||
} | ||
} | ||
|
||
public readonly struct CvScriptRoutine | ||
{ | ||
public ReadOnlyMemory<byte> Data { get; } | ||
|
||
public CvScriptRoutine(ReadOnlyMemory<byte> data) | ||
{ | ||
Data = data; | ||
} | ||
} | ||
} |
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
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,42 @@ | ||
using System; | ||
using System.IO; | ||
using IntelOrca.Biohazard.Extensions; | ||
using Xunit; | ||
|
||
namespace IntelOrca.Biohazard.Tests | ||
{ | ||
internal static class MemoryAssert | ||
{ | ||
public static void AssertAndCompareMemory(ReadOnlyMemory<byte> expected, ReadOnlyMemory<byte> actual) | ||
{ | ||
try | ||
{ | ||
AssertMemory(expected, actual); | ||
} | ||
catch | ||
{ | ||
var path = @"M:\temp\rdt"; | ||
Directory.CreateDirectory(path); | ||
expected.WriteToFile(Path.Combine(path, "expected.dat")); | ||
actual.WriteToFile(Path.Combine(path, "actual.dat")); | ||
throw; | ||
} | ||
} | ||
|
||
public static void AssertMemory(ReadOnlyMemory<byte> expected, ReadOnlyMemory<byte> actual) | ||
{ | ||
var spanExpected = expected.Span; | ||
var spanActual = actual.Span; | ||
|
||
var length = spanExpected.Length; | ||
Assert.Equal(length, spanActual.Length); | ||
for (var i = 0; i < length; i++) | ||
{ | ||
if (spanExpected[i] != spanActual[i]) | ||
{ | ||
Assert.False(true, $"Memory did not match at index {i}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
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