bool IsValidSourcePath(string path, ModContent.ModSource source) { try { foreach (var kv in source.IDFiles) { var filePath = Path.Combine(path, kv.Key); if (!File.Exists(filePath)) { return(false); } using (var fileStream = File.OpenRead(filePath)) using (var csp = SHA1.Create()) { var hash = new string(csp.ComputeHash(fileStream).SelectMany(a => a.ToString("x2")).ToArray()); if (hash != kv.Value) { return(false); } } } } catch (Exception) { return(false); } return(true); }
bool IsValidSourcePath(string path, ModContent.ModSource source) { try { foreach (var kv in source.IDFiles) { var filePath = Path.Combine(path, kv.Key); if (!File.Exists(filePath)) { return(false); } using (var fileStream = File.OpenRead(filePath)) if (CryptoUtil.SHA1Hash(fileStream) != kv.Value) { return(false); } } } catch (Exception) { return(false); } return(true); }
string FindSourcePath(ModContent.ModSource source, IEnumerable <string> volumes) { if (source.Type == ModContent.SourceType.RegistryDirectory || source.Type == ModContent.SourceType.RegistryDirectoryFromFile) { if (source.RegistryKey == null) { return(null); } if (Platform.CurrentPlatform != PlatformType.Windows) { return(null); } // We need an extra check for the platform here to silence a warning when the registry is accessed // TODO: Remove this once our platform checks use the same method if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return(null); } foreach (var prefix in source.RegistryPrefixes) { var path = Microsoft.Win32.Registry.GetValue(prefix + source.RegistryKey, source.RegistryValue, null) as string; if (path == null) { continue; } if (source.Type == ModContent.SourceType.RegistryDirectoryFromFile) { path = Path.GetDirectoryName(path); } return(IsValidSourcePath(path, source) ? path : null); } return(null); } if (source.Type == ModContent.SourceType.Disc) { foreach (var volume in volumes) { if (IsValidSourcePath(volume, source)) { return(volume); } } } return(null); }
bool IsValidSourcePath(string path, ModContent.ModSource source) { try { foreach (var kv in source.IDFiles.Nodes) { var filePath = Path.Combine(path, kv.Key); if (!File.Exists(filePath)) { return(false); } using (var fileStream = File.OpenRead(filePath)) { var offsetNode = kv.Value.Nodes.FirstOrDefault(n => n.Key == "Offset"); var lengthNode = kv.Value.Nodes.FirstOrDefault(n => n.Key == "Length"); if (offsetNode != null || lengthNode != null) { var offset = 0L; if (offsetNode != null) { offset = FieldLoader.GetValue <long>("Offset", offsetNode.Value.Value); } var length = fileStream.Length - offset; if (lengthNode != null) { length = FieldLoader.GetValue <long>("Length", lengthNode.Value.Value); } fileStream.Position = offset; var data = fileStream.ReadBytes((int)length); if (CryptoUtil.SHA1Hash(data) != kv.Value.Value) { return(false); } } else if (CryptoUtil.SHA1Hash(fileStream) != kv.Value.Value) { return(false); } } } } catch (Exception) { return(false); } return(true); }
string FindSourcePath(ModContent.ModSource source, IEnumerable <string> volumes) { if (source.Type == ModContent.SourceType.RegistryDirectory || source.Type == ModContent.SourceType.RegistryDirectoryFromFile) { if (source.RegistryKey == null) { return(null); } if (Platform.CurrentPlatform != PlatformType.Windows) { return(null); } foreach (var prefix in source.RegistryPrefixes) { var path = Microsoft.Win32.Registry.GetValue(prefix + source.RegistryKey, source.RegistryValue, null) as string; if (path == null) { continue; } if (source.Type == ModContent.SourceType.RegistryDirectoryFromFile) { path = Path.GetDirectoryName(path); } return(IsValidSourcePath(path, source) ? path : null); } return(null); } if (source.Type == ModContent.SourceType.Disc) { foreach (var volume in volumes) { if (IsValidSourcePath(volume, source)) { return(volume); } } } return(null); }
string FindSourcePath(ModContent.ModSource source, IEnumerable <string> volumes) { if (source.Type == ModContent.SourceType.Install) { if (source.RegistryKey == null) { return(null); } if (Platform.CurrentPlatform != PlatformType.Windows) { return(null); } var path = Microsoft.Win32.Registry.GetValue(source.RegistryKey, source.RegistryValue, null) as string; if (path == null) { return(null); } return(IsValidSourcePath(path, source) ? path : null); } if (source.Type == ModContent.SourceType.Disc) { foreach (var volume in volumes) { if (IsValidSourcePath(volume, source)) { return(volume); } } } return(null); }
void InstallFromDisc(string path, ModContent.ModSource modSource) { var message = ""; ShowProgressbar("Installing Content", () => message); ShowDisabledCancel(); new Task(() => { var extracted = new List <string>(); try { foreach (var i in modSource.Install) { switch (i.Key) { case "copy": { var sourceDir = Path.Combine(path, i.Value.Value); foreach (var node in i.Value.Nodes) { var sourcePath = Path.Combine(sourceDir, node.Value.Value); var targetPath = Platform.ResolvePath(node.Key); if (File.Exists(targetPath)) { Log.Write("install", "Ignoring installed file " + targetPath); continue; } Log.Write("install", "Copying {0} -> {1}".F(sourcePath, targetPath)); extracted.Add(targetPath); Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); using (var source = File.OpenRead(sourcePath)) using (var target = File.OpenWrite(targetPath)) { var displayFilename = Path.GetFileName(targetPath); var length = source.Length; Action <long> onProgress = null; if (length < ShowPercentageThreshold) { message = "Copying " + displayFilename; } else { onProgress = b => message = "Copying " + displayFilename + " ({0}%)".F(100 * b / length); } CopyStream(source, target, length, onProgress); } } break; } case "extract-raw": { ExtractFromPackage(ExtractionType.Raw, path, i.Value, extracted, m => message = m); break; } case "extract-blast": { ExtractFromPackage(ExtractionType.Blast, path, i.Value, extracted, m => message = m); break; } case "extract-mscab": { ExtractFromMSCab(path, i.Value, extracted, m => message = m); break; } case "extract-iscab": { ExtractFromISCab(path, i.Value, extracted, m => message = m); break; } case "delete": { var sourcePath = Path.Combine(path, i.Value.Value); // Try as an absolute path if (!File.Exists(sourcePath)) { sourcePath = Platform.ResolvePath(i.Value.Value); } Log.Write("debug", "Deleting {0}", sourcePath); File.Delete(sourcePath); break; } default: Log.Write("debug", "Unknown installation command {0} - ignoring", i.Key); break; } } Game.RunAfterTick(Ui.CloseWindow); } catch (Exception e) { Log.Write("install", e.ToString()); foreach (var f in extracted) { Log.Write("install", "Deleting " + f); File.Delete(f); } Game.RunAfterTick(() => { ShowMessage("Installation Failed", "Refer to install.log in the logs directory for details."); ShowBackRetry(() => InstallFromDisc(path, modSource)); }); } }).Start(); }