public SaveToFile ( string path, bool asBinary ) : void | ||
path | string | The file path to save to. |
asBinary | bool | If set to |
return | void |
public void KeyValueBinarySerialization_LoadAsBinaryPreservesBuggyBehavior() { var kv = new KeyValue( "MessageObject" ); kv.Children.Add( new KeyValue( "key", "value" ) ); KeyValue deserializedKv; var temporaryFile = Path.GetTempFileName(); try { kv.SaveToFile( temporaryFile, asBinary: true ); deserializedKv = KeyValue.LoadAsBinary( temporaryFile ); } finally { File.Delete( temporaryFile ); } Assert.Null( deserializedKv.Name ); Assert.Equal(1, deserializedKv.Children.Count ); var actualKv = deserializedKv.Children[ 0 ]; Assert.Equal( kv.Name, actualKv.Name ); Assert.Equal( kv.Children.Count, actualKv.Children.Count ); for ( int i = 0; i < kv.Children.Count; i++ ) { var originalChild = kv.Children[ i ]; var deserializedChild = actualKv.Children[ i ]; Assert.Equal( originalChild.Name, deserializedChild.Name ); Assert.Equal( originalChild.Value, deserializedChild.Value ); } }
public void KeyValueBinarySerializationIsSymmetric() { var kv = new KeyValue( "MessageObject" ); kv.Children.Add( new KeyValue( "key", "value" ) ); KeyValue deserializedKv; var temporaryFile = Path.GetTempFileName(); try { kv.SaveToFile( temporaryFile, asBinary: true ); var loaded = KeyValue.TryLoadAsBinary( temporaryFile, out deserializedKv ); Assert.True( loaded ); } finally { File.Delete( temporaryFile ); } Assert.Equal( kv.Name, deserializedKv.Name ); Assert.Equal( kv.Children.Count, deserializedKv.Children.Count ); for ( int i = 0; i < kv.Children.Count; i++ ) { var originalChild = kv.Children[ i ]; var deserializedChild = deserializedKv.Children[ i ]; Assert.Equal( originalChild.Name, deserializedChild.Name ); Assert.Equal( originalChild.Value, deserializedChild.Value ); } }
public void KeyValuesSavesTextToFile() { var expected = "\"RootNode\"\n{\n\t\"key1\"\t\t\"value1\"\n\t\"key2\"\n\t{\n\t\t\"ChildKey\"\t\t\"ChildValue\"\n\t}\n}\n"; var kv = new KeyValue( "RootNode" ) { Children = { new KeyValue( "key1", "value1" ), new KeyValue( "key2" ) { Children = { new KeyValue( "ChildKey", "ChildValue" ) } } } }; string text; var temporaryFile = Path.GetTempFileName(); try { kv.SaveToFile( temporaryFile, asBinary: false ); text = File.ReadAllText( temporaryFile ); } finally { File.Delete( temporaryFile ); } Assert.Equal( expected, text ); }
public void KeyValuesWritesBinary() { var expectedHexValue = "00525000017374617475730023444F54415F52505F424F54505241435449434500016E756D5F706172616D730030000" + "17761746368696E675F736572766572005B413A313A323130383933353136393A353431325D00017761746368696E675F66726F6D5F73" + "6572766572005B413A313A3836343436383939343A353431325D000808"; var kv = new KeyValue( "RP" ); kv.Children.Add( new KeyValue( "status", "#DOTA_RP_BOTPRACTICE" ) ); kv.Children.Add( new KeyValue( "num_params", "0" ) ); kv.Children.Add( new KeyValue( "watching_server", "[A:1:2108935169:5412]" ) ); kv.Children.Add( new KeyValue( "watching_from_server", "[A:1:864468994:5412]" ) ); string tempFileName = null; try { tempFileName = Path.GetTempFileName(); kv.SaveToFile( tempFileName, asBinary: true ); var binaryValue = File.ReadAllBytes( tempFileName ); var hexValue = BitConverter.ToString( binaryValue ).Replace( "-", "" ); Assert.Equal( expectedHexValue, hexValue ); } finally { if ( tempFileName != null && File.Exists( tempFileName ) ) { File.Delete( tempFileName ); } } }
public void DownloadAndInstall(int revision, HashWebClient.RemoteFileInfo archiveInfo, bool steamBeta, string steamBuild, UpdatesView view) { if (steamBeta) { const int appId = 33930; string gameName = "Arma 2: Operation Arrowhead Beta"; DirectoryInfo armaPath = null; try { armaPath = new DirectoryInfo(CalculatedGameSettings.Current.Arma2OAPath); } catch (ArgumentException aex) { bool overridenPath = string.IsNullOrWhiteSpace(UserSettings.Current.GameOptions.Arma2OADirectoryOverride); Execute.OnUiThreadSync(() => { var popup = new InfoPopup("Invalid path", MainWindow.GetWindow(view)); popup.Headline.Content = "Game could not be found"; popup.SetMessage(overridenPath ? "Invalid game override path, please enter a new game path or remove it" : "Game could not located via the registry, please enter an override path"); popup.Show(); }, null, DispatcherPriority.Input); return; } for (armaPath = armaPath.Parent; armaPath != null; armaPath = armaPath.Parent) { if (armaPath.Name.Equals("steamapps", StringComparison.OrdinalIgnoreCase)) { string manifestName = "appmanifest_" + appId.ToString() + ".acf"; string fullManifestPath = Path.Combine(armaPath.FullName, manifestName); if (File.Exists(fullManifestPath)) { // Kill Steam so we can edit the game configuration. Process[] processes = Process.GetProcessesByName("Steam"); foreach (Process process in processes) { // #YOLO try { process.Kill(); process.WaitForExit(); } catch { MessageBox.Show("Unable to shut down steam to start patching.", "Patch error", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } } var acfKeys = new KeyValue(); var reader = new StreamReader(fullManifestPath); var acfReader = new KVTextReader(acfKeys, reader.BaseStream); reader.Close(); KeyValue currentBuild = acfKeys.Children.FirstOrDefault(k => k.Name == "buildid"); if (!String.IsNullOrEmpty(currentBuild.Value)) { if (Equals(currentBuild.Value, steamBuild)) { Execute.OnUiThreadSync(() => { var popup = new InfoPopup("User intervention required", MainWindow.GetWindow(view)); popup.Headline.Content = "Game update using Steam"; popup.SetMessage(gameName + " might be corrupted.\n" + "Please validate your client files manually.\n" + "Or by clicking on the following link:"); popup.SetLink("steam://validate/" + appId.ToString() + "/", "Update " + gameName); popup.Closed += (sender, args) => view.CheckForUpdates(); popup.Show(); }, null, DispatcherPriority.Input); } else { KeyValue gameState = acfKeys.Children.FirstOrDefault(k => k.Name == "StateFlags"); if (!String.IsNullOrEmpty(gameState.Value)) { currentBuild.Value = steamBuild; gameState.Value = "2"; acfKeys.SaveToFile(fullManifestPath, false); Thread.Sleep(1000); Execute.OnUiThreadSync(() => { var popup = new InfoPopup("User intervention required", MainWindow.GetWindow(view)); popup.Headline.Content = "Game update using Steam"; popup.SetMessage(gameName + " branch switched to BETA.\n" + "Please restart Steam to download update."); popup.Closed += (sender, args) => view.CheckForUpdates(); popup.Show(); }, null, DispatcherPriority.Input); } } } else { MessageBox.Show("Patching failed, '" + gameName + "' is not located inside a SteamLibrary folder.", "Patch error", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } return; } else { Execute.OnUiThreadSync(() => { var popup = new InfoPopup("User intervention required", MainWindow.GetWindow(view)); popup.Headline.Content = "Game update using Steam"; popup.SetMessage(gameName + " is not installed.\n" + "Please install it from the Library tab.\n" + "Or by clicking on the following link:"); popup.SetLink("steam://install/" + appId.ToString() + "/", "Install " + gameName); popup.Closed += (sender, args) => view.CheckForUpdates(); popup.Show(); }, null, DispatcherPriority.Input); return; } } } if (armaPath == null) { MessageBox.Show("Patching failed, '" + gameName + "' is not located inside a SteamLibrary folder.", "Patch error", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } } else { DownloadAndInstall(revision, archiveInfo); } }