Example #1
0
        private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs args)
        {
            if (args?.Exception == null)
            {
                Logging.LogNullError(nameof(args) + " || " + nameof(args.Exception));
                return;
            }

            Logging.LogFatalException(args.Exception);
        }
        private async Task <HttpResponseMessage> UrlHeadToResponse(string request, string referer = null)
        {
            if (!string.IsNullOrEmpty(request))
            {
                return(await UrlRequest(request, HttpMethod.Head, null, referer).ConfigureAwait(false));
            }

            Logging.LogNullError(nameof(request), Identifier);
            return(null);
        }
Example #3
0
        private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs args)
        {
            if ((sender == null) || (args == null) || (args.Exception == null))
            {
                Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.Exception));
                return;
            }

            Logging.LogGenericException(args.Exception);
        }
Example #4
0
        private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
        {
            if (args?.ExceptionObject == null)
            {
                Logging.LogNullError(nameof(args) + " || " + nameof(args.ExceptionObject));
                return;
            }

            Logging.LogFatalException((Exception)args.ExceptionObject);
        }
        internal static void SetEncryptionKey(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                Logging.LogNullError(nameof(key));
                return;
            }

            EncryptionKey = Encoding.UTF8.GetBytes(key);
        }
Example #6
0
        private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
        {
            if ((sender == null) || (args == null) || (args.ExceptionObject == null))
            {
                Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.ExceptionObject));
                return;
            }

            Logging.LogGenericException((Exception)args.ExceptionObject);
        }
        internal static Task SleepAsync(int miliseconds)
        {
            if (miliseconds >= 0)
            {
                return(Task.Delay(miliseconds));
            }

            Logging.LogNullError(nameof(miliseconds));
            return(Task.FromResult(true));
        }
        internal static Task ForEachAsync <T>(this IEnumerable <T> sequence, Func <T, Task> action)
        {
            if (action != null)
            {
                return(Task.WhenAll(sequence.Select(action)));
            }

            Logging.LogNullError(nameof(action));
            return(Task.FromResult(true));
        }
        private async Task <HttpResponseMessage> UrlPostToResponse(string request, Dictionary <string, string> data = null, string referer = null)
        {
            if (!string.IsNullOrEmpty(request))
            {
                return(await UrlRequest(request, HttpMethod.Post, data, referer).ConfigureAwait(false));
            }

            Logging.LogNullError(nameof(request));
            return(null);
        }
Example #10
0
        private async Task <HttpResponseMessage> UrlPostToResponse(string request, IEnumerable <KeyValuePair <string, string> > data = null, string referer = null)
        {
            if (!string.IsNullOrEmpty(request))
            {
                return(await UrlRequest(request, HttpMethod.Post, data, referer).ConfigureAwait(false));
            }

            Logging.LogNullError(nameof(request), Identifier);
            return(null);
        }
Example #11
0
        internal void CorrectDeviceID(string deviceID)
        {
            if (string.IsNullOrEmpty(deviceID))
            {
                Logging.LogNullError(nameof(deviceID), Bot.BotName);
                return;
            }

            DeviceID = deviceID;
        }
