Esempio n. 1
0
        private static async Task OnCreatedJsonFile(string name, string fullPath)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(fullPath))
            {
                ArchiLogger.LogNullError(nameof(name) + " || " + nameof(fullPath));

                return;
            }

            string botName = Path.GetFileNameWithoutExtension(name);

            if (string.IsNullOrEmpty(botName) || (botName[0] == '.'))
            {
                return;
            }

            if (!await CanHandleWriteEvent(fullPath).ConfigureAwait(false))
            {
                return;
            }

            if (botName.Equals(SharedInfo.ASF, StringComparison.OrdinalIgnoreCase))
            {
                ArchiLogger.LogGenericInfo(Strings.GlobalConfigChanged);
                await RestartOrExit().ConfigureAwait(false);

                return;
            }

            if (!IsValidBotName(botName))
            {
                return;
            }

            if (Bot.Bots.TryGetValue(botName, out Bot bot))
            {
                await bot.OnConfigChanged(false).ConfigureAwait(false);
            }
            else
            {
                await Bot.RegisterBot(botName).ConfigureAwait(false);

                if (Bot.Bots.Count > MaximumRecommendedBotsCount)
                {
                    ArchiLogger.LogGenericWarning(string.Format(Strings.WarningExcessiveBotsCount, MaximumRecommendedBotsCount));
                }
            }
        }
Esempio n. 2
0
        private static async Task OnCreatedConfigFile(string name, string fullPath)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(fullPath))
            {
                ArchiLogger.LogNullError(nameof(name) + " || " + nameof(fullPath));

                return;
            }

            string botName = Path.GetFileNameWithoutExtension(name);

            if (string.IsNullOrEmpty(botName) || (botName[0] == '.'))
            {
                return;
            }

            if (!await CanHandleWriteEvent(name).ConfigureAwait(false))
            {
                return;
            }

            if (botName.Equals(SharedInfo.ASF))
            {
                ArchiLogger.LogGenericInfo(Strings.GlobalConfigChanged);
                await RestartOrExit().ConfigureAwait(false);

                return;
            }

            if (!IsValidBotName(botName))
            {
                return;
            }

            if (Bot.Bots.TryGetValue(botName, out Bot bot))
            {
                await bot.OnConfigChanged(false).ConfigureAwait(false);
            }
            else
            {
                await Bot.RegisterBot(botName).ConfigureAwait(false);
            }
        }
Esempio n. 3
0
        internal static async Task InitBots()
        {
            if (Bot.Bots.Count != 0)
            {
                return;
            }

            // Before attempting to connect, initialize our configuration
            await Bot.InitializeSteamConfiguration(Program.GlobalConfig.SteamProtocols, Program.GlobalDatabase.CellID, Program.GlobalDatabase.ServerListProvider).ConfigureAwait(false);

            foreach (string botName in Directory.EnumerateFiles(SharedInfo.ConfigDirectory, "*.json").Select(Path.GetFileNameWithoutExtension).Where(botName => !string.IsNullOrEmpty(botName) && IsValidBotName(botName)).OrderBy(botName => botName))
            {
                Bot.RegisterBot(botName);
            }

            if (Bot.Bots.Count == 0)
            {
                ArchiLogger.LogGenericWarning(Strings.ErrorNoBotsDefined);
            }
        }
Esempio n. 4
0
        private static async Task CreateBot(string botName)
        {
            if (string.IsNullOrEmpty(botName))
            {
                ArchiLogger.LogNullError(nameof(botName));
                return;
            }

            if (Bot.Bots.ContainsKey(botName))
            {
                return;
            }

            // It's entirely possible that some process is still accessing our file, allow at least a second before trying to read it
            await Task.Delay(1000).ConfigureAwait(false);

            if (Bot.Bots.ContainsKey(botName))
            {
                return;
            }

            Bot.RegisterBot(botName);
        }
Esempio n. 5
0
        private static async Task OnCreatedConfigFile(string name, string fullPath)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(fullPath))
            {
                ArchiLogger.LogNullError(nameof(name) + " || " + nameof(fullPath));
                return;
            }

            string botName = Path.GetFileNameWithoutExtension(name);

            if (string.IsNullOrEmpty(botName) || (botName[0] == '.'))
            {
                return;
            }

            DateTime lastWriteTime = DateTime.UtcNow;

            if (LastWriteTimes.TryGetValue(name, out DateTime savedLastWriteTime))
            {
                if (savedLastWriteTime >= lastWriteTime)
                {
                    return;
                }
            }

            LastWriteTimes[name] = lastWriteTime;

            // It's entirely possible that some process is still accessing our file, allow at least a second before trying to read it
            await Task.Delay(1000).ConfigureAwait(false);

            // It's also possible that we got some other event in the meantime
            if (LastWriteTimes.TryGetValue(name, out savedLastWriteTime))
            {
                if (lastWriteTime != savedLastWriteTime)
                {
                    return;
                }

                if (LastWriteTimes.TryRemove(name, out savedLastWriteTime))
                {
                    if (lastWriteTime != savedLastWriteTime)
                    {
                        return;
                    }
                }
            }

            if (botName.Equals(SharedInfo.ASF))
            {
                ArchiLogger.LogGenericInfo(Strings.GlobalConfigChanged);
                await RestartOrExit().ConfigureAwait(false);

                return;
            }

            if (!IsValidBotName(botName))
            {
                return;
            }

            if (Bot.Bots.TryGetValue(botName, out Bot bot))
            {
                await bot.OnConfigChanged(false).ConfigureAwait(false);
            }
            else
            {
                await Bot.RegisterBot(botName).ConfigureAwait(false);
            }
        }