Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
- using System.IO;
- using System.Net;
- using Microsoft.Xna.Framework;
- namespace PGN
- {
- public class Tileset
- {
- string hash;
- int width;
- int height;
- int tileSize;
- Tile[,] tiles;
- public bool IsCompatibleTileset()
- {
- return (tileSize == Utils.TILESIZE);
- }
- public string GetFilePath()
- {
- if (!File.Exists(FilePath))
- throw new Exception("File is not ready!");
- return FilePath;
- }
- public string FilePath
- {
- get { return "UContent\\Tilesets\\" + hash + ".png"; }
- }
- public void DownloadAsset()
- {
- if (IsReady)
- return; //We have this asset already
- WebClient client = new WebClient();
- var remotePath = string.Format("{0}{1}.png", Utils.CDN, hash);
- client.DownloadFile(remotePath, FilePath);
- }
- public bool IsReady
- {
- get { return File.Exists("UContent\\Tilesets\\" + hash + ".png"); }
- }
- public static Tileset FromHash(string hash)
- {
- var path = "UContent\\Tilesets\\" + hash + ".png";
- if (File.Exists(path))
- return FromManifest(path);
- WebClient client = new WebClient();
- var remotePath = string.Format("{0}{1}.xml", Utils.CDN, hash);
- client.DownloadFile(remotePath, path);
- return FromManifest(path);
- }
- public static Tileset FromManifest(string filename)
- {
- var doc = XDocument.Load(filename);
- Tileset set = new Tileset();
- set.width = Convert.ToInt32(doc.Element("tileset").Attribute("width").Value);
- set.height = Convert.ToInt32(doc.Element("tileset").Attribute("height").Value);
- set.hash = doc.Element("tileset").Attribute("hash").Value;
- set.tileSize = Convert.ToInt32(doc.Element("tileset").Attribute("tilesize").Value);
- if (!set.IsCompatibleTileset())
- throw new Exception("tileSize mismatch - not valid for this game");
- set.tiles = new Tile[set.width, set.height];
- foreach (var tile in doc.Element("tileset").Elements("tile"))
- {
- var offsetX = Convert.ToInt32(tile.Attribute("offsetX").Value);
- var offsetY = Convert.ToInt32(tile.Attribute("offsetY").Value);
- var playerPass = (PlayerPass)Convert.ToByte(tile.Element("playerPass").Value);
- set.tiles[offsetX-1, offsetY-1] = new Tile()
- {
- Offset = new Vector2(offsetX, offsetY),
- PlayerPass = playerPass,
- TilesetHash = set.hash
- };
- }
- return set;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment