Skip to content

Commit

Permalink
SLWModLoader can now apply patches
Browse files Browse the repository at this point in the history
  • Loading branch information
thesupersonic16 committed Apr 2, 2017
1 parent aeaa620 commit 2c7072d
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 19 deletions.
40 changes: 40 additions & 0 deletions SLWModLoader/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions SLWModLoader/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,16 @@
<data name="update" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\res\update.bat;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="Disable_Blue_Trail" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\res\patches\sonicgenerations\Disable Blue Trail.bin;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="Enable_Blue_Trail" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\res\patches\sonicgenerations\Enable Blue Trail.bin;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="Disable_FxPipeline" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\res\patches\sonicgenerations\Disable FxPipeline.bin;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="Enable_FxPipeline" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\res\patches\sonicgenerations\Enable FxPipeline.bin;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
4 changes: 4 additions & 0 deletions SLWModLoader/SLWModLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<None Include="res\patches\sonicgenerations\Disable Blue Trail.bin" />
<None Include="res\patches\sonicgenerations\Disable FxPipeline.bin" />
<None Include="res\patches\sonicgenerations\Enable Blue Trail.bin" />
<None Include="res\patches\sonicgenerations\Enable FxPipeline.bin" />
<None Include="res\update.bat" />
</ItemGroup>
<ItemGroup>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
32 changes: 22 additions & 10 deletions SLWModLoader/src/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 99 additions & 9 deletions SLWModLoader/src/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Threading;
using System.IO.Compression;
using System.Linq;

