示例#1
0
        public void sort()
        {
            DeviceUserEntry entry = UserInputService.get_user_entry(_Console);

            if (entry.Equals(default(DeviceUserEntry)))
            {
                return;
            }

            //TODO check for parser for all modules
            var files = Directory.EnumerateFiles(_parser.DirPath, "*.*") // Remove/Add SearchOption if dont need to resort
                        .Where(s => s.EndsWith(".png") || s.EndsWith(".jpg") || s.EndsWith(".tif"));

            if (files.Count() == 0)
            {
                return;
            }
            var sortPath = Directory.CreateDirectory(Path.Combine(_parser.DirPath, entry.SortWord));

            int index = 0;

            foreach (var file in files)
            {
                FileInfo curFile = DevicePathService.get_file(file);
                if (!(entry.SortTime.Year == curFile.CreationTime.Year &&
                      entry.SortTime.Day == curFile.CreationTime.Day &&
                      entry.SortTime.Month == curFile.CreationTime.Month))
                {
                    continue;
                }
                try
                {
                    System.IO.File.Move(curFile.FullName, Path.Combine(sortPath.FullName, curFile.Name));
                    index++;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }

            if (index == 0)
            {
                Directory.Delete(sortPath.FullName);
                _Console.WriteLine("No entries with this creation date found.");
                _Console.ReadLine();
            }
            else
            {
                DeviceNoteService.add_entry(_parser.DirPath, entry);
                var tree = new Node(_parser.DirPath);

                _Console.Clear();
                _Console.WriteLine("Your new Structure! Press Any Key to proceed.");
                _Console.WriteLine("");
                NodePrinter.print_tree(tree, "", true);
                _Console.ReadLine();
            }
        }
 public MonumentVoxelBrain(ItemTypesFactory typesFactory, UserInputService inputService, InfestationVoxelType infestationVoxelType, IVoxelHandle handle)
 {
     this.inputService         = inputService;
     this.infestationVoxelType = infestationVoxelType;
     crystalType = typesFactory.CrystalType;
     this.Handle = handle;
 }
        [MoonSharpVisible(true)] public void SetInputMode(string mode)
        {
            mode = mode.ToLower();
            if (mode == "any")
            {
                UserInputService.SetInputMode(UserInputService.Modes.Any);
            }
            else
            {
                UserInputService.Modes modes = UserInputService.Modes.None;
                foreach (string m in mode.Split(';'))
                {
                    switch (m)
                    {
                    default: break;

                    // keyboard/mouse inputs only
                    case "desktop": modes |= UserInputService.Modes.Desktop; break;

                    // gamepad inputs only
                    case "gamepad": modes |= UserInputService.Modes.Gamepad; break;

                    // "controller" inputs only
                    case "controller": modes |= UserInputService.Modes.Controller; break;
                    }
                }

                UserInputService.SetInputMode(modes);
            }
        }
示例#4
0
        public static void SelectController(string controllerFileName)
        {
            Controller?nextController = null;

            if (m_controllers.TryGetValue(controllerFileName, out var cached))
            {
                nextController = cached;
            }
            if (Controller.TryCreateFromFile(Path.Combine("controllers", controllerFileName)) is Controller controller)
            {
                m_controllers[controllerFileName] = controller;
                nextController = controller;
            }
            else
            {
                DeselectController();
            }

            if (nextController != null)
            {
                if (m_controller != null)
                {
                    UserInputService.RemoveController(m_controller);
                }
                m_controller = nextController;
                UserInputService.RegisterController(m_controller !);
            }
        }
示例#5
0
        [Pure] protected Client()
        {
            DatabaseWorker = new ChartDatabaseWorker();
            Curtain        = CreateTransitionCurtain();

            LayerStack = new LayerStack(this, CreateInitialLayer());
            UserInputService.Initialize(LayerStack);
        }
示例#6
0
        private static IHostBuilder CreateHostBuilder()
        {
            var userInputService = new UserInputService();

            return(new HostBuilder()
                   .AddAppConfiguration(userInputService)
                   .AddServices(userInputService));
        }
示例#7
0
        public void get_user_entry_name_test()
        {
            var console = new Mock <IConsole>();

            console.Setup(item => item.WriteLine("For which day you want to sort the pictures ? (DD / MM / YYYY)"));
            console.Setup(func => func.ReadLine()).Returns("12/03/2222");
            console.Setup(item => item.WriteLine("What did you do @ this day?"));

            UserInputService.get_user_entry(console.Object);

            console.VerifyAll();
        }
示例#8
0
        public async Task GenerateSqlForType_TypeNeedsUserInfo_ShouldCallUserInputRepo()
        {
            //Act
            await _service.GenerateSqlForType("path", "type", "output", false);

            //Assert
            _userInputRepoMock.Verify(x =>
                                      x.GetUserInput(
                                          UserInputService.GetLengthMessage("PublicTestString")
                                          ),
                                      Times.Once
                                      );
        }
        public void GetUserInfo_ClassDoesntNeedExtraInfo_ShouldNotAskUserForAnything()
        {
            //Assemble
            _classInfo.Properties[0].ValidType = ValidType.Bool;

            //Act
            UserInputService.GetUserInfo(_classInfo, _mockRepo.Object);

            //Assert
            _mockRepo.Verify(
                x => x.GetUserInput(It.IsAny <string>()),
                Times.Never
                );
        }
        public void GetUserInfo_ClassContainsString_ShouldAskUserForLength()
        {
            //Assemble
            _classInfo.Properties[0].ValidType = ValidType.String;

            //Act
            UserInputService.GetUserInfo(_classInfo, _mockRepo.Object);

            //Assert
            _mockRepo.Verify(
                x => x.GetUserInput(UserInputService.GetLengthMessage(
                                        _classInfo.Properties[0].CSharpName
                                        )),
                Times.Once
                );
        }
示例#11
0
        public void PromptUserForInput(string message, string defaultValue, string expectedValue,
                                       string expectedStdout, string stdinInput)
        {
            //before
            var service = new UserInputService();

            var stdout = new StringWriter();

            Console.SetOut(stdout);

            var stdin = new StringReader(stdinInput);

            Console.SetIn(stdin);

            //when
            var value = service.PromptUserForInput(message, defaultValue);

            //then
            Assert.Equal(expectedStdout, stdout.ToString());
            Assert.Equal(expectedValue, value);
        }
        public void GetUserInfo_UserGivesInvalidValue_ShouldThrowException()
        {
            //Assemble
            var invalidInput = "invalid input";

            _classInfo.Properties[0].ValidType = ValidType.Decimal;
            _mockRepo
            .Setup(x => x.GetUserInput(It.IsAny <string>()))
            .Returns(invalidInput);

            //Act
            Action act = () => UserInputService.GetUserInfo(_classInfo, _mockRepo.Object);

            //Assert
            act
            .Should()
            .ThrowExactly <InvalidInputException>()
            .WithMessage(
                UserInputService.InvalidInputExceptionBadInput(invalidInput)
                );
        }
        public void GetUserInfo_ClassContainsDouble_ShouldAskUserForPrecisionAndScale()
        {
            //Assemble
            _classInfo.Properties[0].ValidType = ValidType.Double;

            //Act
            UserInputService.GetUserInfo(_classInfo, _mockRepo.Object);

            //Assert
            _mockRepo.Verify(
                x => x.GetUserInput(UserInputService.GetPrecisionMessage(
                                        _classInfo.Properties[0].CSharpName
                                        )),
                Times.Once
                );
            _mockRepo.Verify(
                x => x.GetUserInput(UserInputService.GetScaleMessage(
                                        _classInfo.Properties[0].CSharpName
                                        )),
                Times.Once
                );
        }
示例#14
0
        private static Controller?CreateYuanConController()
        {
            if (!(UserInputService.TryGetGamepadFromName("YuanCon") is Gamepad gamepad))
            {
                return(null);
            }

            var con = new Controller("NeuroSonic YuanCon");

            con.SetButtonToGamepadButton(0, gamepad, 1);
            con.SetButtonToGamepadButton(1, gamepad, 2);
            con.SetButtonToGamepadButton(2, gamepad, 3);
            con.SetButtonToGamepadButton(3, gamepad, 7);
            con.SetButtonToGamepadButton(4, gamepad, 5);
            con.SetButtonToGamepadButton(5, gamepad, 6);

            con.SetButtonToGamepadButton("start", gamepad, 0);
            con.SetButtonToGamepadButton("back", gamepad, 9);

            con.SetAxisToGamepadAxis(0, gamepad, 0, ControllerAxisStyle.Radial);
            con.SetAxisToGamepadAxis(1, gamepad, 1, ControllerAxisStyle.Radial);

            return(con);
        }
示例#15
0
        public Layer(ClientResourceLocator?resourceLocator = null, string?layerPathLua = null, params object[] args)
        {
            ResourceLocator  = resourceLocator ?? ClientResourceLocator.Default;
            m_spriteRenderer = new BasicSpriteRenderer(ResourceLocator);

            m_resources = new ClientResourceManager(ResourceLocator);
            m_script    = new ScriptProgram(ResourceLocator);

            m_renderer2D = new RenderBatch2D(m_resources);

            m_drivingScriptFileName = layerPathLua;
            m_drivingScriptArgs     = args;

            m_script["KeyCode"]             = typeof(KeyCode);
            m_script["MouseButton"]         = typeof(MouseButton);
            m_script["ControllerAxisStyle"] = typeof(ControllerAxisStyle);

            m_script["theori"] = tblTheori = m_script.NewTable();

            tblTheori["isFirstLaunch"] = (Func <bool>)(() => Host.IsFirstLaunch);

            tblTheori["audio"]    = tblTheoriAudio = m_script.NewTable();
            tblTheori["charts"]   = tblTheoriCharts = m_script.NewTable();
            tblTheori["config"]   = tblTheoriConfig = m_script.NewTable();
            tblTheori["game"]     = tblTheoriGame = m_script.NewTable();
            tblTheori["graphics"] = tblTheoriGraphics = m_script.NewTable();
            tblTheori["input"]    = tblTheoriInput = m_script.NewTable();
            tblTheori["layer"]    = tblTheoriLayer = m_script.NewTable();
            tblTheori["modes"]    = tblTheoriModes = m_script.NewTable();
            tblTheori["net"]      = tblTheoriNet = m_script.NewTable();

            tblTheoriInput["textInput"] = evtTextInput = m_script.NewEvent();

            tblTheoriInput["keyboard"]   = tblTheoriInputKeyboard = m_script.NewTable();
            tblTheoriInput["mouse"]      = tblTheoriInputMouse = m_script.NewTable();
            tblTheoriInput["gamepad"]    = tblTheoriInputGamepad = m_script.NewTable();
            tblTheoriInput["controller"] = tblTheoriInputController = m_script.NewTable();

            tblTheoriInputKeyboard["isDown"]      = (Func <KeyCode, bool>)(key => UserInputService.IsKeyDown(key));
            tblTheoriInputKeyboard["pressed"]     = evtKeyPressed = m_script.NewEvent();
            tblTheoriInputKeyboard["released"]    = evtKeyReleased = m_script.NewEvent();
            tblTheoriInputKeyboard["pressedRaw"]  = evtRawKeyPressed = m_script.NewEvent();
            tblTheoriInputKeyboard["releasedRaw"] = evtRawKeyReleased = m_script.NewEvent();

            tblTheoriInputMouse["pressed"]          = evtMousePressed = m_script.NewEvent();
            tblTheoriInputMouse["released"]         = evtMouseReleased = m_script.NewEvent();
            tblTheoriInputMouse["moved"]            = evtMouseMoved = m_script.NewEvent();
            tblTheoriInputMouse["scrolled"]         = evtMouseScrolled = m_script.NewEvent();
            tblTheoriInputMouse["pressedRaw"]       = evtRawMousePressed = m_script.NewEvent();
            tblTheoriInputMouse["releasedRaw"]      = evtRawMouseReleased = m_script.NewEvent();
            tblTheoriInputMouse["movedRaw"]         = evtRawMouseMoved = m_script.NewEvent();
            tblTheoriInputMouse["scrolledRaw"]      = evtRawMouseScrolled = m_script.NewEvent();
            tblTheoriInputMouse["getMousePosition"] = (Func <DynValue>)(() => NewTuple(NewNumber(UserInputService.MouseX), NewNumber(UserInputService.MouseY)));

            tblTheoriInputGamepad["connected"]      = evtGamepadConnected = m_script.NewEvent();
            tblTheoriInputGamepad["disconnected"]   = evtGamepadDisconnected = m_script.NewEvent();
            tblTheoriInputGamepad["pressed"]        = evtGamepadPressed = m_script.NewEvent();
            tblTheoriInputGamepad["released"]       = evtGamepadReleased = m_script.NewEvent();
            tblTheoriInputGamepad["axisChanged"]    = evtGamepadAxisChanged = m_script.NewEvent();
            tblTheoriInputGamepad["pressedRaw"]     = evtRawGamepadPressed = m_script.NewEvent();
            tblTheoriInputGamepad["releasedRaw"]    = evtRawGamepadReleased = m_script.NewEvent();
            tblTheoriInputGamepad["axisChangedRaw"] = evtRawGamepadAxisChanged = m_script.NewEvent();

            tblTheoriInputController["added"]       = evtControllerAdded = m_script.NewEvent();
            tblTheoriInputController["removed"]     = evtControllerRemoved = m_script.NewEvent();
            tblTheoriInputController["pressed"]     = evtControllerPressed = m_script.NewEvent();
            tblTheoriInputController["released"]    = evtControllerReleased = m_script.NewEvent();
            tblTheoriInputController["axisChanged"] = evtControllerAxisChanged = m_script.NewEvent();
            tblTheoriInputController["axisTicked"]  = evtControllerAxisTicked = m_script.NewEvent();

            tblTheori["doStaticLoadsAsync"]  = (Func <bool>)(() => StaticResources.LoadAll());
            tblTheori["finalizeStaticLoads"] = (Func <bool>)(() => StaticResources.FinalizeLoad());

            tblTheoriAudio["queueStaticAudioLoad"] = (Func <string, AudioHandle>)(audioName => StaticResources.QueueAudioLoad($"audio/{ audioName }"));
            tblTheoriAudio["getStaticAudio"]       = (Func <string, AudioHandle>)(audioName => StaticResources.GetAudio($"audio/{ audioName }"));
            tblTheoriAudio["queueAudioLoad"]       = (Func <string, AudioHandle>)(audioName => m_resources.QueueAudioLoad($"audio/{ audioName }"));
            tblTheoriAudio["getAudio"]             = (Func <string, AudioHandle>)(audioName => m_resources.GetAudio($"audio/{ audioName }"));
            tblTheoriAudio["createFakeAudio"]      = (Func <int, int, AudioHandle>)((sampleRate, channels) => new FakeAudioSource(sampleRate, channels));

            tblTheoriCharts["setDatabaseToIdle"] = (Action)(() => Client.DatabaseWorker.SetToIdle());
            tblTheoriCharts["getDatabaseState"]  = (Func <string>)(() => Client.DatabaseWorker.State.ToString());

            tblTheoriCharts["setDatabaseToClean"] = (Action <DynValue>)(arg =>
            {
                Client.DatabaseWorker.SetToClean(arg == DynValue.Void ? (Action?)null : () => m_script.Call(arg));
            });

            tblTheoriCharts["setDatabaseToPopulate"] = NewCallback((ctx, args) =>
            {
                Action?callback = args.Count == 0 ? (Action?)null : () => ctx.Call(args[0]);
                Client.DatabaseWorker.SetToPopulate(callback);
                return(Nil);
            });

            tblTheoriCharts["create"] = (Func <string, ChartHandle>)(modeName =>
            {
                var mode = GameMode.GetInstance(modeName);
                //if (mode == null) return DynValue.Nil;
                return(new ChartHandle(m_resources, m_script, Client.DatabaseWorker, mode !.GetChartFactory().CreateNew()));
            });
            tblTheoriCharts["newEntity"] = (Func <string, Entity>)(entityTypeId =>
            {
                var entityType = Entity.GetEntityTypeById(entityTypeId);
                return((Entity)Activator.CreateInstance(entityType !));
            });
            tblTheoriCharts["saveChartToDatabase"] = (Action <Chart>)(chart =>
            {
                var ser    = chart.GameMode.CreateChartSerializer(TheoriConfig.ChartsDirectory, chart.Info.ChartFileType) ?? new TheoriChartSerializer(TheoriConfig.ChartsDirectory, chart.GameMode);
                var setSer = new ChartSetSerializer(TheoriConfig.ChartsDirectory);

                setSer.SaveToFile(chart.SetInfo);
                ser.SaveToFile(chart);
            });

            tblTheoriCharts["createCollection"]          = (Action <string>)(collectionName => Client.DatabaseWorker.CreateCollection(collectionName));
            tblTheoriCharts["addChartToCollection"]      = (Action <string, ChartInfoHandle>)((collectionName, chart) => Client.DatabaseWorker.AddChartToCollection(collectionName, chart));
            tblTheoriCharts["removeChartFromCollection"] = (Action <string, ChartInfoHandle>)((collectionName, chart) => Client.DatabaseWorker.RemoveChartFromCollection(collectionName, chart));
            tblTheoriCharts["getCollectionNames"]        = (Func <string[]>)(() => Client.DatabaseWorker.CollectionNames);
            tblTheoriCharts["getFolderNames"]            = (Func <string[]>)(() => Directory.GetDirectories(TheoriConfig.ChartsDirectory).Select(d => Path.GetFileName(d)).ToArray());

            tblTheoriCharts["getChartSets"]         = (Func <List <ChartSetInfoHandle> >)(() => Client.DatabaseWorker.ChartSets.Select(info => new ChartSetInfoHandle(m_resources, m_script, Client.DatabaseWorker, info)).ToList());
            tblTheoriCharts["getScores"]            = (Func <ChartInfoHandle, ScoreData[]>)(chart => ChartDatabaseService.GetScoresForChart(chart.Object));
            tblTheoriCharts["getChartSetsFiltered"] = (Func <string?, DynValue, DynValue, DynValue, List <List <ChartInfoHandle> > >)((col, a, b, c) =>
            {
                Logger.Log("Attempting to filter charts...");

                var setInfoHandles = new Dictionary <ChartSetInfo, ChartSetInfoHandle>();
                ChartSetInfoHandle GetSetInfoHandle(ChartSetInfo chartSet)
                {
                    if (!setInfoHandles.TryGetValue(chartSet, out var result))
                    {
                        result = setInfoHandles[chartSet] = new ChartSetInfoHandle(m_resources, m_script, Client.DatabaseWorker, chartSet);
                    }
                    return(result);
                }

                Logger.Log(col ?? "null");
                var charts         = col != null ? Client.DatabaseWorker.GetChartsInCollection(col) : Client.DatabaseWorker.Charts;
                var filteredCharts = from initialChart in charts
                                     let handle = new ChartInfoHandle(GetSetInfoHandle(initialChart.Set), initialChart)
                                                  where m_script.Call(a, handle).CastToBool()
                                                  select handle;

                var groupedCharts = filteredCharts.OrderBy(chart => m_script.Call(b, chart), DynValueComparer.Instance)
                                    .GroupBy(chart => m_script.Call(b, chart))
                                    .Select(theGroup => (theGroup.Key, Value: theGroup
                                                         .OrderBy(chart => chart.DifficultyIndex)
                                                         .ThenBy(chart => chart.DifficultyName)
                                                         .ThenBy(chart => m_script.Call(c, chart), DynValueComparer.Instance)
                                                         .Select(chart => chart)
                                                         .ToList()))
                                    .OrderBy(l => l.Key, DynValueComparer.Instance)
                                    .Select(l => l.Value)
                                    .ToList();

                return(groupedCharts);
            });

            tblTheoriConfig["get"]  = (Func <string, DynValue>)(key => FromObject(m_script.Script, UserConfigManager.GetFromKey(key)));
            tblTheoriConfig["set"]  = (Action <string, DynValue>)((key, value) => UserConfigManager.SetFromKey(key, value.ToObject()));
            tblTheoriConfig["save"] = (Action)(() => UserConfigManager.SaveToFile());

            tblTheoriGame["exit"] = (Action)(() => Host.Exit());

            tblTheoriGraphics["queueStaticTextureLoad"] = (Func <string, Texture>)(textureName => StaticResources.QueueTextureLoad($"textures/{ textureName }"));
            tblTheoriGraphics["getStaticTexture"]       = (Func <string, Texture>)(textureName => StaticResources.GetTexture($"textures/{ textureName }"));
            tblTheoriGraphics["queueTextureLoad"]       = (Func <string, Texture>)(textureName => m_resources.QueueTextureLoad($"textures/{ textureName }"));
            tblTheoriGraphics["getTexture"]             = (Func <string, Texture>)(textureName => m_resources.GetTexture($"textures/{ textureName }"));
            tblTheoriGraphics["createStaticFont"]       = (Func <string, VectorFont?>)(fontName => ResourceLocator.OpenFileStreamWithExtension($"fonts/{ fontName }", new[] { ".ttf", ".otf" }, out string _) is Stream fs ? staticFonts[fontName] = new VectorFont(fs) : null);
            tblTheoriGraphics["createFont"]             = (Func <string, VectorFont?>)(fontName => ResourceLocator.OpenFileStreamWithExtension($"fonts/{ fontName }", new[] { ".ttf", ".otf" }, out string _) is Stream fs ? new VectorFont(fs) : null);
            tblTheoriGraphics["getStaticFont"]          = (Func <string, VectorFont?>)(fontName => staticFonts.TryGetValue(fontName, out var font) ? font : null);
            //tblTheoriGraphics["getFont"] = (Func<string, VectorFont>)(fontName => m_resources.GetTexture($"fonts/{ fontName }"));
            tblTheoriGraphics["getViewportSize"]    = (Func <DynValue>)(() => NewTuple(NewNumber(Window.Width), NewNumber(Window.Height)));
            tblTheoriGraphics["createPathCommands"] = (Func <Path2DCommands>)(() => new Path2DCommands());

            tblTheoriGraphics["flush"]                  = (Action)(() => m_batch?.Flush());
            tblTheoriGraphics["saveTransform"]          = (Action)(() => m_batch?.SaveTransform());
            tblTheoriGraphics["restoreTransform"]       = (Action)(() => m_batch?.RestoreTransform());
            tblTheoriGraphics["resetTransform"]         = (Action)(() => m_batch?.ResetTransform());
            tblTheoriGraphics["translate"]              = (Action <float, float>)((x, y) => m_batch?.Translate(x, y));
            tblTheoriGraphics["rotate"]                 = (Action <float>)(d => m_batch?.Rotate(d));
            tblTheoriGraphics["scale"]                  = (Action <float, float>)((x, y) => m_batch?.Scale(x, y));
            tblTheoriGraphics["shear"]                  = (Action <float, float>)((x, y) => m_batch?.Shear(x, y));
            tblTheoriGraphics["setFillToColor"]         = (Action <float, float, float, float>)((r, g, b, a) => m_batch?.SetFillColor(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f));
            tblTheoriGraphics["setFillToTexture"]       = (Action <Texture, float, float, float, float>)((texture, r, g, b, a) => m_batch?.SetFillTexture(texture, new Vector4(r, g, b, a) / 255.0f));
            tblTheoriGraphics["fillRect"]               = (Action <float, float, float, float>)((x, y, w, h) => m_batch?.FillRectangle(x, y, w, h));
            tblTheoriGraphics["fillRoundedRect"]        = (Action <float, float, float, float, float>)((x, y, w, h, r) => m_batch?.FillRoundedRectangle(x, y, w, h, r));
            tblTheoriGraphics["fillRoundedRectVarying"] = (Action <float, float, float, float, float, float, float, float>)((x, y, w, h, rtl, rtr, rbr, rbl) => m_batch?.FillRoundedRectangleVarying(x, y, w, h, rtl, rtr, rbr, rbl));
            tblTheoriGraphics["setFont"]                = (Action <VectorFont?>)(font => m_batch?.SetFont(font));
            tblTheoriGraphics["setFontSize"]            = (Action <int>)(size => m_batch?.SetFontSize(size));
            tblTheoriGraphics["setTextAlign"]           = (Action <Anchor>)(align => m_batch?.SetTextAlign(align));
            tblTheoriGraphics["fillString"]             = (Action <string, float, float>)((text, x, y) => m_batch?.FillString(text, x, y));
            tblTheoriGraphics["measureString"]          = (Func <string, DynValue>)(text => { var bounds = m_batch?.MeasureString(text) !.Value; return(NewTuple(NewNumber(bounds.X), NewNumber(bounds.Y))); });
            tblTheoriGraphics["fillPathAt"]             = (Action <Path2DCommands, float, float, float, float>)((path, x, y, sx, sy) => m_batch?.FillPathAt(path, x, y, sx, sy));
            tblTheoriGraphics["saveScissor"]            = (Action)(() => m_batch?.SaveScissor());
            tblTheoriGraphics["restoreScissor"]         = (Action)(() => m_batch?.RestoreScissor());
            tblTheoriGraphics["resetScissor"]           = (Action)(() => m_batch?.ResetScissor());
            tblTheoriGraphics["scissor"]                = (Action <float, float, float, float>)((x, y, w, h) => m_batch?.Scissor(x, y, w, h));

            tblTheoriGraphics["openCurtain"]  = (Action)OpenCurtain;
            tblTheoriGraphics["closeCurtain"] = (Action <float, DynValue?>)((duration, callback) =>
            {
                Action?onClosed = (callback == null || callback == Nil) ? (Action?)null : () => m_script.Call(callback !);
                if (duration <= 0)
                {
                    CloseCurtain(onClosed);
                }
                else
                {
                    CloseCurtain(duration, onClosed);
                }
            });
示例#16
0
        public void Run(Client client)
        {
            //Profiler.BeginSession("Main Game Process");

            void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
            {
                switch (client.OnUnhandledException())
                {
                case UnhandledExceptionAction.SafeExit:
                {
                    if (e.ExceptionObject is Exception ex)
                    {
                        Logger.Log(ex.Message);
                        Logger.Log(ex.StackTrace);
                    }
                    else
                    {
                        Logger.Log("Unknown exception thrown?");
                    }

                    PerformExit(true);
                } break;

                case UnhandledExceptionAction.DoNothing: break;     // oh, okay

                case UnhandledExceptionAction.GiveUpRethrow:
                {
                    //Profiler.EndSession();
                    AppDomain.CurrentDomain.UnhandledException -= UnhandledExceptionHandler;
                    throw (Exception)e.ExceptionObject;
                }     // break;
                }
            }

            try
            {
                AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;

                Window.Update();
                client.SetHost(this);

                var  timer          = Stopwatch.StartNew();
                long lastFrameStart = timer.ElapsedMilliseconds;

                long accumulatedTimeMillis = 0;

                while (true)
                {
                    using var _ = Profiler.Scope("Game Loop");

                    long currentTimeMillis = timer.ElapsedMilliseconds;
                    //Time.HighResolution = timer.ElapsedTicks * 1_000_000L / Stopwatch.Frequency;

                    long elapsedTimeMillis = currentTimeMillis - lastFrameStart;
                    Time.Delta = elapsedTimeMillis / 1_000.0f;

                    m_samples[m_sampleIndex] = Time.Delta;
                    m_sampleIndex            = (m_sampleIndex + 1) % m_samples.Length;

                    accumulatedTimeMillis += elapsedTimeMillis;

                    long targetFrameTimeMillis = client.TargetFrameTimeMillis;
                    Time.FixedDelta = targetFrameTimeMillis / 1_000.0f;

                    Mixer.MasterChannel.Volume = TheoriConfig.MasterVolume;

                    client.BeginFrame();

                    // == Input Step (gather, trigger events)

                    {
                        using var __ = Profiler.Scope("Input Step");

                        client.BeginInputStep();

                        UserInputService.Update();
                        Window.Update();

                        client.EndInputStep();
                    }

                    // == Update Step (process update queue, instruct client to update)

                    {
                        using var __ = Profiler.Scope("Update Queue Processing");

                        while (m_updateQueue.Count > 0)
                        {
                            var item = m_updateQueue.Dequeue();
                            item?.Invoke();
                        }
                    }

                    {
                        using var __ = Profiler.Scope("Update Step");

                        client.BeginUpdateStep();

                        Time.Total = currentTimeMillis / 1_000.0f;

                        {
                            using var ___ = Profiler.Scope("High-Rate Update");
                            client.Update(Time.Delta, Time.Total);
                        }

                        {
                            using var ___ = Profiler.Scope("Fixed-Rate Update");

                            long lastFixedUpdateStart = lastFrameStart;
                            while (accumulatedTimeMillis >= targetFrameTimeMillis)
                            {
                                lastFixedUpdateStart += targetFrameTimeMillis;
                                Time.Total            = lastFixedUpdateStart / 1_000.0f;

                                client.FixedUpdate(Time.FixedDelta, Time.Total);
                                accumulatedTimeMillis -= targetFrameTimeMillis;
                            }
                        }

                        Time.Total = currentTimeMillis / 1_000.0f;

                        {
                            using var ___ = Profiler.Scope("Late Update");
                            client.LateUpdate();
                        }

                        client.EndUpdateStep();
                    }

                    // == Render Step (instruct client to render)

                    if (Window.Width > 0 && Window.Height > 0)
                    {
                        using var __ = Profiler.Scope("Render Step");

                        GL.ClearColor(0, 0, 0, 1);
                        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

                        client.BeginRenderStep();

                        client.Render();
                        client.LateRender();

                        client.EndRenderStep();

                        Window.SwapBuffer();
                    }

                    lastFrameStart = currentTimeMillis;

                    if (Profiler.IsEnabled)
                    {
                        Profiler.EndSession();
                        Profiler.IsEnabled = false;
                    }

                    if (elapsedTimeMillis < targetFrameTimeMillis)
                    {
                        Thread.Sleep(1);
                    }
                }
            }
            finally
            {
                PerformExit(true);
            }
        }