예제 #1
0
        public void TestCustomDirectory()
        {
            using (HeadlessGameHost host = new TestRunHeadlessGameHost(nameof(TestCustomDirectory), null)) // don't use clean run as we are writing a config file.
            {
                string       osuDesktopStorage = Path.Combine(host.UserStoragePaths.First(), nameof(TestCustomDirectory));
                const string custom_tournament = "custom";

                // need access before the game has constructed its own storage yet.
                Storage storage = new DesktopStorage(osuDesktopStorage, host);
                // manual cleaning so we can prepare a config file.
                storage.DeleteDirectory(string.Empty);

                using (var storageConfig = new TournamentStorageManager(storage))
                    storageConfig.SetValue(StorageConfig.CurrentTournament, custom_tournament);

                try
                {
                    var osu = LoadTournament(host);

                    storage = osu.Dependencies.Get <Storage>();

                    Assert.That(storage.GetFullPath("."), Is.EqualTo(Path.Combine(host.Storage.GetFullPath("."), "tournaments", custom_tournament)));
                }
                finally
                {
                    host.Exit();
                }
            }
        }
        public void TestThreadSafetyResetOnEnteringThread()
        {
            using (var host = new TestRunHeadlessGameHost(nameof(TestThreadSafetyResetOnEnteringThread), new HostOptions()))
            {
                bool isDrawThread   = false;
                bool isUpdateThread = false;
                bool isInputThread  = false;
                bool isAudioThread  = false;

                var task = Task.Factory.StartNew(() =>
                {
                    var game = new TestGame();
                    game.Scheduler.Add(() => host.Exit());

                    host.Run(game);

                    isDrawThread   = ThreadSafety.IsDrawThread;
                    isUpdateThread = ThreadSafety.IsUpdateThread;
                    isInputThread  = ThreadSafety.IsInputThread;
                    isAudioThread  = ThreadSafety.IsAudioThread;
                }, TaskCreationOptions.LongRunning);

                task.WaitSafely();

                Assert.That(!isDrawThread && !isUpdateThread && !isInputThread && !isAudioThread);
            }
        }
예제 #3
0
        public void CheckIPCLocation()
        {
            // don't use clean run because files are being written before osu! launches.
            using (var host = new TestRunHeadlessGameHost(nameof(CheckIPCLocation), null))
            {
                string basePath = Path.Combine(host.UserStoragePaths.First(), nameof(CheckIPCLocation));

                // Set up a fake IPC client for the IPC Storage to switch to.
                string testStableInstallDirectory = Path.Combine(basePath, "stable-ce");
                Directory.CreateDirectory(testStableInstallDirectory);

                string ipcFile = Path.Combine(testStableInstallDirectory, "ipc.txt");
                File.WriteAllText(ipcFile, string.Empty);

                try
                {
                    var osu = LoadTournament(host);
                    TournamentStorage storage = (TournamentStorage)osu.Dependencies.Get <Storage>();
                    FileBasedIPC      ipc     = null;

                    WaitForOrAssert(() => (ipc = osu.Dependencies.Get <MatchIPCInfo>() as FileBasedIPC)?.IsLoaded == true, @"ipc could not be populated in a reasonable amount of time");

                    Assert.True(ipc.SetIPCLocation(testStableInstallDirectory));
                    Assert.True(storage.AllTournaments.Exists("stable.json"));
                }
                finally
                {
                    host.Exit();
                }
            }
        }
예제 #4
0
        public async Task TestSaveAndReload()
        {
            string firstRunName;

            using (var host = new CleanRunHeadlessGameHost(bypassCleanup: true))
            {
                firstRunName = host.Name;

                try
                {
                    var osu = LoadOsuIntoHost(host, true);

                    await importCollectionsFromStream(osu, TestResources.OpenResource("Collections/collections.db"));

                    // Move first beatmap from second collection into the first.
                    osu.CollectionManager.Collections[0].Beatmaps.Add(osu.CollectionManager.Collections[1].Beatmaps[0]);
                    osu.CollectionManager.Collections[1].Beatmaps.RemoveAt(0);

                    // Rename the second collecction.
                    osu.CollectionManager.Collections[1].Name.Value = "Another";
                }
                finally
                {
                    host.Exit();
                }
            }

            // Name matches the automatically chosen name from `CleanRunHeadlessGameHost` above, so we end up using the same storage location.
            using (HeadlessGameHost host = new TestRunHeadlessGameHost(firstRunName))
            {
                try
                {
                    var osu = LoadOsuIntoHost(host, true);

                    Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));

                    Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
                    Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(2));

                    Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Another"));
                    Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(11));
                }
                finally
                {
                    host.Exit();
                }
            }
        }
