Exemplo n.º 1
0
        static private void SetReflectedObject(UnityEngine.Object inRoot, string inPath, object inValue)
        {
            inPath = inPath.Replace("Array.data", "");

            // locate the parent object
            StringSlice path    = inPath;
            int         lastDot = path.LastIndexOf('.');

            if (lastDot < 0)
            {
                return;
            }

            StringSlice parentPath   = path.Substring(0, lastDot);
            StringSlice fieldName    = path.Substring(lastDot + 1);
            object      parentObject = LocateReflectedObject(inRoot, parentPath.ToString());

            if (parentObject == null)
            {
                return;
            }

            Type objType  = parentObject.GetType();
            int  arrayIdx = -1;

            // capture array
            if (fieldName.EndsWith(']'))
            {
                int         elementIndexStart = fieldName.IndexOf('[');
                int         length            = fieldName.Length - elementIndexStart - 1;
                StringSlice elementIndexSlice = fieldName.Substring(elementIndexStart + 1, length);
                arrayIdx  = Convert.ToInt32(elementIndexSlice.ToString());
                fieldName = fieldName.Substring(0, elementIndexStart);
            }

            FieldInfo field = objType.GetField(fieldName.ToString(), InstanceBindingFlags);

            if (field == null)
            {
                return;
            }

            if (arrayIdx >= 0)
            {
                IList list = field.GetValue(parentObject) as IList;
                if (list != null)
                {
                    list[arrayIdx] = inValue;
                }
                return;
            }

            field.SetValue(parentObject, inValue);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses a tag's contents into data.
        /// </summary>
        static public void Parse(StringSlice inSlice, IDelimiterRules inDelimiters, out TagData outTagData)
        {
            if (inDelimiters == null)
            {
                throw new ArgumentNullException("inDelimiters");
            }

            StringSlice tag = inSlice;

            tag = tag.Trim(MinimalWhitespaceChars);

            bool bRemovedTagBoundaries = false;

            if (tag.StartsWith(inDelimiters.TagStartDelimiter))
            {
                tag = tag.Substring(inDelimiters.TagStartDelimiter.Length);
                bRemovedTagBoundaries = true;
            }
            if (tag.EndsWith(inDelimiters.TagEndDelimiter))
            {
                tag = tag.Substring(0, tag.Length - inDelimiters.TagEndDelimiter.Length);
                bRemovedTagBoundaries = true;
            }

            if (bRemovedTagBoundaries)
            {
                tag = tag.Trim(MinimalWhitespaceChars);
            }

            if (inSlice.Length == 0)
            {
                outTagData = default(TagData);
                return;
            }

            ClosingTagState closeState = 0;

            char endDelim = inDelimiters.RegionCloseDelimiter;

            if (endDelim != 0)
            {
                if (tag.StartsWith(endDelim))
                {
                    closeState |= ClosingTagState.Start;
                    tag         = tag.Substring(1);
                }
                if (tag.EndsWith(endDelim))
                {
                    closeState |= ClosingTagState.End;
                    tag         = tag.Substring(0, tag.Length - 1);
                }
            }

            if (closeState != 0)
            {
                tag = tag.Trim(MinimalWhitespaceChars);
            }

            char[] dataDelims   = inDelimiters.TagDataDelimiters;
            int    dataDelimIdx = tag.Length;

            foreach (var delim in dataDelims)
            {
                int idx = tag.IndexOf(delim);
                if (idx >= 0 && idx < dataDelimIdx)
                {
                    dataDelimIdx = idx;
                    if (idx <= 0)
                    {
                        break;
                    }
                }
            }

            if (dataDelimIdx >= tag.Length)
            {
                outTagData.Id   = tag;
                outTagData.Data = StringSlice.Empty;
            }
            else
            {
                outTagData.Id   = tag.Substring(0, dataDelimIdx).TrimEnd(MinimalWhitespaceChars);
                outTagData.Data = tag.Substring(dataDelimIdx).TrimStart(dataDelims).TrimStart(MinimalWhitespaceChars);
            }

            outTagData.m_CloseState = closeState;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to parse a string slice into a variant.
        /// </summary>
        static public bool TryParse(StringSlice inSlice, bool inbAllowImplicitHash, out Variant outValue)
        {
            inSlice = inSlice.Trim();

            if (inSlice.Length <= 0 || inSlice.Equals("null", true))
            {
                outValue = Variant.Null;
                return(true);
            }

            if (inSlice.StartsWith(StringHashing.CustomHashPrefix) || inSlice.StartsWith(StringHashing.StringPrefix) ||
                (inSlice.Length >= 2 && inSlice.StartsWith('"') && inSlice.EndsWith('"')))
            {
                StringHash32 hash;
                if (StringHash32.TryParse(inSlice, out hash))
                {
                    outValue = hash;
                    return(true);
                }
            }

            bool bBoolVal;

            if (StringParser.TryParseBool(inSlice, out bBoolVal))
            {
                outValue = new Variant(bBoolVal);
                return(true);
            }

            int intVal;

            if (StringParser.TryParseInt(inSlice, out intVal))
            {
                outValue = new Variant(intVal);
                return(true);
            }

            uint uintVal;

            if (StringParser.TryParseUInt(inSlice, out uintVal))
            {
                outValue = new Variant(uintVal);
                return(true);
            }

            float floatVal;

            if (StringParser.TryParseFloat(inSlice, out floatVal))
            {
                outValue = new Variant(floatVal);
                return(true);
            }

            if (inbAllowImplicitHash)
            {
                StringHash32 hash;
                if (StringHash32.TryParse(inSlice, out hash))
                {
                    outValue = hash;
                    return(true);
                }
            }

            outValue = default(Variant);
            return(false);
        }