Ejemplo n.º 1
0
        public TerrainTemplateInfo(TileSet tileSet, MiniYaml my)
        {
            FieldLoader.Load(this, my);

            var nodes = my.ToDictionary()["Tiles"].Nodes;

            if (!PickAny)
            {
                tileInfo = new TerrainTileInfo[Size.X * Size.Y];
                foreach (var node in nodes)
                {
                    if (!int.TryParse(node.Key, out var key) || key < 0 || key >= tileInfo.Length)
                    {
                        throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id));
                    }

                    tileInfo[key] = LoadTileInfo(tileSet, node.Value);
                }
            }
            else
            {
                tileInfo = new TerrainTileInfo[nodes.Count];

                var i = 0;
                foreach (var node in nodes)
                {
                    if (!int.TryParse(node.Key, out var key) || key != i++)
                    {
                        throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id));
                    }

                    tileInfo[key] = LoadTileInfo(tileSet, node.Value);
                }
            }
        }
Ejemplo n.º 2
0
        static Dictionary <string, ModMetadata> ValidateMods()
        {
            var basePath = Platform.ResolvePath(".", "mods");
            var mods     = Directory.GetDirectories(basePath)
                           .Select(x => x.Substring(basePath.Length + 1));

            var ret = new Dictionary <string, ModMetadata>();

            foreach (var m in mods)
            {
                var yamlPath = Platform.ResolvePath(".", "mods", m, "mod.yaml");
                if (!File.Exists(yamlPath))
                {
                    continue;
                }

                var yaml = new MiniYaml(null, MiniYaml.FromFile(yamlPath));
                var nd   = yaml.ToDictionary();
                if (!nd.ContainsKey("Metadata"))
                {
                    continue;
                }

                var mod = FieldLoader.Load <ModMetadata>(nd["Metadata"]);
                mod.Id = m;

                ret.Add(m, mod);
            }

            return(ret);
        }
Ejemplo n.º 3
0
        public static Dictionary <string, ModMetadata> ValidateMods(string[] mods)
        {
            var ret = new Dictionary <string, ModMetadata>();

            foreach (var m in mods)
            {
                var yamlPath = new[] { "mods", m, "mod.yaml" }.Aggregate(Path.Combine);
                if (!File.Exists(yamlPath))
                {
                    continue;
                }

                var yaml = new MiniYaml(null, MiniYaml.FromFile(yamlPath));
                var nd   = yaml.ToDictionary();
                if (!nd.ContainsKey("Metadata"))
                {
                    continue;
                }

                var mod = FieldLoader.Load <ModMetadata>(nd["Metadata"]);
                mod.Id = m;

                ret.Add(m, mod);
            }

            return(ret);
        }
Ejemplo n.º 4
0
        public static void Load(object self, MiniYaml my)
        {
            var loadInfo = typeLoadInfo[self.GetType()];

            Dictionary <string, MiniYaml> md = null;

            foreach (var fli in loadInfo)
            {
                object val;

                if (fli.Loader != null)
                {
                    val = fli.Loader(my);
                }
                else
                {
                    if (md == null)
                    {
                        md = my.ToDictionary();
                    }

                    if (!TryGetValueFromYaml(fli.YamlName, fli.Field, md, out val))
                    {
                        continue;
                    }
                }

                fli.Field.SetValue(self, val);
            }
        }
Ejemplo n.º 5
0
        static object LoadVideos(MiniYaml y)
        {
            var videos    = new MapVideos();
            var nodesDict = y.ToDictionary();

            if (nodesDict.ContainsKey("Videos"))
            {
                FieldLoader.Load(videos, nodesDict["Videos"]);
            }

            return(videos);
        }
Ejemplo n.º 6
0
        static object LoadOptions(MiniYaml y)
        {
            var options   = new MapOptions();
            var nodesDict = y.ToDictionary();

            if (nodesDict.ContainsKey("Options"))
            {
                FieldLoader.Load(options, nodesDict["Options"]);
            }

            return(options);
        }
