Example #1
0
        public static async Task <bool> SaveMap(string name, MapManager.MapData data)
        {
            return(await Task.Run(() =>
            {
                try
                {
                    lock (data)
                    {
                        System.Diagnostics.Stopwatch alltime = new System.Diagnostics.Stopwatch();
                        alltime.Start();
                        byte[] map = MessagePackSerializer.Serialize(value: data, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block));
                        alltime.Stop();

                        /*if(data.Owner != -1 && Math.Round(((double)map.Length) / 1024 / 1024, 4) < 2.5)
                         * {
                         *  if (UserManager.TryGetEPlayerFromID(data.Owner, out var eplr))
                         *  {
                         *      eplr.SendErrorEX($"[InternalError] 序列化属地时发生致命错误, 为保证数据安全已重置此次属地改动.");
                         *  }
                         *  Log.Error($"序列化属地 {data.Name} 时发生致命错误.");
                         *  return false;
                         * }*/
                        Log.Info($"序列化地图 {name}<{data.Name}> 耗时 {alltime.ElapsedMilliseconds} ms, 文件大小为 {Math.Round(((double)map.Length) / 1024 / 1024, 4)} MB, 地图大小为 {data.Width} * {data.Height}.\n开始上传至数据库.");
                        IDbConnection dbConnection = TShock.DB.CloneEx();
                        QueryResult result;
                        try
                        {
                            dbConnection.Open();
                            using (IDbCommand dbCommand = dbConnection.CreateCommand())
                            {
                                var args = new object[] { name, map, map.Length };
                                dbCommand.CommandText = $"REPLACE INTO EternalLandMap SET Name=@0,Data=@1,Length=@2";
                                for (int i = 0; i < args.Length; i++)
                                {
                                    dbCommand.AddParameter("@" + i.ToString(), args[i]);
                                }
                                dbCommand.CommandTimeout = 60000;
                                result = new QueryResult(dbConnection, dbCommand.ExecuteReader());
                                if (!GameData.Map.ContainsKey(name))
                                {
                                    GameData.Map.Add(name, map);
                                }
                                else
                                {
                                    GameData.Map[name] = map;
                                }
                                Log.Info($"地图 {name}<{data.Name}> 上传完成.");
                                return true;
                            }
                        }
                        catch (Exception innerException)
                        {
                            Log.Error("SQL异常.\n" + innerException);
                        }
                    }
                }
                catch (Exception ex) { Log.Error(ex.InnerException == null ? ex : ex.InnerException); }
                return false;
            }));
        }
Example #2
0
 public static bool GetMapData(string name, out MapManager.MapData map)
 {
     map = null;
     if (!Map.ContainsKey(name))
     {
         return(false);
     }
     try
     {
         map = GetMapDataDirect(name).Result;
         if (map == null)
         {
             return(false);
         }
         return(true);
     }
     catch (Exception ex) { Log.Error(ex); map = null; return(false); }
 }
