Beispiel #1
0
        /// <summary>
        /// Resets the Interpreter to work with a new script
        /// </summary>
        /// <param name="file">The file containing the source</param>
        /// <param name="input">The input buffer</param>
        /// <param name="width">Width of the input buffer</param>
        /// <param name="height">Height of the input buffer</param>
        /// <param name="depth">Depth of the input buffer</param>
        /// <param name="channelCount">The Channel Count</param>
        /// <param name="kernelDB">The Kernel DB that will be used</param>
        /// <param name="ignoreDebug">a flag to ignore the brk statement</param>
        public void Reset(string file, MemoryBuffer input, int width, int height, int depth, int channelCount,
                          KernelDatabase kernelDB, bool ignoreDebug)
        {
            //Clear old stuff

            ReleaseResources();

            //Setting variables
            _currentBuffer = new CLBufferInfo(input, false);
            _currentBuffer.SetKey(InputBufferName);

            _ignoreDebug     = ignoreDebug;
            _width           = width;
            _height          = height;
            _depth           = depth;
            _channelCount    = channelCount;
            _kernelDb        = kernelDB;
            _activeChannels  = new byte[_channelCount];
            _currentArgStack = new Stack <object>();
            for (int i = 0; i < _channelCount; i++)
            {
                _activeChannels[i] = 1;
            }

            _activeChannelBuffer =
                CLAPI.CreateBuffer(_activeChannels, MemoryFlag.ReadOnly | MemoryFlag.CopyHostPointer);

            //Parsing File
            _currentBuffer.SetKey(InputBufferName);
            Data = LoadScriptData(file, _currentBuffer, width, height, depth, channelCount, _kernelDb, _flFunctions);

            Reset();
        }
Beispiel #2
0
        /// <summary>
        /// Jumps the interpreter to the specified index
        /// </summary>
        /// <param name="index">the index of the line to jump to</param>
        /// <param name="leaveBuffer">a flag to optionally keep the current buffer</param>
        private void JumpTo(int index, bool leaveBuffer = false)
        {
            Logger.Log("Jumping To Function: " + Data.Source[index], DebugChannel.OpenFL | DebugChannel.Log, 6);
            _jumpStack.Push(new InterpreterState(_currentIndex, _currentBuffer, _currentArgStack));
            _stepResult.HasJumped = true;

#if NO_CL
            int size = 1;
#else
            int size = (int)_currentBuffer.Buffer.Size;
#endif


            if (!leaveBuffer)
            {
                _currentBuffer =
                    new CLBufferInfo(CLAPI.CreateEmpty <byte>(size, MemoryFlag.ReadWrite | MemoryFlag.CopyHostPointer),
                                     true);
                _currentBuffer.SetKey("Internal_JumpBuffer_Stack_Index" + (_jumpStack.Count - 1));
            }

            _currentIndex = index + 1; //+1 because the index is the function header
            _currentWord  = 0;
        }
