Browse Source

Made IConfigProviders auto reload if the file changes

pull/46/head
Anairkoen Schno 5 years ago
parent
commit
57c6caff02
5 changed files with 20 additions and 7 deletions
  1. +2
    -0
      IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs
  2. +4
    -0
      IPA.Loader/Config/IConfigProvider.cs
  3. +6
    -3
      IPA.Loader/Loader/PluginComponent.cs
  4. +3
    -3
      IPA.Loader/Loader/PluginManager.cs
  5. +5
    -1
      IPA/IPA.csproj

+ 2
- 0
IPA.Loader/Config/ConfigProviders/JsonConfigProvider.cs View File

@ -18,6 +18,8 @@ namespace IPA.Config.ConfigProviders
public bool HasChanged { get; private set; } = false; public bool HasChanged { get; private set; } = false;
public DateTime LastModified => File.GetLastWriteTime(Filename + ".json");
private string _filename = null; private string _filename = null;
public string Filename public string Filename
{ {


+ 4
- 0
IPA.Loader/Config/IConfigProvider.cs View File

@ -39,6 +39,10 @@ namespace IPA.Config
/// </summary> /// </summary>
string Filename { set; } string Filename { set; }
/// <summary> /// <summary>
/// Gets the last time the config was modified.
/// </summary>
DateTime LastModified { get; }
/// <summary>
/// Saves configuration to file. Should error if not a root object. /// Saves configuration to file. Should error if not a root object.
/// </summary> /// </summary>
void Save(); void Save();


+ 6
- 3
IPA.Loader/Loader/PluginComponent.cs View File

@ -44,7 +44,7 @@ namespace IPA.Loader
SceneManager.sceneUnloaded += OnSceneUnloaded; SceneManager.sceneUnloaded += OnSceneUnloaded;
foreach (var provider in PluginManager.configProviders) foreach (var provider in PluginManager.configProviders)
if (provider.HasChanged) provider.Save();
if (provider.Item1.HasChanged) provider.Item1.Save();
} }
void Update() void Update()
@ -59,7 +59,10 @@ namespace IPA.Loader
ipaPlugins.OnLateUpdate(); ipaPlugins.OnLateUpdate();
foreach (var provider in PluginManager.configProviders) foreach (var provider in PluginManager.configProviders)
if (provider.HasChanged) provider.Save();
{
if (provider.Item1.HasChanged) provider.Item1.Save();
else if (provider.Item1.LastModified > provider.Item2) provider.Item1.Load(); // auto reload if it changes
}
} }
void FixedUpdate() void FixedUpdate()
@ -86,7 +89,7 @@ namespace IPA.Loader
ipaPlugins.OnApplicationQuit(); ipaPlugins.OnApplicationQuit();
foreach (var provider in PluginManager.configProviders) foreach (var provider in PluginManager.configProviders)
if (provider.HasChanged) provider.Save();
if (provider.Item1.HasChanged) provider.Item1.Save();
quitting = true; quitting = true;
} }


+ 3
- 3
IPA.Loader/Loader/PluginManager.cs View File

@ -79,7 +79,7 @@ namespace IPA.Loader
internal static IConfigProvider SelfConfigProvider { get; set; } = null; internal static IConfigProvider SelfConfigProvider { get; set; } = null;
internal static List<IConfigProvider> configProviders = new List<IConfigProvider>();
internal static List<Tuple<IConfigProvider,DateTime>> configProviders = new List<Tuple<IConfigProvider, DateTime>>();
private static void LoadPlugins() private static void LoadPlugins()
{ {
@ -164,7 +164,7 @@ namespace IPA.Loader
_bsPlugins.Add(selfPlugin); _bsPlugins.Add(selfPlugin);
configProviders.Add(SelfConfigProvider = new JsonConfigProvider() { Filename = Path.Combine("UserData", SelfPlugin.IPA_Name) });
configProviders.Add(new Tuple<IConfigProvider, DateTime>(SelfConfigProvider = new JsonConfigProvider() { Filename = Path.Combine("UserData", SelfPlugin.IPA_Name) }, SelfConfigProvider.LastModified));
SelfConfigProvider.Load(); SelfConfigProvider.Load();
//Load copied plugins //Load copied plugins
@ -268,7 +268,7 @@ namespace IPA.Loader
if (cfgProvider == null) if (cfgProvider == null)
{ {
cfgProvider = new JsonConfigProvider() { Filename = Path.Combine("UserData", $"{bsPlugin.Name}") }; cfgProvider = new JsonConfigProvider() { Filename = Path.Combine("UserData", $"{bsPlugin.Name}") };
configProviders.Add(cfgProvider);
configProviders.Add(new Tuple<IConfigProvider, DateTime>(cfgProvider, cfgProvider.LastModified));
cfgProvider.Load(); cfgProvider.Load();
} }
initArgs.Add(cfgProvider); initArgs.Add(cfgProvider);


+ 5
- 1
IPA/IPA.csproj View File

@ -57,6 +57,9 @@
<PropertyGroup> <PropertyGroup>
<StartupObject>IPA.Program</StartupObject> <StartupObject>IPA.Program</StartupObject>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
@ -118,7 +121,8 @@
</ItemGroup> </ItemGroup>
<Copy SourceFiles="@(Dlls)" DestinationFolder="$(OutputPath)IPA\%(RecursiveDir)" /> <Copy SourceFiles="@(Dlls)" DestinationFolder="$(OutputPath)IPA\%(RecursiveDir)" />
<ItemGroup> <ItemGroup>
<BadDlls Include="$(OutputPath)Mono.Cecil.*" /> <!-- Kill the Mono.Cecil stuff that get put in the root, don't want to dupe it -->
<BadDlls Include="$(OutputPath)Mono.Cecil.*" />
<!-- Kill the Mono.Cecil stuff that get put in the root, don't want to dupe it -->
</ItemGroup> </ItemGroup>
<Delete Files="@(BadDlls)" /> <Delete Files="@(BadDlls)" />
</Target> </Target>


Loading…
Cancel
Save