Skip to content

Commit

Permalink
[RELEASE] SiraUtil 2.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Auros committed Dec 29, 2020
1 parent c574a77 commit cf2ba24
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 20 deletions.
18 changes: 18 additions & 0 deletions SiraUtil/Attributes/SlogAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Diagnostics;

namespace SiraUtil.Attributes
{
/// <summary>
/// Allows SiraUtil to detect if plugins are built for release. : D
/// </summary>
[Conditional("DEBUG")]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class SlogAttribute : Attribute
{
/// <summary>
/// An empty constructor.
/// </summary>
public SlogAttribute() { }
}
}
37 changes: 37 additions & 0 deletions SiraUtil/Converters/FileInfoConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.IO;
using IPA.Config.Data;
using IPA.Config.Stores;

namespace SiraUtil.Converters
{
/// <summary>
/// A config converter for BSIPA which can serialize and deserialize IO <see cref="FileInfo"/> values.
/// </summary>
public class FileInfoConverter : ValueConverter<FileInfo>
{
/// <summary>
/// Converts a config value to a <see cref="FileInfo"/> instance.
/// </summary>
/// <param name="value"></param>
/// <param name="parent"></param>
/// <returns></returns>
public override FileInfo FromValue(Value value, object parent)
{
return value is Text t
? new FileInfo(t.Value)
: throw new System.ArgumentNullException("Value is not a valid IO Path", nameof(value));
}


/// <summary>
/// Converts a <see cref="FileInfo"/> instance into a string.
/// </summary>
/// <param name="obj"></param>
/// <param name="parent"></param>
/// <returns></returns>
public override Value ToValue(FileInfo obj, object parent)
{
return Value.Text(obj.FullName);
}
}
}
2 changes: 1 addition & 1 deletion SiraUtil/Converters/VersionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override Version FromValue(Value value, object parent)
{
return value is Text t
? new Version(t.Value)
: throw new System.ArgumentException("Value cnanot be parsed into a Semver Version", nameof(value));
: throw new System.ArgumentException("Value cannot be parsed into a Semver Version", nameof(value));
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions SiraUtil/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ public static Component Upgrade(this Component monoBehaviour, Type upgradingType
info.SetValue(upgradedDummyComponent, info.GetValue(monoBehaviour));
}
UnityEngine.Object.DestroyImmediate(monoBehaviour);
bool goState = gameObject.activeSelf;
gameObject.SetActive(false);
var upgradedMonoBehaviour = gameObject.AddComponent(upgradingType);
foreach (FieldInfo info in upgradingType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic))
{
info.SetValue(upgradedMonoBehaviour, info.GetValue(upgradedDummyComponent));
}
gameObject.SetActive(goState);
return upgradedMonoBehaviour;
}

