Skip to content

Commit

Permalink
Moved checksum txt file to embedded resource and made and made loggin…
Browse files Browse the repository at this point in the history
…g more clear
  • Loading branch information
TekkaGB committed Mar 17, 2024
1 parent e0ede18 commit c07b9ce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
66 changes: 37 additions & 29 deletions PizzaOven/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace PizzaOven
{
public static class ModLoader
{
private static string version = null;
// Restore all backups created from previous build
public static bool Restart()
{
Expand Down Expand Up @@ -103,8 +104,7 @@ public static bool Build(string mod)
if (extension.Equals(".xdelta", StringComparison.InvariantCultureIgnoreCase))
{
// Attempt to checksum each xdelta patch
string checksumtxt = "Dependencies/XDelta_Common_Checksum.txt";
WindowChecksum(modFile, xdelta, checksumtxt, checksumtxt);
WindowChecksum(modFile, xdelta);
var success = false;
var gotAccessDeniedError = false;
foreach (var file in FilesToPatch)
Expand All @@ -117,25 +117,25 @@ public static bool Build(string mod)
try
{
// Attempt to patch file
Global.logger.WriteLine($"Attempting to patch {file} with {modFile}...", LoggerType.Info);
Global.logger.WriteLine($"Attempting to patch {Path.GetFileName(file)} with {Path.GetFileName(modFile)}...", LoggerType.Info);
Patch(file, modFile, $"{Path.GetDirectoryName(file)}{Global.s}temp", xdelta);
// Only make backup if it doesn't already exist
if (!File.Exists($"{file}.po"))
File.Copy(file, $"{file}.po", true);
File.Move($"{Path.GetDirectoryName(file)}{Global.s}temp", file, true);
Global.logger.WriteLine($"Applied {modFile} to {file}.", LoggerType.Info);
Global.logger.WriteLine($"Applied {Path.GetFileName(modFile)} to {Path.GetFileName(file)}.", LoggerType.Info);
successes++;
if (Path.GetFileName(modFile).ToLowerInvariant().Contains("yyc") && File.Exists($"{Global.config.ModsFolder}{Global.s}Steamworks_x64.dll"))
File.Move($"{Global.config.ModsFolder}{Global.s}Steamworks_x64.dll", $"{Global.config.ModsFolder}{Global.s}Steamworks_x64.dll.po", true);
}
catch (Exception e)
{
if (e is System.UnauthorizedAccessException) {
Global.logger.WriteLine($"Access denied when trying to patch {file} with {modFile}", LoggerType.Warning);
Global.logger.WriteLine($"Access denied when trying to patch {Path.GetFileName(file)} with {Path.GetFileName(modFile)}", LoggerType.Warning);
gotAccessDeniedError = true;
break;
}
Global.logger.WriteLine($"Unable to patch {file} with {modFile}", LoggerType.Warning);
Global.logger.WriteLine($"Unable to patch {Path.GetFileName(file)} with {Path.GetFileName(modFile)}", LoggerType.Warning);
continue;
}
// Stop trying to patch if it was successful
Expand All @@ -146,11 +146,12 @@ public static bool Build(string mod)
{
if (gotAccessDeniedError)
{
Global.logger.WriteLine($"{modFile} got an access denied error while patch a file. Try reinstalling Pizza Tower to a folder you have access to or running Pizza Oven in administrator mode", LoggerType.Error);
Global.logger.WriteLine($"{Path.GetFileName(modFile)} got an access denied error while patch a file. Try reinstalling Pizza Tower to a folder you have access to or running Pizza Oven in administrator mode", LoggerType.Error);
}
else
{
Global.logger.WriteLine($"{modFile} wasn't able to patch any file. Ensure that either the mod xdelta patch or your game version is up to date", LoggerType.Error);
Global.logger.WriteLine($"{Path.GetFileName(modFile)} wasn't able to patch any file. Ensure that either the mod or your game version is up to date. {Path.GetFileName(modFile)} is intended for {version}. " +
$"If this version number matches with your current game version go to {Global.config.ModsFolder} and delete data.win.po and anything else with a .po extension then verify integrity of game files and try again.", LoggerType.Error);
}
errors++;
}
Expand All @@ -163,26 +164,21 @@ public static bool Build(string mod)
{
// Copy over file to lang folder
File.Copy(modFile, $"{Global.config.ModsFolder}{Global.s}lang{Global.s}{Path.GetFileName(modFile)}", true);
Global.logger.WriteLine($"Copied over {modFile} to language folder", LoggerType.Info);
Global.logger.WriteLine($"Copied over {Path.GetFileName(modFile)} to language folder", LoggerType.Info);
successes++;
}
}
// Font .png files
else if (extension.Equals(".png", StringComparison.InvariantCultureIgnoreCase))
{
Global.logger.WriteLine($"{modFile} wasn't able to patch any file. Ensure that either the mod xdelta patch or your game version is up to date", LoggerType.Error);
// Reattempt to write xdelta patch
string checksumtxt = "Dependencies/XDelta_Common_Checksum.txt";
WindowChecksum(modFile, xdelta, checksumtxt, checksumtxt);
errors++;
// Check if png is in fonts folder
if (modFile.Contains("fonts", StringComparison.InvariantCultureIgnoreCase))
{
// Create fonts folder
Directory.CreateDirectory($"{Global.config.ModsFolder}{Global.s}lang{Global.s}fonts");
// Copy over file to fonts folder
File.Copy(modFile, $"{Global.config.ModsFolder}{Global.s}lang{Global.s}fonts{Global.s}{Path.GetFileName(modFile)}", true);
Global.logger.WriteLine($"Copied over {modFile} to fonts folder", LoggerType.Info);
Global.logger.WriteLine($"Copied over {Path.GetFileName(modFile)} to fonts folder", LoggerType.Info);
successes++;
}
}
Expand All @@ -194,7 +190,7 @@ public static bool Build(string mod)
if (!File.Exists($"{dataWin}.po"))
File.Copy(dataWin, $"{dataWin}.po", true);
File.Copy(modFile, dataWin, true);
Global.logger.WriteLine($"Copied over {modFile} to use instead of data.win", LoggerType.Info);
Global.logger.WriteLine($"Copied over {Path.GetFileName(modFile)} to use instead of data.win", LoggerType.Info);
successes++;
}
// Copy over .bank file in case modder provides entire file instead of .xdelta patch
Expand All @@ -207,7 +203,7 @@ public static bool Build(string mod)
if (!File.Exists($"{FileToReplace}.po"))
File.Copy(FileToReplace, $"{FileToReplace}.po", true);
File.Copy(modFile, FileToReplace, true);
Global.logger.WriteLine($"Copied over {modFile} to use in sound folder", LoggerType.Info);
Global.logger.WriteLine($"Copied over {Path.GetFileName(modFile)} to use in sound folder", LoggerType.Info);
}
// Copy the file over if its not vanilla
else
Expand All @@ -227,22 +223,22 @@ public static bool Build(string mod)
{
// Copy over file to game folder
File.Copy(modFile, $"{Global.config.ModsFolder}{Global.s}{Path.GetFileName(modFile)}", true);
Global.logger.WriteLine($"Copied over {modFile} to game folder", LoggerType.Info);
Global.logger.WriteLine($"Copied over {Path.GetFileName(modFile)} to game folder", LoggerType.Info);
successes++;
}
// Video Files
else if (extension.Equals(".mp4", StringComparison.InvariantCultureIgnoreCase))
{
// Copy over file to game folder
File.Copy(modFile, $"{Global.config.ModsFolder}{Global.s}{Path.GetFileName(modFile)}", true);
Global.logger.WriteLine($"Copied over {modFile} to game folder", LoggerType.Info);
Global.logger.WriteLine($"Copied over {Path.GetFileName(modFile)} to game folder", LoggerType.Info);
successes++;
}
}
catch (Exception e)
{
if (e is System.UnauthorizedAccessException)
Global.logger.WriteLine($"Access denied when trying to apply {modFile}. Try reinstalling Pizza Tower to a folder you have access to or running Pizza Oven in administrator mode", LoggerType.Error);
Global.logger.WriteLine($"Access denied when trying to apply {Path.GetFileName(modFile)}. Try reinstalling Pizza Tower to a folder you have access to or running Pizza Oven in administrator mode", LoggerType.Error);
else
throw;
}
Expand Down Expand Up @@ -280,7 +276,7 @@ private static void RestoreDirectory(string path)
catch (Exception e)
{
if (e is System.UnauthorizedAccessException)
Global.logger.WriteLine($"Access denied when trying to restore {file}. Try reinstalling Pizza Tower to a folder you have access to or running Pizza Oven in administrator mode", LoggerType.Error);
Global.logger.WriteLine($"Access denied when trying to restore {Path.GetFileName(file)}. Try reinstalling Pizza Tower to a folder you have access to or running Pizza Oven in administrator mode", LoggerType.Error);
else
throw;
}
Expand All @@ -289,10 +285,9 @@ private static void RestoreDirectory(string path)
}

