Skip to content

Commit 4473c7b

Browse files
General cleanup (#17)
1 parent 22c1c16 commit 4473c7b

File tree

17 files changed

+74
-105
lines changed

17 files changed

+74
-105
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,4 @@ fabric.properties
494494

495495
### Custom ###
496496
launchSettings.json
497+
.idea/.idea.ShinRyuModManager-CE/.idea/sonarlint.xml

ParLibrary.Tests/SllzTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void TestSllz(byte compressionVersion) {
2626
var compressedBinaryFormat = (ParFile)ConvertFormat.With(typeof(Compressor), binaryFormat, parameters);
2727
var decompressedBinaryFormat = (ParFile)ConvertFormat.With(typeof(Decompressor), compressedBinaryFormat);
2828

29-
Assert.IsTrue(compressedBinaryFormat.Stream.Length < decompressedBinaryFormat.Stream.Length);
29+
Assert.IsLessThan(decompressedBinaryFormat.Stream.Length, compressedBinaryFormat.Stream.Length);
3030
Assert.IsTrue(dataStream.Compare(decompressedBinaryFormat.Stream));
3131
}
3232
}

ParLibrary/Converter/ParArchiveWriter.cs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,22 @@ private static void WriteNames(DataWriter writer, IEnumerable<Node> nodes) {
246246
}
247247

