public StageInfo Load(FilePath path) { _info = new StageInfo(); _info.StoragePath = path; var mapPath = Path.Combine(_info.StoragePath.Absolute, "map.xml"); var stream = _dataSource.GetData(FilePath.FromAbsolute(mapPath, _info.StoragePath.BasePath)); var mapXml = XElement.Load(stream); _info.Name = Path.GetFileNameWithoutExtension(_info.StoragePath.Absolute); string tilePathRel = mapXml.Attribute("tiles").Value; var tilePath = FilePath.FromRelative(tilePathRel, _info.StoragePath.BasePath); var tileReader = _readerProvider.GetTilesetReader(tilePath); var tileset = tileReader.Load(tilePath); _info.ChangeTileset(tileset); _info.PlayerStartX = 3; _info.PlayerStartY = 3; LoadMusicXml(mapXml); LoadScreens(mapXml); XElement start = mapXml.Element("Start"); if (start != null) { _info.StartScreen = start.RequireAttribute("screen").Value; _info.PlayerStartX = start.GetAttribute <int>("x"); _info.PlayerStartY = start.GetAttribute <int>("y"); } foreach (XElement contPoint in mapXml.Elements("Continue")) { string screen = contPoint.GetAttribute <string>("screen"); int x = contPoint.GetAttribute <int>("x"); int y = contPoint.GetAttribute <int>("y"); _info.AddContinuePoint(screen, new Point(x, y)); } foreach (XElement join in mapXml.Elements("Join")) { string t = join.Attribute("type").Value; JoinType type; if (t.ToLower() == "horizontal") { type = JoinType.Horizontal; } else if (t.ToLower() == "vertical") { type = JoinType.Vertical; } else { throw new GameXmlException(join, "map.xml file contains invalid join type."); } string s1 = join.RequireAttribute("s1").Value; string s2 = join.RequireAttribute("s2").Value; int offset1 = join.GetAttribute <int>("offset1"); int offset2 = join.GetAttribute <int>("offset2"); int size = join.GetAttribute <int>("size"); JoinDirection direction; XAttribute dirAttr = join.Attribute("direction"); if (dirAttr == null || dirAttr.Value.ToUpper() == "BOTH") { direction = JoinDirection.Both; } else if (dirAttr.Value.ToUpper() == "FORWARD") { direction = JoinDirection.ForwardOnly; } else if (dirAttr.Value.ToUpper() == "BACKWARD") { direction = JoinDirection.BackwardOnly; } else { throw new GameXmlException(dirAttr, "map.xml file contains invalid join direction."); } string bosstile = null; XAttribute bossAttr = join.Attribute("bossdoor"); bool bossdoor = (bossAttr != null); if (bossdoor) { bosstile = bossAttr.Value; } Join j = new Join(); j.direction = direction; j.screenOne = s1; j.screenTwo = s2; j.offsetOne = offset1; j.offsetTwo = offset2; j.type = type; j.Size = size; j.bossDoor = bossdoor; j.bossEntityName = bosstile; _info.Joins.Add(j); } stream.Close(); return(_info); }
public Project Load() { _project = new Project(); var gameFilePath = _dataSource.GetGameFile(); _project.GameFile = gameFilePath; var stream = _dataSource.GetData(gameFilePath); XElement reader = XElement.Load(stream); XAttribute nameAttr = reader.Attribute("name"); if (nameAttr != null) { _project.Name = nameAttr.Value; } XAttribute authAttr = reader.Attribute("author"); if (authAttr != null) { _project.Author = authAttr.Value; } XElement sizeNode = reader.Element("Size"); if (sizeNode != null) { _project.ScreenWidth = sizeNode.TryAttribute <int>("x"); _project.ScreenHeight = sizeNode.TryAttribute <int>("y"); } XElement nsfNode = reader.Element("NSF"); if (nsfNode != null) { LoadNSFInfo(nsfNode); } XElement stagesNode = reader.Element("Stages"); if (stagesNode != null) { foreach (XElement stageNode in stagesNode.Elements("Stage")) { var info = new StageLinkInfo(); info.Name = stageNode.RequireAttribute("name").Value; info.StagePath = FilePath.FromRelative(stageNode.RequireAttribute("path").Value, _project.BaseDir); var winNode = stageNode.Element("Win"); if (winNode != null) { var winHandlerNode = winNode.Element("Next"); if (winHandlerNode != null) { info.WinHandler = _transferReader.Load(winHandlerNode); } } var loseNode = stageNode.Element("Lose"); if (loseNode != null) { var loseHandlerNode = loseNode.Element("Next"); if (loseHandlerNode != null) { info.LoseHandler = _transferReader.Load(loseHandlerNode); } } _project.AddStage(info); } } XElement startNode = reader.Element("Next"); if (startNode != null) { _project.StartHandler = _transferReader.Load(startNode); } _project.AddIncludeFiles(reader.Elements("Include") .Select(e => e.Value) .Where(v => !string.IsNullOrEmpty(v.Trim()))); _project.AddIncludeFolders(reader.Elements("IncludeFolder") .Select(e => e.Value) .Where(v => !string.IsNullOrEmpty(v.Trim()))); var includeReader = new IncludeFileXmlReader(); foreach (var includePath in _project.Includes) { string includefile = includePath.Absolute; includeReader.LoadIncludedFile(_project, includefile); } stream.Close(); return(_project); }
public Tileset Load(FilePath path) { var tileset = new Tileset(); tileset.FilePath = path; var stream = _dataSource.GetData(path); var doc = XDocument.Load(stream); var reader = doc.Element("Tileset"); if (reader == null) { throw new Exception("The specified tileset definition file does not contain a Tileset tag."); } var sheetPath = FilePath.FromRelative(reader.Attribute("tilesheet").Value, path.BasePath); tileset.ChangeSheetPath(sheetPath.Absolute); int size; if (!int.TryParse(reader.Attribute("tilesize").Value, out size)) { throw new Exception("The tileset definition does not contain a valid tilesize attribute."); } tileset.TileSize = size; var propParent = reader.Element("TileProperties"); if (propParent != null) { foreach (XElement propNode in propParent.Elements("Properties")) { var prop = LoadProperties(propNode); tileset.AddProperties(prop); } } foreach (XElement tileNode in reader.Elements("Tile")) { int id = int.Parse(tileNode.Attribute("id").Value); string name = tileNode.Attribute("name").Value; var spriteNode = tileNode.Element("Sprite"); if (spriteNode == null) { throw new GameXmlException(tileNode, "All Tile tags must contain a Sprite tag."); } var sprite = _spriteReader.LoadSprite(spriteNode); var tileSprite = new TileSprite(tileset, sprite); Tile tile = new Tile(id, tileSprite); string propName = "Default"; XAttribute propAttr = tileNode.Attribute("properties"); if (propAttr != null) { propName = propAttr.Value; } tile.Properties = tileset.GetProperties(propName); tile.Sprite.Play(); tileset.Add(tile); } stream.Close(); return(tileset); }