Skip to content

Commit 3766575

Browse files
committed
Wrote ApplyStudy.
1 parent 928c0d7 commit 3766575

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

LibIPS.NET/LibIpsNet.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,41 @@ public IpsError Study(Stream patch, IpsStudy study)
9898
}
9999

100100
}
101-
public IpsError ApplyStudy(Stream patch, IpsStudy study, Stream inFile, Stream outFile)
101+
public IpsError ApplyStudy(Stream patch, IpsStudy study, Stream target)
102102
{
103-
throw new NotImplementedException();
104-
study.Error = IpsError.IpsInvalid;
105-
if (patch.Length < 8) return IpsError.IpsInvalid;
103+
long targetLength = target.Length;
104+
if (study.Error == IpsError.IpsInvalid) return study.Error;
105+
int outlen = (int)Clamp(study.OutlenMin, target.Length, study.OutlenMax);
106+
107+
using (BinaryReader patchReader = new BinaryReader(patch))
108+
using (BinaryWriter targetWriter = new BinaryWriter(target))
109+
{
110+
// Skip PATCH text.
111+
patchReader.BaseStream.Seek(5, SeekOrigin.Begin);
112+
int offset = Read24(patchReader);
113+
while (offset != EndOfFile)
114+
{
115+
int size = Read16(patchReader);
106116

117+
118+
targetWriter.Seek(offset, SeekOrigin.Begin);
119+
// If RLE patch.
120+
if (size == 0)
121+
{
122+
size = Read16(patchReader);
123+
targetWriter.Write(Enumerable.Repeat<byte>(Read8(patchReader), offset).ToArray());
124+
}
125+
// If normal patch.
126+
else
127+
{
128+
targetWriter.Write(patchReader.ReadBytes(size));
129+
130+
}
131+
offset = Read24(patchReader);
132+
}
133+
}
134+
if (study.OutlenMax != 0xFFFFFFFF && targetLength <= study.OutlenMax) study.Error = IpsError.IpsNotThis; // Truncate data without this being needed is a poor idea.
135+
return study.Error;
107136
}
108137

109138
// Known situations where this function does not generate an optimal patch:

0 commit comments

Comments
 (0)