Exemplo n.º 1
0
        public override DreamValue OperatorCombine(DreamValue a, DreamValue b)
        {
            DreamList list = a.GetValueAsDreamList();

            if (b.Type == DreamValue.DreamValueType.DreamObject)
            {
                if (b.TryGetValueAsDreamList(out DreamList bList))
                {
                    foreach (DreamValue value in bList.GetValues())
                    {
                        if (!list.ContainsValue(value))
                        {
                            list.AddValue(value);
                        }
                    }
                }
                else if (!list.ContainsValue(b))
                {
                    list.AddValue(b);
                }
            }
            else if (!list.ContainsValue(b))
            {
                list.AddValue(b);
            }

            return(a);
        }
Exemplo n.º 2
0
        public DreamValue OperatorMask(DreamValue a, DreamValue b)
        {
            DreamList list = a.GetValueAsDreamList();

            if (b.TryGetValueAsDreamList(out DreamList bList))
            {
                for (int i = 1; i <= list.GetLength(); i++)
                {
                    if (!bList.ContainsValue(list.GetValue(new DreamValue(i))))
                    {
                        list.Cut(i, i + 1);
                        i--;
                    }
                }
            }
            else
            {
                for (int i = 1; i <= list.GetLength(); i++)
                {
                    if (list.GetValue(new DreamValue(i)) != b)
                    {
                        list.Cut(i, i + 1);
                        i--;
                    }
                }
            }

            return(a);
        }
Exemplo n.º 3
0
        public override DreamValue OperatorSubtract(DreamValue a, DreamValue b)
        {
            DreamList list     = a.GetValueAsDreamList();
            DreamList listCopy = list.CreateCopy();

            if (b.Type == DreamValue.DreamValueType.DreamObject)
            {
                if (b.TryGetValueAsDreamList(out DreamList bList))
                {
                    foreach (DreamValue value in bList.GetValues())
                    {
                        listCopy.RemoveValue(value);
                    }
                }
                else
                {
                    listCopy.RemoveValue(b);
                }
            }
            else
            {
                listCopy.RemoveValue(b);
            }

            return(new DreamValue(listCopy));
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
        public override DreamValue OperatorRemove(DreamValue a, DreamValue b)
        {
            DreamList list = a.GetValueAsDreamList();

            if (b.Type == DreamValue.DreamValueType.DreamObject)
            {
                if (b.TryGetValueAsDreamList(out DreamList bList))
                {
                    foreach (DreamValue value in bList.GetValues())
                    {
                        list.RemoveValue(value);
                    }
                }
                else
                {
                    list.RemoveValue(b);
                }
            }
            else
            {
                list.RemoveValue(b);
            }

            return(a);
        }
Exemplo n.º 6
0
        public static async Task <DreamValue> NativeProc_Export(AsyncNativeProc.State state)
        {
            var addr = state.Arguments.GetArgument(0, "Addr").Stringify();

            if (!Uri.TryCreate(addr, UriKind.RelativeOrAbsolute, out var uri))
            {
                throw new ArgumentException("Unable to parse URI.");
            }

            if (uri.Scheme is not("http" or "https"))
            {
                throw new NotSupportedException("non-HTTP world.Export is not supported.");
            }

            // TODO: Maybe cache HttpClient.
            var client   = new HttpClient();
            var response = await client.GetAsync(uri);

            var list = DreamList.Create();

            foreach (var header in response.Headers)
            {
                // TODO: How to handle headers with multiple values?
                list.SetValue(new DreamValue(header.Key), new DreamValue(header.Value.First()));
            }

            list.SetValue(new DreamValue("STATUS"), new DreamValue(((int)response.StatusCode).ToString()));
            list.SetValue(new DreamValue("CONTENT"), new DreamValue(await response.Content.ReadAsStringAsync()));

            return(new DreamValue(list));
        }
Exemplo n.º 7
0
        public override void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments)
        {
            base.OnObjectCreated(dreamObject, creationArguments);

            ContentsList = dreamObject.GetVariable("contents").GetValueAsDreamList();

            //New() is not called here
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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));
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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)));
        }
Exemplo n.º 12
0
        public DreamProcIdentifierListIndex(DreamList list, DreamValue listIndex)
        {
            List      = list;
            ListIndex = listIndex;

            if (!list.IsSubtypeOf(DreamPath.List))
            {
                throw new ArgumentException("Parameter must be a dream object of type " + DreamPath.List, nameof(list));
            }
        }