Example #3
0
        public async static void AdminCommand(CommandArgs args)
        {
            var tsp  = args.Player;
            var eplr = tsp.EPlayer();

            if (eplr == null)
            {
                tsp.SendErrorEX("你尚未登录.");
                return;
            }
            string error = "命令無效. ";
            var    cmd   = args.Parameters;

            switch (args.Message.Split(' ')[0].ToLower())
            {
            case "/char":
                if (args.Parameters.Count >= 1)
                {
                    switch (cmd[0].ToLower())
                    {
                    case "save":
                        if (cmd.Count < 2)
                        {
                            tsp.SendErrorEX(error);
                            break;
                        }
                        else if (GameData.Character.ContainsKey(cmd[1]))
                        {
                            tsp.SendErrorEX("角色已存在. 如想更新角色信息请使用//updatecharacter <名称>");
                            break;
                        }
                        var data = new EPlayerData(tsp, cmd[1]);
                        DataBase.SaveCharacter(data);
                        GameData.Character.Add(cmd[1], data);
                        tsp.SendSuccessEX("执行完成.");
                        break;

                    case "update":
                        if (cmd.Count < 2)
                        {
                            tsp.SendErrorEX(error);
                            break;
                        }
                        else if (!GameData.Character.ContainsKey(cmd[1]))
                        {
                            tsp.SendErrorEX($"未找到角色存档: {cmd[1]}");
                            break;
                        }
                        data = new EPlayerData(tsp, cmd[1]);
                        DataBase.SaveCharacter(data);
                        GameData.Character[cmd[1]] = data;
                        tsp.SendSuccessEX("已更新角色数据.");
                        break;

                    case "turnto":
                        if (cmd.Count < 2)
                        {
                            tsp.SendErrorEX(error);
                            break;
                        }
                        else if (!GameData.Character.ContainsKey(cmd[1]))
                        {
                            tsp.SendErrorEX($"未找到角色存档: {cmd[1]}");
                            break;
                        }
                        eplr.ChangeCharacter(cmd[1]);
                        tsp.SendSuccessEX("执行完成.");
                        break;

                    case "turnback":
                        eplr.SetToOriginCharacter();
                        tsp.SendSuccessEX("执行完成.");
                        break;
                    }
                }
                break;

            case "/map":
                if (args.Parameters.Count >= 1)
                {
                    switch (cmd[0].ToLower())
                    {
                    case "set":
                        if (eplr.SettingPoint == 1 || eplr.SettingPoint == 2)
                        {
                            eplr.SendEX($"你已在进行区域{(eplr.SettingPoint == 1 ? "左上" : "右下")}角选定.");
                        }
                        else
                        {
                            eplr.SendEX($"开始进行地图区域选定. 请用任何镐子点击左上角物块.");
                            eplr.SettingPoint = 1;
                        }
                        break;

                    case "create":
                        if (cmd.Count < 2)
                        {
                            tsp.SendErrorEX(error);
                            break;
                        }
                        else if (cmd[1] == "MainWorld" || cmd[1] == "DefaultTerritory")
                        {
                            var d = new MapManager.MapData(0, 0, Main.maxTilesX, Main.maxTilesY, cmd[1]);
                            DataBase.SaveMap(cmd[1], d);
                            tsp.SendSuccessEX("执行完成.");
                            return;
                        }
                        else if (eplr.SettingPoint != 0)
                        {
                            tsp.SendErrorEX($"你正在选择第 {eplr.SettingPoint} 个点位, 无法创建地图.");
                            break;
                        }
                        else if (eplr.ChoosePoint[0] == null || eplr.ChoosePoint[1] == null)
                        {
                            tsp.SendErrorEX($"点位选择不完整, 请重新选择.");
                            break;
                        }
                        else if (GameData.Map.ContainsKey(cmd[1]))
                        {
                            tsp.SendErrorEX($"此地图名已存在. 如想更新地图请使用 {"//map update".ToColorful()}.");
                            break;
                        }
                        var data = new MapManager.MapData(eplr.ChoosePoint[0], eplr.ChoosePoint[1], cmd[1]);
                        DataBase.SaveMap(cmd[1], data);
                        tsp.SendSuccessEX("执行完成.");
                        break;

                    case "goto":
                        if (cmd.Count < 4)
                        {
                            tsp.SendErrorEX(error + ", 需包含生成到的坐标");
                            break;
                        }
                        else if (!GameData.Map.ContainsKey(cmd[1]))
                        {
                            tsp.SendErrorEX($"未找到地图: {cmd[1]}");
                            break;
                        }
                        eplr.JoinMap(MapManager.CreateMultiPlayerMap(cmd[1], int.Parse(cmd[2]), int.Parse(cmd[3])));
                        tsp.SendSuccessEX("执行完成.");
                        break;

                    case "update":
                        if (eplr.IsInAnotherWorld)
                        {
                            eplr.Map.Save();
                            eplr.SendSuccessEX("执行完成.");
                        }
                        else
                        {
                            eplr.SendErrorEX("未处于其他地图.");
                        }
                        break;

                    case "setspawn":
                        if (eplr.IsInAnotherWorld)
                        {
                            eplr.Map.SetSpawn(eplr.TileX, eplr.TileY);
                            eplr.Map.Save();
                            eplr.SendSuccessEX("执行完成.");
                        }
                        else
                        {
                            eplr.SendErrorEX("未处于其他地图.");
                        }
                        break;

                    /*case "wld":
                     *  if (cmd.Count > 2) eplr.JoinMap(MapManager.CreateMultiPlayerMap(new MapManager.MapData(2152, 394, int.Parse(cmd[1]), int.Parse(cmd[2])), 4100, 400));
                     *  else eplr.JoinMap(MapManager.CreateMultiPlayerMap(new MapManager.MapData(int.Parse(cmd[1]), int.Parse(cmd[2]), 200, 200), 4100, 450));
                     *  break;
                     * case "clear":
                     *  eplr.JoinMap(MapManager.CreateMultiPlayerMap(new MapManager.MapData(), 4100, 400));
                     *  break;*/
                    case "back":
                        eplr.BackToOriginMap();
                        break;

                    case "join":
                        if (UserManager.TryGetEPlayeFuzzy(cmd[1], out var t))
                        {
                            eplr.JoinMap(t[0].MapUUID);
                        }
                        break;
                    }
                }
                break;
            }
        }
Example #4
0
 public async static Task <List <Item> > GetItemFromTile(int x, int y, OTAPI.Tile.ITile itile, MapManager.MapData map = null)
 {
     return(await Task.Run(() =>
     {
         List <Item> list = new List <Item>(2)
         {
             [0] = new Item(),
             [1] = new Item()
         };
         try
         {
             int id = 0;
             int stack = 0;
             int secondaryitem = 0;
             int secondarystack = 0;
             if (map == null)
             {
                 WorldGen.KillTile_GetItemDrops(x, y, itile, out id, out stack, out secondaryitem, out secondarystack);
             }
             else
             {
                 map.KillTile_GetItemDrops(x, y, itile, out id, out stack, out secondaryitem, out secondarystack);
             }
             list[0].SetDefaults(id);
             list[0].stack = stack;
             list[1].SetDefaults(id);
             list[1].stack = stack;
         }
         catch { }
         return list;
     }));
 }