public int CompareTo(Map other) { return filename.CompareTo(other.filename); }
private void ParseTrn(string file, Map map) { }
private void ParseBzn(string file, Map map) { using(FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { using(BinaryReader br = new BinaryReader(fs)) { List<byte> bytes = br.ReadBytes((int)fs.Length).ToList(); // don't check the last 6 bytes. "Mission" is 7 bytes for(int i = 0; i < bytes.Count - (MISSION_BYTES.Length - 1); ++i) { if(bytes[i] == MISSION_BYTES[0]) { bool foundMission = true; for(int j = 1; j < MISSION_BYTES.Length; ++j) { if(bytes[i + j] != MISSION_BYTES[j]) { foundMission = false; break; } } if(foundMission) { string prefix = System.Text.Encoding.ASCII.GetString(bytes.GetRange(i - 6, 6).ToArray()); switch(prefix) { case "MultST": map.type = Map.Type.Strategy; break; case "MultDM": map.type = Map.Type.Deathmatch; break; case "Inst4X": map.type = Map.Type.InstantAction; break; default: if(prefix.Substring(1) == "Empty") { map.type = Map.Type.InstantAction; } else if(prefix.Substring(3) == "Lua") { map.type = Map.Type.InstantAction; } else { map.type = Map.Type.Unknown; } break; } break; } } } } } }
// functions to read des, trn, bzn, etc files private void ParseDes(string file, Map map) { using(StreamReader sr = new StreamReader(file)) { string line = null; while((line = sr.ReadLine()) != null) { int colonIdx = line.IndexOf(':'); if(colonIdx != -1 && line.Length > colonIdx) { string key = line.Substring(0, colonIdx).Trim(); string value = line.Substring(colonIdx + 1).Trim(); if(value.Length == 0) continue; switch(key) { case "WORLD": map.world = value; break; case "SIZE": map.size = value; break; case "POWERUPS": map.powerups = value == "Yes"; break; case "GEYSERS": map.geysers = uint.Parse(value); break; case "SCRAP": map.scrap = uint.Parse(value); break; case "AUTHOR": map.author = value; break; case "VERSION": map.version = value; break; default: break; } } } } }
private Map LoadMap(string path) { string desFile = path + ".des"; string trnFile = path + ".trn"; string bznFile = path + ".bzn"; Map ret = new Map(); ret.bznPath = path.Substring(0, path.LastIndexOfAny(new char[] { '/', '\\' })); ret.filename = Path.GetFileNameWithoutExtension(path); if(File.Exists(desFile)) ParseDes(desFile, ret); else if(File.Exists(trnFile)) ParseTrn(trnFile, ret); ParseBzn(bznFile, ret); return ret.type == Map.Type.InstantAction ? ret : null; }