mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-06-09 10:06:34 +02:00
83c2a1be11
* [Dependency] source generator No more reflection, no more codegen at runtime Also various changes to Roslyn helpers to make this easier to write. Requires all types with dependencies to be partial and not have readonly dependency fields. An analyzer enforces this at warning level, the previous injection strategies have remained in the code *for now* as a fallback. No fallback is available for [field: Dependency] properties, due to a Roslyn bug. Code Fixes exist. We love Roslyn * Apply dependencies generator changes to all code * Release notes * Preprocessor got hands * Handle nullable dependencies These are bad but gotta deal with it. * Apply suggestions from code review Co-authored-by: Moony <moony@hellomouse.net> * Fine, let's not use collection expressions --------- Co-authored-by: Moony <moony@hellomouse.net>
57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using System;
|
|
using Robust.Client.UserInterface;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Log;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Robust.Client.State
|
|
{
|
|
internal sealed partial class StateManager : IStateManager
|
|
{
|
|
[Dependency] private IDynamicTypeFactory _typeFactory = default!;
|
|
[Dependency] private IUserInterfaceManager _interfaceManager = default!;
|
|
public event Action<StateChangedEventArgs>? OnStateChanged;
|
|
public State CurrentState { get; private set; }
|
|
|
|
public StateManager()
|
|
{
|
|
CurrentState = new DefaultState();
|
|
}
|
|
|
|
public void FrameUpdate(FrameEventArgs e)
|
|
{
|
|
CurrentState?.FrameUpdate(e);
|
|
}
|
|
|
|
public T RequestStateChange<T>() where T : State, new()
|
|
{
|
|
return (T)RequestStateChange(typeof(T));
|
|
}
|
|
|
|
public State RequestStateChange(Type type)
|
|
{
|
|
if (!typeof(State).IsAssignableFrom(type))
|
|
throw new ArgumentException($"Needs to be derived from {typeof(State).FullName}", nameof(type));
|
|
|
|
return CurrentState?.GetType() == type ? CurrentState : SwitchToState(type);
|
|
}
|
|
|
|
private State SwitchToState(Type type)
|
|
{
|
|
Logger.Debug($"Switching to state {type}");
|
|
|
|
var newState = _typeFactory.CreateInstance<State>(type);
|
|
|
|
var old = CurrentState;
|
|
CurrentState?.ShutdownInternal(_interfaceManager);
|
|
|
|
CurrentState = newState;
|
|
CurrentState.StartupInternal(_interfaceManager);
|
|
|
|
OnStateChanged?.Invoke(new StateChangedEventArgs(old, CurrentState));
|
|
|
|
return CurrentState;
|
|
}
|
|
}
|
|
}
|