Exemple #1
0
        public static DreamValue NativeProc_Cut(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            int       start = arguments.GetArgument(0, "Start").GetValueAsInteger(); //1-indexed
            int       end   = arguments.GetArgument(1, "End").GetValueAsInteger();   //1-indexed
            DreamList list  = (DreamList)instance;

            list.Cut(start, end);
            return(DreamValue.Null);
        }
Exemple #2
0
        public static DreamValue NativeProc_Find(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamValue element = arguments.GetArgument(0, "Elem");
            int        start   = arguments.GetArgument(1, "Start").GetValueAsInteger(); //1-indexed
            int        end     = arguments.GetArgument(2, "End").GetValueAsInteger();   //1-indexed
            DreamList  list    = (DreamList)instance;

            return(new(list.FindValue(element, start, end)));
        }
Exemple #3
0
        public static DreamValue NativeProc_Swap(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamList list   = (DreamList)instance;
            int       index1 = arguments.GetArgument(0, "Index1").GetValueAsInteger();
            int       index2 = arguments.GetArgument(1, "Index2").GetValueAsInteger();

            list.Swap(index1, index2);
            return(DreamValue.Null);
        }
Exemple #4
0
        public static DreamValue NativeProc_Copy(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            int       start    = arguments.GetArgument(0, "Start").GetValueAsInteger(); //1-indexed
            int       end      = arguments.GetArgument(1, "End").GetValueAsInteger();   //1-indexed
            DreamList list     = (DreamList)instance;
            DreamList listCopy = list.CreateCopy(start, end);

            return(new DreamValue(listCopy));
        }
Exemple #5
0
        public static DreamValue NativeProc_Find(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamRegex dreamRegex = DreamMetaObjectRegex.ObjectToDreamRegex[instance];
            DreamValue haystack   = arguments.GetArgument(0, "haystack");
            int        next       = GetNext(instance, arguments.GetArgument(1, "Start"), dreamRegex.IsGlobal);
            int        end        = arguments.GetArgument(2, "End").GetValueAsInteger();

            instance.SetVariable("text", haystack);

            string haystackString;

            if (!haystack.TryGetValueAsString(out haystackString))
            {
                haystackString = String.Empty;
            }

            if (end == 0)
            {
                end = haystackString.Length;
            }
            if (haystackString.Length == next - 1)
            {
                return(new DreamValue(0));
            }

            Match match = dreamRegex.Regex.Match(haystackString, next - 1, end - next);

            if (match.Success)
            {
                instance.SetVariable("index", new DreamValue(match.Index + 1));
                instance.SetVariable("match", new DreamValue(match.Value));
                if (match.Groups.Count > 0)
                {
                    DreamList groupList = DreamList.Create(match.Groups.Count);

                    for (int i = 1; i < match.Groups.Count; i++)
                    {
                        groupList.AddValue(new DreamValue(match.Groups[i].Value));
                    }

                    instance.SetVariable("group", new DreamValue(groupList));
                }

                if (dreamRegex.IsGlobal)
                {
                    instance.SetVariable("next", new DreamValue(match.Index + match.Length));
                }

                return(new DreamValue(match.Index + 1));
            }
            else
            {
                return(new DreamValue(0));
            }
        }
        public static DreamValue NativeProc_Join(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamValue glue  = arguments.GetArgument(0, "Glue");
            int        start = arguments.GetArgument(1, "Start").GetValueAsInteger(); //1-indexed
            int        end   = arguments.GetArgument(0, "End").GetValueAsInteger();   //1-indexed
            DreamList  list  = (DreamList)instance;

            string glueValue = (glue.Type == DreamValueType.String) ? glue.GetValueAsString() : "";

            return(new DreamValue(list.Join(glueValue, start, end)));
        }
        public static DreamValue NativeProc_Find(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamValue element = arguments.GetArgument(0, "Elem");
            int        start   = arguments.GetArgument(1, "Start").GetValueAsInteger(); //1-indexed
            int        end     = arguments.GetArgument(2, "End").GetValueAsInteger();   //1-indexed
            DreamList  list    = (DreamList)instance;

            if (start != 1 || end != 0)
            {
                throw new NotImplementedException("Ranged /list.Find() is not implemented");
            }
            return(new DreamValue(list.FindValue(element)));
        }
Exemple #8
0
        public static DreamValue NativeProc_Insert(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            int       index = arguments.GetArgument(0, "Index").GetValueAsInteger(); //1-indexed
            DreamList list  = (DreamList)instance;

            if (arguments.OrderedArguments.Count < 2)
            {
                throw new Exception("No value given to insert");
            }

            for (int i = 1; i < arguments.OrderedArguments.Count; i++)
            {
                DreamValue item = arguments.OrderedArguments[i];

                if (item.TryGetValueAsDreamList(out DreamList valueList))
                {
                    foreach (DreamValue value in valueList.GetValues())
                    {
                        list.Insert(index++, value);
                    }
                }
                else
                {
                    list.Insert(index++, item);
                }
            }

            return(new DreamValue(index));
        }
