Exemple #1
0
        public static SObject PadEnd(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            var str         = ((SString)instance).Value;
            int totalLength = 0;
            var padStr      = " ";

            if (TypeContract.Ensure(parameters, new[] { typeof(SNumber), typeof(SString) }))
            {
                totalLength = (int)((SNumber)parameters[0]).Value;
                padStr      = ((SString)parameters[1]).Value;
            }
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                totalLength = (int)((SNumber)parameters[0]).Value;
            }

            if (padStr == "" || totalLength <= str.Length)
            {
                return(processor.CreateString(str));
            }

            while (str.Length < totalLength)
            {
                str += padStr;
            }

            if (str.Length > totalLength)
            {
                str = str.Remove(totalLength);
            }

            return(processor.CreateString(str));
        }
Exemple #2
0
        public static SObject TrimEnd(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            string str = ((SString)instance).Value;

            string[] trimChars = { " " };

            if (TypeContract.Ensure(parameters, typeof(SArray)))
            {
                trimChars = ((SArray)parameters[0]).ArrayMembers.Select(m => m.ToString(processor).Value).ToArray();
            }
            else if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                trimChars = new[] { ((SString)parameters[0]).Value };
            }

            foreach (var trimChar in trimChars)
            {
                while (str.EndsWith(trimChar))
                {
                    str = str.Remove(str.Length - trimChar.Length);
                }
            }

            return(processor.CreateString(str));
        }
Exemple #3
0
        public static SObject CharAt(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            var str = ((SString)instance).Value;

            if (str == "")
            {
                return(processor.CreateString(""));
            }

            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                var index = (int)((SNumber)parameters[0]).Value;

                if (index < 0 || index >= str.Length)
                {
                    return(processor.CreateString(""));
                }

                return(processor.CreateString(str[index].ToString()));
            }
            else
            {
                return(processor.CreateString(str[0].ToString()));
            }
        }
Exemple #4
0
        public static SObject Slice(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(SNumber), typeof(SNumber) }))
            {
                var str        = ((SString)instance).Value;
                var sliceBegin = (int)((SNumber)parameters[0]).Value;
                var sliceEnd   = (int)((SNumber)parameters[1]).Value;

                if (sliceBegin < 0)
                {
                    sliceBegin += str.Length;
                }
                if (sliceBegin < 0)
                {
                    sliceBegin = 0;
                }
                if (sliceBegin > str.Length)
                {
                    return(processor.CreateString(""));
                }

                if (sliceEnd < 0)
                {
                    sliceEnd += str.Length;
                }
                if (sliceEnd < 0)
                {
                    sliceEnd = 0;
                }
                if (sliceEnd > str.Length)
                {
                    sliceEnd = str.Length;
                }

                return(processor.CreateString(str.Substring(sliceBegin, sliceEnd - sliceBegin)));
            }
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                var str        = ((SString)instance).Value;
                var sliceBegin = (int)((SNumber)parameters[0]).Value;

                if (sliceBegin < 0)
                {
                    sliceBegin += str.Length;
                }
                if (sliceBegin < 0)
                {
                    sliceBegin = 0;
                }
                if (sliceBegin > str.Length)
                {
                    return(processor.CreateString(""));
                }

                return(processor.CreateString(str.Substring(sliceBegin)));
            }

            return(processor.Undefined);
        }
Exemple #5
0
        public static object Constructor(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { TypeContract.Number, TypeContract.Number }))
            {
                var x = (double)parameters[0];
                var y = (double)parameters[1];

                objLink.SetMember("x", x);
                objLink.SetMember("y", y);
            }

            return(NetUndefined.Instance);
        }
Exemple #6
0
        public static SObject Replace(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(SString), typeof(SString) }))
            {
                var str     = ((SString)instance).Value;
                var replace = ((SString)parameters[0]).Value;
                var with    = ((SString)parameters[1]).Value;

                return(processor.CreateString(string.IsNullOrWhiteSpace(replace) ? str : str.Replace(replace, with)));
            }

            return(processor.Undefined);
        }
Exemple #7
0
        public static object GetData(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(string)))
            {
                var instance  = (EntityComponentWrapper)This;
                var entity    = instance.parent.GetEntity();
                var component = entity.GetComponent(instance.id);

                var dataKey = (string)parameters[0];

                return(component.GetDataOrDefault(dataKey, ""));
            }

            return(NetUndefined.Instance);
        }
Exemple #8
0
        public static SObject Remove(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(SNumber), typeof(SNumber) }))
            {
                var str    = ((SString)instance).Value;
                var start  = (int)((SNumber)parameters[0]).Value;
                var length = (int)((SNumber)parameters[1]).Value;

                if (start < 0)
                {
                    start = 0;
                }
                if (start > str.Length)
                {
                    return(processor.CreateString(str));
                }

                if (length <= 0)
                {
                    return(processor.CreateString(str));
                }
                if (length + start > str.Length)
                {
                    length = str.Length - start;
                }

                return(processor.CreateString(str.Remove(start, length)));
            }
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                var str   = ((SString)instance).Value;
                var start = (int)((SNumber)parameters[0]).Value;

                if (start < 0)
                {
                    start = 0;
                }
                if (start > str.Length)
                {
                    return(processor.CreateString(str));
                }

                return(processor.CreateString(str.Remove(start)));
            }

            return(processor.Undefined);
        }
