示例#1
0
    static IEnumerator TestComponents(List <KtaneModule> modules)
    {
        IEnumerable <BombComponent> untestedComponents = GetUntestedComponents(modules);
        Dictionary <string, string> nameMap            = GetNameMap(modules);

        GameObject fakeModule = new GameObject();

        gameObjects.Add(fakeModule);
        TwitchModule module = fakeModule.AddComponent <TwitchModule>();

        module.enabled = false;

        HashSet <string>          unsupportedModules = new HashSet <string>();
        Dictionary <string, bool> supportStatus      = new Dictionary <string, bool>();

        ComponentSolverFactory.SilentMode = true;

        // Try to create a ComponentSolver for each module so we can see what modules are supported.
        foreach (BombComponent bombComponent in untestedComponents)
        {
            ComponentSolver solver = null;
            try
            {
                module.BombComponent = bombComponent.GetComponent <BombComponent>();

                solver = ComponentSolverFactory.CreateSolver(module);

                module.StopAllCoroutines();                 // Stop any coroutines to prevent any exceptions or from affecting the next module.
            }
            catch (Exception e)
            {
                DebugHelper.LogException(e, $"Couldn't create a component solver for \"{bombComponent.GetModuleDisplayName()}\" during startup for the following reason:");
            }

            ModuleData.DataHasChanged |= solver != null;

            DebugHelper.Log(solver != null
                                ? $"Found a solver of type \"{solver.GetType().FullName}\" for component \"{bombComponent.GetModuleDisplayName()}\". This module is {(solver.UnsupportedModule ? "not supported" : "supported")} by Twitch Plays."
                                : $"No solver found for component \"{bombComponent.GetModuleDisplayName()}\". This module is not supported by Twitch Plays.");

            string moduleID = bombComponent.GetComponent <KMBombModule>()?.ModuleType ?? bombComponent.GetComponent <KMNeedyModule>()?.ModuleType;
            if (solver?.UnsupportedModule != false && moduleID != null)
            {
                unsupportedModules.Add(moduleID);
            }

            supportStatus[bombComponent.GetModuleDisplayName()] = !(solver?.UnsupportedModule != false && moduleID != null);

            yield return(null);
        }

        ComponentSolverFactory.SilentMode = false;
        ModuleData.WriteDataToFile();
        Object.Destroy(fakeModule);

        // Always disable the modules from the spreadsheet
        var disabledSheet = new GoogleSheet("1G6hZW0RibjW7n72AkXZgDTHZ-LKj0usRkbAwxSPhcqA", "1849453757", "modulename");

        yield return(disabledSheet);

        if (disabledSheet.Success && TwitchPlaySettings.data.AllowSheetDisabledModules)
        {
            foreach (var row in disabledSheet.GetRows())
            {
                if (!nameMap.TryGetValue(row["modulename"], out string moduleID))
                {
                    DebugHelper.Log($"Couldn't map \"{row["modulename"]}\" to a module ID when disabling modules from the spreadsheet.");
                    continue;
                }

                unsupportedModules.Add(moduleID);
            }
        }

        // Always disable modules that are marked as "Unplayable"
        foreach (var moduleInfo in modules)
        {
            if (moduleInfo.Compatibility != "Unplayable")
            {
                continue;
            }

            unsupportedModules.Add(moduleInfo.ModuleID);
        }

        // Using the list of unsupported module IDs stored in unsupportedModules, make a Mod Selector profile.
        string profilesPath = Path.Combine(Application.persistentDataPath, "ModProfiles");

        if (Directory.Exists(profilesPath))
        {
            Dictionary <string, object> profileData = new Dictionary <string, object>()
            {
                { "DisabledList", unsupportedModules },
                { "Operation", 1 }
            };

            File.WriteAllText(Path.Combine(profilesPath, "TP_Supported.json"), SettingsConverter.Serialize(profileData));
        }

        alertProgressBar.localScale = Vector3.one;

        // Send a message to chat if any modules aren't marked as having support
        if (supportStatus.Values.Count(status => status) > 0)
        {
            var supportedList = supportStatus.Where(pair => pair.Value).Select(pair => pair.Key).Join(", ");
            IRCConnection.SendMessage($"These modules have TP support: {supportedList}");
            alertText.text = $"These modules have TP support: {supportedList}";
            yield return(new WaitForSeconds(4));
        }
        else
        {
            alertText.text = "Support checks passed succesfully!";
            yield return(new WaitForSeconds(2));
        }

        // Log out the full results of the testing
        DebugHelper.Log($"Support testing results:\n{supportStatus.OrderByDescending(pair => pair.Value).Select(pair => $"{pair.Key} - {(pair.Value ? "" : "Not ")}Supported").Join("\n")}");
    }
    private IEnumerator CreateSolversForAllBombComponents()
    {
        yield return(null);

        if (CheckedMods == null)
        {
            CheckedMods = new HashSet <Mod>();
        }
        if (!(typeof(ModManager).GetField("loadedMods", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(ModManager.Instance) is Dictionary <string, Mod> loadedMods))
        {
            yield break;
        }

        Mod[]           mods         = loadedMods.Values.Where(x => CheckedMods.Add(x)).ToArray();
        KMBombModule[]  bombModules  = mods.SelectMany(x => x.GetModObjects <KMBombModule>()).ToArray();
        KMNeedyModule[] needyModules = mods.SelectMany(x => x.GetModObjects <KMNeedyModule>()).ToArray();
        DebugHelper.Log($"Found {bombModules.Length} solvable modules and {needyModules.Length} needy modules in {mods.Length} mods");
        DebugHelper.Log($"Solvable Modules: {string.Join(", ", bombModules.Select(x => x.ModuleType).ToArray()).Wrap(80)}");
        DebugHelper.Log($"Needy Modules: {string.Join(", ", needyModules.Select(x => x.ModuleType).ToArray()).Wrap(80)}");

        if (bombModules.Length > 0)
        {
            ComponentSolverFactory.SilentMode = true;
            DebugHelper.Log("Creating a solver for each Solvable module");
            foreach (KMBombModule bombComponent in bombModules)
            {
                ComponentSolver solver = null;
                try
                {
                    solver = ComponentSolverFactory.CreateSolver(null, bombComponent.GetComponent <ModBombComponent>(), ComponentTypeEnum.Mod);
                }
                catch (Exception e)
                {
                    DebugHelper.LogException(e, "Couldn't create a component solver during startup for the following reason:");
                }
                DebugHelper.Log(solver != null
                                        ? $"Found a solver of type \"{solver.GetType().FullName}\" for solvable component \"{bombComponent.ModuleDisplayName}\" ({bombComponent.ModuleType}). This module is {(solver.UnsupportedModule ? "not supported" : "supported")} by Twitch Plays."
                                        : $"No solver found for solvable component \"{bombComponent.ModuleDisplayName}\". This module is not supported by Twitch Plays.");
                yield return(null);
            }
            DebugHelper.Log("Finished creating solvers for each Solvable module");
        }

        if (needyModules.Length > 0)
        {
            ComponentSolverFactory.SilentMode = true;
            DebugHelper.Log("Creating a solver for each Needy module");
            foreach (KMNeedyModule bombComponent in needyModules)
            {
                ComponentSolver solver = null;
                try
                {
                    solver = ComponentSolverFactory.CreateSolver(null, bombComponent.GetComponent <ModNeedyComponent>(), ComponentTypeEnum.NeedyMod);
                }
                catch (Exception e)
                {
                    DebugHelper.LogException(e, "Couldn't create a component solver during startup for the following reason:");
                }
                DebugHelper.Log(solver != null
                                        ? $"Found a solver of type \"{solver.GetType().FullName}\" for needy component \"{bombComponent.ModuleDisplayName}\" ({bombComponent.ModuleType}). This module is {(solver.UnsupportedModule ? "not supported" : "supported")} by Twitch Plays."
                                        : $"No solver found for needy component \"{bombComponent.ModuleDisplayName}\". This module is not supported by Twitch Plays.");
                yield return(null);
            }
            DebugHelper.Log("Finished creating solvers for each Needy module");
        }

        ComponentSolverFactory.SilentMode = false;
        ModuleData.WriteDataToFile();
    }
    static IEnumerator TestComponents(IEnumerable <BombComponent> untestedComponents)
    {
        GameObject fakeModule = new GameObject();

        gameObjects.Add(fakeModule);
        TwitchModule module = fakeModule.AddComponent <TwitchModule>();

        module.enabled = false;

        List <string>             unsupportedModules = new List <string>();
        Dictionary <string, bool> supportStatus      = new Dictionary <string, bool>();

        ComponentSolverFactory.SilentMode = true;

        // Try to create a ComponentSolver for each module so we can see what modules are supported.
        foreach (BombComponent bombComponent in untestedComponents)
        {
            ComponentSolver solver = null;
            try
            {
                module.BombComponent = bombComponent.GetComponent <BombComponent>();

                solver = ComponentSolverFactory.CreateSolver(module);

                module.StopAllCoroutines();                 // Stop any coroutines to prevent any exceptions or from affecting the next module.
            }
            catch (Exception e)
            {
                DebugHelper.LogException(e, $"Couldn't create a component solver for \"{bombComponent.GetModuleDisplayName()}\" during startup for the following reason:");
            }

            ModuleData.DataHasChanged |= solver != null;

            DebugHelper.Log(solver != null
                                ? $"Found a solver of type \"{solver.GetType().FullName}\" for component \"{bombComponent.GetModuleDisplayName()}\". This module is {(solver.UnsupportedModule ? "not supported" : "supported")} by Twitch Plays."
                                : $"No solver found for component \"{bombComponent.GetModuleDisplayName()}\". This module is not supported by Twitch Plays.");

            string moduleID = bombComponent.GetComponent <KMBombModule>()?.ModuleType ?? bombComponent.GetComponent <KMNeedyModule>()?.ModuleType;
            if (solver?.UnsupportedModule != false && moduleID != null)
            {
                unsupportedModules.Add(moduleID);
            }

            supportStatus[bombComponent.GetModuleDisplayName()] = !(solver?.UnsupportedModule != false && moduleID != null);

            yield return(null);
        }

        ComponentSolverFactory.SilentMode = false;
        ModuleData.WriteDataToFile();
        Object.Destroy(fakeModule);

        // Using the list of unsupported module IDs stored in unsupportedModules, make a Mod Selector profile.
        string profilesPath = Path.Combine(Application.persistentDataPath, "ModProfiles");

        if (Directory.Exists(profilesPath))
        {
            Dictionary <string, object> profileData = new Dictionary <string, object>()
            {
                { "DisabledList", unsupportedModules },
                { "Operation", 1 }
            };

            File.WriteAllText(Path.Combine(profilesPath, "TP_Supported.json"), SettingsConverter.Serialize(profileData));
        }

        alertProgressBar.localScale = Vector3.one;

        // Send a message to chat if any modules aren't marked as having support
        if (supportStatus.Values.Count(status => status) > 0)
        {
            var supportedList = supportStatus.Where(pair => pair.Value).Select(pair => pair.Key).Join(", ");
            IRCConnection.SendMessage($"Let the Scoring Team know that the following modules have TP support: {supportedList}");
            alertText.text = $"These modules have TP support: {supportedList}";
            yield return(new WaitForSeconds(4));
        }
        else
        {
            alertText.text = "Support checks passed succesfully!";
            yield return(new WaitForSeconds(2));
        }

        // Log out the full results of the testing
        DebugHelper.Log($"Support testing results:\n{supportStatus.OrderByDescending(pair => pair.Value).Select(pair => $"{pair.Key} - {(pair.Value ? "" : "Not ")}Supported").Join("\n")}");
    }