Exemple #9
0
        public override void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments)
        {
            base.OnObjectCreated(dreamObject, creationArguments);

            if (creationArguments.GetArgument(0, "Size").TryGetValueAsInteger(out int size))
            {
                ((DreamList)dreamObject).Resize(size);
            }
        }
Exemple #10
0
        public static DreamValue NativeProc_Replace(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamRegex dreamRegex = DreamMetaObjectRegex.ObjectToDreamRegex[instance];

            if (!dreamRegex.IsGlobal)
            {
                throw new NotImplementedException("Non-global regex replaces are not implemented");
            }

            DreamValue haystack = arguments.GetArgument(0, "haystack");
            DreamValue replace  = arguments.GetArgument(1, "replacement");
            int        start    = arguments.GetArgument(2, "Start").GetValueAsInteger();
            int        end      = arguments.GetArgument(3, "End").GetValueAsInteger();

            string haystackString = haystack.GetValueAsString();

            if (end == 0)
            {
                end = haystackString.Length;
            }

            if (replace.TryGetValueAsProc(out DreamProc replaceProc))
            {
                throw new NotImplementedException("Proc replacements are not implemented");
            }
            else if (replace.TryGetValueAsString(out string replaceString))
            {
                string replaced = dreamRegex.Regex.Replace(haystackString, replaceString, end - start, start - 1);

                instance.SetVariable("text", new DreamValue(replaced));
                return(new DreamValue(replaced));
            }
            else
            {
                throw new ArgumentException("Replacement argument must be a string");
            }
        }
        public override void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments)
        {
            _dreamManager.WorldContentsList.AddValue(new DreamValue(dreamObject));

            DreamValue locArgument = creationArguments.GetArgument(0, "loc");

            if (locArgument.TryGetValueAsDreamObjectOfType(DreamPath.Atom, out _))
            {
                dreamObject.SetVariable("loc", locArgument); //loc is set before /New() is ever called
            }
            else if (creationArguments.ArgumentCount == 0)
            {
                creationArguments.OrderedArguments.Add(DreamValue.Null); //First argument is loc, which is null
            }

            base.OnObjectCreated(dreamObject, creationArguments);
        }
Exemple #12
0
        public static DreamValue NativeProc_Replace(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamRegex dreamRegex = DreamMetaObjectRegex.ObjectToDreamRegex[instance];

            DreamValue haystack = arguments.GetArgument(0, "haystack");
            DreamValue replace  = arguments.GetArgument(1, "replacement");
            int        start    = arguments.GetArgument(2, "Start").GetValueAsInteger();
            int        end      = arguments.GetArgument(3, "End").GetValueAsInteger();

            if (!haystack.TryGetValueAsString(out var haystackString))
            {
                if (haystack == DreamValue.Null)
                {
                    return(DreamValue.Null);
                }
                //TODO Check what actually happens
                throw new ArgumentException("Bad regex haystack");
            }
            string haystackSubstring = haystackString;

            if (end != 0)
            {
                haystackSubstring = haystackString.Substring(0, end - start);
            }

            if (replace.TryGetValueAsProc(out DreamProc replaceProc))
            {
                return(DoProcReplace(replaceProc));
            }
            if (replace.TryGetValueAsString(out string replaceString))
            {
                return(DoTextReplace(replaceString));
            }

            if (replace.TryGetValueAsPath(out var procPath) && procPath.LastElement is not null)
            {
                var dreamMan = IoCManager.Resolve <IDreamManager>();
                if (dreamMan.ObjectTree.TryGetGlobalProc(procPath.LastElement, out DreamProc? proc))
                {
                    return(DoProcReplace(proc));
                }
            }

            throw new ArgumentException("Replacement argument must be a string or a proc");

            DreamValue DoProcReplace(DreamProc proc)
            {
                if (dreamRegex.IsGlobal)
                {
                    throw new NotImplementedException("Proc global regex replacements are not implemented");
                }
                var match              = dreamRegex.Regex.Match(haystackSubstring);
                var groups             = match.Groups;
                List <DreamValue> args = new List <DreamValue>(groups.Count);

                foreach (Group group in groups)
                {
                    args.Add(new DreamValue(group.Value));
                }
                var result = DreamThread.Run(async(state) => await state.Call(proc, instance, null, new DreamProcArguments(args)));

                if (result.TryGetValueAsString(out var replacement))
                {
                    return(DoTextReplace(replacement));
                }
                //TODO Confirm this behavior
                if (result == DreamValue.Null)
                {
                    return(new DreamValue(haystackSubstring));
                }
                throw new ArgumentException("Replacement is not a string");
            }

            DreamValue DoTextReplace(string replacement)
            {
                string replaced = dreamRegex.Regex.Replace(haystackSubstring, replacement, dreamRegex.IsGlobal ? -1 : 1, start - 1);

                if (end != 0)
                {
                    replaced += haystackString.Substring(end - start + 1);
                }

                instance.SetVariable("text", new DreamValue(replaced));
                return(new DreamValue(replaced));
            }
        }