public int FromJson(string json, int idBase) { if (string.IsNullOrEmpty(json)) { Init(1, 1); return 0; } var obj = JsonConvert.DeserializeObject<json_dat>(json); var dat = ZlibStream.UncompressBuffer(obj.data); Init(obj.width, obj.height); Dictionary<short, MapTileDesc> tileDict = new Dictionary<short, MapTileDesc>(); Dictionary<short, List<obj>> objDict = new Dictionary<short, List<obj>>(); for (int i = 0; i < obj.dict.Length; i++) { var o = new List<obj>(); tileDict[(short)i] = loc2Tile(obj.dict[i], o); if (o.Count > 0) objDict[(short)i] = o; } var objs = new List<Tuple<Position, obj>>(); int c = 0; using (NReader rdr = new NReader(new MemoryStream(dat))) for (int y = 0; y < obj.height; y++) for (int x = 0; x < obj.width; x++) { var k = rdr.ReadInt16(); if (tileDict[k].ObjType != 0) { c++; tiles[x, y] = new MapTile() { Desc = tileDict[k], ObjId = idBase + c }; } else tiles[x, y] = new MapTile() { Desc = tileDict[k], ObjId = 0 }; List<obj> o; if (objDict.TryGetValue(k, out o)) foreach (var i in o) objs.Add(new Tuple<Position, obj>(new Position() { X = x, Y = y }, i)); } descs = objs.ToArray(); return c; }
public int FromJson(string json, int idBase) { if (string.IsNullOrEmpty(json)) { Init(1, 1); return(0); } var obj = JsonConvert.DeserializeObject <json_dat>(json); var dat = ZlibStream.UncompressBuffer(obj.data); Init(obj.width, obj.height); Dictionary <short, MapTileDesc> tileDict = new Dictionary <short, MapTileDesc>(); Dictionary <short, List <obj> > objDict = new Dictionary <short, List <obj> >(); for (int i = 0; i < obj.dict.Length; i++) { var o = new List <obj>(); tileDict[(short)i] = loc2Tile(obj.dict[i], o); if (o.Count > 0) { objDict[(short)i] = o; } } var objs = new List <Tuple <Position, obj> >(); int c = 0; using (NReader rdr = new NReader(new MemoryStream(dat))) for (int y = 0; y < obj.height; y++) { for (int x = 0; x < obj.width; x++) { var k = rdr.ReadInt16(); if (tileDict[k].ObjType != 0) { c++; tiles[x, y] = new MapTile() { Desc = tileDict[k], ObjId = idBase + c }; } else { tiles[x, y] = new MapTile() { Desc = tileDict[k], ObjId = 0 } }; List <obj> o; if (objDict.TryGetValue(k, out o)) { foreach (var i in o) { objs.Add(new Tuple <Position, obj>(new Position() { X = x, Y = y }, i)); } } } } descs = objs.ToArray(); return(c); } //public string ToJson() //{ // var obj = new json_dat(); // obj.width = Width; obj.height = Height; // Dictionary<Position, TileRegion> regs = new Dictionary<Position, TileRegion>(); // foreach (var i in Regions) // foreach (var j in i.Value) // regs.Add(j, i.Key); // List<loc> locs = new List<loc>(); // MemoryStream ms = new MemoryStream(); // using (NWriter wtr = new NWriter(ms)) // for (int y = 0; y < obj.height; y++) // for (int x = 0; x < obj.width; x++) // { // var loc = new loc(); // loc.ground = Grounds.type2id[(short)Tiles[x][y]]; // loc.objs = descs.Where(_ => _.Item1.X == x && _.Item1.Y == y).Select(_ => _.Item2).ToArray(); // TileRegion reg; // if (regs.TryGetValue(new Position() { X = x, Y = y }, out reg)) // loc.regions = new obj[] { new obj() { id = reg.ToString().Replace('_', ' ') } }; // int ix = locs.IndexOf(loc); // if (ix == -1) // { // ix = locs.Count; // locs.Add(loc); // } // wtr.Write((short)ix); // } // obj.data = ZlibStream.CompressBuffer(ms.ToArray()); // obj.dict = locs.ToArray(); // return JsonConvert.SerializeObject(obj); //} }