Example #12
0
        private bool FarmHours(ConcurrentHashSet <Game> games)
        {
            if ((games == null) || (games.Count == 0))
            {
                Logging.LogNullError(nameof(games), Bot.BotName);
                return(false);
            }

            float maxHour = games.Max(game => game.HoursPlayed);

            if (maxHour < 0)
            {
                Logging.LogNullError(nameof(maxHour), Bot.BotName);
                return(false);
            }

            if (maxHour >= 2)
            {
                Logging.LogGenericError("Received request for past-2h games!", Bot.BotName);
                return(true);
            }

            Bot.ArchiHandler.PlayGames(games.Select(game => game.AppID), Bot.BotConfig.CustomGamePlayedWhileFarming);

            bool success = true;

            while (maxHour < 2)
            {
                Logging.LogGenericInfo("Still farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName);

                DateTime startFarmingPeriod = DateTime.Now;
                if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay))
                {
                    FarmResetEvent.Reset();
                    success = KeepFarming;
                }

                // Don't forget to update our GamesToFarm hours
                float timePlayed = (float)DateTime.Now.Subtract(startFarmingPeriod).TotalHours;
                foreach (Game game in games)
                {
                    game.HoursPlayed += timePlayed;
                }

                if (!success)
                {
                    break;
                }

                maxHour += timePlayed;
            }

            Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName);
            return(success);
        }
        private static bool ParseItems(Dictionary <ulong, Tuple <uint, Steam.Item.EType> > descriptions, List <KeyValue> input, HashSet <Steam.Item> output)
        {
            if ((descriptions == null) || (input == null) || (input.Count == 0) || (output == null))
            {
                Logging.LogNullError(nameof(descriptions) + " || " + nameof(input) + " || " + nameof(output));
                return(false);
            }

            foreach (KeyValue item in input)
            {
                uint appID = item["appid"].AsUnsignedInteger();
                if (appID == 0)
                {
                    Logging.LogNullError(nameof(appID));
                    return(false);
                }

                ulong contextID = item["contextid"].AsUnsignedLong();
                if (contextID == 0)
                {
                    Logging.LogNullError(nameof(contextID));
                    return(false);
                }

                ulong classID = item["classid"].AsUnsignedLong();
                if (classID == 0)
                {
                    Logging.LogNullError(nameof(classID));
                    return(false);
                }

                uint amount = item["amount"].AsUnsignedInteger();
                if (amount == 0)
                {
                    Logging.LogNullError(nameof(amount));
                    return(false);
                }

                uint             realAppID = 0;
                Steam.Item.EType type      = Steam.Item.EType.Unknown;

                Tuple <uint, Steam.Item.EType> description;
                if (descriptions.TryGetValue(classID, out description))
                {
                    realAppID = description.Item1;
                    type      = description.Item2;
                }

                Steam.Item steamItem = new Steam.Item(appID, contextID, classID, amount, realAppID, type);
                output.Add(steamItem);
            }

            return(true);
        }
Example #14
0
        internal async Task <Dictionary <uint, string> > GetOwnedGames()
        {
            if (!await RefreshSessionIfNeeded().ConfigureAwait(false))
            {
                return(null);
            }

            string request = SteamCommunityURL + "/my/games/?xml=1";

            XmlDocument response = await WebBrowser.UrlGetToXMLRetry(request).ConfigureAwait(false);

            if (response == null)
            {
                return(null);
            }

            XmlNodeList xmlNodeList = response.SelectNodes("gamesList/games/game");

            if ((xmlNodeList == null) || (xmlNodeList.Count == 0))
            {
                return(null);
            }

            Dictionary <uint, string> result = new Dictionary <uint, string>(xmlNodeList.Count);

            foreach (XmlNode xmlNode in xmlNodeList)
            {
                XmlNode appNode = xmlNode.SelectSingleNode("appID");
                if (appNode == null)
                {
                    Logging.LogNullError(nameof(appNode), Bot.BotName);
                    continue;
                }

                uint appID;
                if (!uint.TryParse(appNode.InnerText, out appID))
                {
                    Logging.LogNullError(nameof(appID), Bot.BotName);
                    continue;
                }

                XmlNode nameNode = xmlNode.SelectSingleNode("name");
                if (nameNode == null)
                {
                    Logging.LogNullError(nameof(nameNode), Bot.BotName);
                    continue;
                }

                result[appID] = nameNode.InnerText;
            }

            return(result);
        }
        private async Task <bool> UrlPost(string request, Dictionary <string, string> data = null, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                Logging.LogNullError(nameof(request));
                return(false);
            }

            using (HttpResponseMessage response = await UrlPostToResponse(request, data, referer).ConfigureAwait(false)) {
                return(response != null);
            }
        }
