Browse Source

Made IConfigProviders auto reload if the file changes

pull/46/head
Anairkoen Schno 6 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 DateTime LastModified => File.GetLastWriteTime(Filename + ".json");
private string _filename = null;
public string Filename
{


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

@ -39,6 +39,10 @@ namespace IPA.Config
/// </summary>
string Filename { set; }
/// <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.
/// </summary>
void Save();


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

@ -44,7 +44,7 @@ namespace IPA.Loader
SceneManager.sceneUnloaded += OnSceneUnloaded;
foreach (var provider in PluginManager.configProviders)
if (provider.HasChanged) provider.Save();
if (provider.Item1.HasChanged) provider.Item1.Save();
}
void Update()
@ -59,7 +59,10 @@ namespace IPA.Loader
ipaPlugins.OnLateUpdate();
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()
@ -86,7 +89,7 @@ namespace IPA.Loader
ipaPlugins.OnApplicationQuit();
foreach (var provider in PluginManager.configProviders)
if (provider.HasChanged) provider.Save();
if (provider.Item1.HasChanged) provider.Item1.Save();
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 List<IConfigProvider> configProviders = new List<IConfigProvider>();
internal static List<Tuple<IConfigProvider,DateTime>> configProviders = new List<Tuple<IConfigProvider, DateTime>>();
private static void LoadPlugins()
{
@ -164,7 +164,7 @@ namespace IPA.Loader
_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();
//Load copied plugins
@ -268,7 +268,7 @@ namespace IPA.Loader
if (cfgProvider == null)
{
cfgProvider = new JsonConfigProvider() { Filename = Path.Combine("UserData", $"{bsPlugin.Name}") };
configProviders.Add(cfgProvider);
configProviders.Add(new Tuple<IConfigProvider, DateTime>(cfgProvider, cfgProvider.LastModified));
cfgProvider.Load();
}
initArgs.Add(cfgProvider);


+ 5
- 1
IPA/IPA.csproj View File

@ -57,6 +57,9 @@
<PropertyGroup>
<StartupObject>IPA.Program</StartupObject>
</PropertyGroup>
<PropertyGroup>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -118,7 +121,8 @@
</ItemGroup>
<Copy SourceFiles="@(Dlls)" DestinationFolder="$(OutputPath)IPA\%(RecursiveDir)" />
<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>
<Delete Files="@(BadDlls)" />
</Target>


Loading…
Cancel
Save