Skip to content

Commit 03901da

Browse files
committed
Added exception support.
1 parent 097cde4 commit 03901da

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

LibIPS.NET/LibIpsNet.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ public struct IpsStudy
3535
public long OutlenMax;
3636
public long OutlenMinMem;
3737
};
38-
public IpsError Study(Stream patch, IpsStudy study)
38+
public IpsStudy Study(Stream patch)
3939
{
40+
IpsStudy study = new IpsStudy();
4041
study.Error = IpsError.IpsInvalid;
41-
if (patch.Length < 8) return IpsError.IpsInvalid;
42+
if (patch.Length < 8) throw new Exceptions.IpsInvalidException();
4243

4344
using (var patchReader = new BinaryReader(patch))
4445
{
4546
// If 'PATCH' text was not found, return IPS was invalid error.
46-
if (!patchReader.ReadChars(PatchText.Length).ToString().Equals(PatchText)) return IpsError.IpsInvalid;
47+
if (!patchReader.ReadChars(PatchText.Length).ToString().Equals(PatchText)) throw new Exceptions.IpsInvalidException();
4748

4849
int offset = Read24(patchReader);
4950
int outlen = 0;
@@ -69,7 +70,7 @@ public IpsError Study(Stream patch, IpsStudy study)
6970
if (offset < lastoffset) w_scrambled = true;
7071
lastoffset = offset;
7172
if (thisout > outlen) outlen = thisout;
72-
if (patch.Position >= patch.Length) return IpsError.IpsInvalid;
73+
if (patch.Position >= patch.Length) throw new Exceptions.IpsInvalidException();
7374

7475
offset = Read24(patchReader);
7576

@@ -88,20 +89,19 @@ public IpsError Study(Stream patch, IpsStudy study)
8889
}
8990

9091
}
91-
if (patch.Position != patch.Length) return IpsError.IpsInvalid;
92+
if (patch.Position != patch.Length) throw new Exceptions.IpsInvalidException();
9293

9394
study.Error = IpsError.IpsOk;
9495
if (w_notthis) study.Error = IpsError.IpsNotThis;
9596
if (w_scrambled) study.Error = IpsError.IpsScrambled;
96-
return study.Error;
97-
9897
}
98+
return study;
9999

100100
}
101-
public IpsError ApplyStudy(Stream patch, IpsStudy study, Stream source, Stream target)
101+
public void ApplyStudy(Stream patch, IpsStudy study, Stream source, Stream target)
102102
{
103103
source.CopyTo(target);
104-
if (study.Error == IpsError.IpsInvalid) return study.Error;
104+
if (study.Error == IpsError.IpsInvalid) throw new Exceptions.IpsInvalidException();
105105
int outlen = (int)Clamp(study.OutlenMin, target.Length, study.OutlenMax);
106106

107107
using (BinaryReader patchReader = new BinaryReader(patch))
@@ -131,8 +131,7 @@ public IpsError ApplyStudy(Stream patch, IpsStudy study, Stream source, Stream t
131131
offset = Read24(patchReader);
132132
}
133133
}
134-
if (study.OutlenMax != 0xFFFFFFFF && source.Length <= study.OutlenMax) study.Error = IpsError.IpsNotThis; // Truncate data without this being needed is a poor idea.
135-
return study.Error;
134+
if (study.OutlenMax != 0xFFFFFFFF && source.Length <= study.OutlenMax) throw new Exceptions.IpsNotThisException(); // Truncate data without this being needed is a poor idea.
136135
}
137136

138137
// Known situations where this function does not generate an optimal patch:
@@ -169,11 +168,11 @@ public IpsError ApplyStudy(Stream patch, IpsStudy study, Stream source, Stream t
169168
/// <param name="target">The target file that contains the modified data.</param>
170169
/// <param name="patch">The patch file to contain the resulting patch data.</param>
171170
/// <returns></returns>
172-
public IpsError Create(string source, string target, string patch)
171+
public void Create(string source, string target, string patch)
173172
{
174173
using (FileStream sourceStream = new FileStream(source, FileMode.Open), targetStream = new FileStream(target, FileMode.Open), patchStream = new FileStream(patch, FileMode.Create))
175174
{
176-
return Create(sourceStream, targetStream, patchStream);
175+
Create(sourceStream, targetStream, patchStream);
177176
}
178177
}
179178
/// <summary>
@@ -183,9 +182,9 @@ public IpsError Create(string source, string target, string patch)
183182
/// <param name="target">The target stream that contains the modified data.</param>
184183
/// <param name="patch">The patch file stream to contain the resulting patch data.</param>
185184
/// <returns></returns>
186-
public IpsError Create(FileStream source, FileStream target, FileStream patch)
185+
public void Create(FileStream source, FileStream target, FileStream patch)
187186
{
188-
return Create(source, target, patch);
187+
Create(source, target, patch);
189188
}
190189
/// <summary>
191190
/// Creates an IPS patch stream from a source stream and a target stream.
@@ -194,7 +193,7 @@ public IpsError Create(FileStream source, FileStream target, FileStream patch)
194193
/// <param name="target">The target stream that contains the modified data.</param>
195194
/// <param name="patch">The patch stream to contain the resulting patch data.</param>
196195
/// <returns></returns>
197-
public IpsError Create(Stream source, Stream target, ref Stream patch)
196+
public void Create(Stream source, Stream target, ref Stream patch)
198197
{
199198
long sourcelen = source.Length;
200199
long targetlen = target.Length;
@@ -356,10 +355,8 @@ public IpsError Create(Stream source, Stream target, ref Stream patch)
356355

357356
if (sourcelen > targetlen) Write24((int)targetlen, patchWriter);
358357

359-
if (sixteenmegabytes) return IpsError.Ips16MB;
360-
if (patchWriter.BaseStream.Length == 8) return IpsError.IpsIdentical;
361-
return IpsError.IpsOk;
362-
358+
if (sixteenmegabytes) throw new Exceptions.Ips16MBException(); ;
359+
if (patchWriter.BaseStream.Length == 8) throw new Exceptions.IpsIdenticalException();
363360
}
364361

365362
}

0 commit comments

Comments
 (0)