Example #16
0
        internal async Task <string> GenerateToken()
        {
            uint time = await GetSteamTime().ConfigureAwait(false);

            if (time != 0)
            {
                return(GenerateTokenForTime(time));
            }

            Logging.LogNullError(nameof(time), Bot.BotName);
            return(null);
        }
        private async Task <bool> UrlHead(string request, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                Logging.LogNullError(nameof(request));
                return(false);
            }

            using (HttpResponseMessage response = await UrlHeadToResponse(request, referer).ConfigureAwait(false)) {
                return(response != null);
            }
        }
        private void HandleSharedLibraryLockStatus(IPacketMsg packetMsg)
        {
            if (packetMsg == null)
            {
                Logging.LogNullError(nameof(packetMsg), Bot.BotName);
                return;
            }

            ClientMsgProtobuf <CMsgClientSharedLibraryLockStatus> response = new ClientMsgProtobuf <CMsgClientSharedLibraryLockStatus>(packetMsg);

            Client.PostCallback(new SharedLibraryLockStatusCallback(packetMsg.TargetJobID, response.Body));
        }
Example #19
0
        private async Task <bool> UrlPost(string request, IEnumerable <KeyValuePair <string, string> > data = null, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                Logging.LogNullError(nameof(request), Identifier);
                return(false);
            }

            using (HttpResponseMessage response = await UrlPostToResponse(request, data, referer).ConfigureAwait(false)) {
                return(response != null);
            }
        }
        private void HandleUserNotifications(IPacketMsg packetMsg)
        {
            if (packetMsg == null)
            {
                Logging.LogNullError(nameof(packetMsg), Bot.BotName);
                return;
            }

            ClientMsgProtobuf <CMsgClientUserNotifications> response = new ClientMsgProtobuf <CMsgClientUserNotifications>(packetMsg);

            Client.PostCallback(new NotificationsCallback(packetMsg.TargetJobID, response.Body));
        }
        private void HandlePlayingSessionState(IPacketMsg packetMsg)
        {
            if (packetMsg == null)
            {
                Logging.LogNullError(nameof(packetMsg), Bot.BotName);
                return;
            }

            ClientMsgProtobuf <CMsgClientPlayingSessionState> response = new ClientMsgProtobuf <CMsgClientPlayingSessionState>(packetMsg);

            Client.PostCallback(new PlayingSessionStateCallback(packetMsg.TargetJobID, response.Body));
        }
        private void HandleRedeemGuestPassResponse(IPacketMsg packetMsg)
        {
            if (packetMsg == null)
            {
                Logging.LogNullError(nameof(packetMsg), Bot.BotName);
                return;
            }

            ClientMsgProtobuf <CMsgClientRedeemGuestPassResponse> response = new ClientMsgProtobuf <CMsgClientRedeemGuestPassResponse>(packetMsg);

            Client.PostCallback(new RedeemGuestPassResponseCallback(packetMsg.TargetJobID, response.Body));
        }
Example #23
0
        private static void OnRenamed(object sender, RenamedEventArgs e)
        {
            if ((sender == null) || (e == null))
            {
                Logging.LogNullError(nameof(sender) + " || " + nameof(e));
                return;
            }

            Bot bot = GetBotFromConfigFileName(e.OldName);

            bot?.OnNewConfigLoaded(new BotConfigEventArgs()).Forget();
        }
        private void HandleFSOfflineMessageNotification(IPacketMsg packetMsg)
        {
            if (packetMsg == null)
            {
                Logging.LogNullError(nameof(packetMsg), Bot.BotName);
                return;
            }

            ClientMsgProtobuf <CMsgClientOfflineMessageNotification> response = new ClientMsgProtobuf <CMsgClientOfflineMessageNotification>(packetMsg);

            Client.PostCallback(new OfflineMessageCallback(packetMsg.TargetJobID, response.Body));
        }