Expand Down
21 changes: 21 additions & 0 deletions SiraUtil/Interfaces/IRegistrar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace SiraUtil.Interfaces
{
/// <summary>
/// A generic interface which defines a registration.
/// </summary>
/// <typeparam name="T">The type to register.</typeparam>
public interface IRegistrar<T>
{
/// <summary>
/// Adds a registration to this registrar.
/// </summary>
/// <param name="value"></param>
void Add(T value);

/// <summary>
/// Removes a registration from this registrar.
/// </summary>
/// <param name="value"></param>
void Remove(T value);
}
}
13 changes: 13 additions & 0 deletions SiraUtil/Interfaces/IToggleable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SiraUtil.Interfaces
{
/// <summary>
/// Controls the state for something to be toggled.
/// </summary>
public interface IToggleable
{
/// <summary>
/// The toggleability.
/// </summary>
bool Status { get; set; }
}
}
7 changes: 7 additions & 0 deletions SiraUtil/Objects/ObjectState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace SiraUtil.Objects
/// </summary>
public struct ObjectState
{
/// <summary>
/// The activity of the object at a point in time.
/// </summary>
public readonly bool Active;

/// <summary>
/// The position and rotation of the object at a point in time.
/// </summary>
Expand Down Expand Up @@ -34,6 +39,7 @@ public struct ObjectState
public ObjectState(Transform transform)
{
pose = new Pose(transform.localPosition, transform.localRotation);
Active = transform.gameObject.activeInHierarchy;
scale = transform.localScale;
this.transform = transform;
parent = transform.parent;
Expand All @@ -49,6 +55,7 @@ public void Revert()
{
transform.SetParent(parent);
transform.localScale = scale;
transform.gameObject.SetActive(Active);
transform.localPosition = pose.position;
transform.localRotation = pose.rotation;
}
Expand Down
6 changes: 3 additions & 3 deletions SiraUtil/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace SiraUtil
[Plugin(RuntimeOptions.DynamicInit)]
public class Plugin
{
internal Harmony Harmony { get; set; }
internal static IPALogger Log { get; set; }
internal static Harmony Harmony { get; set; }

private readonly ZenjectManager _zenjectManager;

Expand All @@ -49,13 +49,13 @@ public Plugin(IPA.Config.Config conf, IPALogger logger, PluginMetadata metadata)
{
return prev;
}
var zen = new Zenjector(meta.Id);
var zen = new Zenjector(meta.Id, meta);
_zenjectManager.Add(zen);
return zen;
});

// Setup Own Zenject Stuff
var zenjector = new Zenjector("SiraUtil");
var zenjector = new Zenjector("SiraUtil", metadata);
_zenjectManager.Add(zenjector);

zenjector.OnApp<SiraInstaller>().WithParameters(config);
Expand Down
32 changes: 20 additions & 12 deletions SiraUtil/Services/SiraLogManager.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
using IPA.Logging;
using SiraUtil.Zenject;
using System.Reflection;
using System.Collections.Generic;

namespace SiraUtil.Services
{
internal class SiraLogManager
{
internal struct LoggerContext
{
public Logger logger;
public bool debugMode;
private readonly ZenjectManager _zenjectManager;
private readonly Dictionary<Assembly, LoggerContext> _loggerAssemblies = new Dictionary<Assembly, LoggerContext>();

public LoggerContext(Logger logger, bool defaultToDebugMode)
{
this.logger = logger;
debugMode = defaultToDebugMode;
}
internal SiraLogManager(ZenjectManager zenjectManager)
{
_zenjectManager = zenjectManager;
}

private readonly Dictionary<Assembly, LoggerContext> _loggerAssemblies = new Dictionary<Assembly, LoggerContext>();

internal void AddLogger(Assembly assembly, Logger logger, bool defaultToDebugMode = false)
{
if (!_loggerAssemblies.ContainsKey(assembly))
{
_loggerAssemblies.Add(assembly, new LoggerContext(logger, defaultToDebugMode));
var zen = _zenjectManager.GetZenjector(assembly);
_loggerAssemblies.Add(assembly, new LoggerContext(logger, zen.IsSlog || defaultToDebugMode));
}
}

internal LoggerContext LoggerFromAssembly(Assembly assembly)
{
return _loggerAssemblies[assembly];
}

internal struct LoggerContext
{
public Logger logger;
public bool debugMode;

public LoggerContext(Logger logger, bool defaultToDebugMode)
{
this.logger = logger;
debugMode = defaultToDebugMode;
}
}
}
}
9 changes: 9 additions & 0 deletions SiraUtil/SiraUtil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\MediaLoader.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Mono.Cecil, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<Private>False</Private>
<HintPath>$(BeatSaberDir)\Libs\Mono.Cecil.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<Private>False</Private>
<HintPath>$(BeatSaberDir)\Libs\Newtonsoft.Json.dll</HintPath>
Expand Down Expand Up @@ -184,7 +189,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Accessors.cs" />
<Compile Include="Attributes\SlogAttribute.cs" />
<Compile Include="Config.cs" />
<Compile Include="Converters\FileInfoConverter.cs" />
<Compile Include="Converters\Vector2Converter.cs" />
<Compile Include="Converters\Vector3Converter.cs" />
<Compile Include="Converters\VersionConverter.cs" />
Expand All @@ -193,6 +200,8 @@
<Compile Include="Interfaces\ILocalizer.cs" />
<Compile Include="Interfaces\IModelProvider.cs" />
<Compile Include="Interfaces\IPrefabProvider.cs" />
<Compile Include="Interfaces\IRegistrar.cs" />
<Compile Include="Interfaces\IToggleable.cs" />
<Compile Include="Objects\BeatmapObjectRedecorator.cs" />
<Compile Include="Objects\ObjectState.cs" />
<Compile Include="Objects\ObjectStateContainer.cs" />
Expand Down
27 changes: 26 additions & 1 deletion SiraUtil/Zenject/InstallBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ModestTree;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

namespace SiraUtil.Zenject
{
Expand All @@ -19,7 +20,9 @@ public class InstallBuilder
internal Action<DiContainer> Contextless { get; private set; } = null;
internal HashSet<Type> Exposers { get; private set; } = new HashSet<Type>();
internal Action<SceneContext, DiContainer> Resolved { get; private set; } = null;
internal Action<Context, DiContainer> SceneContextless { get; private set; } = null;
internal HashSet<Tuple<Type, DelegateWrapper>> Mutators { get; private set; } = new HashSet<Tuple<Type, DelegateWrapper>>();
internal HashSet<Func<Scene, Context, DiContainer, bool>> OnFuncs { get; private set; } = new HashSet<Func<Scene, Context, DiContainer, bool>>();
internal HashSet<Tuple<Type, Action<Context, DiContainer>>> Headers { get; private set; } = new HashSet<Tuple<Type, Action<Context, DiContainer>>>();

internal InstallBuilder() { }
Expand All @@ -38,6 +41,17 @@ public InstallBuilder WithParameters(params object[] parameters)
return this;
}

/// <summary>
/// Install the installer at a destination via function.
/// </summary>
/// <param name="func"></param>
/// <returns></returns>
public InstallBuilder On(Func<Scene, Context, DiContainer, bool> func)
{
OnFuncs.Add(func);
return this;
}

/// <summary>
/// Installs the installer at a destination.
/// </summary>
Expand Down Expand Up @@ -190,6 +204,17 @@ public InstallBuilder Pseudo(Action<DiContainer> action)
return this;
}

/// <summary>
/// Install bindings with a pseudo action which acts like an installer
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public InstallBuilder Pseudo(Action<Context, DiContainer> action)
{
SceneContextless = action;
return this;
}

/// <summary>
/// Mainly for prototyping.
/// </summary>
Expand All @@ -213,7 +238,7 @@ public InstallBuilder When(Func<bool> when)

internal void Validate()
{
if (Contextless == null)
if (Contextless == null && SceneContextless == null)
{
Assert.IsNotNull(Type, $"Contextful Zenject Registrations must have a type. {Utilities.ASSERTHIT}");
Assert.That(Type.DerivesFrom<IInstaller>(), $"Type must implement IInstaller {Utilities.ASSERTHIT}");
Expand Down
Loading

0 comments on commit cf2ba24

Please sign in to comment.