Skip to content

Commit e761108

Browse files
committed
Still more changes, I'll do a big refactor (one more, yay)
1 parent f643095 commit e761108

File tree

12 files changed

+111
-142
lines changed

12 files changed

+111
-142
lines changed

TODO.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
## DAT check
44

5-
* Create a Zip infrastructure layer and a corresponding model to abstract away the ZipArchive (it will also allow to pass only this model with a path property and not both the zip and path as two parameters targetZip/targetZipPath)
5+
* Fix/rebuild:
6+
* if no error: copy
7+
* get the list of required files
8+
* search the romset and other if they exist
9+
* if any required file is missing skip the game
10+
* rebuild the zip
611
* Have a progress bar for the steps and a progress bar for the items in the step
712
* Move the localizer service to the web app
813

src/ArcadeManager.Core.Tests/Infrastructure/FileSystemTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public async Task Zip_files_are_replaced()
7979
var targetZip = Path.Combine(filesPath, "test1-copy.zip");
8080

8181
// arrange: file data
82-
var sourceFile = new GameRomFile("test2.zip", filesPath) {
82+
var sourceFile = new GameRomFile {
8383
Name = "test1.txt",
8484
Crc = "8ab2dce2"
8585
};
@@ -123,8 +123,8 @@ public void Zip_files_are_deleted()
123123

124124
// arrange: files list
125125
GameRomFilesList files = [
126-
new("", "") { Name = "test1.txt" },
127-
new("", "") { Name = "test3.txt", Path = "testfolder" }
126+
new() { Name = "test1.txt" },
127+
new() { Name = "test3.txt", Path = "testfolder" }
128128
];
129129

130130
// arrange: read zip
@@ -134,7 +134,7 @@ public void Zip_files_are_deleted()
134134
sut.DeleteZipFile(zip, file);
135135
}
136136

137-
sut.DeleteZipFile(zip, new GameRomFile("", "") { Name = "testfolder/" });
137+
sut.DeleteZipFile(zip, new GameRomFile { Name = "testfolder/" });
138138
}
139139

140140
// assert: re-read zip and check remaining files

src/ArcadeManager.Core.Tests/Models/GameRomsTests.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public void GameRom_is_created_from_xml()
3636
file.Size.Should().Be(524288);
3737
file.Crc.Should().Be("ac44415b");
3838
file.Sha1.Should().Be("218f8b1886eb72b8547127042b5ae47600e18944");
39-
file.ZipFileName.Should().Be("test1.zip");
40-
file.ZipFileFolder.Should().Be("romset");
4139
}
4240

