Skip to content

Commit d429c73

Browse files
committed
Read POK from stream
1 parent cda9a1d commit d429c73

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/Spectron.Files/Pok/PokeFile.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,31 @@ private PokeFile()
3838
/// <returns>A <see cref="PokeFile"/> instance containing the trainers and pokes parsed from the file.</returns>
3939
public static PokeFile Load(string fileName)
4040
{
41-
var lines = File.ReadAllLines(fileName);
41+
using FileStream stream = new(fileName, FileMode.Open, FileAccess.Read);
42+
43+
return Load(stream);
44+
}
45+
46+
/// <summary>
47+
/// Loads a POKE file from the provided stream and parses its contents into a PokeFile object.
48+
/// </summary>
49+
/// <param name="stream">The stream containing the POKE file data to be loaded.</param>
50+
/// <returns>A <see cref="PokeFile"/> instance containing the trainers and pokes parsed from the stream.</returns>
51+
public static PokeFile Load(Stream stream)
52+
{
53+
using StreamReader reader = new(stream);
4254

4355
var pokeFile = new PokeFile();
4456
Trainer? trainer = null;
4557

46-
foreach (var line in lines)
58+
while (reader.ReadLine() is { } line)
4759
{
4860
if (IsNextTrainer(line))
4961
{
5062
trainer = new Trainer(line[1..], []);
5163
}
5264

53-
if (line.StartsWith('M'))
65+
if (IsPoke(line))
5466
{
5567
var poke = ParsePoke(line);
5668

@@ -60,7 +72,7 @@ public static PokeFile Load(string fileName)
6072
}
6173
}
6274

63-
if (line.StartsWith('Z'))
75+
if (IsLastPoke(line))
6476
{
6577
var poke = ParsePoke(line);
6678

@@ -117,4 +129,8 @@ public static PokeFile Load(string fileName)
117129
private static bool IsNextTrainer(string line) => line.StartsWith('N');
118130

119131
private static bool IsLastLine(string line) => line.StartsWith('Y');
132+
133+
private static bool IsPoke(string line) => line.StartsWith('M');
134+
135+
private static bool IsLastPoke(string line) => line.StartsWith('Z');
120136
}

0 commit comments

Comments
 (0)