Ejemplo n.º 7
0
        public static void Load(object self, MiniYaml my)
        {
            var loadInfo = TypeLoadInfo[self.GetType()];
            var missing  = new List <string>();

            Dictionary <string, MiniYaml> md = null;

            foreach (var fli in loadInfo)
            {
                object val;

                if (md == null)
                {
                    md = my.ToDictionary();
                }
                if (fli.Loader != null)
                {
                    if (!fli.Attribute.Required || md.ContainsKey(fli.YamlName))
                    {
                        val = fli.Loader(my);
                    }
                    else
                    {
                        missing.Add(fli.YamlName);
                        continue;
                    }
                }
                else
                {
                    if (!TryGetValueFromYaml(fli.YamlName, fli.Field, md, out val))
                    {
                        if (fli.Attribute.Required)
                        {
                            missing.Add(fli.YamlName);
                        }
                        continue;
                    }
                }

                fli.Field.SetValue(self, val);
            }

            if (missing.Any())
            {
                throw new MissingFieldsException(missing.ToArray());
            }
        }
Ejemplo n.º 8
0
        int[] LoadTiles(TileSet tileSet, MiniYaml y)
        {
            var nodes = y.ToDictionary()["Tiles"].Nodes;

            if (!PickAny)
            {
                var tiles = new int[Size.X * Size.Y];

                for (var i = 0; i < tiles.Length; i++)
                {
                    tiles[i] = -1;
                }

                foreach (var node in nodes)
                {
                    int key;
                    if (!int.TryParse(node.Key, out key) || key < 0 || key >= tiles.Length)
                    {
                        throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id));
                    }

                    tiles[key] = tileSet.GetTerrainIndex(node.Value.Value);
                }

                return(tiles);
            }
            else
            {
                var tiles = new int[nodes.Count];
                var i     = 0;

                foreach (var node in nodes)
                {
                    int key;
                    if (!int.TryParse(node.Key, out key) || key != i++)
                    {
                        throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id));
                    }

                    tiles[key] = tileSet.GetTerrainIndex(node.Value.Value);
                }

                return(tiles);
            }
        }
Ejemplo n.º 9
0
        public TerrainTemplateInfo(TileSet tileSet, MiniYaml my)
        {
            FieldLoader.Load(this, my);

            var nodes = my.ToDictionary()["Tiles"].Nodes;

            if (!PickAny)
            {
                tileInfo = new TerrainTileInfo[Size.X * Size.Y];
                foreach (var node in nodes)
                {
                    if (!int.TryParse(node.Key, out var key))
                    {
                        throw new YamlException("Tileset `{0}` template `{1}` defines a frame `{2}` that is not a valid integer.".F(tileSet.Id, Id, node.Key));
                    }

                    if (key < 0 || key >= tileInfo.Length)
                    {
                        throw new YamlException("Tileset `{0}` template `{1}` references frame {2}, but only [0..{3}] are valid for a {4}x{5} Size template.".F(tileSet.Id, Id, key, tileInfo.Length - 1, Size.X, Size.Y));
                    }

                    tileInfo[key] = LoadTileInfo(tileSet, node.Value);
                }
            }
            else
            {
                tileInfo = new TerrainTileInfo[nodes.Count];

                var i = 0;
                foreach (var node in nodes)
                {
                    if (!int.TryParse(node.Key, out var key))
                    {
                        throw new YamlException("Tileset `{0}` template `{1}` defines a frame `{2}` that is not a valid integer.".F(tileSet.Id, Id, node.Key));
                    }

                    if (key != i++)
                    {
                        throw new YamlException("Tileset `{0}` template `{1}` is missing a definition for frame {2}.".F(tileSet.Id, Id, i - 1));
                    }

                    tileInfo[key] = LoadTileInfo(tileSet, node.Value);
                }
            }
        }
