public static SimulationConfiguration LoadConfigOrDefault() { if (File.Exists("Simulation.cfg")) { string sourceText = File.ReadAllText("Simulation.cfg"); Deserializer yamlDeserializer = new Deserializer(); SimulationConfiguration deserialized = yamlDeserializer.Deserialize <SimulationConfiguration>(sourceText); CheckLoadedConfig(deserialized); // Write config with updated values SerializerBuilder builder = new SerializerBuilder(); builder.EmitDefaults(); Serializer yamlSerializer = builder.Build(); string serialized = yamlSerializer.Serialize(deserialized); File.WriteAllText("Simulation.cfg", serialized); return(deserialized); } else { // Write out default config if there is no config for easier adjustment SerializerBuilder builder = new SerializerBuilder(); builder.EmitDefaults(); Serializer yamlSerializer = builder.Build(); string serialized = yamlSerializer.Serialize(DefaultConfig); File.WriteAllText("Simulation.cfg", serialized); return(DefaultConfig); } }
public static GameConfig LoadConfigOrDefault() { if (File.Exists("Config.cfg")) { string sourceText = File.ReadAllText("Config.cfg"); Deserializer yamlDeserializer = new Deserializer(); GameConfig deserialized = yamlDeserializer.Deserialize <GameConfig>(sourceText); CheckLoadedConfig(deserialized); SerializerBuilder builder = new SerializerBuilder(); builder.EmitDefaults(); Serializer yamlSerializer = builder.Build(); string serialized = yamlSerializer.Serialize(deserialized); File.WriteAllText("Config.cfg", serialized); return(deserialized); } else { // Write out default config if there is no config for easier adjustment SerializerBuilder builder = new SerializerBuilder(); builder.EmitDefaults(); Serializer yamlSerializer = builder.Build(); string serialized = yamlSerializer.Serialize(DefaultConfig); File.WriteAllText("Config.cfg", serialized); return(DefaultConfig); } }
public void SerializationIncludesKeyFromAnonymousTypeWhenEmittingDefaults() { var writer = new StringWriter(); var obj = new { MyString = (string)null }; SerializerBuilder.EmitDefaults().Build().Serialize(writer, obj, obj.GetType()); writer.ToString().Should().Contain("MyString"); }
public static void DataToYaml(TextWriter writer, IEnumerable <Dictionary <string, object> > datatable, YamlColumnNamesType yamlColumnNames = null) { var builder = new SerializerBuilder(); builder.EmitDefaults(); var serializer = builder.Build(); var tree = yamlColumnNames == null ? datatable : ConvertDataTableWithYamlColumns(datatable, yamlColumnNames); serializer.Serialize(writer, tree); }
public void SerializationOfNullInListsAreAlwaysEmittedWhenUsingEmitDefaults() { var writer = new StringWriter(); var obj = new[] { "foo", null, "bar" }; SerializerBuilder.EmitDefaults().Build().Serialize(writer, obj); var serialized = writer.ToString(); Regex.Matches(serialized, "-").Count.Should().Be(3, "there should have been 3 elements"); }
public static string Serialize(object obj) { SerializerBuilder sb = new SerializerBuilder(); sb.EmitDefaults(); Serializer s = sb.Build(); StringWriter w = new StringWriter(); s.Serialize(w, obj, obj.GetType()); return(w.ToString()); }
/// <summary> /// Serialize the configuration to a YAML string. /// </summary> public override string ToString() { var builder = new SerializerBuilder(); builder.EmitDefaults(); // Force even default values to be written, like 0, false. var serializer = builder.Build(); var strWriter = new StringWriter(); serializer.Serialize(strWriter, this); return(strWriter.ToString()); }
public void SerializationOfDefaultsWorkInJson() { var writer = new StringWriter(); var obj = new Example { MyString = null }; SerializerBuilder.EmitDefaults().JsonCompatible().Build().Serialize(writer, obj, typeof(Example)); writer.ToString().Should().Contain("MyString"); }
public void SerializationIncludesKeyWhenEmittingDefaults() { var writer = new StringWriter(); var obj = new Example { MyString = null }; SerializerBuilder.EmitDefaults().Build().Serialize(writer, obj, typeof(Example)); writer.ToString().Should().Contain("MyString"); }
public void SerializationEmitsPropertyWhenUsingEmitDefaultsAndDefaultValueAttribute() { var writer = new StringWriter(); var obj = new DefaultsExample { Value = DefaultsExample.DefaultValue }; SerializerBuilder.EmitDefaults().Build().Serialize(writer, obj); var serialized = writer.ToString(); serialized.Should().Contain("Value"); }
public static bool Serialize(this object obj, string file) { try { using (StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite), Encoding.UTF8)) { SerializerBuilder serializer = new SerializerBuilder(); serializer.EmitDefaults().Build().Serialize(writer, obj); return(true); } } catch (Exception) { return(false); } }
public static void Serialize(TextWriter tw, object data, bool serializeAsJson = false, bool emitDefaultValues = false) { Serializer serializer = null; SerializerBuilder builder = new SerializerBuilder(); if (serializeAsJson) { builder.JsonCompatible(); } if (emitDefaultValues) { builder.EmitDefaults(); } serializer = builder.Build(); serializer.Serialize(tw, data); }
public static void Serialize(TextWriter tw, object data, bool serializeAsJson = false, bool emitDefaultValues = false, IYamlTypeConverter converter = null) { Serializer serializer = null; SerializerBuilder builder = new SerializerBuilder(); if (serializeAsJson) { builder.JsonCompatible(); } if (emitDefaultValues) { builder.EmitDefaults(); } if (converter != null) { builder.WithTypeConverter(converter); } serializer = builder.Build() as Serializer; serializer.Serialize(tw, data); }
private void createButton_Click(object sender, RoutedEventArgs e) { var filename = gunName.Text.ToLower() + ".yaml"; if (!File.Exists(filename)) { File.CreateText(filename).Close(); } var gun = new Gun(); gun.GunName = gunName.Text; gun.GunSpriteName = spriteName.Text; gun.ShortDescription = shortDesc.Text; gun.LongDescription = longDesc.Text; gun.GetProjectileFrom = getProjectile.Text; gun.NumberOfShots = int.Parse(numberOfShots.Text); gun.ArmorToGainOnPickup = int.Parse(armorToGainOnPickup.Text); gun.ReflectBulletsDuringReload = reflectBullets.IsChecked ?? false; gun.DirectionlessScreenShake = directionlessScreenShake.IsChecked ?? false; gun.ClearsCooldownsLikeAWP = clearsCooldownsLikeAwp.IsChecked ?? false; gun.AppliesHoming = hasHoming.IsChecked ?? false; gun.AppliedHomingAngularVelocity = float.Parse(homingVelocity.Text); gun.AppliedHomingDetectRadius = float.Parse(homingRange.Text); gun.CanSneakAttack = canSneakAttack.IsChecked ?? false; gun.SneakAttackDamageMultiplier = float.Parse(sneakAttackDamageMultiplier.Text); gun.RequiresFundsToShoot = usesMoney.IsChecked ?? false; gun.CurrencyCostPerShot = int.Parse(moneyPerShot.Text); gun.LocalActiveReload = localActiveReload.IsChecked ?? false; gun.UsesRechargeLikeActiveItem = usesActiveItemRecharge.IsChecked ?? false; gun.ActiveItemStyleRechargeAmount = float.Parse(activeItemStyleRechargeAmount.Text); gun.CanCriticalFire = canCriticalFire.IsChecked ?? false; gun.CriticalChance = float.Parse(criticalChance.Text); gun.CriticalDamageMultiplier = float.Parse(criticalDamage.Text); gun.GainsRateOfFireAsContinueAttack = gainsRateOfFireAsContinueAttack.IsChecked ?? false; gun.RateOfFireMultiplierAdditionPerSecond = float.Parse(rateOfFireMultiplier.Text); gun.Quality = ConvertToQuality(qualityBox.SelectedIndex); gun.CanBeDropped = canBeDropped.IsChecked ?? false; gun.PersistsOnDeath = persistsOnDeath.IsChecked ?? false; gun.RespawnsIfPitfall = respawnsIfPitfall.IsChecked ?? false; gun.UsesCustomCost = customShopPriceCheck.IsChecked ?? false; gun.CustomCost = int.Parse(customShopPrice.Text); gun.PersistsOnPurchase = persistsOnPurchase.IsChecked ?? false; gun.CanBeSold = canBeSold.IsChecked ?? false; gun.MaxAmmo = int.Parse(maxAmmo.Text); gun.AdditionalClipCapacity = int.Parse(additionalClipCapacity.Text); gun.InfiniteAmmo = infiniteAmmo.IsChecked ?? false; gun.UsesBossDamageModifier = usesBossDamageModifier.IsChecked ?? false; gun.CustomBossDamageModifier = float.Parse(bossDamageModifier.Text); gun.CanGainAmmo = canGainAmmo.IsChecked ?? false; gun.BlankDamageScalingOnEmptyClip = float.Parse(blankDamageMultiplier.Text); gun.BlankDamageToEnemies = float.Parse(blankDamage.Text); gun.BlankDuringReload = blankDuringReload.IsChecked ?? false; gun.BlankKnockbackPower = float.Parse(blankKnockbackPower.Text); gun.BlankReloadRadius = float.Parse(blankRadius.Text); gun.DoesScreenShake = usesScreenShake.IsChecked ?? false; gun.ScreenShakeMagnitude = float.Parse(screenShakeMagnitude.Text); gun.ScreenShakeSpeed = float.Parse(screenShakeSpeed.Text); gun.ScreenShakeTime = float.Parse(screenShakeTime.Text); gun.ScreenShakeFalloff = float.Parse(screenShakeFalloff.Text); gun.ReloadTime = float.Parse(reloadTime.Text); gun.ClipCapacity = int.Parse(shotsInClip.Text); gun.AngleVariance = float.Parse(angleVariance.Text); gun.AmmoCost = int.Parse(ammoCost.Text); gun.ShootStyle = shootStyleBox.SelectedIndex; gun.BurstCooldownTime = float.Parse(burstCooldownTime.Text); gun.BurstShotCount = int.Parse(burstShotCount.Text); gun.ChargedGunChargeTime = float.Parse(chargeTime.Text); gun.UsesShotgunStyleVelocityRandomizer = usesShotgunStyleVelocityRandomizer.IsChecked ?? false; gun.DecreaseFinalSpeedPercentMin = float.Parse(decreaseFinalSpeedPercentMin.Text); gun.IncreaseFinalSpeedPercentMax = float.Parse(increaseFinalSpeedPercentMax.Text); gun.UsesCustomSprite = usesCustomSprites.IsChecked ?? false; gun.DefaultSprite = !string.IsNullOrWhiteSpace(spriteDir.Text) ? spriteDir.Text.Substring(spriteDir.Text.LastIndexOf("\\") + 1, spriteDir.Text.LastIndexOf(".") - spriteDir.Text.LastIndexOf("\\") - 1) : null; gun.SpriteFPS = int.Parse(spriteFps.Text); gun.ProjectileScaleMultiplier = float.Parse(projectileScaleMultiplier.Text); gun.AppliesStun = stunsEnemies.IsChecked ?? false; gun.StunChance = float.Parse(stunChance.Text); gun.StunDuration = float.Parse(stunDuration.Text); gun.AppliesBleed = bleedsEnemies.IsChecked ?? false; gun.BleedChance = float.Parse(bleedChance.Text); gun.BleedDuration = float.Parse(bleedDuration.Text); gun.AppliesCharm = charmsEnemies.IsChecked ?? false; gun.CharmChance = float.Parse(charmChance.Text); gun.CharmDuration = float.Parse(charmDuration.Text); gun.AppliesFire = firesEnemies.IsChecked ?? false; gun.FireChance = float.Parse(fireChance.Text); gun.FireDuration = float.Parse(fireDuration.Text); gun.AppliesFreeze = freezesEnemies.IsChecked ?? false; gun.FreezeChance = float.Parse(freezeChance.Text); gun.FreezeDuration = float.Parse(freezeDuration.Text); gun.AppliesPoison = poisonsEnemies.IsChecked ?? false; gun.PoisonChance = float.Parse(poisonChance.Text); gun.AppliesSpeed = speedsEnemies.IsChecked ?? false; gun.SpeedChance = float.Parse(speedChance.Text); gun.SpeedDuration = float.Parse(speedDuration.Text); gun.SpeedMultiplier = float.Parse(speedMultiplier.Text); gun.AppliesKnockbackToPlayer = appliesKnockbackToPlayer.IsChecked ?? false; gun.PlayerKnockbackForce = float.Parse(playerKnockbackForce.Text); gun.DamagesWalls = damagesWalls.IsChecked ?? false; gun.ProjectileDamage = float.Parse(projectileDamage.Text); gun.ProjectileSpeed = float.Parse(projectileSpeed.Text); gun.ProjectileForce = float.Parse(projectileForce.Text); gun.ProjectileRange = float.Parse(projectileRange.Text); gun.CollidesWithEnemies = collidesWithEnemies.IsChecked ?? false; gun.CollidesWithPlayer = collidesWithPlayer.IsChecked ?? false; gun.CollidesWithProjectiles = collidesWithProjectiles.IsChecked ?? false; gun.CollidesOnlyWithPlayerProjectiles = collidesWithPlayerProjectiles.IsChecked ?? false; gun.DoesBounce = doesBounce.IsChecked ?? false; gun.NumberOfBounces = int.Parse(bounces.Text); gun.DoesPierce = doesPierce.IsChecked ?? false; gun.NumberOfPierces = int.Parse(pierces.Text); var serializerbuilder = new SerializerBuilder(); serializerbuilder.EmitDefaults(); var serializer = serializerbuilder.Build(); var yaml = serializer.Serialize(gun); File.WriteAllText(filename, yaml); }