コード例 #1
0
        //MapPacket
        private static void HandlePacket(MapPacket packet)
        {
            if (packet.Deleted)
            {
                if (MapInstance.Get(packet.MapId) != null)
                {
                    if (Globals.CurrentMap == MapInstance.Get(packet.MapId))
                    {
                        Globals.MainForm.EnterMap(MapList.List.FindFirstMap());
                    }

                    MapInstance.Get(packet.MapId).Delete();
                }
            }
            else
            {
                var map = new MapInstance(packet.MapId);
                map.Load(packet.Data);
                map.LoadTileData(packet.TileData);
                map.AttributeData = packet.AttributeData;
                map.MapGridX      = packet.GridX;
                map.MapGridY      = packet.GridY;
                map.SaveStateAsUnchanged();
                map.InitAutotiles();
                map.UpdateAdjacentAutotiles();
                if (MapInstance.Get(packet.MapId) != null)
                {
                    lock (MapInstance.Get(packet.MapId).MapLock)
                    {
                        if (Globals.CurrentMap == MapInstance.Get(packet.MapId))
                        {
                            Globals.CurrentMap = map;
                        }

                        MapInstance.Get(packet.MapId).Delete();
                    }
                }

                MapInstance.Lookup.Set(packet.MapId, map);
                if (!Globals.InEditor && Globals.HasGameData)
                {
                    Globals.CurrentMap = map;
                    Globals.LoginForm.BeginInvoke(Globals.LoginForm.EditorLoopDelegate);
                }
                else if (Globals.InEditor)
                {
                    if (Globals.FetchingMapPreviews || Globals.CurrentMap == map)
                    {
                        var currentMapId = Globals.CurrentMap.Id;
                        var img          = Database.LoadMapCacheLegacy(packet.MapId, map.Revision);
                        if (img == null && !Globals.MapsToScreenshot.Contains(packet.MapId))
                        {
                            Globals.MapsToScreenshot.Add(packet.MapId);
                        }

                        img?.Dispose();
                        if (Globals.FetchingMapPreviews)
                        {
                            if (Globals.MapsToFetch.Contains(packet.MapId))
                            {
                                Globals.MapsToFetch.Remove(packet.MapId);
                                if (Globals.MapsToFetch.Count == 0)
                                {
                                    Globals.FetchingMapPreviews = false;
                                    Globals.PreviewProgressForm.BeginInvoke(
                                        (MethodInvoker) delegate { Globals.PreviewProgressForm.Dispose(); }
                                        );
                                }
                                else
                                {
                                    //TODO Localize
                                    Globals.PreviewProgressForm.SetProgress(
                                        "Fetching Maps: " +
                                        (Globals.FetchCount - Globals.MapsToFetch.Count) +
                                        "/" +
                                        Globals.FetchCount,
                                        (int)((float)(Globals.FetchCount - Globals.MapsToFetch.Count) /
                                              (float)Globals.FetchCount *
                                              100f), false
                                        );
                                }
                            }
                        }

                        Globals.CurrentMap = MapInstance.Get(currentMapId);
                    }

                    if (packet.MapId != Globals.LoadingMap)
                    {
                        return;
                    }

                    Globals.CurrentMap = MapInstance.Get(Globals.LoadingMap);
                    MapUpdatedDelegate();
                    if (map.Up != Guid.Empty)
                    {
                        PacketSender.SendNeedMap(map.Up);
                    }

                    if (map.Down != Guid.Empty)
                    {
                        PacketSender.SendNeedMap(map.Down);
                    }

                    if (map.Left != Guid.Empty)
                    {
                        PacketSender.SendNeedMap(map.Left);
                    }

                    if (map.Right != Guid.Empty)
                    {
                        PacketSender.SendNeedMap(map.Right);
                    }
                }

                if (Globals.CurrentMap.Id == packet.MapId && Globals.MapGrid != null && Globals.MapGrid.Loaded)
                {
                    for (var y = Globals.CurrentMap.MapGridY + 1; y >= Globals.CurrentMap.MapGridY - 1; y--)
                    {
                        for (var x = Globals.CurrentMap.MapGridX - 1; x <= Globals.CurrentMap.MapGridX + 1; x++)
                        {
                            if (x >= 0 && x < Globals.MapGrid.GridWidth && y >= 0 && y < Globals.MapGrid.GridHeight)
                            {
                                var needMap = MapInstance.Get(Globals.MapGrid.Grid[x, y].MapId);
                                if (needMap == null && Globals.MapGrid.Grid[x, y].MapId != Guid.Empty)
                                {
                                    PacketSender.SendNeedMap(Globals.MapGrid.Grid[x, y].MapId);
                                }
                            }
                        }
                    }
                }
            }
        }