-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.cs
More file actions
102 lines (89 loc) · 3.22 KB
/
Repository.cs
File metadata and controls
102 lines (89 loc) · 3.22 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using com.greasyeggplant.chronicle.data.csvData;
using CsvHelper;
using CsvHelper.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace com.greasyeggplant.chronicle.data.entities
{
public class Repository : Localizer
{
public static readonly string GameFolder = "Jagex/Chronicle";
public static readonly string DataFolder = "Game/Chronicle_Data/Config/CSV";
public static readonly string CardsFileName = "cards.csv";
public static readonly string LocalizationFileName = "loc.csv";
private static readonly CsvConfiguration ReaderConfiguration = new CsvConfiguration()
{
Delimiter = "\t",
IsHeaderCaseSensitive = false,
WillThrowOnMissingField = false
};
//TODO: Prevent access to data if not intialized
public bool IsInitialized { get; private set; }
public List<Card> Cards { get; private set; }
private CardFactory CardFactory { get; set; }
private List<CsvCard> CardData { get; set; }
private List<CsvDescription> Localizations { get; set; }
public Repository()
{
CardFactory = new CardFactory();
CardFactory.Localizer = this;
IsInitialized = false;
}
public void Initialize()
{
//TODO: Error Handling
ReadCsvs();
MapData();
IsInitialized = true;
}
private void ReadCsvs()
{
CardData = GetList<CsvCard>(CardsFileName);
Localizations = GetList<CsvDescription>(LocalizationFileName);
}
private void MapData()
{
Cards = new List<Card>();
foreach(CsvCard csvCard in CardData)
{
Card c = CardFactory.CreateCard(csvCard);
if (c != null)
{
Cards.Add(c);
}
}
}
public LocalizedString GetLocalization(int descId)
{
CsvDescription localization = Localizations.FirstOrDefault(l => l.Id == descId);
if(localization == null)
{
//TODO: Raise Warning
return null;
}
return new LocalizedString
{
Id = descId,
Text = new Dictionary<Language, string>
{
{ Language.English, localization.En },
{ Language.French, localization.Fr },
{ Language.German, localization.De },
{ Language.Spanish, localization.Es },
}
};
}
private List<T> GetList<T>(string filename)
{
string filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), GameFolder, DataFolder, filename);
using (FileStream fileStream = File.OpenRead(filepath))
using (StreamReader streamReader = new StreamReader(fileStream))
using (CsvReader reader = new CsvReader(streamReader, ReaderConfiguration))
{
return reader.GetRecords<T>().ToList();
}
}
}
}