Beispiel #1
0
        private VariableList GetListVariable(Plugin p, string varname, bool createNew)
        {
            if (p.listvariables.ContainsKey(varname) == true)
            {
                return(p.listvariables[varname]);
            }
            VariableList vl = new VariableList();

            if (createNew == true)
            {
                p.listvariables[varname] = vl;
            }
            return(vl);
        }
Beispiel #2
0
        public string ExpandVariables(LoggerDelegate logger, object o, bool numeric, string expr)
        {
            Match  m, mx;
            string newexpr = expr;
            int    i       = 1;

            while (true)
            {
                m = rex.Match(newexpr);
                if (m.Success == false)
                {
                    m = rexnum.Match(newexpr);
                    if (m.Success == false)
                    {
                        break;
                    }
                }
                string x     = m.Groups["id"].Value;
                string val   = "";
                bool   found = false;
                if (testmode == true)
                {
                    if (x == "_since")
                    {
                        val = ((int)Math.Floor((DateTime.UtcNow - triggered).TotalSeconds)).ToString();
                    }
                    else if (x == "_sincems")
                    {
                        val = ((int)Math.Floor((DateTime.UtcNow - triggered).TotalMilliseconds)).ToString();
                    }
                    else
                    {
                        val = (numeric == true ? "1" : "test");
                    }
                    found = true;
                }
                else
                {
                    int gn;
                    if (Int32.TryParse(x, out gn) == true)
                    {
                        if (gn >= 0 && gn < numgroups.Count)
                        {
                            val   = numgroups[gn];
                            found = true;
                        }
                    }
                    if (found == false)
                    {
                        if (namedgroups.ContainsKey(x) == true)
                        {
                            val   = namedgroups[x];
                            found = true;
                        }
                    }
                    if (found == false)
                    {
                        if (x == "_since")
                        {
                            val   = ((int)Math.Floor((DateTime.UtcNow - triggered).TotalSeconds)).ToString();
                            found = true;
                        }
                        else if (x == "_sincems")
                        {
                            val   = ((int)Math.Floor((DateTime.UtcNow - triggered).TotalMilliseconds)).ToString();
                            found = true;
                        }
                        else if (x == "_systemtime")
                        {
                            val   = ((long)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString();
                            found = true;
                        }
                        else if (x == "_systemtimems")
                        {
                            val   = ((long)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds).ToString();
                            found = true;
                        }
                        else if (x == "_response")
                        {
                            val   = contextResponse;
                            found = true;
                        }
                        else if (x.IndexOf("_jsonresponse") == 0)
                        {
                            mx = rexlidx.Match(x);
                            if (mx.Success == true)
                            {
                                string pathspec = mx.Groups["index"].Value;
                                if (contextJsonParsed == false)
                                {
                                    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                                    contextJsonResponse = jsonSerializer.Deserialize <dynamic>(contextResponse);
                                    contextJsonParsed   = true;
                                }
                                string[] path   = pathspec.Split("/".ToCharArray());
                                dynamic  curdir = contextJsonResponse;
                                foreach (string px in path)
                                {
                                    curdir = curdir[px];
                                }
                                val   = curdir.ToString();
                                found = true;
                            }
                        }
                        else if (x.IndexOf("evar:") == 0)
                        {
                            string varname = x.Substring(5);
                            lock (plug.simplevariables) // verified
                            {
                                if (plug.simplevariables.ContainsKey(varname) == true)
                                {
                                    val = "1";
                                }
                                else
                                {
                                    val = "0";
                                }
                                found = true;
                            }
                        }
                        else if (x.IndexOf("elvar:") == 0)
                        {
                            string varname = x.Substring(6);
                            lock (plug.listvariables) // verified
                            {
                                if (plug.listvariables.ContainsKey(varname) == true)
                                {
                                    val = "1";
                                }
                                else
                                {
                                    val = "0";
                                }
                                found = true;
                            }
                        }
                        else if (x.IndexOf("var:") == 0)
                        {
                            string varname = x.Substring(4);
                            lock (plug.simplevariables) // verified
                            {
                                if (plug.simplevariables.ContainsKey(varname) == true)
                                {
                                    val   = plug.simplevariables[varname].Value;
                                    found = true;
                                }
                            }
                        }
                        else if (x.IndexOf("lvar:") == 0)
                        {
                            string varname = x.Substring(5);
                            mx = rexlprp.Match(varname);
                            if (mx.Success == true)
                            {
                                string gname = mx.Groups["name"].Value;
                                string gprop = mx.Groups["prop"].Value;
                                switch (gprop)
                                {
                                case "size":
                                {
                                    lock (plug.listvariables)
                                    {
                                        VariableList vl = GetListVariable(plug, gname, false);
                                        val   = vl.Size().ToString();
                                        found = true;
                                    }
                                }
                                break;

                                case "indexof":
                                {
                                    string garg = mx.Groups["arg"].Value;
                                    lock (plug.listvariables)
                                    {
                                        VariableList vl = GetListVariable(plug, gname, false);
                                        val   = vl.IndexOf(garg).ToString();
                                        found = true;
                                    }
                                }
                                break;
                                }
                            }
                            else
                            {
                                mx = rexlidx.Match(varname);
                                if (mx.Success == true)
                                {
                                    string gname  = mx.Groups["name"].Value;
                                    string gindex = mx.Groups["index"].Value;
                                    lock (plug.listvariables)
                                    {
                                        VariableList vl = GetListVariable(plug, gname, false);
                                        if (gindex == "last")
                                        {
                                            gindex = vl.Size().ToString();
                                        }
                                        int iindex;
                                        if (Int32.TryParse(gindex, out iindex) == true)
                                        {
                                            val = vl.Peek(iindex);
                                        }
                                        found = true;
                                    }
                                    found = true;
                                }
                            }
                        }
                        else if (x.IndexOf("numeric:") == 0)
                        {
                            string numexpr = x.Substring(8);
                            val   = I18n.ThingToString(EvaluateNumericExpression(logger, o, numexpr));
                            found = true;
                        }
                        else if (x.IndexOf("string:") == 0)
                        {
                            string numexpr = x.Substring(7);
                            val   = EvaluateStringExpression(logger, o, numexpr);
                            found = true;
                        }
                        else if (x.IndexOf("func:") == 0)
                        {
                            val   = "";
                            found = true;
                            string funcexpr = x.Substring(5);
                            int    splitter = funcexpr.IndexOf(":");
                            if (splitter > 0)
                            {
                                string funcdef = funcexpr.Substring(0, splitter);
                                Match  rxm     = rexfunc.Match(funcdef);
                                if (rxm.Success == true)
                                {
                                    string   funcname = rxm.Groups["name"].Value.ToLower();
                                    string   funcarg  = rxm.Groups["arg"].Value;
                                    string[] args     = SplitArguments(funcarg);
                                    int      argc     = args.Count();
                                    string   funcval  = funcexpr.Substring(splitter + 1);
                                    switch (funcname)
                                    {
                                    case "toupper":     // toupper()
                                        val = funcval.ToUpper();
                                        break;

                                    case "tolower":     // tolower()
                                        val = funcval.ToLower();
                                        break;

                                    case "length":     // length()
                                        val = funcval.Length.ToString();
                                        break;

                                    case "hex2dec":     // hex2dec()
                                        val = "" + int.Parse(funcval, System.Globalization.NumberStyles.HexNumber);
                                        break;

                                    case "padleft":     // padleft(charcode,length)
                                        if (argc != 2)
                                        {
                                            throw new ArgumentException(I18n.Translate("internal/Context/padleftargerror", "Padleft function requires two arguments, {0} were given", argc));
                                        }
                                        else
                                        {
                                            val = funcval.PadLeft(Int32.Parse(args[1]), (char)Int32.Parse(args[0]));
                                        }
                                        break;

                                    case "padright":     // padright(charcode,length)
                                        if (argc != 2)
                                        {
                                            throw new ArgumentException(I18n.Translate("internal/Context/padrightargerror", "Padright function requires two arguments, {0} were given", argc));
                                        }
                                        else
                                        {
                                            val = funcval.PadRight(Int32.Parse(args[1]), (char)Int32.Parse(args[0]));
                                        }
                                        break;

                                    case "substring":     // substring(startindex, length) or substring(startindex)
                                        if (argc != 1 && argc != 2)
                                        {
                                            throw new ArgumentException(I18n.Translate("internal/Context/substringargerror", "Substring function requires one or two arguments, {0} were given", argc));
                                        }
                                        else
                                        {
                                            switch (argc)
                                            {
                                            case 1:
                                                val = funcval.Substring(Int32.Parse(args[0]));
                                                break;

                                            case 2:
                                                val = funcval.Substring(Int32.Parse(args[0]), Int32.Parse(args[1]));
                                                break;
                                            }
                                        }
                                        break;

                                    case "indexof":     // indexof(stringtosearch)
                                        if (argc != 1)
                                        {
                                            throw new ArgumentException(I18n.Translate("internal/Context/indexofargerror", "Indexof function requires one argument, {0} were given", argc));
                                        }
                                        else
                                        {
                                            val = "" + funcval.IndexOf(args[0]);
                                        }
                                        break;

                                    case "trim":     // trim() or trim(charcode,charcode,charcode,...)
                                        if (argc == 0 || args[0] == "")
                                        {
                                            val = funcval.Trim();
                                        }
                                        else
                                        {
                                            string xx = "";
                                            foreach (string xyz in args)
                                            {
                                                xx += (char)Int32.Parse(xyz);
                                            }
                                            val = funcval.Trim(xx.ToCharArray());
                                        }
                                        break;

                                    case "trimleft":     // trimleft() or trimleft(charcode,charcode,charcode,...)
                                        if (argc == 0 || args[0] == "")
                                        {
                                            val = funcval.TrimStart();
                                        }
                                        else
                                        {
                                            string xx = "";
                                            foreach (string xyz in args)
                                            {
                                                xx += (char)Int32.Parse(xyz);
                                            }
                                            val = funcval.TrimStart(xx.ToCharArray());
                                        }
                                        break;

                                    case "trimright":     // trimright() or trimright(charcode,charcode,charcode,...)
                                        if (argc == 0 || args[0] == "")
                                        {
                                            val = funcval.TrimEnd();
                                        }
                                        else
                                        {
                                            string xx = "";
                                            foreach (string xyz in args)
                                            {
                                                xx += (char)Int32.Parse(xyz);
                                            }
                                            val = funcval.TrimEnd(xx.ToCharArray());
                                        }
                                        break;

                                    case "format":     // format(type,formatstring)
                                        if (argc != 2)
                                        {
                                            throw new ArgumentException(I18n.Translate("internal/Context/formatargerror", "Format function requires two arguments, {0} were given", argc));
                                        }
                                        else
                                        {
                                            Type   type      = Type.GetType(args[0]);
                                            object converted = Convert.ChangeType(funcval, type, CultureInfo.InvariantCulture);
                                            val = String.Format("{0:" + args[1] + "}", converted);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        else if (x == "_ffxivplayer")
                        {
                            VariableClump vc = PluginBridges.BridgeFFXIV.GetMyself();
                            if (vc != null)
                            {
                                val = vc.GetValue("name");
                            }
                            found = true;
                        }
                        else if (x == "_ffxivpartyorder")
                        {
                            val   = plug.cfg.FfxivPartyOrdering + " " + plug.cfg.FfxivCustomPartyOrder;
                            found = true;
                        }
                        else if (x == "_incombat")
                        {
                            val   = (plug.mainform.InCombat == true) ? "1" : "0";
                            found = true;
                        }
                        else if (x == "_duration")
                        {
                            try
                            {
                                if (plug.mainform.InCombat == true)
                                {
                                    val = ((int)Math.Floor(plug.mainform.ActiveZone.ActiveEncounter.Duration.TotalSeconds)).ToString();
                                }
                                else
                                {
                                    val = "0";
                                }
                            }
                            catch (Exception)
                            {
                                val = "0";
                            }
                            found = true;
                        }
                        else if (x.IndexOf("_ffxivparty") == 0)
                        {
                            mx = rexnump.Match(x);
                            if (mx.Success == true)
                            {
                                string        gindex = mx.Groups["index"].Value;
                                string        gprop  = mx.Groups["prop"].Value;
                                int           iindex = 0;
                                Int64         honk;
                                VariableClump vc      = null;
                                bool          foundid = false;
                                if (Int64.TryParse(gindex, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out honk) == true)
                                {
                                    vc = PluginBridges.BridgeFFXIV.GetIdPartyMember(gindex, out foundid);
                                }
                                if (foundid == false)
                                {
                                    if (Int32.TryParse(gindex, out iindex) == true)
                                    {
                                        vc = PluginBridges.BridgeFFXIV.GetPartyMember(iindex);
                                    }
                                    else
                                    {
                                        vc = PluginBridges.BridgeFFXIV.GetNamedPartyMember(gindex);
                                    }
                                }
                                if (vc != null)
                                {
                                    val = vc.GetValue(gprop);
                                }
                            }
                            found = true;
                        }
                        else if (x.IndexOf("_ffxiventity") == 0)
                        {
                            mx = rexnump.Match(x);
                            if (mx.Success == true)
                            {
                                string        gindex = mx.Groups["index"].Value;
                                string        gprop  = mx.Groups["prop"].Value;
                                Int64         honk;
                                VariableClump vc      = null;
                                bool          foundid = false;
                                if (Int64.TryParse(gindex, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out honk) == true)
                                {
                                    vc = PluginBridges.BridgeFFXIV.GetIdEntity(gindex, out foundid);
                                }
                                if (foundid == false)
                                {
                                    vc = PluginBridges.BridgeFFXIV.GetNamedEntity(gindex);
                                }
                                if (vc != null)
                                {
                                    val = vc.GetValue(gprop);
                                }
                            }
                            found = true;
                        }
                        else if (x == "_ffxivtime")
                        {
                            TimeSpan ez   = GetEorzeanTime();
                            int      mins = (int)Math.Floor(ez.TotalMinutes);
                            val   = mins.ToString();
                            found = true;
                        }
                        else if (x == "_lastencounter")
                        {
                            Advanced_Combat_Tracker.FormActMain act = Advanced_Combat_Tracker.ActGlobals.oFormActMain;
                            FieldInfo fi   = act.GetType().GetField("defaultTextFormat", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
                            dynamic   texf = fi.GetValue(act);
                            val = "";
                            if (texf != null)
                            {
                                int zones = act.ZoneList.Count;
                                for (int ii = zones - 1; ii >= 0; ii--)
                                {
                                    int encs = act.ZoneList[ii].Items.Count;
                                    for (int jj = encs - 1; jj >= 1; jj--)
                                    {
                                        if (act.ZoneList[ii].Items[jj] != act.ActiveZone.ActiveEncounter)
                                        {
                                            val = act.GetTextExport(act.ZoneList[ii].Items[jj], texf);
                                            ii  = 0;
                                            jj  = 0;
                                        }
                                    }
                                }
                            }
                            found = true;
                        }
                        else if (x == "_activeencounter")
                        {
                            Advanced_Combat_Tracker.FormActMain act = Advanced_Combat_Tracker.ActGlobals.oFormActMain;
                            FieldInfo fi   = act.GetType().GetField("defaultTextFormat", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
                            dynamic   texf = fi.GetValue(act);
                            val   = act.GetTextExport(act.ActiveZone.ActiveEncounter, texf);
                            found = true;
                        }
                        else if (x.IndexOf("_textaura") == 0)
                        {
                            mx = rexnump.Match(x);
                            if (mx.Success == true)
                            {
                                string gindex = mx.Groups["index"].Value;
                                string gprop  = mx.Groups["prop"].Value.ToLower();
                                val = "";
                                lock (plug.textauras)
                                {
                                    if (plug.textauras.ContainsKey(gindex) == true)
                                    {
                                        Forms.AuraContainerForm acf = plug.textauras[gindex];
                                        switch (gprop)
                                        {
                                        case "x":
                                            val = acf.Left.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "y":
                                            val = acf.Top.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "w":
                                            val = acf.Width.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "h":
                                            val = acf.Height.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "opacity":
                                            val = acf.PresentableOpacity.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "text":
                                            val = acf.CurrentText;
                                            break;
                                        }
                                    }
                                }
                                found = true;
                            }
                        }
                        else if (x.IndexOf("_imageaura") == 0)
                        {
                            mx = rexnump.Match(x);
                            if (mx.Success == true)
                            {
                                string gindex = mx.Groups["index"].Value;
                                string gprop  = mx.Groups["prop"].Value.ToLower();
                                val = "";
                                lock (plug.imageauras)
                                {
                                    if (plug.imageauras.ContainsKey(gindex) == true)
                                    {
                                        Forms.AuraContainerForm acf = plug.imageauras[gindex];
                                        switch (gprop)
                                        {
                                        case "x":
                                            val = acf.Left.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "y":
                                            val = acf.Top.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "w":
                                            val = acf.Width.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "h":
                                            val = acf.Height.ToString(CultureInfo.InvariantCulture);
                                            break;

                                        case "opacity":
                                            val = acf.PresentableOpacity.ToString(CultureInfo.InvariantCulture);
                                            break;
                                        }
                                    }
                                }
                                found = true;
                            }
                        }
                        else if (x == "_screenwidth")
                        {
                            System.Windows.Forms.Screen scr = System.Windows.Forms.Screen.PrimaryScreen;
                            val   = scr.WorkingArea.Width.ToString(CultureInfo.InvariantCulture);
                            found = true;
                        }
                        else if (x == "_screenheight")
                        {
                            System.Windows.Forms.Screen scr = System.Windows.Forms.Screen.PrimaryScreen;
                            val   = scr.WorkingArea.Height.ToString(CultureInfo.InvariantCulture);
                            found = true;
                        }
                        else if (x == "_screenminx")
                        {
                            val   = plug.MinX.ToString(CultureInfo.InvariantCulture);
                            found = true;
                        }
                        else if (x == "_screenminy")
                        {
                            val   = plug.MinY.ToString(CultureInfo.InvariantCulture);
                            found = true;
                        }
                        else if (x == "_screenmaxx")
                        {
                            val   = plug.MaxX.ToString(CultureInfo.InvariantCulture);
                            found = true;
                        }
                        else if (x == "_screenmaxy")
                        {
                            val   = plug.MaxY.ToString(CultureInfo.InvariantCulture);
                            found = true;
                        }
                        else if (x == "_fps")
                        {
                            if (plug.sc != null)
                            {
                                val = plug.sc.FPS.ToString(CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                val = "0";
                            }
                            found = true;
                        }
                    }
                }
                newexpr = newexpr.Substring(0, m.Index) + val + newexpr.Substring(m.Index + m.Length);

                /*
                 * if (logger != null)
                 * {
                 *  logger(o, I18n.Translate("internal/Context/expansionstep", "Expansion step {0} from '{1}' to '{2}' ({3}) = '{4}'", i, expr, newexpr, val, found));
                 * }
                 * else if (plug != null)
                 * {
                 *  plug.FilteredAddToLog(Plugin.DebugLevelEnum.Verbose, I18n.Translate("internal/Context/expansionstep", "Expansion step {0} from '{1}' to '{2}' ({3}) = '{4}'", i, expr, newexpr, val, found));
                 * }*/
                i++;
            }
            ;
            if (trig != null)
            {
                if (trig.DebugLevel == Plugin.DebugLevelEnum.Inherit)
                {
                    if (plug != null)
                    {
                        if (plug.cfg.DebugLevel < Plugin.DebugLevelEnum.Verbose)
                        {
                            return(newexpr);
                        }
                    }
                }
                else
                {
                    if (trig.DebugLevel < Plugin.DebugLevelEnum.Verbose)
                    {
                        return(newexpr);
                    }
                }
            }
            if (newexpr.CompareTo(expr) != 0)
            {
                if (logger != null)
                {
                    logger(o, I18n.Translate("internal/Context/expansion", "Variable expansion from '{0}' to '{1}'", expr, newexpr));
                }
                else if (plug != null)
                {
                    plug.FilteredAddToLog(Plugin.DebugLevelEnum.Verbose, I18n.Translate("internal/Context/expansion", "Variable expansion from '{0}' to '{1}'", expr, newexpr));
                }
            }
            return(newexpr);
        }