示例#1
0
        static void SaveCore(Level lvl)
        {
            PlayerBot[] bots = lvl.Bots.Items;
            string      path = Paths.BotsPath(lvl.MapName);

            if (!File.Exists(path) && bots.Length == 0)
            {
                return;
            }

            List <BotProperties> props = new List <BotProperties>(bots.Length);

            for (int i = 0; i < bots.Length; i++)
            {
                BotProperties data = new BotProperties();
                data.FromBot(bots[i]);
                props.Add(data);
            }

            try {
                using (StreamWriter w = new StreamWriter(path)) { WriteAll(w, props); }
            } catch (Exception ex) {
                Logger.LogError("Error saving bots to " + path, ex);
            }
        }
示例#2
0
        static void LoadCore(Level lvl)
        {
            string path = Paths.BotsPath(lvl.MapName);

            if (!File.Exists(path))
            {
                return;
            }
            List <BotProperties> props = null;

            try {
                props = ReadAll(path);
            } catch (Exception ex) {
                Logger.LogError("Reading bots file", ex); return;
            }

            foreach (BotProperties data in props)
            {
                PlayerBot bot = new PlayerBot(data.Name, lvl);
                data.ApplyTo(bot);

                bot.SetModel(bot.Model);
                LoadAi(data, bot);
                PlayerBot.Add(bot, false);
            }
        }
示例#3
0
        internal static void UpgradeBots(SchedulerTask task)
        {
            if (!File.Exists(oldBotsFile))
            {
                return;
            }
            File.Copy(oldBotsFile, oldBotsFile + ".bak", true);
            Logger.Log(LogType.SystemActivity, "Making bots file per-level.. " +
                       "saved backup of global bots file to extra/bots.json.bak");

            List <BotProperties> bots = BotsFile.ReadAll(oldBotsFile);
            Dictionary <string, List <BotProperties> > botsByLevel = new Dictionary <string, List <BotProperties> >();

            foreach (BotProperties bot in bots)
            {
                List <BotProperties> levelBots;
                if (String.IsNullOrEmpty(bot.Level))
                {
                    continue;
                }

                if (!botsByLevel.TryGetValue(bot.Level, out levelBots))
                {
                    levelBots = new List <BotProperties>();
                    botsByLevel[bot.Level] = levelBots;
                }
                levelBots.Add(bot);
            }

            foreach (var kvp in botsByLevel)
            {
                string path = Paths.BotsPath(kvp.Key);
                using (StreamWriter w = new StreamWriter(path)) {
                    BotsFile.WriteAll(w, kvp.Value);
                }
            }

            if (Server.mainLevel.Bots.Count == 0)
            {
                BotsFile.Load(Server.mainLevel);
            }
            File.Delete(oldBotsFile);
        }