-
Notifications
You must be signed in to change notification settings - Fork 16
/
DataFileLoadActions.cs
53 lines (45 loc) · 1.8 KB
/
DataFileLoadActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using AutomaticTypeMapper;
using System.IO;
using System.Linq;
namespace EOLib.Localization
{
[MappedType(BaseType = typeof(IDataFileLoadActions))]
public class DataFileLoadActions : IDataFileLoadActions
{
private readonly IDataFileRepository _dataFileRepository;
private readonly IEDFLoaderService _edfLoaderService;
public DataFileLoadActions(IDataFileRepository dataFileRepository,
IEDFLoaderService edfLoaderService)
{
_dataFileRepository = dataFileRepository;
_edfLoaderService = edfLoaderService;
}
public void LoadDataFiles()
{
if (!Directory.Exists(DataFileConstants.DataFilePath))
throw new DataFileLoadException();
var files = Directory.GetFiles(DataFileConstants.DataFilePath, "*.edf")
.OrderBy(x => x)
.ToArray();
if (files.Length != DataFileConstants.ExpectedNumberOfDataFiles)
throw new DataFileLoadException();
_dataFileRepository.DataFiles.Clear();
for (int i = 1; i <= DataFileConstants.ExpectedNumberOfDataFiles; ++i)
{
if (!DataFileNameIsValid(i, files[i - 1]))
throw new DataFileLoadException();
var fileToLoad = (DataFiles)i;
_dataFileRepository.DataFiles[fileToLoad] = _edfLoaderService.LoadFile(files[i - 1], fileToLoad);
}
}
private bool DataFileNameIsValid(int fileNumber, string fileName)
{
var expectedFormat = $"data/dat0{fileNumber:00}.edf";
return expectedFormat == fileName;
}
}
public interface IDataFileLoadActions
{
void LoadDataFiles();
}
}