Ejemplo n.º 10
0
        static Dictionary <string, ModMetadata> ValidateMods()
        {
            var basePath = Platform.ResolvePath(".", "mods");
            var mods     = Directory.GetDirectories(basePath)
                           .Select(x => x.Substring(basePath.Length + 1));

            var ret = new Dictionary <string, ModMetadata>();

            foreach (var m in mods)
            {
                try
                {
                    var yamlPath = Platform.ResolvePath(".", "mods", m, "mod.yaml");
                    if (!File.Exists(yamlPath))
                    {
                        continue;
                    }

                    var yaml = new MiniYaml(null, MiniYaml.FromFile(yamlPath));
                    var nd   = yaml.ToDictionary();
                    if (!nd.ContainsKey("Metadata"))
                    {
                        continue;
                    }

                    var mod = FieldLoader.Load <ModMetadata>(nd["Metadata"]);
                    mod.Id = m;

                    if (nd.ContainsKey("ContentInstaller"))
                    {
                        mod.Content = FieldLoader.Load <ContentInstaller>(nd["ContentInstaller"]);
                    }

                    ret.Add(m, mod);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An exception occurred when trying to load ModMetadata for `{0}`:".F(m));
                    Console.WriteLine(ex.Message);
                }
            }

            return(ret);
        }
Ejemplo n.º 11
0
        static MiniYaml GetParent(MiniYaml node, Dictionary <string, MiniYaml> allUnits)
        {
            MiniYaml inherits;

            node.ToDictionary().TryGetValue("Inherits", out inherits);
            if (inherits == null || string.IsNullOrEmpty(inherits.Value))
            {
                return(null);
            }

            MiniYaml parent;

            allUnits.TryGetValue(inherits.Value, out parent);
            if (parent == null)
            {
                throw new InvalidOperationException(
                          "Bogus inheritance -- actor type {0} does not exist".F(inherits.Value));
            }

            return(parent);
        }
Ejemplo n.º 12
0
        // Support upgrading format 5 maps to a more
        // recent version by defining upgradeForMod.
        public Map(string path, string upgradeForMod)
        {
            Path      = path;
            Container = GlobalFileSystem.OpenPackage(path, null, int.MaxValue);

            AssertExists("map.yaml");
            AssertExists("map.bin");

            var yaml = new MiniYaml(null, MiniYaml.FromStream(Container.GetContent("map.yaml")));

            FieldLoader.Load(this, yaml);

            // Support for formats 1-3 dropped 2011-02-11.
            // Use release-20110207 to convert older maps to format 4
            // Use release-20110511 to convert older maps to format 5
            if (MapFormat < 5)
            {
                throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path));
            }

            // Format 5 -> 6 enforces the use of RequiresMod
            if (MapFormat == 5)
            {
                if (upgradeForMod == null)
                {
                    throw new InvalidDataException("Map format {0} is not supported, but can be upgraded.\n File: {1}".F(MapFormat, path));
                }

                Console.WriteLine("Upgrading {0} from Format 5 to Format 6", path);

                // TODO: This isn't very nice, but there is no other consistent way
                // of finding the mod early during the engine initialization.
                RequiresMod = upgradeForMod;
            }

            var nd = yaml.ToDictionary();

            // Load players
            foreach (var my in nd["Players"].ToDictionary().Values)
            {
                var player = new PlayerReference(my);
                Players.Add(player.Name, player);
            }

            Actors = Exts.Lazy(() =>
            {
                var ret = new Dictionary <string, ActorReference>();
                foreach (var kv in nd["Actors"].ToDictionary())
                {
                    ret.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.ToDictionary()));
                }
                return(ret);
            });

            // Smudges
            Smudges = Exts.Lazy(() =>
            {
                var ret = new List <SmudgeReference>();
                foreach (var name in nd["Smudges"].ToDictionary().Keys)
                {
                    var vals = name.Split(' ');
                    var loc  = vals[1].Split(',');
                    ret.Add(new SmudgeReference(vals[0], new int2(
                                                    Exts.ParseIntegerInvariant(loc[0]),
                                                    Exts.ParseIntegerInvariant(loc[1])),
                                                Exts.ParseIntegerInvariant(vals[2])));
                }

                return(ret);
            });

            RuleDefinitions          = MiniYaml.NodesOrEmpty(yaml, "Rules");
            SequenceDefinitions      = MiniYaml.NodesOrEmpty(yaml, "Sequences");
            VoxelSequenceDefinitions = MiniYaml.NodesOrEmpty(yaml, "VoxelSequences");
            WeaponDefinitions        = MiniYaml.NodesOrEmpty(yaml, "Weapons");
            VoiceDefinitions         = MiniYaml.NodesOrEmpty(yaml, "Voices");
            NotificationDefinitions  = MiniYaml.NodesOrEmpty(yaml, "Notifications");
            TranslationDefinitions   = MiniYaml.NodesOrEmpty(yaml, "Translations");

            MapTiles     = Exts.Lazy(() => LoadMapTiles());
            MapResources = Exts.Lazy(() => LoadResourceTiles());
            TileShape    = Game.modData.Manifest.TileShape;

            // The Uid is calculated from the data on-disk, so
            // format changes must be flushed to disk.
            // TODO: this isn't very nice
            if (MapFormat < 6)
            {
                Save(path);
            }

            Uid = ComputeHash();

            if (Container.Exists("map.png"))
            {
                CustomPreview = new Bitmap(Container.GetContent("map.png"));
            }

            PostInit();
        }
