Skip to content

Commit

Permalink
Fix misc config uninit
Browse files Browse the repository at this point in the history
  • Loading branch information
sgkoishi committed Feb 20, 2024
1 parent aa0ff31 commit 85b38d6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
16 changes: 15 additions & 1 deletion Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,21 @@ public T Value
get => this.IsDefault ? this._defaultValue : this._value!;
set
{
if (EqualityComparer<T>.Default.Equals(value, this._defaultValue))
if (typeof(T).IsGenericType && typeof(T).GetInterface(typeof(IEnumerable<>).Name) is Type st)
{
var se = typeof(Enumerable).GetMethods()
.First(m => m.Name == nameof(Enumerable.SequenceEqual) && m.GetParameters().Length == 2)
.MakeGenericMethod(st.GetGenericArguments());
if (se != null)
{
this.IsDefault = (bool) se.Invoke(null, [value, this._defaultValue])!;
if (!this.IsDefault)
{
this._value = value;
}
}
}
else if (EqualityComparer<T>.Default.Equals(value, this._defaultValue))
{
this.IsDefault = true;
}
Expand Down
10 changes: 9 additions & 1 deletion Core/Ext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ public static class ConfigExt
{
public static void Mutate<T>(this Optional<T> t, Action<T> mut)
{
mut(t._defaultValue);
if (t._value is not null)

Check failure on line 28 in Core/Ext.cs

View workflow job for this annotation

GitHub Actions / build

'Optional<T>._value' is inaccessible due to its protection level

Check failure on line 28 in Core/Ext.cs

View workflow job for this annotation

GitHub Actions / build

'Optional<T>._value' is inaccessible due to its protection level
{
mut(t._value);

Check failure on line 30 in Core/Ext.cs

View workflow job for this annotation

GitHub Actions / build

'Optional<T>._value' is inaccessible due to its protection level

Check failure on line 30 in Core/Ext.cs

View workflow job for this annotation

GitHub Actions / build

'Optional<T>._value' is inaccessible due to its protection level
}
else
{
mut(t._defaultValue);
t.Value = t._defaultValue;
}
}
}
}
2 changes: 1 addition & 1 deletion Core/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ private void LoadConfig(TSPlayer? initiator)
catch (Exception ex)
{
initiator?.SendErrorMessage($"Failed to load config: {ex.Message}");
this.config ??= new Config();
}

this.config ??= new Config();
OnConfigLoad?.Invoke(this, prev);

try
Expand Down
11 changes: 10 additions & 1 deletion Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,15 @@ public static void AddPermission(TShockAPI.Group? group, params string[] perm)
TShockAPI.TShock.Groups.AddPermissions(group!.Name, perm.ToList());
}

public static void OnceFlag(string key, Action action)
{
if (!File.Exists(Path.Combine(TShockAPI.TShock.SavePath, key)))
{
action();
File.WriteAllText(Path.Combine(TShockAPI.TShock.SavePath, key), string.Empty);
}
}

public class ConsolePlayer : TSPlayer
{
public static ConsolePlayer Instance = new ConsolePlayer("Console");
Expand Down Expand Up @@ -594,4 +603,4 @@ public void Reset()
this._index = -1;
}
}
}
}
12 changes: 6 additions & 6 deletions Core/Vanilla.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ private void DefaultPermissionSetup()
return;
}

if (File.Exists(Path.Combine(TShockAPI.TShock.SavePath, Misc.PresetLock)) && !preset.AlwaysApply)
if (preset.AlwaysApply)
{
return;
this.PermissionSetup();
}
else
{
Utils.OnceFlag(Misc.PresetLock, this.PermissionSetup);
}

this.PermissionSetup();
}

public event Action<Plugin>? OnPermissionSetup;
Expand Down Expand Up @@ -112,7 +114,5 @@ private void PermissionSetup()
Utils.AliasPermission(TShockAPI.Permissions.su,
Permission.Admin.Sudo,
Permission.Admin.ResetCharacterAll);

File.WriteAllText(Path.Combine(TShockAPI.TShock.SavePath, Misc.PresetLock), string.Empty);
}
}
41 changes: 18 additions & 23 deletions Misc/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,23 @@ public Plugin(Main game) : base(game)
public override void Initialize()
{
var core = ServerApi.Plugins.Get<Omni.Plugin>() ?? throw new Exception("Core Omni is null.");
core.OnConfigLoad += (plugin, prev) =>
Utils.OnceFlag("chireiden.omni.misc.preset.lock", () =>
{
if (prev == null)
core.config.HideCommands.Mutate(list => list.AddRange(new List<string> {
DefinedConsts.Commands.PvPStatus,
DefinedConsts.Commands.TeamStatus,
DefinedConsts.Commands.Chat,
DefinedConsts.Commands.Admin.GarbageCollect,
DefinedConsts.Commands.Admin.UpsCheck,
DefinedConsts.Commands.Admin.SqliteVacuum,
}));
core.config.Mode.Value.Vanilla.Value.Permissions.Mutate(list => list.AddRange(new List<string>
{
plugin.config.HideCommands.Mutate(list => list.AddRange(new List<string> {
DefinedConsts.Commands.PvPStatus,
DefinedConsts.Commands.TeamStatus,
DefinedConsts.Commands.Chat,
DefinedConsts.Commands.Admin.GarbageCollect,
DefinedConsts.Commands.Admin.UpsCheck,
DefinedConsts.Commands.Admin.SqliteVacuum,
}));
plugin.config.Mode.Value.Vanilla.Value.Permissions.Mutate(list => list.AddRange(new List<string>
{
DefinedConsts.Permission.TogglePvP,
DefinedConsts.Permission.ToggleTeam,
DefinedConsts.Permission.SyncLoadout,
}));
}
};
DefinedConsts.Permission.TogglePvP,
DefinedConsts.Permission.ToggleTeam,
DefinedConsts.Permission.SyncLoadout,
}));
});
core.OnPermissionSetup += (plugin) =>
{
var guest = TShockAPI.TShock.Groups.GetGroupByName(TShockAPI.TShock.Config.Settings.DefaultGuestGroupName);
Expand Down Expand Up @@ -108,10 +105,7 @@ public override void Initialize()
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Permission_SummonBoss;
TShockAPI.GetDataHandlers.TogglePvp.Register(this.GDHook_Permission_TogglePvp);
TShockAPI.GetDataHandlers.PlayerTeam.Register(this.GDHook_Permission_PlayerTeam);
TShockAPI.Hooks.GeneralHooks.ReloadEvent += (args) =>
{
this.LoadConfig(TShockAPI.TSPlayer.Server);
};
TShockAPI.Hooks.GeneralHooks.ReloadEvent += (args) => this.LoadConfig(TShockAPI.TSPlayer.Server);
Utils.ConsolePlayer.Instance.SendSuccessMessage($"{this.Name} initialized.");
}

Expand All @@ -127,9 +121,10 @@ private void LoadConfig(TShockAPI.TSPlayer? initiator)
catch (Exception ex)
{
initiator?.SendErrorMessage($"Failed to load config: {ex.Message}");
this.config ??= new Config();
}

this.config ??= new Config();

try
{
if (!Directory.Exists(TShockAPI.TShock.SavePath))
Expand Down

0 comments on commit 85b38d6

Please sign in to comment.