much simpler rules loader

This commit is contained in:
Chris Forbes
2010-04-01 21:31:15 +13:00
parent c5fdda9141
commit 38605faecf
16 changed files with 110 additions and 120 deletions

View File

@@ -1,51 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.GameRules
{
public class InfoLoader<T> : IEnumerable<KeyValuePair<string, T>>
{
readonly Dictionary<string, T> infos = new Dictionary<string, T>();
public InfoLoader(params Pair<string, Func<string,T>>[] srcs)
{
foreach (var src in srcs)
foreach (var name in Rules.Categories[src.First])
{
var t = src.Second(name);
FieldLoader.Load(t, Rules.AllRules.GetSection(name));
infos[name] = t;
}
}
public T this[string name]
{
get { return infos[name.ToLowerInvariant()]; }
}
public IEnumerator<KeyValuePair<string, T>> GetEnumerator() { return infos.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return infos.GetEnumerator(); }
}
}

View File

@@ -29,13 +29,11 @@ namespace OpenRA
public static class Rules
{
public static IniFile AllRules;
public static Dictionary<string, List<string>> Categories = new Dictionary<string, List<string>>();
public static InfoLoader<WarheadInfo> WarheadInfo;
public static InfoLoader<VoiceInfo> VoiceInfo;
public static TechTree TechTree;
public static Dictionary<string, ActorInfo> Info;
public static Dictionary<string, WeaponInfo> Weapons;
public static Dictionary<string, VoiceInfo> Voices;
public static void LoadRules(string map, Manifest m)
{
@@ -43,38 +41,21 @@ namespace OpenRA
legacyRules.Insert(0, map);
AllRules = new IniFile(legacyRules.Select(a => FileSystem.Open(a)).ToArray());
LoadCategories(
"Weapon",
"Warhead",
"Projectile",
"Voice");
WarheadInfo = new InfoLoader<WarheadInfo>(
Pair.New<string, Func<string, WarheadInfo>>("Warhead", _ => new WarheadInfo()));
VoiceInfo = new InfoLoader<VoiceInfo>(
Pair.New<string, Func<string, VoiceInfo>>("Voice", _ => new VoiceInfo()));
Log.Write("Using rules files: ");
foreach (var y in m.Rules)
Log.Write(" -- {0}", y);
var yamlRules = m.Rules.Select(a => MiniYaml.FromFile(a)).Aggregate(MiniYaml.Merge);
Info = new Dictionary<string, ActorInfo>();
foreach( var kv in yamlRules )
Info.Add(kv.Key.ToLowerInvariant(), new ActorInfo(kv.Key.ToLowerInvariant(), kv.Value, yamlRules));
var weaponsYaml = m.Weapons.Select(a => MiniYaml.FromFile(a)).Aggregate(MiniYaml.Merge);
Weapons = new Dictionary<string, WeaponInfo>();
foreach (var kv in weaponsYaml)
Weapons.Add(kv.Key.ToLowerInvariant(), new WeaponInfo(kv.Key.ToLowerInvariant(), kv.Value));
Info = LoadYamlRules(m.Rules, (k, y) => new ActorInfo(k.Key.ToLowerInvariant(), k.Value, y));
Weapons = LoadYamlRules(m.Weapons, (k, _) => new WeaponInfo(k.Key.ToLowerInvariant(), k.Value));
Voices = LoadYamlRules(m.Voices, (k, _) => new VoiceInfo(k.Value));
TechTree = new TechTree();
}
static void LoadCategories(params string[] types)
static Dictionary<string, T> LoadYamlRules<T>(string[] files, Func<KeyValuePair<string, MiniYaml>, Dictionary<string, MiniYaml>, T> f)
{
foreach (var t in types)
Categories[t] = AllRules.GetSection(t + "Types").Select(x => x.Key.ToLowerInvariant()).ToList();
var y = files.Select(a => MiniYaml.FromFile(a)).Aggregate(MiniYaml.Merge);
return y.ToDictionary(kv => kv.Key.ToLowerInvariant(), kv => f(kv, y));
}
}
}

View File

@@ -34,8 +34,10 @@ namespace OpenRA.GameRules
public readonly Lazy<Dictionary<string, VoicePool>> Pools;
public VoiceInfo()
public VoiceInfo( MiniYaml y )
{
FieldLoader.Load(this, y);
Pools = Lazy.New(() =>
new Dictionary<string, VoicePool>
{