Esempio n. 1
0
    public async Task <Image> ExecuteStepsAsync(Step[] filledsteps)
    {
        Image Bitmap = null;

        foreach (var step in filledsteps)
        {
            if (step is TemplateStep step2)
            {
                if (Bitmap is null)
                {
                    Bitmap = step2.GetImage(client);
                }
                else
                {
                    Bitmap.Mutate(x => x.DrawImage(step2.GetImage(client), new Point(0, 0), 1));
                }
            }
            else if (step is PictureStep step1)
            {
                using var resizedbytes = (await ImageModule.ResizeAsync(await step1.Image().GetBytesAsync(client),
                                                                        new Size((int)step1.xSize, (int)step1.ySize), PngFormat.Instance)).Item1;
                using var resizedimg = await Image.LoadAsync(resizedbytes);

                Bitmap.Mutate(x => x.DrawImage(resizedimg, new Point((int)step.x, (int)step.y), 1));
            }
        }

        return(Bitmap);
    }
Esempio n. 2
0
    public async Task AddEmote(CommandContext ctx, [Description("Name like `Kappa`")] string name,
                               [Description("Url of the thing")] SdImage url)
    {
        var lang = await Language.GetLanguageFromCtxAsync(ctx);

        var bytes = await url.GetBytesAsync(HttpClient);

        var st = new MemoryStream(bytes);

        try
        {
            if (bytes.Length > 256000)
            {
                await ctx.RespondAsync(string.Format(lang.EmoteWasLargerThan256K, FileSizeUtils.FormatSize(st.Length)));

                st = (await ImageModule.ResizeAsync(bytes, new Size(128, 128))).Item1;
            }

            var emote = await ctx.Guild.CreateEmojiAsync(name, st,
                                                         reason : "Added via silverbot by " + ctx.User.Username);

            await ctx.RespondAsync(emote.ToString());
        }
        finally
        {
            await st.DisposeAsync();
        }
    }
Esempio n. 3
0
        public RunResult Run(string source)
        {
            _outputBuffer.Clear();

            var output = new LimitedTextWriter(new StringWriter(_outputBuffer), MaxOutputChars, MaxOutputLines);

            try
            {
                using (_connection = Database.CreateConnection())
                {
                    _transaction = _connection.BeginTransaction();

                    _state = new MondState
                    {
                        Options = new MondCompilerOptions
                        {
                            DebugInfo          = MondDebugInfoLevel.StackTrace,
                            UseImplicitGlobals = true,
                        },
                        Libraries = new MondLibraryManager
                        {
                            new ModifiedCoreLibraries(),
                            new ConsoleOutputLibraries(),
                            new ModifiedJsonLibraries(),
                            new HttpLibraries(),
                            new ImageLibraries(),
                            new RegexLibraries(),
                            new DateTimeLibraries(),
                            new AsyncLibraries(),
                        }
                    };

                    // eagerly initialize module cache (so it doesn't try to load from DB)
                    var moduleCache = MondValue.Object(_state);
                    moduleCache.Prototype = MondValue.Null;
                    _state["__modules"]   = moduleCache;

                    var searchDir = Path.Combine(Environment.CurrentDirectory, "Modules");

                    var requireWhitelist = new HashSet <string>
                    {
                        "Seq.mnd",
                        "Seq.Scalar.mnd",
                        "Seq.Sorting.mnd",
                    };

                    _state.Libraries.Configure(libs =>
                    {
                        var require = libs.Get <RequireLibrary>();
                        if (require != null)
                        {
                            require.Options            = _state.Options;
                            require.SearchBesideScript = false;
                            require.Resolver           = (name, directories) =>
                            {
                                string foundModule = null;

                                if (requireWhitelist.Contains(name))
                                {
                                    var modulePath = Path.Combine(searchDir, name);
                                    if (File.Exists(modulePath))
                                    {
                                        foundModule = modulePath;
                                    }
                                }

                                if (foundModule == null)
                                {
                                    throw new MondRuntimeException("require: module could not be found: {0}", name);
                                }

                                return(foundModule);
                            };
                        }

                        var consoleOut = libs.Get <ConsoleOutputLibrary>();
                        consoleOut.Out = output;
                    });

                    _state.EnsureLibrariesLoaded();

                    var global = _state.Run("return global;");
                    global.Prototype = MondValue.Null;

                    _variableCache    = new Dictionary <string, CacheEntry>();
                    _loadingVariables = new HashSet <string>();

                    var ops = MondValue.Object(_state);
                    ops["__get"]    = MondValue.Function(VariableGetterOldOperator);
                    _state["__ops"] = ops;

                    _state["__get"] = MondValue.Function(VariableGetter);
                    _state["__set"] = MondValue.Function(VariableSetter);

                    var program = source;

                    GC.Collect();

                    var result = _state.Run(program, "mondbox");

                    if (result != MondValue.Undefined)
                    {
                        output.WriteLine();

                        if (result["moveNext"])
                        {
                            output.WriteLine("sequence (15 max):");
                            foreach (var i in result.Enumerate(_state).Take(15))
                            {
                                output.WriteLine(i.Serialize());
                            }
                        }
                        else
                        {
                            output.WriteLine(result.Serialize());
                        }
                    }

                    SaveChanges(output);

                    _transaction.Commit();
                }
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                output.WriteLine(e.Message);
            }

            return(new RunResult(_outputBuffer.ToString(), ImageModule.GetImageData()));
        }