// xdelta print header
private static void WindowChecksum(string patch, string xdelta, string checksumFilePath, string commonChecksum)
private static void WindowChecksum(string patch, string xdelta)
{
int vcdiffCopyWindowLength = 0;
string versionInfo = string.Empty;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
Expand Down Expand Up @@ -331,7 +326,12 @@ private static void WindowChecksum(string patch, string xdelta, string checksumF
try
{
// Read all in .txt file
string[] checksumLines = File.ReadAllLines(checksumFilePath);
string[] checksumLines = null;
using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream("PizzaOven.Dependencies.XDelta_Common_Checksum.txt"))
using (StreamReader reader = new StreamReader(stream))
{
checksumLines = EnumerateLines(reader).ToArray();
}
string prevLine = null;
foreach (string checksumLine in checksumLines)
{
Expand All @@ -345,14 +345,14 @@ private static void WindowChecksum(string patch, string xdelta, string checksumF
// Compare .txt and window length checksum
if (checksum == vcdiffCopyWindowLength)
{
Global.logger.WriteLine($"Match found checksum file {commonChecksum}: {vcdiffCopyWindowLength}", LoggerType.Info);
Global.logger.WriteLine($"Match found checksum: {vcdiffCopyWindowLength}", LoggerType.Info);
// Version txt above matching checksum
if (!string.IsNullOrEmpty(prevLine))
{
versionInfo = prevLine;
Global.logger.WriteLine($"Patch applies to Pizza Tower: {versionInfo}", LoggerType.Info);
version = prevLine;
Global.logger.WriteLine($"Patch applies to Pizza Tower: {version}", LoggerType.Info);
}
break;
return;
}
}
}
Expand All @@ -363,8 +363,16 @@ private static void WindowChecksum(string patch, string xdelta, string checksumF
{
Global.logger.WriteLine($"Error while checking checksum file, {ex.Message}", LoggerType.Error);
}
version = null;
}


private static IEnumerable<string> EnumerateLines(TextReader reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
}
12 changes: 5 additions & 7 deletions PizzaOven/PizzaOven.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<UseWPF>true</UseWPF>
<ApplicationIcon>Assets\PizzaOvenIcon.ico</ApplicationIcon>
<AssemblyName>PizzaOven</AssemblyName>
<AssemblyVersion>1.0.13.0</AssemblyVersion>
<AssemblyVersion>1.0.14.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
<None Remove="Assets\PizzaOvenLogo.png" />
<None Remove="Assets\PizzaOvenPreview.png" />
<None Remove="Assets\RobotoMono-Regular.ttf" />
<None Remove="Dependencies\XDelta_Common_Checksum.txt" />
<None Remove="GameBanana.png" />
</ItemGroup>

Expand All @@ -36,6 +37,9 @@
<Resource Include="Assets\PizzaOvenLogo.png" />
<Resource Include="Assets\PizzaOvenPreview.png" />
<Resource Include="Assets\RobotoMono-Regular.ttf" />
<EmbeddedResource Include="Dependencies\XDelta_Common_Checksum.txt">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand All @@ -50,12 +54,6 @@
</None>
</ItemGroup>

<ItemGroup>
<None Update="Dependencies\XDelta_Common_Checksum.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Page Update="UI\ChoiceWindow.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
Expand Down

0 comments on commit c07b9ce

Please sign in to comment.