Пример #1
0
        public static bool TryPositionToIndex(StoryVar position, int total, out int index)
        {
            index = -1;
            bool fromEnd = false;

            if (StoryVar.TryConvertTo <int>(position, out index))
            {
                if (index <= 0)
                {
                    index   = Math.Abs(index);
                    fromEnd = true;
                }
                else
                {
                    index -= 1;
                }
            }
            else
            {
                string str;
                if (!StoryVar.TryConvertTo <string>(position, out str))
                {
                    return(false);
                }

                Match match = rx_Position.Match(str);
                if (!match.Success)
                {
                    return(false);
                }

                fromEnd = match.Groups["last"].Success;

                if (match.Groups["index"].Success)
                {
                    index = int.Parse(match.Groups["index"].Value) - 1;
                }
                else if (fromEnd)
                {
                    index = 0;
                }
                else
                {
                    return(false);
                }
            }

            if (fromEnd)
            {
                index = total - 1 - index;
            }

            return(true);
        }
Пример #2
0
        public HarloweDatamap(params StoryVar[] vals) : this()
        {
            if (vals.Length % 2 != 0)
            {
                throw new VarTypeException("To create a datamap you must pass an even number of parameters.");
            }
            for (int i = 0; i < vals.Length; i += 2)
            {
                string key;
                if (!StoryVar.TryConvertTo <string>(vals[i], out key))
                {
                    throw new VarTypeException("To create a datamap, every odd parameter (an entry name) must be string.");
                }

                Dictionary[key] = vals[i + 1];
            }
        }
Пример #3
0
        // ................................
        // Objects

        protected StoryVar obj(params StoryVar[] vals)
        {
            if (vals.Length % 2 != 0)
            {
                throw new VarTypeException("To create an object you must pass an even number of parameters.");
            }

            var dictionary = new Dictionary <string, StoryVar>();

            for (int i = 0; i < vals.Length; i += 2)
            {
                string key;
                if (!StoryVar.TryConvertTo <string>(vals[i], out key))
                {
                    throw new VarTypeException("To create an object, every odd parameter (an entry name) must be a string.");
                }

                dictionary[key] = vals[i + 1];
            }

            return(new StoryVar(dictionary));
        }