Exemple #9
0
        public static SObject StartsWith(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                var str      = (SString)instance;
                var includes = (SString)parameters[0];

                if (includes.Value == "")
                {
                    return(processor.CreateBool(str.Value == ""));
                }

                return(processor.CreateBool(str.Value.StartsWith(includes.Value)));
            }

            return(processor.Undefined);
        }
Exemple #10
0
        public static object Constructor(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(string), typeof(int), typeof(int) }, 1))
            {
                var helper = new ParameterHelper(parameters);

                string id = helper.Pop <string>();
                objLink.SetMember("id", id);

                var moveModel = GetMoveModel(This);

                objLink.SetMember("PP", helper.Pop(moveModel.PP));
                objLink.SetMember("maxPP", helper.Pop(moveModel.PP));
            }

            return(NetUndefined.Instance);
        }
Exemple #11
0
        public static SObject IndexOf(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                var str    = instance as SString;
                var search = parameters[0] as SString;

                if (!str.Value.Contains(search.Value) || search.Value == "")
                {
                    return(processor.CreateNumber(-1));
                }

                return(processor.CreateNumber(str.Value.IndexOf(search.Value)));
            }

            return(processor.Undefined);
        }
Exemple #12
0
        public static SObject LastIndexOf(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                var str    = (SString)instance;
                var search = (SString)parameters[0];

                if (!str.Value.Contains(search.Value) || search.Value == "")
                {
                    return(processor.CreateNumber(-1));
                }

                return(processor.CreateNumber(str.Value.LastIndexOf(search.Value, StringComparison.Ordinal)));
            }

            return(processor.Undefined);
        }
Exemple #13
0
        public static object SetData(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(string), typeof(string) }))
            {
                var instance  = (EntityComponentWrapper)This;
                var entity    = instance.parent.GetEntity();
                var component = entity.GetComponent(instance.id);

                var dataKey   = (string)parameters[0];
                var dataValue = (string)parameters[1];

                component.SetData(dataKey, dataValue);

                return(dataValue);
            }

            return(NetUndefined.Instance);
        }
Exemple #14
0
        public static object GetComponent(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(string) }))
            {
                var wrapper = (EntityWrapper)This;
                var entity  = wrapper.GetEntity();

                var component = new EntityComponentWrapper
                {
                    parent = wrapper,
                    id     = parameters[0] as string
                };

                return(component);
            }

            return(NetUndefined.Instance);
        }
Exemple #15
0
        public static SObject Repeat(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                var repeatStr = ((SString)instance).Value;
                var times     = (int)((SNumber)parameters[0]).Value;
                var str       = "";

                for (int i = 0; i < times; i++)
                {
                    str += repeatStr;
                }

                return(processor.CreateString(str));
            }

            return(processor.CreateString(((SString)instance).Value));
        }
Exemple #16
0
        public static SObject Split(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            int limit = -1;

            string[] delimiters;
            string   str = ((SString)instance).Value;

            if (TypeContract.Ensure(parameters, new[] { typeof(SArray), typeof(SNumber) }))
            {
                delimiters = ((SArray)parameters[0]).ArrayMembers.Select(m => m.ToString(processor).Value).ToArray();
                limit      = (int)((SNumber)parameters[1]).Value;
            }
            else if (TypeContract.Ensure(parameters, typeof(SArray)))
            {
                delimiters = ((SArray)parameters[0]).ArrayMembers.Select(m => m.ToString(processor).Value).ToArray();
            }
            else if (TypeContract.Ensure(parameters, new[] { typeof(SString), typeof(SNumber) }))
            {
                delimiters = new[] { ((SString)parameters[0]).Value };
                limit      = (int)((SNumber)parameters[1]).Value;
            }
            else if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                delimiters = new[] { ((SString)parameters[0]).Value };
            }
            else
            {
                return(processor.CreateArray(new SObject[] { processor.CreateString(str) }));
            }

            var split = str.Split(delimiters, StringSplitOptions.None);

            if (limit >= 0 && split.Length > limit)
            {
                var result = new string[limit];
                Array.Copy(split, result, limit);
                split = result;
            }

            return(processor.CreateArray(split.Select(processor.CreateString).ToArray <SObject>()));
        }
Exemple #17
0
        public static SObject PadStart(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            var    str         = (instance as SString).Value;
            int    totalLength = 0;
            var    padStr      = " ";
            string newPadded   = "";

            if (TypeContract.Ensure(parameters, new[] { typeof(SNumber), typeof(SString) }))
            {
                totalLength = (int)(parameters[0] as SNumber).Value;
                padStr      = (parameters[1] as SString).Value;
            }
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                totalLength = (int)(parameters[0] as SNumber).Value;
            }

            if (padStr == "" || totalLength <= str.Length)
            {
                return(processor.CreateString(str));
            }

            while (newPadded.Length + str.Length < totalLength)
            {
                newPadded += padStr;
            }

            if (newPadded.Length + str.Length > totalLength)
            {
                newPadded = newPadded.Remove(totalLength - str.Length);
            }

            str = newPadded + str;

            return(processor.CreateString(str));
        }