コード例 #1
0
        private async void Randomize(object sender, EventArgs e)
        {
            if (working)
            {
                return;
            }
            ReadControlFlags(this);
            // TODO: Allow this to be empty
            string gameDir = null;

            if (!string.IsNullOrWhiteSpace(exe.Text))
            {
                gameDir = Path.GetDirectoryName(exe.Text);
                if (!File.Exists($@"{gameDir}\Data0.bdt"))
                {
                    SetError("Error: Data0.bdt not found for the mod to merge. Leave it blank to use Fog Gate Randomizer by itself.");
                    return;
                }
                if (new DirectoryInfo(gameDir).FullName == Directory.GetCurrentDirectory())
                {
                    SetError("Error: Data0.bdt is not from a different mod! Leave it blank to use Fog Gate Randomizer by itself.");
                    return;
                }
            }
            if (fixedseed.Text.Trim() != "")
            {
                if (uint.TryParse(fixedseed.Text.Trim(), out uint seed))
                {
                    options.Seed = (int)seed;
                }
                else
                {
                    SetError("Invalid fixed seed");
                    return;
                }
            }
            else
            {
                options.Seed = new Random().Next();
            }
            fixedseed.Text = options.Seed.ToString();
            UpdateOptions(null, null);  // Can split up maybe
            SetError();

            working = true;
            string prevText = randb.Text;

            randb.Text = $"Randomizing...";
            setStatus("Randomizing...");
            RandomizerOptions rand = options.Copy();

            randb.BackColor = Color.LightYellow;
            Randomizer randomizer = new Randomizer();
            await Task.Factory.StartNew(() => {
                Directory.CreateDirectory("spoiler_logs");
                string runId      = $@"spoiler_logs\{DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss")}_log_{rand.Seed}_{rand.ConfigHash()}.txt";
                TextWriter log    = File.CreateText(runId);
                TextWriter stdout = Console.Out;
                Console.SetOut(log);
                try
                {
                    ItemReader.Result itemInfo = randomizer.Randomize(rand, FromGame.DS3, gameDir, Directory.GetCurrentDirectory());
                    setStatus($"Done. Info in {runId} | Restart your game!" + (itemInfo.Randomized ? $" | Key item hash: {itemInfo.ItemHash}" : ""), success: true);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    SetError($"Error encountered: {ex.Message}\r\n\r\nIt may work to try again with a different seed. {(gameDir == null ? "" : "The merged mod might also not be compatible. ")}See most recent file in spoiler_logs directory for the full error.");
                    setStatus($"Error! See error message in {runId}", true);
                }
                finally
                {
                    log.Close();
                    Console.SetOut(stdout);
                }
            });

            randb.Enabled   = true;
            randb.Text      = prevText;
            randb.BackColor = SystemColors.Control;
            working         = false;
            UpdateExePath();
        }