Esempio n. 1
0
        public static int Main(string[] args)
        {
            #if DEBUG
            args = "-s example.js -w ./world -x 0 -y 4 -z 0".Split(' ');
            #endif

            #if !DEBUG
            try
            {
            #endif
                Options options = new Options();
                Parser cmdParser = new Parser();

                if (!cmdParser.ParseArguments(args, options))
                    throw new ArgumentException("Invalid Commandline parameter!");

                IntVector3 position = default(IntVector3);
                if (options.Output)
                {
                    if (options.PositionY != 0)
                    {
                        position = new IntVector3();
                        position.x = options.PositionX;
                        position.y = options.PositionY;
                        position.z = options.PositionZ;
                    }
                }

                JsScriptExecutor executor = new JsScriptExecutor();
                executor.Run(options.LibPath, options.ScriptFile, options.WorldDirectory, position, options.IsSchematic);
            #if !DEBUG
            }
            catch (Exception e)
            {
                Console.WriteLine("An Error of type {0} occured!", e.GetType());
                Console.WriteLine("Error Message: {0}", e.Message);
                return 1;
            }
            #endif
            return 0;
        }
        public void Run(string coreJsPath, string scriptPath, string worldDirectory, IntVector3 position, bool isSchematic)
        {
            string coreCode = File.ReadAllText(coreJsPath);
            string usercode = File.ReadAllText(scriptPath);
            JavascriptContext jsContext = new JavascriptContext();

            if (!isSchematic)
                jsContext.SetParameter("api", new JsApi(worldDirectory));
            else
                jsContext.SetParameter("api", new JsSchematicApi(worldDirectory));
            #if DEBUG
            jsContext.Run(coreCode);
            jsContext.Run("var startPosition = new Util.Vector3(" + position.x + ", " + position.y + ", " + position.z + ");");
            #else
            try
            {
                jsContext.Run(coreCode);
                jsContext.Run(string.Format("var startPosition = new Util.Vector3({0}, {1}, {2});", position.x, position.y, position.z));
            }
            catch (JavascriptException e)
            {
                string error = string.Format("Javascripterror: '{0}' at Line {1} Column {2} to {3}", e.Message, e.Line, e.StartColumn, e.EndColumn);
                throw new SystemException("Error in CommandblockJS Core Javascript code! Please make sure you are using the latest build!\n\n" + error);
            }
            #endif

            #if DEBUG
            jsContext.Run(usercode + "\n cbjsWorker();");
            #else
            try
            {
                jsContext.Run(usercode + "\n cbjsWorker();");
            }
            catch(JavascriptException e)
            {
                string message = string.Format("Javascripterror: '{0}' at Line {1} Column {2} to {3}", e.Message, e.Line, e.StartColumn, e.EndColumn);
                throw new ApplicationException(message);
            }
            #endif
        }
        private void onGenerateButtonClick(object sender, RoutedEventArgs ev)
        {
            tbConsole.Clear();
            if (!Directory.Exists("Log/"))
                Directory.CreateDirectory("Log/");

            var tb = new TextBoxStreamWriter(tbConsole, cout);

            Console.SetOut(tb);

            Console.WriteLine("Running CommandBlockJS");

            Thread.Sleep(1000);

            string world = cbWorldFolder.Text;

            IntVector3 pos = new IntVector3();

            int.TryParse(tbPosX.Text, out pos.x);
            int.TryParse(tbPosY.Text, out pos.y);
            int.TryParse(tbPosZ.Text, out pos.z);

            var js = new CommandBlocksJS.Cmd.JsScriptExecutor();
            try
            {
                js.Run("core.js", tbFileName.Text, world, pos, IsSchematic.IsChecked == true);

                Console.WriteLine("Generation finished successfully");
                tb.Save();
                if (cbClose.IsChecked == true)
                    Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine("Generation failed");
                tb.Save();
            }
        }