Skip to content

Commit

Permalink
Bump version to 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
amelkor committed Jul 29, 2021
1 parent 7aed5a5 commit 9aab8ac
Show file tree
Hide file tree
Showing 93 changed files with 497 additions and 223 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Microsoft.Extensions.Hosting for Unity

## [1.2.0] - 2021-07-23
### Fixed
- Removed reload on change for FileConfigurationSources (caused UnityEditor to enter play mode with long delay)

### Added
- ScriptableObject registration
- MonoBehaviour HostedService registration
- Possibility to pass args to the host
- Host log level parameter in the host Inspector
- Host graceful shutduwn timeout parameter in the host Inspector

## [1.1.0] - 2021-05-27
### Fixed
- Dependency resolving for MonoBehaviours

## [1.0.4] - 2021-05-27
### Fixed
- Dependency resolving for MonoBehaviours
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Documentation.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Documentation/microsoft.extensions.hosting.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Editor/Bsr.Microsoft.Extensions.Hosting.Unity.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Bsr.Microsoft.Extensions.Hosting.Unity.Editor",
"rootNamespace": "",
"rootNamespace": "Microsoft.Extensions.Hosting.Unity",
"references": [
"GUID:71c6c6e852165ba4581c85f94bb30ef3"
"GUID:f02d2e96a369c224ba36e2b6953983a3"
],
"includePlatforms": [
"Editor"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Extensions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Extensions/ReflectionExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Gizmos.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Gizmos/host_container_icon.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Gizmos/host_service_icon.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions Editor/MonoBehaviourHostRootEditor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
namespace Editor
using System.Diagnostics;
using UnityEditor;

namespace Microsoft.Extensions.Hosting.Unity.Editor
{
public class MonoBehaviourHostRootEditor
[CustomEditor(typeof(MonoBehaviourHostRoot))]
public class MonoBehaviourHostRootEditor : UnityEditor.Editor
{

private const string GITHUB_URL = "https://github.com/amelkor/Microsoft.Extensions.Hosting.Unity";

public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox(
"This script is controlled by HostManager component and keeps resolved MonoBehaviour services.",
MessageType.Info);

// ReSharper disable once InvertIf
if (EditorGUILayout.LinkButton("For additional information visit the GitHub link"))
{
var ps = new ProcessStartInfo(GITHUB_URL)
{
UseShellExecute = true,
Verb = "open"
};
Process.Start(ps);
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/MonoBehaviourHostRootEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENCE.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Add the entry below to the project's manifest.json
"name": "microsoft.extensions.hosting",
"url": "https://registry.npmjs.org",
"scopes": [
"com.blacksmallriver.microsoft.extensions.hosting"
"com.blacksmallriver.hosting",
"com.blacksmallriver"
]
}
]
Expand All @@ -31,7 +32,7 @@ Or add it in Unity Editor:
>
> URL: https://registry.npmjs.org
>
> scope: com.blacksmallriver.microsoft.extensions.hosting
> scope: com.blacksmallriver.hosting
### UPM Package install via git URL (no package updates will be available this way):

Expand Down
2 changes: 1 addition & 1 deletion README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Abstractions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions Runtime/Abstractions/IUnityObjectServiceCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@

namespace Microsoft.Extensions.Hosting.Unity
{
public interface IMonoBehaviourServiceCollectionBuilder
public interface IUnityObjectServiceCollectionBuilder
{
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourSingleton(MonoBehaviour component, Type type = default, bool useHostLifetime = false);
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourSingleton<T>() where T : MonoBehaviour;
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourHostedService<T>() where T : MonoBehaviour, IHostedService;
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourHostedService<T, TImpl>() where T : MonoBehaviour, IHostedService where TImpl : MonoBehaviour, T;
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourSingleton<T, TImpl>(TImpl component, bool useHostLifetime = false) where TImpl : MonoBehaviour, T where T : class;
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourSingleton<T, TImpl>() where T : class where TImpl : MonoBehaviour, T;
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourTransient<T>() where T : MonoBehaviour;
IMonoBehaviourServiceCollectionBuilder AddMonoBehaviourTransient<T, TImpl>() where T : class where TImpl : MonoBehaviour, T;
IUnityObjectServiceCollectionBuilder AddMonoBehaviourSingleton(MonoBehaviour component, Type type = default, bool useHostLifetime = false);
IUnityObjectServiceCollectionBuilder AddMonoBehaviourSingleton<T>() where T : MonoBehaviour;
IUnityObjectServiceCollectionBuilder AddMonoBehaviourHostedService<T>() where T : MonoBehaviour, IHostedService;
IUnityObjectServiceCollectionBuilder AddMonoBehaviourHostedService<T, TImpl>() where T : MonoBehaviour, IHostedService where TImpl : MonoBehaviour, T;
IUnityObjectServiceCollectionBuilder AddMonoBehaviourSingleton<T, TImpl>(TImpl component, bool useHostLifetime = false) where TImpl : MonoBehaviour, T where T : class;
IUnityObjectServiceCollectionBuilder AddMonoBehaviourSingleton<T, TImpl>() where T : class where TImpl : MonoBehaviour, T;
IUnityObjectServiceCollectionBuilder AddMonoBehaviourTransient<T>() where T : MonoBehaviour;
IUnityObjectServiceCollectionBuilder AddMonoBehaviourTransient<T, TImpl>() where T : class where TImpl : MonoBehaviour, T;

IMonoBehaviourServiceCollectionBuilder AddScriptableObjectSingleton<T>() where T : ScriptableObject;
IMonoBehaviourServiceCollectionBuilder AddScriptableObjectSingleton<T, TImpl>() where T : ScriptableObject where TImpl : ScriptableObject, T;
IMonoBehaviourServiceCollectionBuilder AddScriptableObjectTransient<T>() where T : ScriptableObject;
IMonoBehaviourServiceCollectionBuilder AddScriptableObjectTransient<T, TImpl>() where T : class where TImpl : ScriptableObject, T;
IUnityObjectServiceCollectionBuilder AddScriptableObjectSingleton(ScriptableObject scriptableObject, Type type);
IUnityObjectServiceCollectionBuilder AddScriptableObjectSingleton<T>(T scriptableObject) where T : ScriptableObject;
IUnityObjectServiceCollectionBuilder AddScriptableObjectSingleton<T, TImpl>(TImpl scriptableObject) where T : ScriptableObject where TImpl : ScriptableObject, T;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Runtime/Configuration.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 32 additions & 9 deletions Runtime/Configuration/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Hosting.Unity
namespace Microsoft.Extensions.Hosting.Unity.Configuration
{
/// <summary>
/// Configuration provider automatically creates Config/globalsettings.json under the Game's directory.
/// Inherit from this class and extend it with properties to use them as configuration keys.
/// </summary>
[Serializable]
public class AppSettings : IConfigurationProvider
public abstract class GlobalSettings : IConfigurationProvider
{
internal const string ConfigDirPath = "Config";
internal const string ConfigFilePath = "appsettings.json";
internal const string ConfigFilePath = "globalsettings.json";
internal const string LocalFilePath = ConfigDirPath + "/" + ConfigFilePath;
private static readonly System.Text.Json.JsonSerializerOptions _serializerOptions = new System.Text.Json.JsonSerializerOptions {Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, WriteIndented = true};
private readonly Type _type;
private readonly Dictionary<string, PropertyInfo> _properties;
private IConfigurationRoot _configurationRoot;
private ConfigurationReloadToken _reloadToken = new ConfigurationReloadToken();

protected AppSettings()
protected GlobalSettings()
{
_type = GetType();
_properties = _type.GetProperties(
Expand All @@ -32,6 +36,10 @@ protected AppSettings()
.ToDictionary(p => p.Name, p => p);
}

/// <summary>
/// For internal purpose.
/// </summary>
/// <param name="configuration"></param>
internal void SetConfiguration(IConfiguration configuration)
{
// ReSharper disable once InvertIf
Expand Down Expand Up @@ -69,6 +77,9 @@ public void Set(string key, string value)

public IChangeToken GetReloadToken() => _reloadToken;

/// <summary>
/// Read configuration from the config file.
/// </summary>
public void Load()
{
var json = BeforeLoad(File.ReadAllText(LocalFilePath));
Expand All @@ -81,7 +92,6 @@ public void Load()
}

OnReload();
_configurationRoot?.Reload();
}

public virtual IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string parentPath)
Expand All @@ -101,14 +111,15 @@ private static string Segment(string key, int prefixLength)
return indexOf < 0 ? key.Substring(prefixLength) : key.Substring(prefixLength, indexOf - prefixLength);
}

/// <summary>
/// Save configuration to the config file.
/// </summary>
public void Save()
{
var json = BeforeSave(System.Text.Json.JsonSerializer.Serialize(this, _type, _serializerOptions));

Directory.CreateDirectory(ConfigDirPath);
File.WriteAllText(LocalFilePath, json);

_configurationRoot?.Reload();
}

private void OnReload()
Expand All @@ -117,8 +128,20 @@ private void OnReload()
previousToken.OnReload();
}

public virtual string BeforeLoad(string json) => json;

/// <summary>
/// Override to perform extra actions onto the serialized data that just was loaded from the config file.
/// Could be used to decrypt or unzip the data before deserializing.
/// </summary>
/// <param name="content">Raw data that was read from the config file.</param>
/// <returns>Should return a valid json.</returns>
public virtual string BeforeLoad(string content) => content;

/// <summary>
/// Override to perform extra actions onto the serialized json.
/// Could be used to encrypt or zip the data before writing to the config file.
/// </summary>
/// <param name="json">Json serialized data.</param>
/// <returns>A content to save into the config file.</returns>
public virtual string BeforeSave(string json) => json;

public override string ToString() => _type.Name;
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Configuration/GlobalSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions Runtime/Configuration/GlobalSettingsConfigurationSource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
namespace Microsoft.Extensions.Hosting.Unity.Configuration
using System.IO;
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.Hosting.Unity.Configuration
{
public class GlobalSettings
public class GlobalSettingsConfigurationSource<TSettings> : IConfigurationSource where TSettings : GlobalSettings, new()
{
internal TSettings Provider { get; private set; }

public IConfigurationProvider Build(IConfigurationBuilder builder)
{
var settings = new TSettings();

if (!File.Exists(GlobalSettings.LocalFilePath))
settings.Save();
else
settings.Load();

Provider = settings;
return settings;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Runtime/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
internal static class Constants
{
public const string ThisAssetName = "Service Host";
public const string MonoBehaviourHostRootInstanceKey = "MONO_BEHAVIOUR_HOST_ROOT_INSTANCE";
}
}
2 changes: 1 addition & 1 deletion Runtime/Constants.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Extensions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9aab8ac

Please sign in to comment.