Skip to content

Commit

Permalink
fix:option loads
Browse files Browse the repository at this point in the history
  • Loading branch information
yukieiji committed Jun 26, 2024
1 parent 8622da3 commit c39493e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 42 deletions.
2 changes: 1 addition & 1 deletion ExtremeRoles.Test/GameTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void Run()
GameMudderEndTestingBehaviour.Instance.Logger = this.Log;
GameMudderEndTestingBehaviour.Instance.StartCoroutine(
GameMudderEndTestingBehaviour.Instance.Run(
new("Random", 3),
new("Random", 256),
new("IRoleAbilityRole", 5,
[
ExtremeRoleId.Carpenter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ public void Initialize(in Initializer initialize)
int xIndex = 0;
int yIndex = 0;

foreach (var tab in Enum.GetValues<OptionTab>())
foreach (var (tab, tabContainer) in OptionManager.Instance)
{
var menu = initialize.NewMenu;
var button = initialize.NewTagButton;
if (OptionManager.Instance.TryGetTab(tab, out var optionTab))
{
menu.AllCategory = optionTab.Category.ToArray();
}

menu.AllCategory = tabContainer.Category.ToArray();

button.gameObject.name = $"{tab}Button";
button.ChangeButtonText(Translation.GetString(string.Format(
Expand Down
6 changes: 5 additions & 1 deletion ExtremeRoles/Module/CustomOption/OptionManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

Expand All @@ -18,7 +19,7 @@

namespace ExtremeRoles.Module.CustomOption;

public sealed class OptionManager
public sealed class OptionManager : IEnumerable<KeyValuePair<OptionTab, OptionTabContainer>>
{
public readonly static OptionManager Instance = new ();

Expand Down Expand Up @@ -77,6 +78,9 @@ public static void ShareOption(in MessageReader reader)
}
}

public IEnumerator<KeyValuePair<OptionTab, OptionTabContainer>> GetEnumerator() => this.options.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() { throw new Exception(); }

public bool TryGetTab(OptionTab tab, [NotNullWhen(true)] out OptionTabContainer? container)
=> this.options.TryGetValue(tab, out container) && container is not null;

Expand Down
107 changes: 72 additions & 35 deletions ExtremeRoles/Module/CustomOptionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using ExtremeRoles.Performance;
using ExtremeRoles.Extension.Il2Cpp;

#nullable enable

namespace ExtremeRoles.Module;

public static class CustomOptionCsvProcessor
Expand Down Expand Up @@ -54,21 +56,28 @@ public static bool Export()
string.Format("{1}{0}{2}{0}{3}{0}{4}",
comma, "Name", "OptionValue", "CustomOptionName", "SelectedIndex")); //ヘッダー

/*
foreach (IOptionInfo option in OptionManager.Instance.GetAllIOption())
foreach (var (tab, tabContainer) in OptionManager.Instance)
{
if (option.Id == 0) { continue; }
csv.WriteLine(
string.Format("{1}{0}{2}{0}{3}{0}{4}",
comma,
cleaner.Clean(option.GetTranslatedName()),
cleaner.Clean(option.GetTranslatedValue()),
cleaner.Clean(option.Name),
option.CurSelection));
foreach (var cate in tabContainer.Category)
{
if (cate.Id == 0 &&
tab is OptionTab.General)
{
continue;
}

foreach (var option in cate.Options)
{
csv.WriteLine(
string.Format("{1}{0}{2}{0}{3}{0}{4}",
comma,
cleaner.Clean(option.Title),
cleaner.Clean(option.ValueString),
cleaner.Clean(option.Info.Name),
option.Selection));
}
}
}
*/

csv.WriteLine(
string.Format(
Expand All @@ -85,9 +94,9 @@ public static bool Export()
"{1}{0}{1}", comma, string.Empty));
var gameOptionManager = GameOptionsManager.Instance;

foreach (GameModes gameMode in Enum.GetValues(typeof(GameModes)))
foreach (GameModes gameMode in Enum.GetValues<GameModes>())
{
IGameOptions option = gameMode switch
IGameOptions? option = gameMode switch
{
GameModes.Normal or GameModes.NormalFools =>
gameOptionManager.normalGameHostOptions.Cast<IGameOptions>(),
Expand Down Expand Up @@ -119,13 +128,33 @@ public static bool Import()
{
using var csv = new StreamReader(csvName, new UTF8Encoding(true));

string infoData = csv.ReadLine(); // verHeader
if (csv is null)
{
return false;
}

string? infoData = csv.ReadLine(); // verHeader
if (infoData is null)
{
return false;
}

string[] info = infoData.Split(comma);
string exrVersion = info[2];
exrVersion = exrVersion.Replace("ExtremeRoles ver.", "");
if (!Version.TryParse(exrVersion, out var version) ||
version is null ||
version.Major <= 10)
{
ExtremeRolesPlugin.Logger.LogError(
$"Can't load v11 below options data, wait for next update.");
return false;
}

ExtremeRolesPlugin.Logger.LogInfo(
$"Loading from {info[1]} with {info[2]} {info[3]} Data");
$"Loading from {info[1]} with {exrVersion} {info[3]} Data");

string line = csv.ReadLine(); // ヘッダー
string? line = csv.ReadLine(); // ヘッダー
while ((line = csv.ReadLine()) != null)
{
string[] option = line.Split(comma);
Expand All @@ -137,7 +166,8 @@ public static bool Import()
case vanilaOptionKey:
GameModes mode = (GameModes)Enum.Parse(typeof(GameModes), option[1]);
if (!importedVanillaOptions.TryGetValue(
mode, out List<byte> modeOption))
mode, out var modeOption) &&
modeOption is null)
{
modeOption = new List<byte>();
importedVanillaOptions.Add(mode, modeOption);
Expand Down Expand Up @@ -188,32 +218,39 @@ public static bool Import()
break;
}
}
/*
var options = OptionManager.Instance;
var optionMng = OptionManager.Instance;
var cleaner = new StringCleaner();

foreach (IOptionInfo option in options.GetAllIOption())
foreach (var (tab, tabContainer) in optionMng)
{
if (option.Id == 0) { continue; }
if (importedOption.TryGetValue(
cleaner.Clean(option.Name),
out int selection))
foreach (var cate in tabContainer.Category)
{
ExtremeRolesPlugin.Logger.LogInfo(
$"Update Option : {option.Name} to Selection:{selection}");
option.UpdateSelection(selection);
option.SaveConfigValue();
if (cate.Id == 0 && tab is OptionTab.General) { continue; }

foreach (var option in cate.Options)
{
string name = option.Info.Name;
if (!importedOption.TryGetValue(
cleaner.Clean(name),
out int selection))
{
continue;
}

ExtremeRolesPlugin.Logger.LogInfo(
$"Update Option : {name} to Selection:{selection}");

option.Selection = selection;
}
}
}
if (AmongUsClient.Instance &&
if (AmongUsClient.Instance != null &&
AmongUsClient.Instance.AmHost &&
CachedPlayerControl.LocalPlayer)
CachedPlayerControl.LocalPlayer != null)
{
options.ShareOptionSelections();// Share all selections
optionMng.ShereAllOption();// Share all selections
}

*/
ExtremeRolesPlugin.Logger.LogInfo("---------- Option Import Complete ----------");

return true;
Expand Down

0 comments on commit c39493e

Please sign in to comment.