4341
[Fact]
@@ -107,10 +105,10 @@ public void GameRomFile_is_removed()
107105
{
108106
// arrange
109107
var data = new GameRomFilesList {
110-
new("", "") { Name = "a" },
111-
new("", "") { Name = "b" },
112-
new("", "") { Name = "a", Path = "sub" },
113-
new("", "") { Name = "b", Path = "sub" }
108+
new() { Name = "a" },
109+
new() { Name = "b" },
110+
new() { Name = "a", Path = "sub" },
111+
new() { Name = "b", Path = "sub" }
114112
};
115113

116114
// act

src/ArcadeManager.Core.Tests/Services/DatCheckerTests.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Rom_is_checked()
3636
var game = new GameRom {
3737
Name = "test"
3838
};
39-
game.RomFiles.Add(new GameRomFile("", "") {
39+
game.RomFiles.Add(new GameRomFile() {
4040
Name = "test.1",
4141
Crc = "abcd",
4242
Size = 1234
@@ -48,7 +48,7 @@ public void Rom_is_checked()
4848
var processed = new GameRomList();
4949

5050
var zipFiles = new GameRomFilesList {
51-
new GameRomFile("", "") {
51+
new GameRomFile() {
5252
Name = "test.1",
5353
Crc = "abcd",
5454
Size = 1234
@@ -104,12 +104,12 @@ public void Rom_missing_romfile_is_error()
104104
Name = "test"
105105
};
106106
game.RomFiles.AddRange([
107-
new GameRomFile("", "") {
107+
new GameRomFile() {
108108
Name = "test.1",
109109
Crc = "abcd",
110110
Size = 1234
111111
},
112-
new GameRomFile("", "") {
112+
new GameRomFile() {
113113
Name = "test.2",
114114
Crc = "def",
115115
Size = 456
@@ -122,7 +122,7 @@ public void Rom_missing_romfile_is_error()
122122
var processed = new GameRomList();
123123

124124
var zipFiles = new GameRomFilesList {
125-
new GameRomFile("", "") {
125+
new GameRomFile() {
126126
Name = "test.1",
127127
Crc = "abcd",
128128
Size = 1234
@@ -155,7 +155,7 @@ public void Rom_bad_crc_is_error()
155155
Name = "test"
156156
};
157157
game.RomFiles.AddRange([
158-
new GameRomFile("", "") {
158+
new GameRomFile() {
159159
Name = "test.1",
160160
Crc = "abcd",
161161
Size = 1234
@@ -168,7 +168,7 @@ public void Rom_bad_crc_is_error()
168168
var processed = new GameRomList();
169169

170170
var zipFiles = new GameRomFilesList {
171-
new GameRomFile("", "") {
171+
new GameRomFile() {
172172
Name = "test.1",
173173
Crc = "def",
174174
Size = 1234
@@ -198,30 +198,30 @@ public async Task Rom_is_fixed()
198198
var game = new GameRom {
199199
Name = "test"
200200
};
201-
game.RomFiles.Add(new GameRomFile("", "") {
201+
game.RomFiles.Add(new GameRomFile() {
202202
Name = "test.1",
203203
Crc = "abcd",
204204
Size = 1234,
205205
ErrorReason = ErrorReason.MissingFile
206206
});
207207
GameRomList processed = [game];
208208

209-
var fixFolder = new ReadOnlyGameRomFileList(new List<ReadOnlyGameRomFile>() {
210-
new ReadOnlyGameRomFile("test.zip", "roms", "test.1", "", 1234, "abcd", "")
211-
});
209+
var fixFolder = new ReadOnlyGameRomFileList([
210+
new("test.zip", "roms", "test.1", "", 1234, "abcd", "")
211+
]);
212212

213213
// arrange: services
214214
A.CallTo(() => fs.PathJoin("roms", "test.zip")).Returns("roms/test.zip");
215215
A.CallTo(() => fs.PathJoin("fix", "test.zip")).Returns("fix/test.zip");
216216
A.CallTo(() => fs.FileExists("roms/test.zip")).Returns(true);
217217
A.CallTo(() => fs.FileExists("fix/test.zip")).Returns(true);
218-
A.CallTo(() => fs.ReplaceZipFile(A<ZipFile>._, A<ZipFile>._, A<GameRomFile>._)).Returns(true);
218+
A.CallTo(() => fs.ReplaceZipFile(A<ZipFile>._, A<ZipFile>._, A<IGameRomFile>._)).Returns(true);
219219

220220
// act (the game.RomFiles is re-cast so the list is cloned)
221-
await sut.FixGame(null, game, [..game.RomFiles], processed, fixFolder, messageHandler);
221+
await sut.FixGame(null, game, [..game.RomFiles], processed, fixFolder, new(), messageHandler);
222222

223223
// assert
224-
A.CallTo(() => fs.ReplaceZipFile(A<ZipFile>._, A<ZipFile>._, A<GameRomFile>._)).MustHaveHappened();
224+
A.CallTo(() => fs.ReplaceZipFile(A<ZipFile>._, A<ZipFile>._, A<IGameRomFile>._)).MustHaveHappened();
225225
game.RomFiles[0].ErrorReason.Should().Be(ErrorReason.None);
226226
}
227227

@@ -232,14 +232,14 @@ public void Cleanup_removes_excess_files()
232232
var game = new GameRom {
233233
Name = "test"
234234
};
235-
game.RomFiles.Add(new("", "") { Name = "a" });
236-
game.RomFiles.Add(new("", "") { Name = "b" });
235+
game.RomFiles.Add(new() { Name = "a" });
236+
game.RomFiles.Add(new() { Name = "b" });
237237

238238
// arrange: zip files
239239
var zipFiles = new GameRomFilesList {
240-
new("", "") { Name = "a" },
241-
new("", "") { Name = "b" },
242-
new("", "") { Name = "c" }
240+
new() { Name = "a" },
241+
new() { Name = "b" },
242+
new() { Name = "c" }
243243
};
244244

245245
// act

src/ArcadeManager.Core/Infrastructure/FileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public IEnumerable<GameRomFile> GetZipFiles(ZipFile zip, string fileName, string
428428
// loop on entry, skipping folders (which are entries with no short name)
429429
foreach (var entry in zip.Entries.Where(e => !string.IsNullOrEmpty(e.Name)))
430430
{
431-
result.Add(new GameRomFile(fileName, folder)
431+
result.Add(new GameRomFile
432432
{
433433
Name = entry.Name,
434434
Path = Path.GetDirectoryName(entry.FullName),

src/ArcadeManager.Core/Models/Roms/GameRom.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public bool HasError
8585
/// Gets the list of rom files
8686
/// </summary>
8787
public GameRomFilesList RomFiles { get; } = [];
88+
89+
/// <summary>
90+
/// Gets a value indicating whether the game itself has an error (ex the whole file is missing)
91+
/// </summary>
92+
public bool HasOwnError => _ownError != ErrorReason.None;
8893

8994
/// <summary>
9095
/// Sets an error on the game
@@ -155,7 +160,7 @@ public static GameRom FromXml(XElement gameXml, string folder)
155160
// add the files
156161
foreach (var romXml in gameXml.Descendants("rom").Where(r => r.Attribute("status")?.Value != "nodump"))
157162
{
158-
game.RomFiles.Add(GameRomFile.FromXml(game.Name, romXml, folder));
163+
game.RomFiles.Add(GameRomFile.FromXml(game, romXml));
159164
}
160165

161166
return game;
Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
using System.Diagnostics;
2+
using System.Text.Json.Serialization;
23
using System.Xml.Linq;
34

45
namespace ArcadeManager.Core.Models.Roms;
56

67
/// <summary>
78
/// A game rom file (rom files inside the zip)
89
/// </summary>
9-
[DebuggerDisplay("{Name} ({Crc})")]
10-
public class GameRomFile(string zipFileName, string zipFileFolder) : IGameRomFile
10+
[DebuggerDisplay("{Path} \\ {Name} ({Crc})")]
11+
public class GameRomFile : IGameRomFile
1112
{
12-
/// <summary>
13-
/// Gets or sets the zip file name
14-
/// </summary>
15-
public string ZipFileName => zipFileName;
16-
17-
/// <summary>
18-
/// Gets or sets the zip file folder
19-
/// </summary>
20-
public string ZipFileFolder => zipFileFolder;
21-
22-
/// <summary>
23-
/// Gets the full zip file path
24-
/// </summary>
25-
public string ZipFilePath => System.IO.Path.Join(ZipFileFolder, ZipFileName);
13+
[JsonIgnore]
14+
public GameRom Rom { get; set; }
2615

2716
/// <summary>
2817
/// Gets or sets the file name (including extension)
@@ -64,49 +53,37 @@ public class GameRomFile(string zipFileName, string zipFileFolder) : IGameRomFil
6453
/// </summary>
6554
public string ErrorDetails { get; set; }
6655

67-
/// <summary>
68-
/// Clones a rom file object by replacing some data for the specified game
69-
/// </summary>
70-
/// <param name="zipFilePath">The path to the zip file we're processing</param>
71-
/// <returns>The cloned GameRomFile</returns>
72-
public GameRomFile CloneFor(string zipFilePath)
73-
{
74-
return new GameRomFile(System.IO.Path.GetFileName(zipFilePath), System.IO.Path.GetDirectoryName(zipFilePath))
75-
{
76-
Name = Name,
77-
Size = Size,
78-
Crc = Crc,
79-
Sha1 = Sha1,
80-
Path = Path,
81-
ErrorDetails = ErrorDetails,
82-
ErrorReason = ErrorReason
83-
};
56+
public ReadOnlyGameRomFile ToReadOnly(string filePath) {
57+
var fileName = System.IO.Path.GetFileName(filePath);
58+
var folderName = System.IO.Path.GetDirectoryName(filePath);
59+
60+
return new ReadOnlyGameRomFile(fileName, folderName, Name, Path, Size, Crc, Sha1);
8461
}
8562

8663
/// <summary>
8764
/// Creates a new rom file info from a XML node from the DAT
8865
/// </summary>
89-
/// <param name="game">The game name</param>
66+
/// <param name="game">The game</param>
9067
/// <param name="romXml">The XML node from the DAT</param>
91-
/// <param name="folder">The rom file folder</param>
9268
/// <returns>The file infos</returns>
93-
public static GameRomFile FromXml(string game, XElement romXml, string folder)
69+
public static GameRomFile FromXml(GameRom game, XElement romXml)
9470
{
9571
var name = romXml.Attribute("name").Value;
9672

9773
var crc = romXml.Attribute("crc")?.Value;
9874
var status = romXml.Attribute("status")?.Value;
9975
if (crc == null && status != "nodump")
10076
{
101-
throw new ArgumentNullException($"No crc for file {name} in game {game}");
77+
throw new ArgumentNullException($"No crc for file {name} in game {game.Name}");
10278
}
10379

104-
return new GameRomFile($"{game}.zip", folder)
80+
return new GameRomFile()
10581
{
10682
Name = name,
107-
Size = long.Parse(romXml.Attribute("size")?.Value ?? throw new ArgumentNullException($"No size for file {name} in game {game}")),
83+
Size = long.Parse(romXml.Attribute("size")?.Value ?? throw new ArgumentNullException($"No size for file {name} in game {game.Name}")),
10884
Crc = crc,
10985
Sha1 = romXml.Attribute("sha1")?.Value,
86+
Rom = game
11087
};
11188
}
11289
}

src/ArcadeManager.Core/Models/Roms/GameRomFilesList.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
namespace ArcadeManager.Core.Models.Roms;
23

34
/// <summary>
@@ -34,13 +35,15 @@ public void RemoveFile(string fileName, string filePath = null)
3435
}
3536

3637
/// <summary>
37-
/// Replaces a file in this collection with the specified one
38+
/// Marks a rom file as fixed
3839
/// </summary>
39-
/// <param name="file">The file to replace</param>
40-
/// <param name="zipFilePath">The path to the zip file we're processing</param>
41-
public void ReplaceFile(GameRomFile file, string zipFilePath)
40+
/// <param name="romFileName">The name of the rom file to mark as fixed</param>
41+
public void Fixed(string romFileName)
4242
{
43-
RemoveFile(file.Name, file.Path);
44-
Add(file.CloneFor(zipFilePath));
43+
var file = this[romFileName];
44+
if (file != null) {
45+
file.ErrorDetails = null;
46+
file.ErrorReason = ErrorReason.None;
47+
}
4548
}
4649
}

src/ArcadeManager.Core/Models/Roms/ReadOnlyGameRomFile.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ArcadeManager.Core.Models.Roms;
55
/// <summary>
66
/// A game rom file (rom files inside the zip)
77
/// </summary>
8-
[DebuggerDisplay("{Name} ({Crc})")]
8+
[DebuggerDisplay("{Path}\\{Name} ({Crc})")]
99
public class ReadOnlyGameRomFile(
1010
string zipFileName,
1111
string zipFileFolder,
@@ -31,16 +31,8 @@ public class ReadOnlyGameRomFile(
3131

3232
public string Sha1 => sha1;
3333

34-
public GameRomFile ToGameRomFile(string zipPath) {
35-
var fileName = zipFileName;
36-
var fileFolder = zipFileFolder;
37-
38-
if (!string.IsNullOrEmpty(zipPath)) {
39-
fileName = System.IO.Path.GetFileName(zipPath);
40-
fileFolder = System.IO.Path.GetDirectoryName(zipPath);
41-
}
42-
43-
return new GameRomFile(fileName, fileFolder) {
34+
public GameRomFile ToGameRomFile() {
35+
return new GameRomFile {
4436
Name = Name,
4537
Path = Path,
4638
Crc = Crc,

src/ArcadeManager.Core/Models/Zip/ZipEntry.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1+
using System.Diagnostics;
12
using System.IO.Compression;
23
using System.Security.Cryptography;
34
using System.Text;
45

56
namespace ArcadeManager.Core.Models.Zip;
67

7-
public class ZipEntry
8+
[DebuggerDisplay("{FullName} ({Crc})")]
9+
public class ZipEntry(ZipArchiveEntry entry)
810
{
9-
private readonly ZipArchiveEntry entry;
10-
11-
public ZipEntry(ZipArchiveEntry entry)
12-
{
13-
this.entry = entry;
14-
}
15-
1611
public string Name => entry?.Name;
1712

1813
public string FullName => entry?.FullName;

0 commit comments

Comments
 (0)