Example #25
0
        private async Task <Uri> UrlHeadToUri(string request, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                Logging.LogNullError(nameof(request), Identifier);
                return(null);
            }

            using (HttpResponseMessage response = await UrlHeadToResponse(request, referer).ConfigureAwait(false)) {
                return(response?.RequestMessage.RequestUri);
            }
        }
Example #26
0
            internal PurchaseResponseCallback(JobID jobID, CMsgClientPurchaseResponse msg)
            {
                if ((jobID == null) || (msg == null))
                {
                    throw new ArgumentNullException(nameof(jobID) + " || " + nameof(msg));
                }

                JobID          = jobID;
                PurchaseResult = (EPurchaseResult)msg.purchase_result_details;

                if (msg.purchase_receipt_info == null)
                {
                    return;
                }

                KeyValue receiptInfo = new KeyValue();

                using (MemoryStream ms = new MemoryStream(msg.purchase_receipt_info)) {
                    if (!receiptInfo.TryReadAsBinary(ms))
                    {
                        Logging.LogNullError(nameof(ms));
                        return;
                    }
                }

                List <KeyValue> lineItems = receiptInfo["lineitems"].Children;

                if (lineItems.Count == 0)
                {
                    return;
                }

                Items = new Dictionary <uint, string>(lineItems.Count);
                foreach (KeyValue lineItem in lineItems)
                {
                    uint packageID = lineItem["PackageID"].AsUnsignedInteger();
                    if (packageID == 0)
                    {
                        Logging.LogNullError(nameof(packageID));
                        return;
                    }

                    string gameName = lineItem["ItemDescription"].Value;
                    if (string.IsNullOrEmpty(gameName))
                    {
                        Logging.LogNullError(nameof(gameName));
                        return;
                    }

                    gameName         = WebUtility.HtmlDecode(gameName);             // Apparently steam expects client to decode sent HTML
                    Items[packageID] = WebUtility.HtmlDecode(gameName);
                }
            }
Example #27
0
        private static Version GetNetVersion()
        {
            uint release;

            using (RegistryKey registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\")) {
                if (registryKey == null)
                {
                    Logging.LogNullError(nameof(registryKey));
                    return(null);
                }

                object releaseObj = registryKey.GetValue("Release");
                if (releaseObj == null)
                {
                    Logging.LogNullError(nameof(releaseObj));
                    return(null);
                }

                if (!uint.TryParse(releaseObj.ToString(), out release) || (release == 0))
                {
                    Logging.LogNullError(nameof(release));
                    return(null);
                }
            }

            if (release >= 394747)
            {
                return(new Version(4, 6, 2));
            }

            if (release >= 394254)
            {
                return(new Version(4, 6, 1));
            }

            if (release >= 393295)
            {
                return(new Version(4, 6));
            }

            if (release >= 379893)
            {
                return(new Version(4, 5, 2));
            }

            if (release >= 378675)
            {
                return(new Version(4, 5, 1));
            }

            return(release >= 378389 ? new Version(4, 5) : null);
        }
Example #28
0
        private static void ParseArgs(IEnumerable <string> args)
        {
            if (args == null)
            {
                Logging.LogNullError(nameof(args));
                return;
            }

            foreach (string arg in args)
            {
                switch (arg)
                {
                case "":
                    break;

                case "--client":
                    Mode = EMode.Client;
                    break;

                case "--server":
                    Mode = EMode.Server;
                    WCF.StartServer();
                    break;

                default:
                    if (arg.StartsWith("--", StringComparison.Ordinal))
                    {
                        Logging.LogGenericWarning("Unrecognized parameter: " + arg);
                        continue;
                    }

                    if (Mode != EMode.Client)
                    {
                        Logging.LogGenericWarning("Ignoring command because --client wasn't specified: " + arg);
                        continue;
                    }

                    Logging.LogGenericInfo("Command sent: " + arg);

                    // We intentionally execute this async block synchronously
                    Logging.LogGenericInfo("Response received: " + WCF.SendCommand(arg));

                    /*
                     * Task.Run(async () => {
                     *      Logging.LogGenericNotice("WCF", "Response received: " + await WCF.SendCommand(arg).ConfigureAwait(false));
                     * }).Wait();
                     */
                    break;
                }
            }
        }
