Skip to content

Commit

Permalink
Merge pull request #117 from ethanmoffat/localization_dataencoder
Browse files Browse the repository at this point in the history
Improve code around EDF files. EDF files are now immutable and split from EDFLoaderService. EDFLoaderService supports saving as well as loading files.
  • Loading branch information
ethanmoffat committed Mar 13, 2022
2 parents 8783687 + 2b66e6f commit d50bbb9
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 322 deletions.
106 changes: 106 additions & 0 deletions EOLib.IO/Services/DataEncoderService.cs
@@ -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
@@ -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.

0 comments on commit d50bbb9

Please sign in to comment.