Esempio n. 1
0
        public void ReturnHandler(object sender, MapReturnedEventArgs e)
        {
            Config config = Main.Get().GetConfig();

            HeightData data = Main.GetResizedHeightData(e.HeightMap, Math.Min(e.HeightMap.Width, (int)config.mapDetailLevel), Math.Min(e.HeightMap.Height, (int)config.mapDetailLevel));

            e.HeightMap.Dispose();

            Main.StretchHeightValues(ref data);

            if (this.maps.Contains(e.Label))
            {
                this.WriteToConsole("Warning: Map with label \"" + e.Label + "\" was returned more than once!");
                this.maps[e.Label] = data;
            }
            else
            {
                this.maps.Add(e.Label, data);
            }
        }
Esempio n. 2
0
        public void ExecuteScript(string script, bool syntaxCheckOnly, uint[] parameters)
        {
            // kill current syntax check in progress (if any is running)
            if (this.IsGGenRunning() && mode == Mode.SyntaxCheck && syntaxCheckOnly == false)
            {
                // wait for the syntax check to finish (it usually takes just milliseconds)
                this.GGenThread.Join();

                // do the check one the requested script is finished
                this.ScheduleSyntaxCheck();
            }
            // syntax checks are not allowed while a script is being executed
            else if (this.IsGGenRunning() && syntaxCheckOnly == true)
            {
                // do the check one the requested script is finished
                if (!this.isCheckScheduled)
                {
                    this.ScheduleSyntaxCheck();
                }

                return;
            }
            // having two generators running at once shouldn't happen
            else if (this.IsGGenRunning() && mode == Mode.Standard)
            {
                throw new Exception("Two GeoGens can't possibly be running at one time");
            }

            if (!syntaxCheckOnly)
            {
                this.mode = Mode.Standard;
                this.ButtonsRunMode();
                this.AddStatus("Executing");

                this.ClearData();

                this.WriteToConsole("Starting at " + DateTime.Now.ToString("HH:mm:ss") + "...");
            }
            else
            {
                this.mode = Mode.SyntaxCheck;

                this.lastCheckContent = "";
                this.AddStatus("Checking syntax");
            }

            System.Threading.ThreadStart starter = new System.Threading.ThreadStart(delegate
            {
                try
                {
                    try
                    {
                        ggen.SetScript(script);
                    }
                    catch (SyntaxErrorException)
                    {
                        this.MapGenerationFailed("Compilation failed!");

                        return;
                    }

                    try
                    {
                        ggen.LoadArgs();
                    }
                    catch (ArgsUnreadableException)
                    {
                        this.MapGenerationFailed("Map header is unreadable!");

                        return;
                    }

                    // do not generate the map during syntax check runs
                    if (!syntaxCheckOnly)
                    {
                        // no list of arguments was passed to the function -> use params from the param table
                        if (parameters == null)
                        {
                            var zipped = this.parameters.Item.Cast <PropertyGridEx.CustomProperty>().Zip(ggen.Args, (Property, Arg) => new { Property, Arg });

                            foreach (var item in zipped)
                            {
                                // fill in only matching arguments
                                if (item.Property.Type.Name == "Boolean" && item.Arg.Type == ScriptArgType.Bool)
                                {
                                    item.Arg.Value = (uint)item.Property.Value;
                                }

                                else if (item.Property.Type.Name == "UInt32" && item.Arg.Type == ScriptArgType.Int)
                                {
                                    item.Arg.Value = (uint)item.Property.Value;
                                }
                                else if (item.Property.Type.Name == "Int32" && item.Arg.Type == ScriptArgType.Int)
                                {
                                    item.Arg.Value = (uint)((int)item.Property.Value);
                                }
                                else if (item.Property.Type.Name == "String" && item.Arg.Type == ScriptArgType.Enum)
                                {
                                    item.Arg.Value = (uint)item.Property.Choices.IndexOf(item.Property.Value);
                                }
                            }

                            /*int i = 0;
                             * foreach (PropertyGridEx.CustomProperty property in this.parameters.Item)
                             * {
                             *
                             *
                             *  Generator.ScriptArg arg = ggen.Args[i];
                             *
                             *  // we ran out of parameters...
                             *  if (i == ggen.Args.Count())
                             *  {
                             *      break;
                             *  }
                             *
                             *  // fill in only matching arguments
                             *  if (property.Type.Name == "Boolean" && arg.Type == ScriptArgType.Bool)
                             *  {
                             *      arg.Value = (uint)property.Value;
                             *  }
                             *
                             *  else if (property.Type.Name == "UInt32" && arg.Type == ScriptArgType.Int)
                             *  {
                             *      arg.Value = (uint)property.Value;
                             *  }
                             *  else if (property.Type.Name == "Int32" && arg.Type == ScriptArgType.Int)
                             *  {
                             *      arg.Value = (uint)((int)property.Value);
                             *  }
                             *  else if (property.Type.Name == "String" && arg.Type == ScriptArgType.Enum)
                             *  {
                             *      arg.Value = (uint)property.Choices.IndexOf(property.Value);
                             *  }
                             *
                             *  i++;
                             * }*/
                        }
                        // the argument list was passed to the function
                        else
                        {
                            var zipped = parameters.Zip(ggen.Args, (Param, Arg) => new { Param, Arg });

                            foreach (var item in zipped)
                            {
                                item.Arg.Value = item.Param;
                            }

                            /*int i = 0;
                             * foreach (uint currentParam in parameters)
                             * {
                             *  Generator.ScriptArg arg = ggen.Args[i];
                             *
                             *  // we ran out of parameters...
                             *  if (i == ggen.Args.Length)
                             *  {
                             *      break;
                             *  }
                             *
                             *  arg.Value = currentParam;
                             *
                             *  i++;
                             * }*/
                        }

                        this.startTime = System.DateTime.Now.Ticks / 10000;

                        HeightData result;
                        try
                        {
                            if (this.config.seed == 0)
                            {
                                ggen.Seed = (uint)DateTime.Now.Ticks;
                            }
                            else
                            {
                                ggen.Seed = this.config.seed;
                            }

                            result = ggen.Generate();
                        }
                        catch (GenerationFailedException)
                        {
                            this.MapGenerationFailed("Map generation failed!");

                            return;
                        }
                        catch (ExceptionInCallbackException e)
                        {
                            // the thread was aborted
                            if (e.InnerException is ThreadAbortException)
                            {
                                this.ggen.Reset();
                                this.BeginInvoke(new MethodInvoker(delegate()
                                {
                                    this.MapGenerationFailed("Aborted!");
                                }));
                                return;
                            }

                            // else unknown exception, we want to debug this case
                            throw;
                        }

                        // map was generated successfully
                        HeightData result2 = Main.GetResizedHeightData(result, Math.Min(result.Width, (int)config.mapDetailLevel), Math.Min(result.Height, (int)config.mapDetailLevel));

                        result.Dispose();

                        Main.StretchHeightValues(ref result2);

                        maps.Add("[Main]", result2);

                        this.RemoveStatus("Executing");

                        this.Invoke(new MethodInvoker(delegate()
                        {
                            // was this part of a benchmark?
                            if (benchmarkStatus != null)
                            {
                                this.Benchmark();

                                return;
                            }

                            this.ReloadMaps(null);

                            this.WriteToConsole("Finished after " + Math.Round((System.DateTime.Now.Ticks / 10000 - this.startTime) / 1000d, 3) + " seconds!" + Environment.NewLine);

                            this.ButtonsNoRunMode();
                        }));
                    }
                    else
                    {
                        this.Invoke(new MethodInvoker(delegate()
                        {
                            this.RebuildArgsTable();
                            this.SetErrorStatus(false);
                        }));
                    }
                }
                catch (InternalErrorException e)
                {
                    this.WriteToConsole("Error: " + e.InnerException.Message);
                    this.MapGenerationFailed("GeoGen has unexpectedly crashed!");
                }
                finally
                {
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        this.RemoveStatus("Executing");
                        this.RemoveStatus("Checking syntax");
                        this.ExecuteScheduledCheck();
                    }));
                }
            });

            GGenThread = new System.Threading.Thread(starter);

            GGenThread.Start();
        }