namespace SLWModLoader
{
Expand All @@ -16,6 +17,7 @@ public partial class MainForm : Form
public static readonly string LWExecutablePath = Path.Combine(Program.StartDirectory, "slw.exe");
public static readonly string ModsFolderPath = Path.Combine(Program.StartDirectory, "mods");
public static readonly string ModsDbPath = Path.Combine(ModsFolderPath, "ModsDB.ini");
public static Dictionary<string, byte[]> GenerationsPatches = new Dictionary<string, byte[]>();
public ModsDatabase ModsDb;

public MainForm()
Expand All @@ -35,19 +37,44 @@ private void MainForm_Load(object sender, EventArgs e)
{
Text += $" (v{Program.VersionString})";

if(File.Exists(LWExecutablePath))
if (File.Exists(LWExecutablePath))
{
Text += " - Sonic Lost World";

PatchGroupBox.Visible = false;
}
if (File.Exists(GensExecutablePath))
{
Text += " - Sonic Generations";
GenerationsPatches.Add("Enable Blue Trail", Resources.Enable_Blue_Trail);
GenerationsPatches.Add("Disable Blue Trail", Resources.Disable_Blue_Trail);
GenerationsPatches.Add("", null);
GenerationsPatches.Add("Enable FxPipeline", Resources.Enable_FxPipeline);
GenerationsPatches.Add("Disable FxPipeline", Resources.Disable_FxPipeline);

for (int i = 0; i < GenerationsPatches.Count; i++)
{
if (GenerationsPatches.ToList()[i].Value == null)
continue;
Button btn = new Button()
{
Text = GenerationsPatches.ToList()[i].Key,
Size = new System.Drawing.Size(128, 32),
Location = new System.Drawing.Point(12 + 140 * (i / 3), 16 + (i % 3) * 42)
};
btn.Click += new EventHandler(PatchButton_Click);
PatchGroupBox.Controls.Add(btn);
}
}

if (File.Exists(LWExecutablePath) || File.Exists(GensExecutablePath))
{
LoadMods();
OrderModList();
if(!isCPKREDIRInstalled())
{
if (MessageBox.Show("Your "+(File.Exists(LWExecutablePath) ? "Sonic Lost World" : "Sonic Generations") +" executable has not yet been Installed for use with CPKREDIR, which is required to load mods.\nWould you like to patch it now?", Program.ProgramName, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
if (MessageBox.Show("Your "+(File.Exists(LWExecutablePath) ? "Sonic Lost World" : "Sonic Generations") +
" executable has not yet been Installed for use with CPKREDIR, which is required to load mods.\nWould you like to patch it now?",
Program.ProgramName, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
InstallCPKREDIR(true);
}
Expand Down Expand Up @@ -411,13 +438,16 @@ private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)

if(modItem.UpdateServer.Length == 0 && modItem.Url.Length != 0)
{ // No Update Server, But has Website
if (MessageBox.Show($"{Program.ProgramName} can not check for updates for {modItem.Title} because no update server has been set.\n\nThis Mod does have a website, do you want to open it to check for updates manually?\n\n URL: {modItem.Url}", Program.ProgramName, MessageBoxButtons.YesNo) == DialogResult.Yes)
if (MessageBox.Show($"{Program.ProgramName} can not check for updates for {modItem.Title} because no update server has been set.\n\n" +
$"This Mod does have a website, do you want to open it to check for updates manually?\n\n URL: {modItem.Url}", Program.ProgramName,
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Process.Start(modItem.Url);
}
}else if (modItem.UpdateServer.Length == 0 && modItem.Url.Length == 0)
{ // No Update Server and Website
MessageBox.Show($"{Program.ProgramName} can not check for updates for {modItem.Title} because no update server has been set.", Program.ProgramName, MessageBoxButtons.OK);
MessageBox.Show($"{Program.ProgramName} can not check for updates for {modItem.Title} because no update server has been set.",
Program.ProgramName, MessageBoxButtons.OK);
}else if (modItem.UpdateServer.Length != 0)
{ // Has Update Server
try
Expand All @@ -428,9 +458,12 @@ private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
if (mod_version["Main"]["VersionString"] != modItem.Version)
{
if (MessageBox.Show($"There's a newer version of {modItem.Title} available!\n\n"+
$"Do you want to update from version {modItem.Version} to {mod_version["Main"]["VersionString"]}? (about {mod_version["Main"]["DownloadSizeString"]})", Program.ProgramName, MessageBoxButtons.YesNo) == DialogResult.Yes)
$"Do you want to update from version {modItem.Version} to " +
$"{mod_version["Main"]["VersionString"]}? (about {mod_version["Main"]["DownloadSizeString"]})",
Program.ProgramName, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
UpdateModForm muf = new UpdateModForm(modItem.Title, wc.DownloadString(modItem.UpdateServer + "mod_update_files.txt"), modItem.RootDirectory);
UpdateModForm muf = new UpdateModForm(modItem.Title, wc.DownloadString(modItem.UpdateServer + "mod_update_files.txt"),
modItem.RootDirectory);
muf.ShowDialog();
RefreshModsList();
}
Expand All @@ -449,7 +482,8 @@ private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)

private void deleteModToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete \"" + ModsList.FocusedItem.Text + "\"?", Program.ProgramName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
if (MessageBox.Show("Are you sure you want to delete \"" + ModsList.FocusedItem.Text + "\"?", Program.ProgramName, MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.Yes)
{
Directory.Delete(ModsDb.GetMod(ModsList.FocusedItem.Text).RootDirectory, true);
RefreshModsList();
Expand Down Expand Up @@ -492,7 +526,8 @@ private void desciptionToolStripMenuItem_Click(object sender, EventArgs e)

private void RemoveModButton_Click(object sender, EventArgs e)
{
if(MessageBox.Show("Are you sure you want to delete \""+ ModsList.FocusedItem.Text+"\"?", Program.ProgramName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
if(MessageBox.Show("Are you sure you want to delete \""+ ModsList.FocusedItem.Text+"\"?", Program.ProgramName, MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.Yes)
{
Directory.Delete(ModsDb.GetMod(ModsList.FocusedItem.Text).RootDirectory, true);
RefreshModsList();
Expand Down Expand Up @@ -600,5 +635,60 @@ private void ModsList_DragEnter(object sender, DragEventArgs e)
}
}

private void PatchButton_Click(object sender, EventArgs e)
{
if (File.Exists(GensExecutablePath))
{
try
{
// Creates a bckup if one hasn't been made.
if (!File.Exists(GensExecutablePath.Substring(0, GensExecutablePath.Length - 4) + "_Backup2.exe"))
File.Copy(GensExecutablePath, GensExecutablePath.Substring(0, GensExecutablePath.Length - 4) + "_Backup2.exe");

var button = (Button)sender;
LogFile.AddMessage("Installing " + button.Text);

// A byte array holding the patch data.
var patchBytes = GenerationsPatches[button.Text];
// Reads all the bytes from the Generations executeable.
var executeableBytes = File.ReadAllBytes(GensExecutablePath);
// Opens a FileStream to the Generations executeable.
var executeableStream = File.OpenWrite(GensExecutablePath);
// Current position of the patch file.
var position = 0;
// Amount of changes.
var changes = BitConverter.ToInt32(patchBytes, position);
// Adds 4 to position since we just read 4 bytes.
position += 4;
// Writes all bytes from executeableBytes back to the Generations executeable.
executeableStream.Write(executeableBytes, 0, executeableBytes.Length);
for (int i = 0; i < changes; ++i)
{
executeableStream.Position = BitConverter.ToInt32(patchBytes, position);
// Adds 4 to position since we just read 4 bytes.
position += 4;
// Gets the size of the current edit.
var size = BitConverter.ToInt32(patchBytes, position);
// Adds 4 to position since we just read 4 bytes.
position += 4;
// Writes the data to the executeable.
for (int i2 = 0; i2 < size; ++i2)
{
executeableStream.WriteByte(patchBytes[position + i2]);
}
position += size;
}
// Closes the FileStream.
executeableStream.Close();
MessageBox.Show("Done.", Program.ProgramName);
LogFile.AddMessage("Finished Installing " + button.Text);
}
catch (Exception ex)
{
LogFile.AddMessage("Exception thrown while applying a patch: " + ex);
LogFile.AddMessage("Button Name: " + ((Button)sender).Text);
}
}
}
}
}

0 comments on commit 2c7072d

Please sign in to comment.