Ejemplo n.º 13
0
 public ModelSequenceFormat(MiniYaml yaml)
 {
     Type     = yaml.Value;
     Metadata = new ReadOnlyDictionary <string, MiniYaml>(yaml.ToDictionary());
 }
Ejemplo n.º 14
0
 public TerrainFormat(MiniYaml yaml)
 {
     Type     = yaml.Value;
     Metadata = new ReadOnlyDictionary <string, MiniYaml>(yaml.ToDictionary());
 }
Ejemplo n.º 15
0
        public static List <MiniYamlNode> NodesOrEmpty(MiniYaml y, string s)
        {
            var nd = y.ToDictionary();

            return(nd.ContainsKey(s) ? nd[s].Nodes : new List <MiniYamlNode>());
        }
Ejemplo n.º 16
0
        // The standard constructor for most purposes
        public Map(string path)
        {
            Path      = path;
            Container = GlobalFileSystem.OpenPackage(path, null, int.MaxValue);

            AssertExists("map.yaml");
            AssertExists("map.bin");

            var yaml = new MiniYaml(null, MiniYaml.FromStream(Container.GetContent("map.yaml"), path));

            FieldLoader.Load(this, yaml);

            // Support for formats 1-3 dropped 2011-02-11.
            // Use release-20110207 to convert older maps to format 4
            // Use release-20110511 to convert older maps to format 5
            // Use release-20141029 to convert older maps to format 6
            if (MapFormat < 6)
            {
                throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path));
            }

            var nd = yaml.ToDictionary();

            // Format 6 -> 7 combined the Selectable and UseAsShellmap flags into the Class enum
            if (MapFormat < 7)
            {
                MiniYaml useAsShellmap;
                if (nd.TryGetValue("UseAsShellmap", out useAsShellmap) && bool.Parse(useAsShellmap.Value))
                {
                    Visibility = MapVisibility.Shellmap;
                }
                else if (Type == "Mission" || Type == "Campaign")
                {
                    Visibility = MapVisibility.MissionSelector;
                }
            }

            SpawnPoints = Exts.Lazy(() =>
            {
                var spawns = new List <CPos>();
                foreach (var kv in ActorDefinitions.Where(d => d.Value.Value == "mpspawn"))
                {
                    var s = new ActorReference(kv.Value.Value, kv.Value.ToDictionary());

                    spawns.Add(s.InitDict.Get <LocationInit>().Value(null));
                }

                return(spawns.ToArray());
            });

            RuleDefinitions          = MiniYaml.NodesOrEmpty(yaml, "Rules");
            SequenceDefinitions      = MiniYaml.NodesOrEmpty(yaml, "Sequences");
            VoxelSequenceDefinitions = MiniYaml.NodesOrEmpty(yaml, "VoxelSequences");
            WeaponDefinitions        = MiniYaml.NodesOrEmpty(yaml, "Weapons");
            VoiceDefinitions         = MiniYaml.NodesOrEmpty(yaml, "Voices");
            NotificationDefinitions  = MiniYaml.NodesOrEmpty(yaml, "Notifications");
            TranslationDefinitions   = MiniYaml.NodesOrEmpty(yaml, "Translations");
            PlayerDefinitions        = MiniYaml.NodesOrEmpty(yaml, "Players");

            ActorDefinitions  = MiniYaml.NodesOrEmpty(yaml, "Actors");
            SmudgeDefinitions = MiniYaml.NodesOrEmpty(yaml, "Smudges");

            MapTiles     = Exts.Lazy(LoadMapTiles);
            MapResources = Exts.Lazy(LoadResourceTiles);
            MapHeight    = Exts.Lazy(LoadMapHeight);

            TileShape      = Game.ModData.Manifest.TileShape;
            SubCellOffsets = Game.ModData.Manifest.SubCellOffsets;
            LastSubCell    = (SubCell)(SubCellOffsets.Length - 1);
            DefaultSubCell = (SubCell)Game.ModData.Manifest.SubCellDefaultIndex;

            if (Container.Exists("map.png"))
            {
                using (var dataStream = Container.GetContent("map.png"))
                    CustomPreview = new Bitmap(dataStream);
            }

            PostInit();

            // The Uid is calculated from the data on-disk, so
            // format changes must be flushed to disk.
            // TODO: this isn't very nice
            if (MapFormat < 7)
            {
                Save(path);
            }

            Uid = ComputeHash();
        }
