Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion DDGModernizer/Dashboard.Designer.cs

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

81 changes: 67 additions & 14 deletions DDGModernizer/Dashboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ private void ValidateGoButton()

button_Go.Enabled = enabled;

button_PatchSave.Enabled = enabled;

button_GameConfig.Enabled = Directory.Exists(textBox_GameFolder.Text);

checkBox_Borderless.Enabled = (currentVersion == DDGVersion.PRO_2 || currentVersion == DDGVersion.FINAL);
Expand All @@ -235,19 +237,7 @@ private void button_Go_Click(object sender, EventArgs e)
{
try
{
Patcher patcher = new Patcher(currentVersion);

patcher.SetModuleEnabled(Patcher.MODULE_KEY_ASPECT, checkBox_AspectEnable.Checked);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "AspectX", (float)upDown_XAspect.Value);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "AspectY", (float)upDown_YAspect.Value);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "AspectRatio", (float)upDown_WinX.Value / (float)upDown_WinY.Value);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "WinZoom", (float)upDown_WinZoom.Value);

patcher.SetModuleEnabled(Patcher.MODULE_KEY_DRAW_DISTANCE, checkBox_DrawDistEnable.Checked);
patcher.SetModuleArg(Patcher.MODULE_KEY_DRAW_DISTANCE, "RendPow", (float)upDown_RendPow.Value);

patcher.SetModuleEnabled(Patcher.MODULE_KEY_BORDERLESS, checkBox_Borderless.Checked);

Patcher patcher = createAndConfigurePatcher();
patcher.PatchAndRun(textBox_EXEPath.Text, textBox_GameFolder.Text);
}
catch (Exception exception)
Expand Down Expand Up @@ -423,7 +413,10 @@ private void button_GameConfig_Click(object sender, EventArgs e)
switch (currentVersion)
{
case DDGVersion.FINAL:
path = Path.Combine(textBox_GameFolder.Text, "dgfncal.exe");
// Check for translated file, with fall back to original file
path = Path.Combine(textBox_GameFolder.Text, "ddgfncal.exe");
if (!File.Exists(path))
path = Path.Combine(textBox_GameFolder.Text, "dgfncal.exe");
break;

case DDGVersion.PRO_2:
Expand Down Expand Up @@ -452,5 +445,65 @@ private void button_GameConfig_Click(object sender, EventArgs e)
}

#endregion

#region Patch and Save Button
private void button_PatchSave_Click(object sender, EventArgs e)
{
string filePath = null;
try
{
using (SaveFileDialog openFileDialog = new SaveFileDialog())
{
openFileDialog.InitialDirectory = Path.GetDirectoryName(textBox_EXEPath.Text);
openFileDialog.FileName = $"{Path.GetFileNameWithoutExtension(textBox_EXEPath.Text)}_patched{Path.GetExtension(textBox_EXEPath.Text)}";
openFileDialog.Filter = "exe files (*.exe)|*.exe|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (textBox_EXEPath.Text.Equals(openFileDialog.FileName, StringComparison.OrdinalIgnoreCase))
throw new Exception("Can't save over original executable!");
filePath = openFileDialog.FileName;
}
}

if (!string.IsNullOrWhiteSpace(filePath))
{
Patcher patcher = createAndConfigurePatcher();
patcher.PatchAndSave(textBox_EXEPath.Text, filePath);
MessageBox.Show("Success!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception exception)
{
// Delete new file in case of error
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath))
try { File.Delete(filePath); } catch { }

MessageBox.Show(exception.Message);
}
}
#endregion

#region Configure Patcher
Patcher createAndConfigurePatcher()
{
Patcher patcher = new Patcher(currentVersion);

patcher.SetModuleEnabled(Patcher.MODULE_KEY_ASPECT, checkBox_AspectEnable.Checked);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "AspectX", (float)upDown_XAspect.Value);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "AspectY", (float)upDown_YAspect.Value);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "AspectRatio", (float)upDown_WinX.Value / (float)upDown_WinY.Value);
patcher.SetModuleArg(Patcher.MODULE_KEY_ASPECT, "WinZoom", (float)upDown_WinZoom.Value);

patcher.SetModuleEnabled(Patcher.MODULE_KEY_DRAW_DISTANCE, checkBox_DrawDistEnable.Checked);
patcher.SetModuleArg(Patcher.MODULE_KEY_DRAW_DISTANCE, "RendPow", (float)upDown_RendPow.Value);

patcher.SetModuleEnabled(Patcher.MODULE_KEY_BORDERLESS, checkBox_Borderless.Checked);

return patcher;
}
#endregion
}
}
70 changes: 49 additions & 21 deletions DDGModernizer/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,56 @@ public void PatchAndRun(string exePath, string workingDir)
File.Copy(exePath, patchExePath);

// Go through each module's injections and inject the bytes
FileStream file = File.Open(patchExePath, FileMode.Open, FileAccess.ReadWrite);
using (FileStream file = File.Open(patchExePath, FileMode.Open, FileAccess.ReadWrite))
{
// Apply patches to new file
applyPatches(file);
}

// Launch the patched EXE
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = patchExePath,
WorkingDirectory = workingDir
}
};

process.Start();
process.WaitForExit();

// Clean up
File.Delete(patchExePath);
Directory.Delete(tempDir);
}

#endregion

#region Save
public void PatchAndSave(string originalExePath, string newExePath)
{
// Load original file into memory
byte[] fileBytes = File.ReadAllBytes(originalExePath);
using (MemoryStream ms = new MemoryStream(fileBytes))
{
// Apply patches to memory loaded file
applyPatches(ms);

// Save modified file
ms.Position = 0;
using (FileStream fileStream = new FileStream(newExePath, FileMode.Create, FileAccess.Write))
{
ms.CopyTo(fileStream);
}
}
}
#endregion

#region Apply
void applyPatches(Stream file)
{
// Go through each module's injections and inject the bytes
foreach (var module in moduleList)
{
if (module.enabled == false)
Expand Down Expand Up @@ -392,27 +440,7 @@ public void PatchAndRun(string exePath, string workingDir)
file.Write(preppedBytes, 0, preppedBytes.Length);
}
}

file.Close();

// Launch the patched EXE
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = patchExePath,
WorkingDirectory = workingDir
}
};

process.Start();
process.WaitForExit();

// Clean up
File.Delete(patchExePath);
Directory.Delete(tempDir);
}

#endregion
}
}
2 changes: 2 additions & 0 deletions DDGModernizer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ static class Program
[STAThread]
static void Main()
{
// Force en-Us to avoid decimal parsing errors
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-Us");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Dashboard());
Expand Down