Exemplo n.º 13
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));
            }
        }
Exemplo n.º 14
0
        public override void OnObjectDeleted(DreamObject dreamObject)
        {
            if (dreamObject.GetVariable("loc").TryGetValueAsDreamObjectOfType(DreamPath.Atom, out DreamObject loc))
            {
                DreamList contents = loc.GetVariable("contents").GetValueAsDreamList();

                contents.RemoveValue(new DreamValue(dreamObject));
            }

            base.OnObjectDeleted(dreamObject);
        }
Exemplo n.º 15
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)));
        }
Exemplo n.º 16
0
        public override void OnVariableSet(DreamObject dreamObject, string variableName, DreamValue variableValue, DreamValue oldVariableValue)
        {
            base.OnVariableSet(dreamObject, variableName, variableValue, oldVariableValue);

            if (variableName == "len")
            {
                DreamList list   = (DreamList)dreamObject;
                int       newLen = variableValue.GetValueAsInteger();

                list.Resize(newLen);
            }
        }
Exemplo n.º 17
0
        public void OnVariableSet(DreamObject dreamObject, string varName, DreamValue value, DreamValue oldValue)
        {
            ParentType?.OnVariableSet(dreamObject, varName, value, oldValue);

            if (varName == "len")
            {
                DreamList list = (DreamList)dreamObject;
                value.TryGetValueAsInteger(out var newLen);

                list.Resize(newLen);
            }
        }
Exemplo n.º 18
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;

            if (start != 1 || end != 0)
            {
                throw new NotImplementedException("Ranged /list.Find() is not implemented");
            }
            return(new DreamValue(list.FindValue(element)));
        }
Exemplo n.º 19
0
        public override DreamValue OnVariableGet(DreamObject dreamObject, string variableName, DreamValue variableValue)
        {
            if (variableName == "len")
            {
                DreamList list = (DreamList)dreamObject;

                return(new DreamValue(list.GetLength()));
            }
            else
            {
                return(base.OnVariableGet(dreamObject, variableName, variableValue));
            }
        }
Exemplo n.º 20
0
        public void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments)
        {
            ParentType?.OnObjectCreated(dreamObject, creationArguments);

            // Named arguments are ignored
            if (creationArguments.OrderedArguments.Count > 1)   // Multi-dimensional
            {
                DreamList[] lists = { (DreamList)dreamObject };

                int dimensions = creationArguments.OrderedArguments.Count;
                for (int argIndex = 0; argIndex < dimensions; argIndex++)
                {
                    DreamValue arg = creationArguments.OrderedArguments[argIndex];
                    arg.TryGetValueAsInteger(out int size);

                    DreamList[] newLists = null;
                    if (argIndex < dimensions)
                    {
                        newLists = new DreamList[size * lists.Length];
                    }

                    for (int i = 0; i < lists.Length; i++)
                    {
                        DreamList list = lists[i];

                        for (int j = 0; j < size; j++)
                        {
                            if (argIndex < dimensions - 1)
                            {
                                DreamList newList = DreamList.Create();

                                list.AddValue(new DreamValue(newList));
                                newLists[i * size + j] = newList;
                            }
                            else
                            {
                                list.AddValue(DreamValue.Null);
                            }
                        }
                    }

                    lists = newLists;
                }
            }
            else if (creationArguments.OrderedArguments.Count == 1 && creationArguments.OrderedArguments[0].TryGetValueAsInteger(out int size))
            {
                ((DreamList)dreamObject).Resize(size);
            }
        }
Exemplo n.º 21
0
        public bool TryGetValueAsDreamList(out DreamList list)
        {
            if (TryGetValueAsDreamObjectOfType(DreamPath.List, out DreamObject listObject))
            {
                list = (DreamList)listObject;

                return(true);
            }
            else
            {
                list = null;

                return(false);
            }
        }
Exemplo n.º 22
0
        public DreamValue OperatorOr(DreamValue a, DreamValue b)
        {
            DreamList list = a.GetValueAsDreamList();

            if (b.TryGetValueAsDreamList(out DreamList bList))      // List | List
            {
                list = list.Union(bList);
            }
            else                                                    // List | x
            {
                list = list.CreateCopy();
                list.AddValue(b);
            }

            return(new DreamValue(list));
        }
