Skip to content

Commit

Permalink
Move exception handling out of FIleSHA
Browse files Browse the repository at this point in the history
Callers should be aware of exceptions anyway
  • Loading branch information
blowfishpro committed Oct 1, 2017
1 parent f09622f commit ce43104
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions ModuleManager/MMPatchLoader.cs
Expand Up @@ -174,13 +174,23 @@ private IEnumerable<string> GenerateModList(IPatchProgress progress)
else
kspAssemblyVersion = mod.versionMajor + "." + mod.versionMinor;

string fileSha;
try
{
fileSha = FileSHA(mod.assembly.Location);
}
catch(Exception e)
{
progress.Exception("Exception while generating SHA for assembly " + assemblyName.Name, e);
}

modListInfo.AppendFormat(
format,
assemblyName.Name,
assemblyName.Version,
fileVersionInfo.FileVersion,
kspAssemblyVersion,
FileSHA(mod.assembly.Location)
fileSha
);

// modlist += String.Format(" {0,-50} SHA256 {1}\n", modInfo, FileSHA(mod.assembly.Location));
Expand Down Expand Up @@ -491,36 +501,26 @@ private void SaveModdedPhysics()
configs[0].config.Save(physicsPath);
}

private string FileSHA(string filename)
private static string FileSHA(string filename)
{
try
{
if (File.Exists(filename))
{
System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create();
if (!File.Exists(filename)) throw new FileNotFoundException("File does not exist", filename);

byte[] data = null;
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
{
data = sha.ComputeHash(fs);
}
System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create();

string hashedValue = string.Empty;
byte[] data = null;
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
{
data = sha.ComputeHash(fs);
}

foreach (byte b in data)
{
hashedValue += String.Format("{0,2:x2}", b);
}
string hashedValue = string.Empty;

return hashedValue;
}
}
catch (Exception e)
foreach (byte b in data)
{
logger.Exception("Exception hashing file " + filename, e);
return "0";
hashedValue += String.Format("{0,2:x2}", b);
}
return "0";

return hashedValue;
}

private void IsCacheUpToDate()
Expand Down

0 comments on commit ce43104

Please sign in to comment.