Skip to content

Commit

Permalink
Fix config save and load
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Feb 20, 2024
1 parent c715aed commit 3e8f8d3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CentrED/CentrEDGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ protected override void LoadContent()
protected override void UnloadContent()
{
CEDClient.Disconnect();
Config.Save();
}

protected override void Update(GameTime gameTime)
Expand All @@ -73,6 +72,7 @@ protected override void Update(GameTime gameTime)
Metrics.Stop("UpdateClient");
UIManager.Update(gameTime, IsActive);
MapManager.Update(gameTime, IsActive, !UIManager.CapturingMouse, !UIManager.CapturingKeyboard);
Config.AutoSave();
}
catch(Exception e)
{
Expand Down
10 changes: 8 additions & 2 deletions CentrED/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class ConfigRoot

public static class Config
{
private static readonly TimeSpan ConfigSaveRate = TimeSpan.FromSeconds(30);
private static DateTime LastConfigSave = DateTime.Now;
private static readonly JsonSerializerOptions SerializerOptions = new()
{
IncludeFields = true
Expand All @@ -34,8 +36,12 @@ static Config()
Instance = JsonSerializer.Deserialize<ConfigRoot>(jsonText, SerializerOptions);
}

public static void Save()
public static void AutoSave()
{
File.WriteAllText(_configFilePath, JsonSerializer.Serialize(Instance, SerializerOptions));
if (DateTime.Now > LastConfigSave + ConfigSaveRate)
{
File.WriteAllText(_configFilePath, JsonSerializer.Serialize(Instance, SerializerOptions));
LastConfigSave = DateTime.Now;
}
}
}
6 changes: 5 additions & 1 deletion CentrED/UI/Windows/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace CentrED.UI.Windows;

public abstract class Window
{
public Window()
{
Show = Config.Instance.Layout[Name].IsOpen;
}

public abstract string Name { get; }

public virtual string Shortcut => "";
Expand All @@ -21,7 +26,6 @@ public bool Show

public virtual void DrawMenuItem()
{

ImGui.MenuItem(Name, Shortcut, ref _show);
}

Expand Down

0 comments on commit 3e8f8d3

Please sign in to comment.