248248
private static void WriteFolders(DataWriter writer, IEnumerable<Node> folders) {
249-
foreach (var node in folders) {
249+
foreach (var tags in folders.Select(x => x.Tags)) {
250250
var attributes = 0x00000010;
251251

252-
if (node.Tags.TryGetValue("DirectoryInfo", out var directoryInfo)) {
252+
if (tags.TryGetValue("DirectoryInfo", out var directoryInfo)) {
253253
DirectoryInfo info = directoryInfo;
254254
attributes = (int)info.Attributes;
255255
}
256256

257-
if (node.Tags.TryGetValue("Attributes", out var attrs)) {
257+
if (tags.TryGetValue("Attributes", out var attrs)) {
258258
attributes = (int)attrs;
259259
}
260260

261-
writer.Write((int)node.Tags["FolderCount"]);
262-
writer.Write((int)node.Tags["FirstFolderIndex"]);
263-
writer.Write((int)node.Tags["FileCount"]);
264-
writer.Write((int)node.Tags["FirstFileIndex"]);
261+
writer.Write((int)tags["FolderCount"]);
262+
writer.Write((int)tags["FirstFolderIndex"]);
263+
writer.Write((int)tags["FileCount"]);
264+
writer.Write((int)tags["FirstFileIndex"]);
265265
writer.Write(attributes);
266266
writer.Write(0x00000000);
267267
writer.Write(0x00000000);
@@ -275,39 +275,33 @@ private static void WriteFiles(DataWriter writer, IEnumerable<Node> files, long
275275
foreach (var node in files) {
276276
var parFile = node.GetFormatAs<ParFile>();
277277

278-
if (parFile == null) {
278+
if (parFile == null)
279279
continue;
280-
}
281-
282-
if (node.Stream!.Length > 2048) {
283-
blockSize = 2048 + (-node.Stream.Length % 2048);
280+
281+
var streamLength = node.Stream!.Length;
282+
283+
if (streamLength > 2048 || streamLength >= blockSize) {
284+
blockSize = 2048 + (-streamLength % 2048);
284285
dataPosition = Align(dataPosition, 2048);
285286
} else {
286-
if (node.Stream.Length < blockSize) {
287-
blockSize -= node.Stream.Length;
288-
} else {
289-
blockSize = 2048 + (-node.Stream.Length % 2048);
290-
dataPosition = Align(dataPosition, 2048);
291-
}
287+
blockSize -= streamLength;
292288
}
293289

294290
ulong seconds = 0;
295291
var attributes = parFile.Attributes;
296292

297293
if (!parameters.ResetFileDates) {
298294
var date = parFile.FileDate;
299-
var baseDate = new DateTime(1970, 1, 1);
295+
var baseDate = DateTime.UnixEpoch;
300296

301297
if (node.Tags.TryGetValue("Timestamp", out var timestamp)) {
302298
date = baseDate.AddSeconds(timestamp);
303299
}
304300

305-
if (node.Tags.TryGetValue("FileInfo", out var fileInfo)) {
306-
if (fileInfo is FileInfo info) {
307-
attributes = HandleAttributes(info);
308-
309-
date = info.LastWriteTime;
310-
}
301+
if (node.Tags.TryGetValue("FileInfo", out var fileInfo) && fileInfo is FileInfo info) {
302+
attributes = HandleAttributes(info);
303+
304+
date = info.LastWriteTime;
311305
}
312306

313307
seconds = (ulong)(date - baseDate).TotalSeconds;

ParLibrary/ParFile.cs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ public class ParFile : BinaryFormat, IConverter<BinaryFormat, ParFile> {
1515
/// Initializes a new instance of the <see cref="ParFile"/> class.
1616
/// </summary>
1717
public ParFile() {
18-
CanBeCompressed = true;
19-
IsCompressed = false;
2018
DecompressedSize = 0;
21-
Attributes = 0x00000020;
2219
FileDate = DateTime.Now;
2320
}
2421

@@ -29,10 +26,7 @@ public ParFile() {
2926
public ParFile(DataStream stream) : base(stream) {
3027
ArgumentNullException.ThrowIfNull(stream);
3128

32-
CanBeCompressed = true;
33-
IsCompressed = false;
3429
DecompressedSize = (uint)stream.Length;
35-
Attributes = 0x00000020;
3630
FileDate = DateTime.Now;
3731
}
3832

@@ -43,18 +37,15 @@ public ParFile(DataStream stream) : base(stream) {
4337
/// <param name="offset">Start offset.</param>
4438
/// <param name="length">Data length.</param>
4539
public ParFile(DataStream stream, long offset, long length) : base(stream, offset, length) {
46-
CanBeCompressed = true;
47-
IsCompressed = false;
4840
DecompressedSize = (uint)length;
49-
Attributes = 0x00000020;
5041
FileDate = DateTime.Now;
5142
}
52-
43+
5344
/// <summary>
5445
/// Gets or sets a value indicating whether the file can be compressed.
5546
/// </summary>
56-
public bool CanBeCompressed { get; init; }
57-
47+
public bool CanBeCompressed { get; init; } = true;
48+
5849
/// <summary>
5950
/// Gets or sets a value indicating whether the file is compressed.
6051
/// </summary>
@@ -64,32 +55,24 @@ public ParFile(DataStream stream, long offset, long length) : base(stream, offse
6455
/// Gets or sets the file size (decompressed).
6556
/// </summary>
6657
public uint DecompressedSize { get; init; }
67-
58+
6859
/// <summary>
6960
/// Gets or sets the file attributes.
7061
/// </summary>
71-
public int Attributes { get; init; }
62+
public int Attributes { get; init; } = 0x00000020;
7263

7364
/// <summary>
7465
/// Gets or sets the file date (as ulong).
7566
/// </summary>
7667
public ulong Timestamp { get; init; }
77-
68+
7869
/// <summary>
7970
/// Gets or sets the file date (as DateTime).
8071
/// </summary>
8172
public DateTime FileDate {
82-
get {
83-
var baseDate = new DateTime(1970, 1, 1);
84-
85-
return baseDate.AddSeconds(Timestamp);
86-
}
87-
88-
private init {
89-
var baseDate = new DateTime(1970, 1, 1);
90-
91-
Timestamp = (ulong)(value - baseDate).TotalSeconds;
92-
}
73+
get => DateTime.UnixEpoch.AddSeconds(Timestamp);
74+
75+
private init => Timestamp = (ulong)(value - DateTime.UnixEpoch).TotalSeconds;
9376
}
9477

9578
/// <inheritdoc/>

ParLibrary/Sllz/Compressor.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static DataStream Compress(DataStream inputDataStream, CompressorParamet
8282
break;
8383

8484
case 2 when inputDataStream.Length < 0x1B:
85-
throw new FormatException($"SLLZv2: Input size must more than 0x1A.");
85+
throw new FormatException("SLLZv2: Input size must more than 0x1A.");
8686

8787
case 2:
8888
compressedDataStream = CompressV2(inputDataStream);
@@ -135,9 +135,7 @@ private static DataStream CompressV1(DataStream inputDataStream) {
135135
outputData[flagPosition] = 0x00;
136136
outputPosition++;
137137

138-
if (outputPosition >= outputSize) {
139-
throw new SllzCompressorException("Compressed size is bigger than original size.");
140-
}
138+
ThrowIfCompressedIsLarger(outputPosition, outputSize);
141139

142140
while (inputPosition < inputData.Length) {
143141
var windowSize = Math.Min(inputPosition, MAX_WINDOW_SIZE);
@@ -158,18 +156,14 @@ private static DataStream CompressV1(DataStream inputDataStream) {
158156
outputData[flagPosition] = 0x00;
159157
outputPosition++;
160158

161-
if (outputPosition >= outputSize) {
162-
throw new SllzCompressorException("Compressed size is bigger than original size.");
163-
}
159+
ThrowIfCompressedIsLarger(outputPosition, outputSize);
164160
}
165161

166162
outputData[outputPosition] = inputData[inputPosition];
167163
inputPosition++;
168164
outputPosition++;
169165

170-
if (outputPosition >= outputSize) {
171-
throw new SllzCompressorException("Compressed size is bigger than original size.");
172-
}
166+
ThrowIfCompressedIsLarger(outputPosition, outputSize);
173167
} else {
174168
currentFlag |= (byte)(1 << (7 - bitCount));
175169
bitCount++;
@@ -183,9 +177,7 @@ private static DataStream CompressV1(DataStream inputDataStream) {
183177
outputData[flagPosition] = 0x00;
184178
outputPosition++;
185179

186-
if (outputPosition >= outputSize) {
187-
throw new SllzCompressorException("Compressed size is bigger than original size.");
188-
}
180+
ThrowIfCompressedIsLarger(outputPosition, outputSize);
189181
}
190182

191183
var offset = (short)((match.Item1 - 1) << 4);
@@ -195,16 +187,12 @@ private static DataStream CompressV1(DataStream inputDataStream) {
195187
outputData[outputPosition] = (byte)tuple;
196188
outputPosition++;
197189

198-
if (outputPosition >= outputSize) {
199-
throw new SllzCompressorException("Compressed size is bigger than original size.");
200-
}
190+
ThrowIfCompressedIsLarger(outputPosition, outputSize);
201191

202192
outputData[outputPosition] = (byte)(tuple >> 8);
203193
outputPosition++;
204-
205-
if (outputPosition >= outputSize) {
206-
throw new SllzCompressorException("Compressed size is bigger than original size.");
207-
}
194+
195+
ThrowIfCompressedIsLarger(outputPosition, outputSize);
208196

209197
inputPosition += match.Item2;
210198
}
@@ -281,4 +269,10 @@ private static byte[] ZlibCompress(byte[] decompressedData) {
281269

282270
return outputMemoryStream.ToArray();
283271
}
272+
273+
private static void ThrowIfCompressedIsLarger(uint outputPosition, uint outputSize) {
274+
if (outputPosition >= outputSize) {
275+
throw new SllzCompressorException("Compressed size is bigger than original size.");
276+
}
277+
}
284278
}

ShinRyuModManager-CE/GameModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public static void DoY5HActProcedure(MLO mlo) {
8585

8686
try {
8787
new DirectoryInfo(outputFakeDir).Delete(true);
88-
} catch { }
88+
} catch {
89+
// ignore
90+
}
8991
} else {
9092
var outputPath = Path.Combine(parlessDir.FullName, dir.Name + ".par");
9193
Gibbed.Yakuza0.Pack.Program.Main([dir.FullName], outputPath);

ShinRyuModManager-CE/LibMeta.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using JetBrains.Annotations;
12
using ShinRyuModManager.Helpers;
23

34
namespace ShinRyuModManager;
45

6+
[UsedImplicitly]
57
public class LibMeta {
68
public Guid GUID { get; set; }
79
public string Name { get; set; }

ShinRyuModManager-CE/ModLoadOrder/MLO.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class MLO {
99
private const uint VERSION = 0x020000; // 2.0
1010
private const uint FILESIZE = 0x0; // Remaining faithful to RGG by adding a filesize that's not used
1111

12-
public readonly List<string> Mods;
13-
public readonly List<ParlessFile> Files;
14-
public readonly List<ParlessFolder> ParlessFolders;
15-
public readonly List<CpkFolder> CpkFolders;
12+
public List<string> Mods { get; }
13+
public List<ParlessFile> Files { get; }
14+
public List<ParlessFolder> ParlessFolders { get; }
15+
public List<CpkFolder> CpkFolders { get; }
1616

1717
public MLO(List<int> modIndices, List<string> mods, OrderedSet<string> fileSet, List<ParlessFolder> parlessFolders, Dictionary<string, List<int>> cpkFolders) {
1818
var files = fileSet.ToList();

ShinRyuModManager-CE/ModLoadOrder/Mods/Mod.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,8 @@ public void AddFiles(string path, string check) {
136136
case "speech":
137137
cpkDataPath = GamePath.RemoveModPath(path);
138138

139-
if (GamePath.CurrentGame == Game.Yakuza5) {
139+
if (GamePath.CurrentGame is Game.Yakuza5 or <= Game.YakuzaKiwami) {
140140
RepackCpKs.Add(cpkDataPath + ".cpk");
141-
//Log.Verbose("Adding CPK folder: {CpkDataPath}", cpkDataPath);
142-
} else {
143-
if (GamePath.CurrentGame <= Game.YakuzaKiwami) {
144-
RepackCpKs.Add(cpkDataPath + ".cpk");
145-
}
146141
}
147142

148143
break;
@@ -264,10 +259,8 @@ public void AddFiles(string path, string check) {
264259
}
265260

266261
protected static string CheckFolder(string name) {
267-
foreach (var folder in Constants.IncompatiblePars.Where(name.StartsWith)) {
268-
return folder;
269-
}
270-
271-
return "";
262+
var folder = Constants.IncompatiblePars.FirstOrDefault(name.StartsWith);
263+
264+
return folder ?? string.Empty;
272265
}
273266
}

ShinRyuModManager-CE/ModLoadOrder/Mods/ModInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
namespace ShinRyuModManager.ModLoadOrder.Mods;
55

6-
public partial class ModInfo : ObservableObject, IEquatable<ModInfo> {
7-
6+
public sealed partial class ModInfo : ObservableObject, IEquatable<ModInfo> {
87
[ObservableProperty] private string _name;
98
[ObservableProperty] private bool _enabled;
109

0 commit comments

Comments
 (0)