Ejemplo n.º 17
0
        // The standard constructor for most purposes
        public Map(string path)
        {
            Path      = path;
            Container = GlobalFileSystem.OpenPackage(path, null, int.MaxValue);

            AssertExists("map.yaml");
            AssertExists("map.bin");

            var yaml = new MiniYaml(null, MiniYaml.FromStream(Container.GetContent("map.yaml"), path));

            FieldLoader.Load(this, yaml);

            // Support for formats 1-3 dropped 2011-02-11.
            // Use release-20110207 to convert older maps to format 4
            // Use release-20110511 to convert older maps to format 5
            // Use release-20141029 to convert older maps to format 6
            if (MapFormat < 6)
            {
                throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path));
            }

            var nd = yaml.ToDictionary();

            // Format 6 -> 7 combined the Selectable and UseAsShellmap flags into the Class enum
            if (MapFormat < 7)
            {
                MiniYaml useAsShellmap;
                if (nd.TryGetValue("UseAsShellmap", out useAsShellmap) && bool.Parse(useAsShellmap.Value))
                {
                    Visibility = MapVisibility.Shellmap;
                }
                else if (Type == "Mission" || Type == "Campaign")
                {
                    Visibility = MapVisibility.MissionSelector;
                }
            }

            // Load players
            foreach (var my in nd["Players"].ToDictionary().Values)
            {
                var player = new PlayerReference(my);
                Players.Add(player.Name, player);
            }

            Actors = Exts.Lazy(() =>
            {
                var ret = new Dictionary <string, ActorReference>();
                foreach (var kv in nd["Actors"].ToDictionary())
                {
                    ret.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.ToDictionary()));
                }
                return(ret);
            });

            // Smudges
            Smudges = Exts.Lazy(() =>
            {
                var ret = new List <SmudgeReference>();
                foreach (var name in nd["Smudges"].ToDictionary().Keys)
                {
                    var vals = name.Split(' ');
                    var loc  = vals[1].Split(',');
                    ret.Add(new SmudgeReference(vals[0], new int2(
                                                    Exts.ParseIntegerInvariant(loc[0]),
                                                    Exts.ParseIntegerInvariant(loc[1])),
                                                Exts.ParseIntegerInvariant(vals[2])));
                }

                return(ret);
            });

            RuleDefinitions          = MiniYaml.NodesOrEmpty(yaml, "Rules");
            SequenceDefinitions      = MiniYaml.NodesOrEmpty(yaml, "Sequences");
            VoxelSequenceDefinitions = MiniYaml.NodesOrEmpty(yaml, "VoxelSequences");
            WeaponDefinitions        = MiniYaml.NodesOrEmpty(yaml, "Weapons");
            VoiceDefinitions         = MiniYaml.NodesOrEmpty(yaml, "Voices");
            NotificationDefinitions  = MiniYaml.NodesOrEmpty(yaml, "Notifications");
            TranslationDefinitions   = MiniYaml.NodesOrEmpty(yaml, "Translations");

            MapTiles     = Exts.Lazy(() => LoadMapTiles());
            MapResources = Exts.Lazy(() => LoadResourceTiles());
            MapHeight    = Exts.Lazy(() => LoadMapHeight());

            TileShape      = Game.ModData.Manifest.TileShape;
            SubCellOffsets = Game.ModData.Manifest.SubCellOffsets;
            LastSubCell    = (SubCell)(SubCellOffsets.Length - 1);
            DefaultSubCell = (SubCell)Game.ModData.Manifest.SubCellDefaultIndex;

            if (Container.Exists("map.png"))
            {
                using (var dataStream = Container.GetContent("map.png"))
                    CustomPreview = new Bitmap(dataStream);
            }

            PostInit();

            // The Uid is calculated from the data on-disk, so
            // format changes must be flushed to disk.
            // TODO: this isn't very nice
            if (MapFormat < 7)
            {
                Save(path);
            }

            Uid = ComputeHash();
        }
