public void TestSerialization() { Stream originalDataStream = Helpers.GetResourceNamed("shortcuts.vdf"); byte[] originalData = new byte[originalDataStream.Length]; originalDataStream.Read(originalData, 0, (int)originalDataStream.Length); originalDataStream.Seek(0, SeekOrigin.Begin); var entries = VDFParser.VDFParser.Parse(originalDataStream); byte[] serializedData = VDFSerializer.Serialize(entries); Assert.AreEqual(originalData, serializedData); }
public static void WriteShortcuts(VDFEntry[] vdf, string vdfPath) { byte[] result = VDFSerializer.Serialize(vdf); try { File.WriteAllBytes(vdfPath, result); } catch (Exception e) { throw e; } }
public static void WriteShortcuts(VDFEntry[] vdf, string userPath) { byte[] result = VDFSerializer.Serialize(vdf); try { File.WriteAllBytes(userPath + "\\config\\shortcuts.vdf", result); } catch (Exception e) { throw e; } }
public static void ClearAllShortcuts() { Debug.WriteLine("DBG: Clearing all elements in shortcuts.vdf"); string[] tags = Settings.Default.Tags.Split(','); string steam_folder = SteamManager.GetSteamFolder(); if (Directory.Exists(steam_folder)) { var users = SteamManager.GetUsers(steam_folder); var exePath = @"""" + System.Reflection.Assembly.GetExecutingAssembly().Location + @""""; var exeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); foreach (var user in users) { try { VDFEntry[] shortcuts = new VDFEntry[0]; try { if (!Directory.Exists(user + @"\\config\\")) { Directory.CreateDirectory(user + @"\\config\\"); } //Write the file with all the shortcuts File.WriteAllBytes(user + @"\\config\\shortcuts.vdf", VDFSerializer.Serialize(shortcuts)); } catch (Exception ex) { throw new Exception("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); } } catch (Exception ex) { MessageBox.Show("Error: Program failed while trying to clear your Steam shortcuts:" + Environment.NewLine + ex.Message + ex.StackTrace); } } MessageBox.Show("All non-Steam shortcuts has been cleared."); } }
private void BwrSave_DoWork(object sender, DoWorkEventArgs e) { string steam_folder = SteamManager.GetSteamFolder(); if (Directory.Exists(steam_folder)) { var users = SteamManager.GetUsers(steam_folder); var selected_apps = Apps.Entries.Where(app => app.Selected); //To make things faster, decide icons before looping users foreach (var app in selected_apps) { app.Icon = app.widestSquareIcon(); } foreach (var user in users) { try { VDFEntry[] shortcuts = new VDFEntry[0]; try { shortcuts = SteamManager.ReadShortcuts(user); } catch (Exception ex) { //If it's a short VDF, let's just overwrite it if (ex.GetType() != typeof(VDFTooShortException)) { throw new Exception("Error: Program failed to load existing Steam shortcuts." + Environment.NewLine + ex.Message); } } if (shortcuts != null) { var exePath = @"""" + System.Reflection.Assembly.GetExecutingAssembly().Location + @""""; var exeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); foreach (var app in selected_apps) { VDFEntry newApp = new VDFEntry() { AppName = app.Name, Exe = exePath, StartDir = exeDir, LaunchOptions = app.Aumid, AllowDesktopConfig = 1, AllowOverlay = 1, Icon = app.Icon, Index = shortcuts.Length, IsHidden = 0, OpenVR = 0, ShortcutPath = "", Tags = new string[0], Devkit = 0, DevkitGameID = "", LastPlayTime = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds(), }; //Resize this array so it fits the new entries Array.Resize(ref shortcuts, shortcuts.Length + 1); shortcuts[shortcuts.Length - 1] = newApp; } try { if (!Directory.Exists(user + @"\\config\\")) { Directory.CreateDirectory(user + @"\\config\\"); } //Write the file with all the shortcuts File.WriteAllBytes(user + @"\\config\\shortcuts.vdf", VDFSerializer.Serialize(shortcuts)); } catch (Exception ex) { throw new Exception("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); } } } catch (Exception ex) { MessageBox.Show("Error: Program failed exporting your games:" + Environment.NewLine + ex.Message + ex.StackTrace); } } } }
private async Task ExportGames() { string steam_folder = SteamManager.GetSteamFolder(); if (Directory.Exists(steam_folder)) { var users = SteamManager.GetUsers(steam_folder); var selected_apps = Apps.Entries.Where(app => app.Selected); var exePath = @"""" + System.Reflection.Assembly.GetExecutingAssembly().Location + @""""; var exeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); List <Task> gridImagesDownloadTasks = new List <Task>(); bool downloadGridImages = !String.IsNullOrEmpty(Properties.Settings.Default.SteamGridDbApiKey); //To make things faster, decide icons and download grid images before looping users foreach (var app in selected_apps) { app.Icon = app.widestSquareIcon(); if (downloadGridImages) { gridImagesDownloadTasks.Add(DownloadTempGridImages(app.Name, exePath)); } } foreach (var user in users) { try { VDFEntry[] shortcuts = new VDFEntry[0]; try { shortcuts = SteamManager.ReadShortcuts(user); } catch (Exception ex) { //If it's a short VDF, let's just overwrite it if (ex.GetType() != typeof(VDFTooShortException)) { throw new Exception("Error: Program failed to load existing Steam shortcuts." + Environment.NewLine + ex.Message); } } if (shortcuts != null) { foreach (var app in selected_apps) { VDFEntry newApp = new VDFEntry() { AppName = app.Name, Exe = exePath, StartDir = exeDir, LaunchOptions = app.Aumid, AllowDesktopConfig = 1, AllowOverlay = 1, Icon = app.Icon, Index = shortcuts.Length, IsHidden = 0, OpenVR = 0, ShortcutPath = "", Tags = new string[2] { "XBOX", "READY TO PLAY" }, Devkit = 0, DevkitGameID = "", LastPlayTime = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds(), }; //Resize this array so it fits the new entries Array.Resize(ref shortcuts, shortcuts.Length + 1); shortcuts[shortcuts.Length - 1] = newApp; } try { if (!Directory.Exists(user + @"\\config\\")) { Directory.CreateDirectory(user + @"\\config\\"); } //Write the file with all the shortcuts File.WriteAllBytes(user + @"\\config\\shortcuts.vdf", VDFSerializer.Serialize(shortcuts)); } catch (Exception ex) { throw new Exception("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); } } } catch (Exception ex) { MessageBox.Show("Error: Program failed exporting your games:" + Environment.NewLine + ex.Message + ex.StackTrace); } } if (gridImagesDownloadTasks.Count > 0) { await Task.WhenAll(gridImagesDownloadTasks); await Task.Run(() => { foreach (var user in users) { CopyTempGridImagesToSteamUser(user); } RemoveTempGridImages(); }); } } }
/// <summary> /// Main Task to export the selected games to steam /// </summary> /// <param name="restartSteam"></param> /// <returns></returns> private async Task <bool> ExportGames(bool restartSteam) { string[] tags = Settings.Default.Tags.Split(','); string steam_folder = SteamManager.GetSteamFolder(); if (Directory.Exists(steam_folder)) { var users = SteamManager.GetUsers(steam_folder); var selected_apps = Apps.Entries.Where(app => app.Selected); var exePath = @"""" + System.Reflection.Assembly.GetExecutingAssembly().Location + @""""; var exeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); List <Task> gridImagesDownloadTasks = new List <Task>(); bool downloadGridImages = !String.IsNullOrEmpty(Properties.Settings.Default.SteamGridDbApiKey); //To make things faster, decide icons and download grid images before looping users Debug.WriteLine("downloadGridImages: " + (downloadGridImages)); foreach (var app in selected_apps) { app.Icon = app.widestSquareIcon(); if (downloadGridImages) { Debug.WriteLine("Downloading grid images for app " + app.Name); gridImagesDownloadTasks.Add(DownloadTempGridImages(app.Name, exePath)); } } // Export the selected apps and the downloaded images to each user // in the steam folder by modifying it's VDF file foreach (var user in users) { try { VDFEntry[] shortcuts = new VDFEntry[0]; try { shortcuts = SteamManager.ReadShortcuts(user); } catch (Exception ex) { //If it's a short VDF, let's just overwrite it if (ex.GetType() != typeof(VDFTooShortException)) { throw new Exception("Error: Program failed to load existing Steam shortcuts." + Environment.NewLine + ex.Message); } } if (shortcuts != null) { foreach (var app in selected_apps) { VDFEntry newApp = new VDFEntry() { AppName = app.Name, Exe = exePath, StartDir = exeDir, LaunchOptions = app.Aumid, AllowDesktopConfig = 1, AllowOverlay = 1, Icon = app.Icon, Index = shortcuts.Length, IsHidden = 0, OpenVR = 0, ShortcutPath = "", Tags = tags, Devkit = 0, DevkitGameID = "", LastPlayTime = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds(), }; Boolean isFound = false; for (int i = 0; i < shortcuts.Length; i++) { Debug.WriteLine(shortcuts[i].ToString()); if (shortcuts[i].AppName == app.Name) { isFound = true; Debug.WriteLine(app.Name + " already added to Steam. Updating existing shortcut."); shortcuts[i] = newApp; } } if (!isFound) { //Resize this array so it fits the new entries Array.Resize(ref shortcuts, shortcuts.Length + 1); shortcuts[shortcuts.Length - 1] = newApp; } } try { if (!Directory.Exists(user + @"\\config\\")) { Directory.CreateDirectory(user + @"\\config\\"); } //Write the file with all the shortcuts File.WriteAllBytes(user + @"\\config\\shortcuts.vdf", VDFSerializer.Serialize(shortcuts)); } catch (Exception ex) { throw new Exception("Error: Program failed while trying to write your Steam shortcuts" + Environment.NewLine + ex.Message); } } } catch (Exception ex) { MessageBox.Show("Error: Program failed exporting your games:" + Environment.NewLine + ex.Message + ex.StackTrace); } } if (gridImagesDownloadTasks.Count > 0) { await Task.WhenAll(gridImagesDownloadTasks); await Task.Run(() => { foreach (var user in users) { CopyTempGridImagesToSteamUser(user); } RemoveTempGridImages(); }); } } if (restartSteam) { Func <Process> getSteam = () => Process.GetProcessesByName("steam").SingleOrDefault(); Process steam = getSteam(); if (steam != null) { string steamExe = steam.MainModule.FileName; //we always ask politely Debug.WriteLine("Requesting Steam shutdown"); Process.Start(steamExe, "-exitsteam"); bool restarted = false; Stopwatch watch = new Stopwatch(); watch.Start(); //give it N seconds to sort itself out int waitSeconds = 8; while (watch.Elapsed.TotalSeconds < waitSeconds) { Thread.Sleep(TimeSpan.FromSeconds(0.5f)); if (getSteam() == null) { Debug.WriteLine("Restarting Steam"); Process.Start(steamExe); restarted = true; break; } } if (!restarted) { Debug.WriteLine("Steam instance not restarted"); MessageBox.Show("Failed to restart Steam, please launch it manually", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); return(false); } } else { Debug.WriteLine("Steam instance not found to be restarted"); } } return(true); }