예제 #5
0
        public async Task TestSaveAndReload()
        {
            using (HeadlessGameHost host = new TestRunHeadlessGameHost("TestSaveAndReload", bypassCleanup: true))
            {
                try
                {
                    var osu = LoadOsuIntoHost(host, true);

                    await importCollectionsFromStream(osu, TestResources.OpenResource("Collections/collections.db"));

                    // Move first beatmap from second collection into the first.
                    osu.CollectionManager.Collections[0].Beatmaps.Add(osu.CollectionManager.Collections[1].Beatmaps[0]);
                    osu.CollectionManager.Collections[1].Beatmaps.RemoveAt(0);

                    // Rename the second collecction.
                    osu.CollectionManager.Collections[1].Name.Value = "Another";
                }
                finally
                {
                    host.Exit();
                }
            }

            using (HeadlessGameHost host = new TestRunHeadlessGameHost("TestSaveAndReload"))
            {
                try
                {
                    var osu = LoadOsuIntoHost(host, true);

                    Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));

                    Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
                    Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(2));

                    Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Another"));
                    Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(11));
                }
                finally
                {
                    host.Exit();
                }
            }
        }
예제 #6
0
        private void runGameWithLogic(Action <Game> logic, Func <Game, bool> exitCondition = null)
        {
            Storage storage = null;

            try
            {
                using (var host = new TestRunHeadlessGameHost($"{GetType().Name}-{Guid.NewGuid()}", new HostOptions()))
                {
                    using (var game = new TestGame())
                    {
                        game.Schedule(() =>
                        {
                            storage = host.Storage;
                            host.UpdateThread.Scheduler.AddDelayed(() =>
                            {
                                if (exitCondition?.Invoke(game) == true)
                                {
                                    host.Exit();
                                }
                            }, 0, true);

                            logic(game);
                        });

                        host.Run(game);
                    }
                }
            }
            finally
            {
                try
                {
                    storage?.DeleteDirectory(string.Empty);
                }
                catch
                {
                    // May fail due to the file handles still being open on Windows, but this isn't a big problem for us
                }
            }
        }
예제 #7
0
        public void TestMigration()
        {
            using (HeadlessGameHost host = new TestRunHeadlessGameHost(nameof(TestMigration), null)) // don't use clean run as we are writing test files for migration.
            {
                string osuRoot    = Path.Combine(host.UserStoragePaths.First(), nameof(TestMigration));
                string configFile = Path.Combine(osuRoot, "tournament.ini");

                if (File.Exists(configFile))
                {
                    File.Delete(configFile);
                }

                // Recreate the old setup that uses "tournament" as the base path.
                string oldPath = Path.Combine(osuRoot, "tournament");

                string videosPath = Path.Combine(oldPath, "Videos");
                string modsPath   = Path.Combine(oldPath, "Mods");
                string flagsPath  = Path.Combine(oldPath, "Flags");

                Directory.CreateDirectory(videosPath);
                Directory.CreateDirectory(modsPath);
                Directory.CreateDirectory(flagsPath);

                // Define testing files corresponding to the specific file migrations that are needed
                string bracketFile = Path.Combine(osuRoot, TournamentGameBase.BRACKET_FILENAME);

                string drawingsConfig = Path.Combine(osuRoot, "drawings.ini");
                string drawingsFile   = Path.Combine(osuRoot, "drawings.txt");
                string drawingsResult = Path.Combine(osuRoot, "drawings_results.txt");

                // Define sample files to test recursive copying
                string videoFile = Path.Combine(videosPath, "video.mp4");
                string modFile   = Path.Combine(modsPath, "mod.png");
                string flagFile  = Path.Combine(flagsPath, "flag.png");

                File.WriteAllText(bracketFile, "{}");
                File.WriteAllText(drawingsConfig, "test");
                File.WriteAllText(drawingsFile, "test");
                File.WriteAllText(drawingsResult, "test");
                File.WriteAllText(videoFile, "test");
                File.WriteAllText(modFile, "test");
                File.WriteAllText(flagFile, "test");

                try
                {
                    var osu = LoadTournament(host);

                    var storage = osu.Dependencies.Get <Storage>();

                    string migratedPath = Path.Combine(host.Storage.GetFullPath("."), "tournaments", "default");

                    videosPath = Path.Combine(migratedPath, "Videos");
                    modsPath   = Path.Combine(migratedPath, "Mods");
                    flagsPath  = Path.Combine(migratedPath, "Flags");

                    videoFile = Path.Combine(videosPath, "video.mp4");
                    modFile   = Path.Combine(modsPath, "mod.png");
                    flagFile  = Path.Combine(flagsPath, "flag.png");

                    Assert.That(storage.GetFullPath("."), Is.EqualTo(migratedPath));

                    Assert.True(storage.Exists(TournamentGameBase.BRACKET_FILENAME));
                    Assert.True(storage.Exists("drawings.txt"));
                    Assert.True(storage.Exists("drawings_results.txt"));

                    Assert.True(storage.Exists("drawings.ini"));

                    Assert.True(storage.Exists(videoFile));
                    Assert.True(storage.Exists(modFile));
                    Assert.True(storage.Exists(flagFile));
                }
                finally
                {
                    host.Exit();
                }
            }
        }