public GridGeneratorInfo(int id, List <TextNode> nodes) { this.id = id; var fields = TypeLoader.GetFields(this); foreach (var node in nodes) { switch (node.Key) { case nameof(Pieces): Pieces = new GridPiece[node.Children.Count]; for (int i = 0; i < node.Children.Count; i++) { var node2 = node.Children[i]; Pieces[i] = new GridPiece(node2.Children); } break; default: TypeLoader.SetValue(this, fields, node); break; } } }
static void load() { var fields = TypeLoader.GetFields(typeof(Settings), false); var nodes = TextNodeLoader.FromFile(FileExplorer.MainDirectory, "Settings.yaml"); foreach (var node in nodes) { switch (node.Key) { case "Keys": foreach (var key in node.Children) { KeyDictionary.Add(key.Key, KeyInput.ToKey(key.Value)); } break; default: // null is used for static classes. TypeLoader.SetValue(null, fields, node); break; } } }
public static void Initialize(bool newSettings) { var fields = TypeLoader.GetFields(typeof(Settings), false); foreach (var field in fields) { if (field.IsLiteral || field.IsInitOnly) { continue; } var attributes = field.GetCustomAttributes(typeof(DefaultValueAttribute), false); if (attributes.Length != 0) { var attribute = (DefaultValueAttribute)attributes[0]; field.SetValue(null, attribute.Default); } } if (!newSettings && FileExplorer.Exists(FileExplorer.MainDirectory, "Settings.yaml")) { load(); } if (KeyDictionary.Count == 0) { defaultKeys(); } if (FirstStarted) { Save(); } }
public DamageWarhead(List <TextNode> nodes) { var fields = TypeLoader.GetFields(this); foreach (var node in nodes) { switch (node.Key) { case nameof(ArmorModifiers): foreach (var node2 in node.Children) { ArmorModifiers.Add(node2.Key, node2.Convert <float>()); } break; default: TypeLoader.SetValue(this, fields, node); break; } } if (RangeSteps.Length != Falloff.Length) { throw new InvalidNodeException($"Range step length ({RangeSteps.Length}) does not match with given falloff values ({Falloff.Length})."); } maxRange = FalloffHelper.GetMax(Falloff, RangeSteps); }
public Spell(List <TextNode> nodes) { var fields = TypeLoader.GetFields(this); foreach (var node in nodes) { switch (node.Key) { case nameof(Effects): Effects = new Effect[node.Children.Count]; for (int i = 0; i < node.Children.Count; i++) { var node2 = node.Children[i]; Effects[i] = new Effect(node2.Children); } break; default: TypeLoader.SetValue(this, fields, node); break; } } if (Effects != null && Effects.Length > 0) { MaxDuration = Effects.Max(e => e.Duration); } }
MapType(string name, List <TextNode> nodes) { Name = name; var fields = TypeLoader.GetFields(this); foreach (var node in nodes) { switch (node.Key) { case nameof(TerrainGenerationBase): TerrainGenerationBase = new TerrainGeneratorInfo(node.Convert <int>(), node.Children); break; case nameof(NoiseMaps): NoiseMaps = new NoiseMapInfo[node.Children.Count]; for (int i = 0; i < NoiseMaps.Length; i++) { var child = node.Children[i]; NoiseMaps[i] = new NoiseMapInfo(child.Convert <int>(), child.Children); } break; case nameof(PatrolPlacers): PatrolPlacers = new PatrolPlacerInfo[node.Children.Count]; for (int i = 0; i < PatrolPlacers.Length; i++) { PatrolPlacers[i] = new PatrolPlacerInfo(node.Children[i].Children); } break; case nameof(WeatherEffects): WeatherEffects = new WeatherEffect[node.Children.Count]; for (int i = 0; i < WeatherEffects.Length; i++) { WeatherEffects[i] = new WeatherEffect(node.Children[i].Children); } break; default: TypeLoader.SetValue(this, fields, node); break; } } if (TerrainGenerationBase == null) { throw new MissingNodeException(name, "BaseTerrainGeneration"); } }
public static void Save() { // HACK: while saving, set FirstStarted to false. var firstStarted = FirstStarted; FirstStarted = false; using var writer = new System.IO.StreamWriter(FileExplorer.MainDirectory + "Settings.yaml"); var fields = TypeLoader.GetFields(typeof(Settings), false); foreach (var field in fields) { if (field.IsLiteral || field.IsInitOnly) { continue; } var fieldValue = field.GetValue(null); var attributes = field.GetCustomAttributes(typeof(DefaultValueAttribute), false); if (attributes.Length != 0) { var attribute = (DefaultValueAttribute)attributes[0]; if (fieldValue.Equals(attribute.Default)) { continue; } } writer.WriteLine($"{field.Name}={fieldValue}"); } writer.WriteLine("Keys="); foreach (var key in KeyDictionary) { writer.WriteLine($"\t{key.Key}={key.Value}"); } writer.Flush(); writer.Close(); FirstStarted = firstStarted; }
public Piece(string innerName, string path, List <TextNode> nodes) { InnerName = innerName; Path = path; var fields = TypeLoader.GetFields(this); foreach (var node in nodes) { switch (node.Key) { case "Terrain": groundData = node.Convert <ushort[]>(); break; case "Walls": wallData = node.Convert <short[]>(); break; case "Actors": foreach (var actor in node.Children) { try { var id = uint.Parse(actor.Key); if (MapFormat == 0) { actors.Add(new ActorInit(id, actor)); } else { actors.Add(new ActorInit(id, actor.Children)); } } catch (Exception e) { throw new InvalidPieceException($"unable to load actor '{actor.Key}' in piece '{Name}'.", e); } } break; case "Weapons": foreach (var weapon in node.Children) { try { var id = uint.Parse(weapon.Key); weapons.Add(new WeaponInit(id, weapon.Children)); } catch (Exception e) { throw new InvalidPieceException($"unable to load weapon '{weapon.Key}' in piece '{Name}'.", e); } } break; case "Particles": foreach (var particle in node.Children) { try { particles.Add(new ParticleInit(particle.Children)); } catch (Exception e) { throw new InvalidPieceException($"unable to load particle '{particle.Key}' in piece '{Name}'.", e); } } break; case "MapFormat": TypeLoader.SetValue(this, fields, node); if (MapFormat != CurrentMapFormat) { Log.LoaderWarning("Pieces", $"Attempting to load old map format {MapFormat} (current: {CurrentMapFormat})"); } break; default: TypeLoader.SetValue(this, fields, node); break; } } if (groundData.Length != Size.X * Size.Y) { throw new InvalidPieceException($"The count of given terrains ({groundData.Length}) is not the size ({Size.X * Size.Y}) of the piece '{Name}'"); } if (wallData.Length != (Size.X + 1) * (Size.Y + 1) * 2 * 2) { throw new InvalidPieceException($"The count of given walls ({groundData.Length}) is smaller as the size ({Size.X * Size.Y}) on the piece '{Name}'"); } }