Ejemplo n.º 18
0
        static Dictionary <string, ModMetadata> ValidateMods()
        {
            var ret = new Dictionary <string, ModMetadata>();

            foreach (var pair in GetCandidateMods())
            {
                IReadOnlyPackage package = null;
                try
                {
                    if (Directory.Exists(pair.Second))
                    {
                        package = new Folder(pair.Second);
                    }
                    else
                    {
                        try
                        {
                            package = new ZipFile(null, pair.Second);
                        }
                        catch
                        {
                            throw new InvalidDataException(pair.Second + " is not a valid mod package");
                        }
                    }

                    if (!package.Contains("mod.yaml"))
                    {
                        package.Dispose();
                        continue;
                    }

                    var yaml = new MiniYaml(null, MiniYaml.FromStream(package.GetStream("mod.yaml")));
                    var nd   = yaml.ToDictionary();
                    if (!nd.ContainsKey("Metadata"))
                    {
                        package.Dispose();
                        continue;
                    }

                    var metadata = FieldLoader.Load <ModMetadata>(nd["Metadata"]);
                    metadata.Id      = pair.First;
                    metadata.Package = package;

                    if (nd.ContainsKey("RequiresMods"))
                    {
                        metadata.RequiresMods = nd["RequiresMods"].ToDictionary(my => my.Value);
                    }
                    else
                    {
                        metadata.RequiresMods = new Dictionary <string, string>();
                    }

                    if (nd.ContainsKey("ContentInstaller"))
                    {
                        metadata.Content = FieldLoader.Load <ContentInstaller>(nd["ContentInstaller"]);
                    }

                    // Mods in the support directory and oramod packages (which are listed later
                    // in the CandidateMods list) override mods in the main install.
                    ret[pair.First] = metadata;
                }
                catch (Exception ex)
                {
                    if (package != null)
                    {
                        package.Dispose();
                    }
                    Console.WriteLine("An exception occurred when trying to load ModMetadata for `{0}`:".F(pair.First));
                    Console.WriteLine(ex.Message);
                }
            }

            return(ret);
        }