/// <summary> /// Read and parse .fomap file /// </summary> /// <param name="filename">path to file</param> /// <param name="readHeader">if false, header Data won't be saved</param> /// <param name="readTiles">if false, tiles list won't be saved</param> /// <param name="readObjects">if false, mapObject list won't be saved</param> /// <returns>Newly loaded map, null if file don't exists</returns> public static FOnlineMap Load( string filename, bool readHeader = false, bool readTiles = false, bool readObjects = false ) { if( File.Exists( filename ) ) { string[] lines = File.ReadAllLines( filename ); FOnlineMap fomap = new FOnlineMap( filename ); bool inHeader = false, inTiles = false, inObjects = false; string reVarVal = "^(.*?)[\t ]+(.*?)$"; string reTile = "^(tile|roof)[\t ]+([0-9]+)[\t ]+([0-9]+)[\t ]+(.*?)$"; Match match = null; foreach( string line in lines ) { if( line.Length == 0 ) continue; // TODO: make it regexp if( line == "[Header]" ) { inHeader = true; inTiles = false; inObjects = false; continue; } else if( line == "[Tiles]" ) { inHeader = false; inTiles = true; inObjects = false; continue; } else if( line == "[Objects]" ) { inHeader = false; inTiles = false; inObjects = true; continue; } if( inHeader && readHeader ) { match = Regex.Match( line, reVarVal ); if( match.Success ) { fomap.header.Add( match.Groups[1].Value, match.Groups[2].Value ); } } else if( inTiles && readTiles ) { match = Regex.Match( line, reTile ); if( match.Success ) { int x, y; if( int.TryParse( match.Groups[2].Value, out x ) && int.TryParse( match.Groups[3].Value, out y ) ) { if( match.Groups[1].Value == "tile" ) { fomap.tiles.Add( new Tuple<int, int, string>( x, y, match.Groups[4].Value ) ); } else if( match.Groups[1].Value == "roof" ) { fomap.roofs.Add( new Tuple<int, int, string>( x, y, match.Groups[4].Value ) ); } } } } else if( inObjects && readObjects ) { match = Regex.Match( line, reVarVal ); if( match.Success ) { if( fomap.objects.Count == 0 || match.Groups[1].Value.ToUpper() == "MAPOBJTYPE" ) { Dictionary<string, string> dictionary = new Dictionary<string, string>(); dictionary.Add( match.Groups[1].Value, match.Groups[2].Value ); fomap.objects.Add( dictionary ); } else { fomap.objects[fomap.objects.Count - 1].Add( match.Groups[1].Value, match.Groups[2].Value ); } } } } return (fomap); } else return (null); }
/// <summary> /// Read and parse .fomap file /// </summary> /// <param name="filename">path to file</param> /// <param name="readHeader">if false, header Data won't be saved</param> /// <param name="readTiles">if false, tiles list won't be saved</param> /// <param name="readObjects">if false, mapObject list won't be saved</param> /// <returns>Newly loaded map, null if file don't exists</returns> public static FOnlineMap Load(string filename, bool readHeader = false, bool readTiles = false, bool readObjects = false) { if (File.Exists(filename)) { string[] lines = File.ReadAllLines(filename); FOnlineMap fomap = new FOnlineMap(filename); bool inHeader = false, inTiles = false, inObjects = false; string reVarVal = "^(.*?)[\t ]+(.*?)$"; string reTile = "^(tile|roof)[\t ]+([0-9]+)[\t ]+([0-9]+)[\t ]+(.*?)$"; Match match = null; foreach (string line in lines) { if (line.Length == 0) { continue; } // TODO: make it regexp if (line == "[Header]") { inHeader = true; inTiles = false; inObjects = false; continue; } else if (line == "[Tiles]") { inHeader = false; inTiles = true; inObjects = false; continue; } else if (line == "[Objects]") { inHeader = false; inTiles = false; inObjects = true; continue; } if (inHeader && readHeader) { match = Regex.Match(line, reVarVal); if (match.Success) { fomap.header.Add(match.Groups[1].Value, match.Groups[2].Value); } } else if (inTiles && readTiles) { match = Regex.Match(line, reTile); if (match.Success) { int x, y; if (int.TryParse(match.Groups[2].Value, out x) && int.TryParse(match.Groups[3].Value, out y)) { if (match.Groups[1].Value == "tile") { fomap.tiles.Add(new Tuple <int, int, string>(x, y, match.Groups[4].Value)); } else if (match.Groups[1].Value == "roof") { fomap.roofs.Add(new Tuple <int, int, string>(x, y, match.Groups[4].Value)); } } } } else if (inObjects && readObjects) { match = Regex.Match(line, reVarVal); if (match.Success) { if (fomap.objects.Count == 0 || match.Groups[1].Value.ToUpper() == "MAPOBJTYPE") { Dictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add(match.Groups[1].Value, match.Groups[2].Value); fomap.objects.Add(dictionary); } else { fomap.objects[fomap.objects.Count - 1].Add(match.Groups[1].Value, match.Groups[2].Value); } } } } return(fomap); } else { return(null); } }