Example #29
0
        internal static BotConfig Load(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                Logging.LogNullError(nameof(filePath));
                return(null);
            }

            if (!File.Exists(filePath))
            {
                return(null);
            }

            BotConfig botConfig;

            try {
                botConfig = JsonConvert.DeserializeObject <BotConfig>(File.ReadAllText(filePath));
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return(null);
            }

            if (botConfig == null)
            {
                Logging.LogNullError(nameof(botConfig));
                return(null);
            }

            // Support encrypted passwords
            if ((botConfig.PasswordFormat != CryptoHelper.ECryptoMethod.PlainText) && !string.IsNullOrEmpty(botConfig.SteamPassword))
            {
                // In worst case password will result in null, which will have to be corrected by user during runtime
                botConfig.SteamPassword = CryptoHelper.Decrypt(botConfig.PasswordFormat, botConfig.SteamPassword);
            }

            // User might not know what he's doing
            // Ensure that he can't screw core ASF variables
            if (botConfig.GamesPlayedWhileIdle.Count <= CardsFarmer.MaxGamesPlayedConcurrently)
            {
                return(botConfig);
            }

            Logging.LogGenericWarning("Playing more than " + CardsFarmer.MaxGamesPlayedConcurrently + " games concurrently is not possible, only first " + CardsFarmer.MaxGamesPlayedConcurrently + " entries from GamesPlayedWhileIdle will be used");

            HashSet <uint> validGames = new HashSet <uint>(botConfig.GamesPlayedWhileIdle.Take(CardsFarmer.MaxGamesPlayedConcurrently));

            botConfig.GamesPlayedWhileIdle.IntersectWith(validGames);
            botConfig.GamesPlayedWhileIdle.TrimExcess();

            return(botConfig);
        }
Example #30
0
        internal Dictionary <uint, string> GetOwnedGames(ulong steamID)
        {
            if ((steamID == 0) || string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey))
            {
                // TODO: Correct this when Mono 4.4+ will be a latest stable one | https://bugzilla.xamarin.com/show_bug.cgi?id=39455
                Logging.LogNullError("steamID || SteamApiKey", Bot.BotName);
                //Logging.LogNullError(nameof(steamID) + " || " + nameof(Bot.BotConfig.SteamApiKey), Bot.BotName);
                return(null);
            }

            KeyValue response = null;

            using (dynamic iPlayerService = WebAPI.GetInterface("IPlayerService", Bot.BotConfig.SteamApiKey)) {
                iPlayerService.Timeout = Timeout;

                for (byte i = 0; (i < WebBrowser.MaxRetries) && (response == null); i++)
                {
                    try {
                        response = iPlayerService.GetOwnedGames(
                            steamid: steamID,
                            include_appinfo: 1,
                            secure: !Program.GlobalConfig.ForceHttp
                            );
                    } catch (Exception e) {
                        Logging.LogGenericException(e, Bot.BotName);
                    }
                }
            }

            if (response == null)
            {
                Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName);
                return(null);
            }

            Dictionary <uint, string> result = new Dictionary <uint, string>(response["games"].Children.Count);

            foreach (KeyValue game in response["games"].Children)
            {
                uint appID = (uint)game["appid"].AsUnsignedLong();
                if (appID == 0)
                {
                    Logging.LogNullError(nameof(appID));
                    continue;
                }

                result[appID] = game["name"].Value;
            }

            return(result);
        }