Skip to content

Commit

Permalink
Merge pull request #68 from geefr/development
Browse files Browse the repository at this point in the history
Beataroni: Implement mod validation action, actually fix path names
  • Loading branch information
geefr authored Dec 5, 2020
2 parents 82fcda3 + 04afd2c commit c8efedd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
73 changes: 51 additions & 22 deletions Beataroni/Beataroni/Services/modinstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO.Compression;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace Beataroni.Services
{
Expand Down Expand Up @@ -35,16 +36,20 @@ public class ModInstaller
* Ensure the case of path is correct, so that files are placed
* in the correct folders under BS install
*/
private string FixPath(string path)
private string FixPath(string entry)
{
var foundPath = BSPathsToFix.Find(x =>
// Ported from QBeat/util.cpp
foreach (var correctPath in BSPathsToFix)
{
if (path.Length < x.Length) return false;
var testPath = path.Remove(x.Length);
return string.Equals(testPath, x, StringComparison.OrdinalIgnoreCase);
});
if (string.IsNullOrEmpty(foundPath)) return path;
return $"{foundPath}{path.Substring(foundPath.Length)}";
var path = entry;
if (path.StartsWith(correctPath, StringComparison.OrdinalIgnoreCase))
{
path = path.Remove(0, correctPath.Length);
path = $"{correctPath}{path}";
return path;
}
}
return entry;
}

public bool InstallMod(Mod m, string bsInstall, InstallLogLine log)
Expand All @@ -54,6 +59,7 @@ public bool InstallMod(Mod m, string bsInstall, InstallLogLine log)
if (!(dl.type.Equals("steam") || dl.type.Equals("universal")))
{
// TODO: At the moment Beataroni only supports steam installs - As Oculus doesn't work on Linux
continue;
}

// Download the file
Expand All @@ -79,19 +85,10 @@ public bool InstallMod(Mod m, string bsInstall, InstallLogLine log)
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
// Ported from QBeat/util.cpp
foreach (var correctPath in BSPathsToFix)
{
var path = entry.FullName;
if (path.StartsWith(correctPath, StringComparison.OrdinalIgnoreCase))
{
path = path.Remove(0, correctPath.Length);
path = $"{correctPath}{path}";
}
}
var path = FixPath(entry.FullName);

// Gets the full path to ensure that relative segments are removed.
string destinationPath = Path.GetFullPath(Path.Combine(bsInstall, entry.FullName));
string destinationPath = Path.GetFullPath(Path.Combine(bsInstall, path));

// Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
// are case-insensitive.
Expand Down Expand Up @@ -242,10 +239,42 @@ public bool UninstallMod(Mod m, InstallLogLine log)
return false;
}

public bool ValidateMod(Mod m, InstallLogLine log)
public bool ValidateMod(Mod m, string bsInstall, InstallLogLine log)
{
// TODO
return false;
foreach( var dl in m.downloads )
{
if (!(dl.type.Equals("steam") || dl.type.Equals("universal")))
{
// TODO: At the moment Beataroni only supports steam installs - As Oculus doesn't work on Linux
continue;
}

foreach( var hash in dl.hashMd5 )
{
var path = FixPath(hash.file);
path = Path.Combine(bsInstall, path);
try
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(path))
{
var fileHash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-","");
if( !string.Equals(fileHash, hash.hash, StringComparison.OrdinalIgnoreCase) )
{
log($"ValidateMod: Failed to validate {path} : Expected {hash.hash} : Actual {fileHash}");
return false;
}
}
}
}
catch(Exception e)
{
log($"ValidateMod: Failed to calc md5 for {path}: {e.Message}");
}
}
}
return true;
}

public bool IsModInstalled(Mod m, InstallLogLine log)
Expand Down
4 changes: 2 additions & 2 deletions Beataroni/Beataroni/ViewModels/ModInstallViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ public void InstallMods(string bsInstall)
}

CurrentStep = $"Current Step: Validate";
if (!installer.ValidateMod(m.mod, LogLine))
if (!installer.ValidateMod(m.mod, bsInstall, LogLine))
{
installLogText += $"{m.mod.name}: Validate failed (todo)\n";
this.RaisePropertyChanged(nameof(InstallLogText));
// continue;
continue;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Beataroni/Beataroni/Views/ModInstallView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
x:Class="Beataroni.Views.ModInstallView">
<StackPanel>
<TextBlock Text="Mod Installation Progress" HorizontalAlignment="Center" />
<TextBlock HorizontalAlignment="Center">TODO: uninstall,validate,isinstalled actions not implemented</TextBlock>
<TextBlock HorizontalAlignment="Center">TODO: uninstall,isinstalled actions not implemented</TextBlock>

<TextBlock Text="{Binding CurrentMod}" HorizontalAlignment="Center" />
<TextBlock Text="{Binding CurrentStep}" HorizontalAlignment="Center" />
Expand Down

0 comments on commit c8efedd

Please sign in to comment.