Beispiel #3
0
        /// <summary>
        /// Define handler that loads defined textures
        /// </summary>
        /// <param name="arg">The Line of the definition</param>
        private static void DefineTexture(string[] arg, Dictionary <string, CLBufferInfo> defines, int width, int height,
                                          int depth, int channelCount, KernelDatabase kernelDb)
        {
            if (arg.Length < 2)
            {
                Logger.Crash(new FLInvalidFunctionUseException(ScriptDefineKey, "Invalid Define statement"), true);
                return;
            }

            string varname = arg[0].Trim();


            if (defines.ContainsKey(varname))
            {
                Logger.Log("Overwriting " + varname, DebugChannel.Warning | DebugChannel.OpenFL, 10);
                defines.Remove(varname);
            }

            MemoryFlag flags = MemoryFlag.ReadWrite;

            string[] flagTest = varname.Split(' ');
            if (flagTest.Length > 1)
            {
                varname = flagTest[1];
                if (flagTest[0] == "r")
                {
                    flags = MemoryFlag.ReadOnly;
                }

                else if (flagTest[0] == "w")
                {
                    flags = MemoryFlag.WriteOnly;
                }
            }

            string[] args = arg[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);


            string filename = args[0].Trim();

            byte[] activeChannels = new byte[channelCount];
            for (int i = 0; i < activeChannels.Length; i++)
            {
                activeChannels[i] = 1;
            }

            int InputBufferSize = width * height * depth * channelCount;

            if (IsSurroundedBy(filename, FilepathIndicator))
            {
                string fn = filename.Replace(FilepathIndicator, "");
                if (File.Exists(fn))
                {
                    Bitmap       bmp  = new Bitmap((Bitmap)System.Drawing.Image.FromFile(fn), width, height);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateFromImage(bmp,
                                                                               MemoryFlag.CopyHostPointer | flags), true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else
                {
                    Logger.Crash(
                        new FLInvalidFunctionUseException(DefineKey, "Invalid Filepath",
                                                          new InvalidFilePathException(fn)), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
            }
            else if (filename == "rnd")
            {
                MemoryBuffer buf = CLAPI.CreateEmpty <byte>(InputBufferSize, flags | MemoryFlag.CopyHostPointer);
                CLAPI.WriteRandom(buf, randombytesource, activeChannels, false);

                CLBufferInfo info = new CLBufferInfo(buf, true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
            else if (filename == "urnd")
            {
                MemoryBuffer buf = CLAPI.CreateEmpty <byte>(InputBufferSize, flags | MemoryFlag.CopyHostPointer);
                CLAPI.WriteRandom(buf, randombytesource, activeChannels, true);

                CLBufferInfo info = new CLBufferInfo(buf, true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
            else if (filename == "empty")
            {
                CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, flags), true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
            else if (filename == "wfc" || filename == "wfcf")
            {
                bool force = filename == "wfcf";
                if (args.Length < 10)
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[2], out int n))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[3], out int widh))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[4], out int heigt))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!bool.TryParse(args[5], out bool periodicInput))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!bool.TryParse(args[6], out bool periodicOutput))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[7], out int symmetry))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[8], out int ground))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[9], out int limit))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else
                {
                    string fn = args[1].Trim().Replace(FilepathIndicator, "");
                    if (File.Exists(fn))
                    {
                        Bitmap         bmp;
                        WFCOverlayMode wfc = new WFCOverlayMode(fn, n, widh,
                                                                heigt, periodicInput, periodicOutput, symmetry, ground);
                        if (force)
                        {
                            do
                            {
                                wfc.Run(limit);
                                bmp = new Bitmap(wfc.Graphics(), new Size(width, height)); //Apply scaling
                            } while (!wfc.Success);
                        }
                        else
                        {
                            wfc.Run(limit);
                            bmp = new Bitmap(wfc.Graphics(), new Size(width, height)); //Apply scaling
                        }

                        CLBufferInfo info = new CLBufferInfo(CLAPI.CreateFromImage(bmp,
                                                                                   MemoryFlag.CopyHostPointer | flags), true);
                        info.SetKey(varname);
                        defines.Add(varname, info);
                    }
                    else
                    {
                        Logger.Crash(
                            new FLInvalidFunctionUseException("wfc", "Invalid WFC Image statement",
                                                              new InvalidFilePathException(fn)), true);
                        CLBufferInfo info =
                            new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite), true);
                        info.SetKey(varname);
                        defines.Add(varname, info);
                    }
                }
            }

            else
            {
                string s = "";
                foreach (string s1 in args)
                {
                    s += s1 + " ";
                }

                Logger.Crash(new FLInvalidFunctionUseException(DefineKey, "Define statement wrong: " + s), true);
                CLBufferInfo info =
                    new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite), true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
        }
        /// <summary>
        /// Define handler that loads defined scripts
        /// </summary>
        /// <param name="arg">The Line of the definition</param>
        private static void DefineScript(string[] arg, Dictionary <string, CLBufferInfo> defines, int width, int height,
                                         int depth, int channelCount, KernelDatabase kernelDb)
        {
            if (arg.Length < 2)
            {
                Logger.Crash(new FLInvalidFunctionUseException(ScriptDefineKey, "Invalid Define statement"), true);
                return;
            }

            string varname = arg[0].Trim();

            if (defines.ContainsKey(varname))
            {
                Logger.Log("Overwriting " + varname, DebugChannel.Warning | DebugChannel.OpenFL, 10);
                defines.Remove(varname);
            }

            string[] args = arg[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);


            string filename = args[0].Trim();

            int InputBufferSize = width * height * depth * channelCount;

            if (IsSurroundedBy(filename, FilepathIndicator))
            {
                Logger.Log("Loading SubScript...", DebugChannel.Log | DebugChannel.OpenFL, 10);

                MemoryBuffer buf =
                    CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite);


                string fn = filename.Replace(FilepathIndicator, "");


                if (File.Exists(fn))
                {
                    Interpreter interpreter = new Interpreter(fn, buf, width, height,
                                                              depth, channelCount, kernelDb, true);

                    do
                    {
                        interpreter.Step();
                    } while (!interpreter.Terminated);

                    CLBufferInfo info = interpreter.GetActiveBufferInternal();
                    info.SetKey(varname);
                    defines.Add(varname, info);
                    interpreter.ReleaseResources();
                }
                else
                {
                    Logger.Crash(
                        new FLInvalidFunctionUseException(ScriptDefineKey, "Not a valid filepath as argument.",
                                                          new InvalidFilePathException(fn)),
                        true);

                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
            }
            else
            {
                Logger.Crash(new FLInvalidFunctionUseException(ScriptDefineKey, "Not a valid filepath as argument."),
                             true);

                CLBufferInfo info =
                    new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite), true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
        }