Exemplo n.º 23
0
        public DreamList CreateDreamList()
        {
            DreamList list = DreamList.Create();

            foreach (DreamValue argument in OrderedArguments)
            {
                list.AddValue(argument);
            }

            foreach (KeyValuePair <string, DreamValue> argument in NamedArguments)
            {
                list.SetValue(new DreamValue(argument.Key), argument.Value);
            }

            return(list);
        }
Exemplo n.º 24
0
        public override void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments)
        {
            DreamObject loc = DreamMetaObjectAtom.FindLocArgument(creationArguments);

            base.OnObjectCreated(dreamObject, creationArguments);

            if (loc != null && loc.IsSubtypeOf(DreamPath.Turf))
            {
                DreamList contents = loc.GetVariable("contents").GetValueAsDreamList();
                while (contents.GetLength() > 0)   //Transfer all the old turf's contents
                {
                    contents.GetValue(new DreamValue(1)).GetValueAsDreamObjectOfType(DreamPath.Atom).SetVariable("loc", new DreamValue(dreamObject));
                }

                Program.DreamMap.SetTurf(loc.GetVariable("x").GetValueAsInteger(), loc.GetVariable("y").GetValueAsInteger(), dreamObject);
            }
        }
Exemplo n.º 25
0
        public override void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments)
        {
            base.OnObjectCreated(dreamObject, creationArguments);

            ContentsList = dreamObject.GetVariable("contents").GetValueAsDreamList();

            dreamObject.SetVariable("log", new DreamValue(new ConsoleOutputResource()));

            DreamValue fps = dreamObject.ObjectDefinition.Variables["fps"];

            if (fps.Value != null)
            {
                dreamObject.SetVariable("tick_lag", new DreamValue(10.0f / fps.GetValueAsInteger()));
            }

            //New() is not called here
        }
Exemplo n.º 26
0
        public DreamValue OnVariableGet(DreamObject dreamObject, string varName, DreamValue value)
        {
            switch (varName)
            {
            case "len":
            {
                DreamList list = (DreamList)dreamObject;
                return(new DreamValue(list.GetLength()));
            }

            case "type":
                return(new DreamValue(DreamPath.List));

            default:
                return(ParentType?.OnVariableGet(dreamObject, varName, value) ?? value);
            }
        }
Exemplo n.º 27
0
        public DreamValue OperatorAppend(DreamValue a, DreamValue b)
        {
            DreamList list = a.GetValueAsDreamList();

            if (b.TryGetValueAsDreamList(out DreamList bList))
            {
                foreach (DreamValue value in bList.GetValues())
                {
                    list.AddValue(value);
                }
            }
            else
            {
                list.AddValue(b);
            }

            return(a);
        }
Exemplo n.º 28
0
        public static DreamValue NativeProc_Remove(DreamObject instance, DreamObject usr, DreamProcArguments arguments)
        {
            DreamList         list           = (DreamList)instance;
            List <DreamValue> argumentValues = arguments.GetAllArguments();
            bool itemRemoved = false;

            foreach (DreamValue argument in argumentValues)
            {
                if (list.ContainsValue(argument))
                {
                    list.RemoveValue(argument);

                    itemRemoved = true;
                }
            }

            return(new DreamValue(itemRemoved ? 1 : 0));
        }
Exemplo n.º 29
0
        public void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments)
        {
            DreamList contents = DreamList.Create();

            contents.ValueAssigned += (DreamList list, DreamValue key, DreamValue value) => {
                if (value.TryGetValueAsDreamObjectOfType(DreamPath.Turf, out DreamObject turf))
                {
                    int x = turf.GetVariable("x").GetValueAsInteger();
                    int y = turf.GetVariable("y").GetValueAsInteger();
                    int z = turf.GetVariable("z").GetValueAsInteger();

                    _dreamMapManager.SetArea(x, y, z, dreamObject);
                }
            };

            _dreamManager.AreaContents.Add(dreamObject, contents);

            ParentType?.OnObjectCreated(dreamObject, creationArguments);
        }
Exemplo n.º 30
0
        public DreamValue OperatorAdd(DreamValue a, DreamValue b)
        {
            DreamList list     = a.GetValueAsDreamList();
            DreamList listCopy = list.CreateCopy();

            if (b.TryGetValueAsDreamList(out DreamList bList))
            {
                foreach (DreamValue value in bList.GetValues())
                {
                    listCopy.AddValue(value);
                }
            }
            else
            {
                listCopy.AddValue(b);
            }

            return(new DreamValue(listCopy));
        }