public override string ToCSharp(List <string> definedVariables, ConfigSettings settings)
        {
            /*
             *   if (Conditions.Check(myVar, StrComparison.Contains, "hello"))
             *     data.STATUS = "SUCCESS";
             *
             *   else if (Conditions.Check(myList, ListComparison.Contains, "item") || Conditions.Check(data.COOKIES, DictComparison.HasKey, "name"))
             *     { data.STATUS = "FAIL"; return; }
             *
             *   else if (myBool)
             *     { data.STATUS = "BAN"; return; }
             */

            using var writer = new StringWriter();
            var banIfNoMatch = Settings["banIfNoMatch"];
            var nonEmpty     = Keychains.Where(kc => kc.Keys.Count > 0).ToList();

            // If there are no keychains
            if (nonEmpty.Count == 0)
            {
                writer.WriteLine($"if ({CSharpWriter.FromSetting(banIfNoMatch)})");

                if (settings.GeneralSettings.ContinueStatuses.Contains("BAN"))
                {
                    writer.WriteLine(" { data.STATUS = \"BAN\"; }");
                    writer.WriteLine("if (CheckGlobalBanKeys(data)) { data.STATUS = \"BAN\"; }");
                }
                else
                {
                    writer.WriteLine("  { data.STATUS = \"BAN\"; return; }");
                    writer.WriteLine("if (CheckGlobalBanKeys(data)) { data.STATUS = \"BAN\"; return; }");
                }

                if (settings.GeneralSettings.ContinueStatuses.Contains("RETRY"))
                {
                    writer.WriteLine("if (CheckGlobalRetryKeys(data)) { data.STATUS = \"RETRY\"; }");
                }
                else
                {
                    writer.WriteLine("if (CheckGlobalRetryKeys(data)) { data.STATUS = \"RETRY\"; return; }");
                }

                return(writer.ToString());
            }

            // Write all the keychains
            for (var i = 0; i < nonEmpty.Count; i++)
            {
                var keychain = nonEmpty[i];

                if (i == 0)
                {
                    writer.Write("if (");
                }
                else
                {
                    writer.Write("else if (");
                }

                var conditions = keychain.Keys.Select(k => CSharpWriter.ConvertKey(k));

                var chainedCondition = keychain.Mode switch
                {
                    KeychainMode.OR => string.Join(" || ", conditions),
                    KeychainMode.AND => string.Join(" && ", conditions),
                    _ => throw new Exception("Invalid Keychain Mode")
                };

                writer.Write(chainedCondition);
                writer.WriteLine(")");

                // Continue on this status
                if (settings.GeneralSettings.ContinueStatuses.Contains(keychain.ResultStatus))
                {
                    writer.WriteLine($" {{ data.STATUS = \"{keychain.ResultStatus}\"; }}");
                }

                // Do not continue on this status (return)
                else
                {
                    writer.WriteLine($"  {{ data.STATUS = \"{keychain.ResultStatus}\"; return; }}");
                }
            }

            // The whole purpose of this is to make the code a bit prettier
            if (banIfNoMatch.InputMode == SettingInputMode.Fixed)
            {
                if (((BoolSetting)banIfNoMatch.FixedSetting).Value)
                {
                    writer.WriteLine("else");

                    if (settings.GeneralSettings.ContinueStatuses.Contains("BAN"))
                    {
                        writer.WriteLine(" { data.STATUS = \"BAN\"; }");
                    }
                    else
                    {
                        writer.WriteLine("  { data.STATUS = \"BAN\"; return; }");
                    }
                }
                else
                {
                }
            }
            else
            {
                writer.WriteLine($"else if ({CSharpWriter.FromSetting(banIfNoMatch)})");

                if (settings.GeneralSettings.ContinueStatuses.Contains("BAN"))
                {
                    writer.WriteLine(" { data.STATUS = \"BAN\"; }");
                }
                else
                {
                    writer.WriteLine("  { data.STATUS = \"BAN\"; return; }");
                }
            }

            // Check global ban keys
            if (settings.GeneralSettings.ContinueStatuses.Contains("BAN"))
            {
                writer.WriteLine("if (CheckGlobalBanKeys(data)) { data.STATUS = \"BAN\"; }");
            }
            else
            {
                writer.WriteLine("if (CheckGlobalBanKeys(data)) { data.STATUS = \"BAN\"; return; }");
            }

            if (settings.GeneralSettings.ContinueStatuses.Contains("RETRY"))
            {
                writer.WriteLine("if (CheckGlobalRetryKeys(data)) { data.STATUS = \"RETRY\"; }");
            }
            else
            {
                writer.WriteLine("if (CheckGlobalRetryKeys(data)) { data.STATUS = \"RETRY\"; return; }");
            }

            return(writer.ToString());
        }
    }
        private string TranspileStatement(string input, List <string> definedVariables)
        {
            Match match;

            // CODE LABEL
            // #MYLABEL => MYLABEL:
            if ((match = Regex.Match(input, $"^#({validTokenRegex})$")).Success)
            {
                return($"{match.Groups[1].Value}:");
            }

            // JUMP
            // JUMP #MYLABEL => goto MYLABEL;
            if ((match = Regex.Match(input, $"^JUMP #({validTokenRegex})$")).Success)
            {
                return($"goto {match.Groups[1].Value};");
            }

            // END
            // END => }
            if (input == "END")
            {
                return("}");
            }

            // REPEAT
            // REPEAT 10 => for (int xyz = 0; xyz < 10; xyz++) {
            if ((match = Regex.Match(input, $"^REPEAT (.+)$")).Success)
            {
                var i = VariableNames.RandomName();
                return($"for (var {i} = 0; {i} < {match.Groups[1].Value}; {i}++){System.Environment.NewLine}{{");
            }

            // FOREACH
            // FOREACH v IN list => foreach (var v in list) {
            if ((match = Regex.Match(input, $"^FOREACH ({validTokenRegex}) IN ({validTokenRegex})$")).Success)
            {
                return($"foreach (var {match.Groups[1].Value} in {match.Groups[2].Value}){System.Environment.NewLine}{{");
            }

            // LOG
            // LOG myVar => data.Logger.Log(myVar);
            if ((match = Regex.Match(input, $"^LOG (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[1].Value});");
            }

            // CLOG
            // CLOG Tomato "hello" => data.Logger.Log("hello", LogColors.Tomato);
            if ((match = Regex.Match(input, $"^CLOG ([A-Za-z]+) (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[2].Value}, LogColors.{match.Groups[1].Value});");
            }

            // WHILE
            // WHILE a < b => while (a < b) {
            if ((match = Regex.Match(input, $"^WHILE (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.KeyTypes.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"while ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"while ({line}){System.Environment.NewLine}{{");
                }
            }

            // IF
            // IF a < b => if (a < b) {
            if ((match = Regex.Match(input, $"^IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.KeyTypes.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"if ({line}){System.Environment.NewLine}{{");
                }
            }

            // ELSE
            // ELSE => } else {
            if (input == "ELSE")
            {
                return($"}}{System.Environment.NewLine}else{System.Environment.NewLine}{{");
            }

            // ELSE IF
            // ELSE IF a < b => } else if (a < b) {
            if ((match = Regex.Match(input, $"ELSE IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.KeyTypes.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"}}{System.Environment.NewLine}else if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"}}{System.Environment.NewLine}else if ({line}){System.Environment.NewLine}{{");
                }
            }

            // TRY
            // TRY => try {
            if (input == "TRY")
            {
                return($"try{System.Environment.NewLine}{{");
            }

            // CATCH
            // CATCH => } catch {
            if (input == "CATCH")
            {
                return($"}}{System.Environment.NewLine}catch{System.Environment.NewLine}{{");
            }

            // LOCK
            // LOCK globals => lock (globals) {
            if ((match = Regex.Match(input, $"^LOCK (.+)$")).Success)
            {
                return($"lock({match.Groups[1].Value}){System.Environment.NewLine}{{");
            }

            // SET VAR
            // SET VAR myString "hello" => string myString = "hello";
            if ((match = Regex.Match(input, $"^SET VAR ({validTokenRegex}) (.+)$")).Success)
            {
                if (definedVariables.Contains(match.Groups[1].Value))
                {
                    return($"{match.Groups[1].Value} = {match.Groups[2].Value};");
                }
                else
                {
                    definedVariables.Add(match.Groups[1].Value);
                    return($"string {match.Groups[1].Value} = {match.Groups[2].Value};");
                }
            }

            // SET CAP
            // SET CAP myCapture "hello" => string myString = "hello"; data.MarkForCapture(nameof(myCapture));
            if ((match = Regex.Match(input, $"^SET CAP ({validTokenRegex}) (.+)$")).Success)
            {
                if (definedVariables.Contains(match.Groups[1].Value))
                {
                    return($"{match.Groups[1].Value} = {match.Groups[2].Value};{System.Environment.NewLine}data.MarkForCapture(nameof({match.Groups[1].Value}));");
                }
                else
                {
                    definedVariables.Add(match.Groups[1].Value);
                    return($"string {match.Groups[1].Value} = {match.Groups[2].Value};{System.Environment.NewLine}data.MarkForCapture(nameof({match.Groups[1].Value}));");
                }
            }

            throw new NotSupportedException();
        }
Beispiel #3
0
        private string TranspileStatement(string input, List <string> definedVariables)
        {
            Match match;

            // (RESOURCES) TAKEONE
            // TAKEONE FROM "MyResource" => "myString"
            if ((match = Regex.Match(input, "TAKEONE FROM (\"[^\"]+\") => @?\"?([^\"]+)\"?")).Success)
            {
                return($"string {match.Groups[2].Value} = globals.Resources[{match.Groups[1].Value}].TakeOne();");
            }

            // (RESOURCES) TAKE
            // TAKE 5 FROM "MyResource" => "myList"
            if ((match = Regex.Match(input, "TAKE ([0-9]+) FROM (\"[^\"]+\") => @?\"?([^\"]+)\"?")).Success)
            {
                return($"List<string> {match.Groups[3].Value} = globals.Resources[{match.Groups[2].Value}].Take({match.Groups[1].Value});");
            }

            // CODE LABEL
            // #MYLABEL => MYLABEL:
            if ((match = Regex.Match(input, $"^#({validTokenRegex})$")).Success)
            {
                return($"{match.Groups[1].Value}:");
            }

            // JUMP
            // JUMP #MYLABEL => goto MYLABEL;
            if ((match = Regex.Match(input, $"^JUMP #({validTokenRegex})$")).Success)
            {
                return($"goto {match.Groups[1].Value};");
            }

            // END
            // END => }
            if (input == "END")
            {
                return("}");
            }

            // REPEAT
            // REPEAT 10 => for (int xyz = 0; xyz < 10; xyz++) {
            if ((match = Regex.Match(input, $"^REPEAT (.+)$")).Success)
            {
                var i = VariableNames.RandomName();
                return($"for (var {i} = 0; {i} < ({match.Groups[1].Value}).AsInt(); {i}++){System.Environment.NewLine}{{");
            }

            // FOREACH
            // FOREACH v IN list => foreach (var v in list) {
            if ((match = Regex.Match(input, $"^FOREACH ({validTokenRegex}) IN ({validTokenRegex})$")).Success)
            {
                return($"foreach (var {match.Groups[1].Value} in {match.Groups[2].Value}){System.Environment.NewLine}{{");
            }

            // LOG
            // LOG myVar => data.Logger.Log(myVar);
            if ((match = Regex.Match(input, $"^LOG (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[1].Value});");
            }

            // CLOG
            // CLOG Tomato "hello" => data.Logger.Log("hello", LogColors.Tomato);
            if ((match = Regex.Match(input, $"^CLOG ([A-Za-z]+) (.+)$")).Success)
            {
                return($"data.Logger.Log({match.Groups[2].Value}, LogColors.{match.Groups[1].Value});");
            }

            // WHILE
            // WHILE a < b => while (a < b) {
            if ((match = Regex.Match(input, $"^WHILE (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.keyIdentifiers.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"while ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"while ({line}){System.Environment.NewLine}{{");
                }
            }

            // IF
            // IF a < b => if (a < b) {
            if ((match = Regex.Match(input, $"^IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.keyIdentifiers.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"if ({line}){System.Environment.NewLine}{{");
                }
            }

            // ELSE
            // ELSE => } else {
            if (input == "ELSE")
            {
                return($"}}{System.Environment.NewLine}else{System.Environment.NewLine}{{");
            }

            // ELSE IF
            // ELSE IF a < b => } else if (a < b) {
            if ((match = Regex.Match(input, $"ELSE IF (.+)$")).Success)
            {
                var line = match.Groups[1].Value.Trim();
                if (LoliCodeParser.keyIdentifiers.Any(t => line.StartsWith(t)))
                {
                    var keyType = LineParser.ParseToken(ref line);
                    var key     = LoliCodeParser.ParseKey(ref line, keyType);
                    return($"}}{System.Environment.NewLine}else if ({CSharpWriter.ConvertKey(key)}){System.Environment.NewLine}{{");
                }
                else
                {
                    return($"}}{System.Environment.NewLine}else if ({line}){System.Environment.NewLine}{{");
                }
            }

            // TRY
            // TRY => try {
            if (input == "TRY")
            {
                return($"try{System.Environment.NewLine}{{");
            }

            // CATCH
            // CATCH => } catch {
            if (input == "CATCH")
            {
                return($"}}{System.Environment.NewLine}catch{System.Environment.NewLine}{{");
            }

            // FINALLY
            // FINALLY => } finally {
            if (input == "FINALLY")
            {
                return($"}}{System.Environment.NewLine}finally{System.Environment.NewLine}{{");
            }

            // LOCK
            // LOCK globals => lock (globals) {
            if ((match = Regex.Match(input, $"^LOCK (.+)$")).Success)
            {
                return($"lock({match.Groups[1].Value}){System.Environment.NewLine}{{");
            }

            // ACQUIRELOCK
            // ACQUIRELOCK globals => await data.AsyncLocker.Acquire(nameof(globals), data.CancellationToken);
            if ((match = Regex.Match(input, $"^ACQUIRELOCK (.+)$")).Success)
            {
                return($"await data.AsyncLocker.Acquire(nameof({match.Groups[1].Value}), data.CancellationToken);");
            }

            // RELEASELOCK
            // RELEASELOCK globals => data.AsyncLocker.Release(nameof(globals));
            if ((match = Regex.Match(input, $"^RELEASELOCK (.+)$")).Success)
            {
                return($"data.AsyncLocker.Release(nameof({match.Groups[1].Value}));");
            }

            // SET VAR
            // SET VAR myString "hello" => string myString = "hello";
            if ((match = Regex.Match(input, $"^SET VAR @?\"?({validTokenRegex})\"? (.+)$")).Success)
            {
                if (definedVariables.Contains(match.Groups[1].Value))
                {
                    return($"{match.Groups[1].Value} = {match.Groups[2].Value};");
                }
                else
                {
                    definedVariables.Add(match.Groups[1].Value);
                    return($"string {match.Groups[1].Value} = {match.Groups[2].Value};");
                }
            }

            // SET CAP
            // SET CAP myCapture "hello" => string myString = "hello"; data.MarkForCapture(nameof(myCapture));
            if ((match = Regex.Match(input, $"^SET CAP @?\"?({validTokenRegex})\"? (.+)$")).Success)
            {
                if (definedVariables.Contains(match.Groups[1].Value))
                {
                    return($"{match.Groups[1].Value} = {match.Groups[2].Value};{System.Environment.NewLine}data.MarkForCapture(nameof({match.Groups[1].Value}));");
                }
                else
                {
                    definedVariables.Add(match.Groups[1].Value);
                    return($"string {match.Groups[1].Value} = {match.Groups[2].Value};{System.Environment.NewLine}data.MarkForCapture(nameof({match.Groups[1].Value}));");
                }
            }

            // SET USEPROXY
            // SET USEPROXY TRUE => data.UseProxy = "true";
            if ((match = Regex.Match(input, "^SET USEPROXY (TRUE|FALSE)$")).Success)
            {
                return($"data.UseProxy = {match.Groups[1].Value.ToLower()};");
            }

            // SET PROXY
            // SET PROXY "127.0.0.1" 9050 SOCKS5 => data.Proxy = new Proxy("127.0.0.1", 9050, ProxyType.Socks5);
            // SET PROXY "127.0.0.1" 9050 SOCKS5 "username" "password" => data.Proxy = new Proxy("127.0.0.1", 9050, ProxyType.Socks5, "username", "password");
            if (input.StartsWith("SET PROXY "))
            {
                var setProxyParams = input["SET PROXY ".Length..].Split(' ');