Skip to content

Commit

Permalink
Fixed Patch Editor removing functions
Browse files Browse the repository at this point in the history
	modified:   Sonic-06-Mod-Manager/src/Environment3/ModCreator.cs
	modified:   Sonic-06-Mod-Manager/src/Environment3/PatchCreator.cs
	modified:   Sonic-06-Mod-Manager/src/Environment3/RushInterface.Designer.cs
	modified:   Sonic-06-Mod-Manager/src/Environment3/RushInterface.cs
	modified:   Sonic-06-Mod-Manager/src/Environment3/RushInterface.resx
	modified:   Sonic-06-Mod-Manager/src/UnifyPatcher.cs
  • Loading branch information
HyperPolygon64 committed Feb 2, 2020
1 parent 758236f commit 4f7979e
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Sonic-06-Mod-Manager/src/Environment3/ModCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ private void Btn_Create_Click(object sender, EventArgs e)
.Replace(">", "")
.Replace("|", "");

if (Directory.Exists(Path.Combine(Properties.Settings.Default.Path_ModsDirectory, safeTitle)) && !edit)
UnifyMessenger.UnifyMessage.ShowDialog($"A mod called {safeTitle} already exists. Please rename your mod.",
if (Directory.Exists(Path.Combine(Properties.Settings.Default.Path_ModsDirectory, safeTitle)))
UnifyMessenger.UnifyMessage.ShowDialog($"A mod called '{text_Title.Text}' already exists. Please rename your mod.",
"I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else {
pic_Thumbnail.BackgroundImage.Dispose();
Expand Down
87 changes: 64 additions & 23 deletions Sonic-06-Mod-Manager/src/Environment3/PatchCreator.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Drawing;
using Unify.Messenger;
using Unify.Serialisers;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;

// Sonic '06 Mod Manager is licensed under the MIT License:
/*
Expand Down Expand Up @@ -98,41 +100,80 @@ private void btn_Create_Click(object sender, EventArgs e)
.Replace("|", "")
.Replace(" ", "");

if (File.Exists($"{Path.Combine(Program.Patches, safeTitle)}.mlua") && !edit)
UnifyMessenger.UnifyMessage.ShowDialog($"A patch called {safeTitle} already exists. Please rename your patch.",
if (File.Exists($"{Path.Combine(Program.Patches, safeTitle)}.mlua"))
UnifyMessenger.UnifyMessage.ShowDialog($"A patch called '{text_Title.Text}' already exists. Please rename your patch.",
"I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else {
string newPath = $"{Path.Combine(Program.Patches, safeTitle)}.mlua";

using (Stream configCreate = File.Open(newPath, FileMode.Create))
using (StreamWriter configInfo = new StreamWriter(configCreate)) {
configInfo.WriteLine("--[Patch]--");
configInfo.WriteLine($"Title(\"{text_Title.Text}\")");
if (text_Author.Text != string.Empty) configInfo.WriteLine($"Author(\"{text_Author.Text}\")");
configInfo.WriteLine($"Platform(\"{combo_System.Text}\")");
if (text_Blurb.Text != string.Empty) configInfo.WriteLine($"Blurb(\"{text_Blurb.Text}\")");

if (tb_Description.Text != string.Empty) {
string descriptionText = string.Empty;
string lastLine = tb_Description.Lines.Last();
foreach (var newLine in tb_Description.Lines) {
if (newLine != lastLine) descriptionText += $"{newLine}\\n";
else descriptionText += newLine;
if (!edit) {
using (Stream configCreate = File.Open(newPath, FileMode.Create))
using (StreamWriter configInfo = new StreamWriter(configCreate)) {
configInfo.WriteLine("--[Patch]--");
configInfo.WriteLine($"Title(\"{text_Title.Text}\")");
if (text_Author.Text != string.Empty) configInfo.WriteLine($"Author(\"{text_Author.Text}\")");
configInfo.WriteLine($"Platform(\"{combo_System.Text}\")");
if (text_Blurb.Text != string.Empty) configInfo.WriteLine($"Blurb(\"{text_Blurb.Text}\")");

if (tb_Description.Text != string.Empty) {
string descriptionText = string.Empty;
string lastLine = tb_Description.Lines.Last();
foreach (var newLine in tb_Description.Lines) {
if (newLine != lastLine) descriptionText += $"{newLine}\\n";
else descriptionText += newLine;
}
configInfo.WriteLine($"Description(\"{descriptionText}\")");
}

configInfo.WriteLine("\n--[Functions]--");
configInfo.Close();
}
configInfo.WriteLine($"Description(\"{descriptionText}\")");
}

configInfo.WriteLine("\n--[Functions]--");
configInfo.Close();
}

if (!edit)
try { Process.Start(newPath); }
catch {
UnifyMessenger.UnifyMessage.ShowDialog($"Please associate the MLUA format with your text editor of choice.",
"Unable to load patch...", MessageBoxButtons.OK, MessageBoxIcon.Error);
Process.Start(Program.Patches);
}
} else {
// Backup functions from edited patch
List<string> patch = new List<string>();
using (StreamReader patchScript = new StreamReader(this.patch, Encoding.Default)) {
string line;
bool functionsReached = false;
while ((line = patchScript.ReadLine()) != null) {
if (line.StartsWith("--[Functions]--")) functionsReached = true;
if (functionsReached) patch.Add(line);
}
}

File.Delete(this.patch);

using (Stream configOpen = File.Open(newPath, FileMode.Create))
using (StreamWriter configInfo = new StreamWriter(configOpen)) {
configInfo.WriteLine("--[Patch]--");
configInfo.WriteLine($"Title(\"{text_Title.Text}\")");
if (text_Author.Text != string.Empty) configInfo.WriteLine($"Author(\"{text_Author.Text}\")");
configInfo.WriteLine($"Platform(\"{combo_System.Text}\")");
if (text_Blurb.Text != string.Empty) configInfo.WriteLine($"Blurb(\"{text_Blurb.Text}\")");

if (tb_Description.Text != string.Empty) {
string descriptionText = string.Empty;
string lastLine = tb_Description.Lines.Last();
foreach (var newLine in tb_Description.Lines) {
if (newLine != lastLine) descriptionText += $"{newLine}\\n";
else descriptionText += newLine;
}
configInfo.WriteLine($"Description(\"{descriptionText}\")");
}

// Write stored functions to patch
configInfo.WriteLine();
foreach (string function in patch) configInfo.WriteLine(function);

configInfo.Close();
}
}

Close();
}
Expand Down
Loading

0 comments on commit 4f7979e

Please sign in to comment.