Skip to content
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

Improve code around EDF files. #117

Merged
merged 7 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
106 changes: 106 additions & 0 deletions EOLib.IO/Services/DataEncoderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using AutomaticTypeMapper;
using System.Collections.Generic;
using System.Linq;

namespace EOLib.IO.Services
{
[AutoMappedType]
public class DataEncoderService : IDataEncoderService
{
public List<byte> Interleave(IReadOnlyList<byte> data)
{
var numArray = new byte[data.Count];
var index1 = 0;
var num = 0;

while (index1 < data.Count)
{
numArray[index1] = data[num++];
index1 += 2;
}

var index2 = index1 - 1;
if (data.Count % 2 != 0)
index2 -= 2;

while (index2 >= 0)
{
numArray[index2] = data[num++];
index2 -= 2;
}

return numArray.ToList();
}

public List<byte> Deinterleave(IReadOnlyList<byte> data)
{
var numArray = new byte[data.Count];
var index1 = 0;
var num = 0;

while (index1 < data.Count)
{
numArray[num++] = data[index1];
index1 += 2;
}

var index2 = index1 - 1;
if (data.Count % 2 != 0)
index2 -= 2;

while (index2 >= 0)
{
numArray[num++] = data[index2];
index2 -= 2;
}

return numArray.ToList();
}

public List<byte> FlipMSB(IReadOnlyList<byte> data)
{
return data.Select(x => (byte)(x ^ 0x80u)).ToList();
}

public List<byte> SwapMultiples(IReadOnlyList<byte> data, int multi)
{
int num1 = 0;

var result = data.ToArray();

for (int index1 = 0; index1 <= data.Count; ++index1)
{
if (index1 != data.Count && data[index1] % multi == 0)
{
++num1;
}
else
{
if (num1 > 1)
{
for (int index2 = 0; index2 < num1 / 2; ++index2)
{
byte num2 = data[index1 - num1 + index2];
result[index1 - num1 + index2] = data[index1 - index2 - 1];
result[index1 - index2 - 1] = num2;
}
}
num1 = 0;
}
}

return result.ToList();
}
}

public interface IDataEncoderService
{
List<byte> Interleave(IReadOnlyList<byte> data);

List<byte> Deinterleave(IReadOnlyList<byte> data);

List<byte> FlipMSB(IReadOnlyList<byte> data);

List<byte> SwapMultiples(IReadOnlyList<byte> data, int multi);
}
}
7 changes: 5 additions & 2 deletions EOLib.Localization.Test/DataFileLoadActionsTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using EOLib.IO.Services;
using NUnit.Framework;

namespace EOLib.Localization.Test
Expand All @@ -9,12 +10,14 @@ public class DataFileLoadActionsTest
{
private IDataFileLoadActions _actions;
private IDataFileRepository _dataFileRepository;
private IEDFLoaderService _edfLoaderService;

[SetUp]
public void SetUp()
{
_dataFileRepository = new DataFileRepository();
_actions = new DataFileLoadActions(_dataFileRepository);
_edfLoaderService = new EDFLoaderService(new DataEncoderService());
_actions = new DataFileLoadActions(_dataFileRepository, _edfLoaderService);
}

[TearDown]
Expand Down Expand Up @@ -53,7 +56,7 @@ public void WhenLoadDataFiles_RepositoryHasExpectedNumberOfFiles()
{
CreateRequiredDirectory();
GivenEDFFilesInRequiredDirectory();
_dataFileRepository.DataFiles.Add(DataFiles.Credits, new EDFFile("data/dat001.edf", DataFiles.Credits));
_dataFileRepository.DataFiles.Add(DataFiles.Credits, new EDFFile(DataFiles.Credits));

_actions.LoadDataFiles();

Expand Down
140 changes: 0 additions & 140 deletions EOLib.Localization.Test/EDFFileTest.cs

This file was deleted.