private async Task DownLoadRepository(string repPath) { repPath = $"{repPath}/archive/master.zip"; var request = WebRequest.CreateHttp(repPath); request.Method = "GET"; try { using (var response = await request.GetResponseAsync()) { using (var responseStream = response.GetResponseStream()) { using (var fileToDownload = new FileStream($@"{scriptDir}\{Path.GetFileName(repPath)}", FileMode.Create, FileAccess.ReadWrite)) { await responseStream.CopyToAsync(fileToDownload); } } } } catch (WebException ex) { throw ex; } var zipPath = $@"{scriptDir}\{Path.GetFileName(repPath)}"; if (File.Exists(zipPath)) { var directory = $"{repPath.Replace("http://github.com/", "").Split('/')[1]}-master"; using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { var result = from currEntry in archive.Entries where Path.GetDirectoryName(currEntry.FullName).Contains(directory) where !String.IsNullOrEmpty(currEntry.Name) select currEntry; foreach (ZipArchiveEntry entry in result) { var outPath = $@"{scriptDir}\{entry.FullName.Replace($"{directory}/", "")}"; if (outPath.EndsWith(".lua") && File.Exists(outPath.Replace(".lua", ".bak"))) { outPath = outPath.Replace(".lua", ".bak"); } var localScriptVersion = ScriptsData.FirstOrDefault(x => x.ScriptLocalPath == outPath.Replace(MainViewModel.AssemblyPath, "") && x.ScriptPath == null && x.RepositoryPath == null); var conflictResult = false; if (localScriptVersion != null) { ScriptConflictName = entry.Name; var dialogResult = await DialogHost.Show((this.View as UserControl).FindName("ScriptConflictContent"), "ScriptConflict", delegate(object sender, DialogClosingEventArgs args) { conflictResult = (bool)args.Parameter; }); } EnsureDirectoryExists(outPath); entry.ExtractToFile(outPath, true); if (Path.GetDirectoryName(outPath) == scriptDir) { var scriptConfig = ScriptsData.FirstOrDefault(x => x.ScriptName == Path.GetFileNameWithoutExtension(entry.Name)); if (scriptConfig != null) { if (conflictResult) { scriptConfig.RepositoryPath = LuaPath; scriptConfig.ScriptPath = null; } continue; } var newScriptData = new ScriptConfigModel { IsEnabled = outPath.EndsWith(".bak") ? false : true, ScriptName = Path.GetFileNameWithoutExtension(entry.Name), ScriptLocalPath = outPath.Trim('/').Replace(MainViewModel.AssemblyPath, ""), RepositoryPath = LuaPath }; ScriptsData.Add(newScriptData); } } } } }
private async Task DownLoadScript(string url, bool isEnabled) { var fileName = Path.GetFileName(url); if (!isEnabled) { fileName.Replace(".lua", ".bak"); } var request = WebRequest.CreateHttp(url); request.Method = "GET"; try { using (var response = await request.GetResponseAsync()) { using (var responseStream = response.GetResponseStream()) { if (File.Exists($@"{scriptDir}\{fileName}")) { var scriptConfig = ScriptsData.FirstOrDefault(x => x.ScriptLocalPath == $@"{scriptDir}\{fileName}".Replace(MainViewModel.AssemblyPath, "") && x.ScriptPath == null && x.RepositoryPath == null); var conflictResult = false; if (scriptConfig != null) { ScriptConflictName = fileName; var dialogResult = await DialogHost.Show((this.View as UserControl).FindName("ScriptConflictContent"), "ScriptConflict", delegate(object sender, DialogClosingEventArgs args) { conflictResult = (bool)args.Parameter; }); if (conflictResult) { scriptConfig.RepositoryPath = null; scriptConfig.ScriptPath = LuaPath; } else { return; } } } else { var newScriptData = new ScriptConfigModel { IsEnabled = isEnabled, ScriptName = Path.GetFileNameWithoutExtension(fileName), ScriptLocalPath = $@"{scriptDir}\{fileName}".Replace(MainViewModel.AssemblyPath, ""), ScriptPath = LuaPath }; ScriptsData.Add(newScriptData); } using (var fileToDownload = new FileStream($@"{scriptDir}\{fileName}", FileMode.Create, FileAccess.ReadWrite)) { await responseStream.CopyToAsync(fileToDownload); } } } } catch (Exception ex) { throw ex; } }
private void FileWatcherHandle(object sender, FileSystemEventArgs e) { try { if (!Regex.IsMatch(Path.GetExtension(e.FullPath), @"\.lua|\.bak", RegexOptions.IgnoreCase)) { return; } if (e.ChangeType == WatcherChangeTypes.Changed) { var scriptConfig = ScriptsData.FirstOrDefault(x => x.ScriptLocalPath == e.FullPath.Replace(MainViewModel.AssemblyPath, "") && x.ScriptName == Path.GetFileNameWithoutExtension(e.Name)); if (scriptConfig != null) { scriptConfig.ScriptPath = null; scriptConfig.RepositoryPath = null; UpdateScriptConfig(); return; } } if (e.ChangeType == WatcherChangeTypes.Renamed) { var args = e as RenamedEventArgs; var isEnabled = e.FullPath.EndsWith(".bak") ? false : true; var scriptConfig = ScriptsData.FirstOrDefault(x => x.ScriptLocalPath == args.OldFullPath.Replace(MainViewModel.AssemblyPath, "") && x.ScriptName == Path.GetFileNameWithoutExtension(args.OldName)); if (scriptConfig != null) { scriptConfig.IsEnabled = isEnabled; scriptConfig.ScriptLocalPath = args.FullPath.Replace(MainViewModel.AssemblyPath, ""); scriptConfig.ScriptName = Path.GetFileNameWithoutExtension(args.Name); UpdateScriptConfig(); } else { var newScriptConfig = new ScriptConfigModel { IsEnabled = isEnabled, ScriptLocalPath = e.FullPath.Replace(MainViewModel.AssemblyPath, ""), ScriptName = Path.GetFileNameWithoutExtension(e.Name) }; System.Windows.Application.Current.Dispatcher.Invoke(() => { ScriptsData.Add(newScriptConfig); }); UpdateScriptConfig(); } return; } if (e.ChangeType == WatcherChangeTypes.Created) { var isEnabled = e.FullPath.EndsWith(".bak") ? false : true; if (File.Exists(e.FullPath)) { var scriptConfig = ScriptsData.FirstOrDefault(x => x.ScriptLocalPath == e.FullPath.Replace(MainViewModel.AssemblyPath, "") && x.ScriptName == Path.GetFileNameWithoutExtension(e.Name)); if (scriptConfig != null) { scriptConfig.ScriptPath = null; scriptConfig.RepositoryPath = null; UpdateScriptConfig(); return; } } var newScriptConfig = new ScriptConfigModel { IsEnabled = isEnabled, ScriptLocalPath = e.FullPath.Replace(MainViewModel.AssemblyPath, ""), ScriptName = Path.GetFileNameWithoutExtension(e.Name) }; System.Windows.Application.Current.Dispatcher.Invoke(() => { ScriptsData.Add(newScriptConfig); }); UpdateScriptConfig(); return; } if (e.ChangeType == WatcherChangeTypes.Deleted) { var scriptConfig = ScriptsData.FirstOrDefault(x => x.ScriptLocalPath == e.FullPath.Replace(MainViewModel.AssemblyPath, "") && x.ScriptName == Path.GetFileNameWithoutExtension(e.Name)); if (scriptConfig != null) { System.Windows.Application.Current.Dispatcher.Invoke(() => { ScriptsData.Remove(scriptConfig); }); UpdateScriptConfig(); } } } catch (Exception ex) { DotaViewModel.GameBrowserVm.MainViewModel.ShowError(ex.Message); Log.Error(ex, "null"); } }