This commit is contained in:
2025-03-14 19:53:29 +01:00
commit 0d3516774e
159 changed files with 9069 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
using UnityEngine;
namespace RebootKit.Engine.Foundation
{
public abstract class ControllerAsset<TController> : ScriptableObject where TController : IController
{
public abstract TController Create();
}
public abstract class ControllerAsset : ControllerAsset<IController>
{
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 44e7c0513ef44634cbe6c6814772bfe1

View File

@@ -0,0 +1,51 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace RebootKit.Engine.Foundation {
public static class EntryPoint {
private static readonly Logger _logger = new(nameof(EntryPoint));
private static CancellationTokenSource _cancellationTokenSource;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
public static void Start() {
if (_cancellationTokenSource != null) {
_cancellationTokenSource.Cancel();
}
RR.Shared = null;
ServicesLocator.Shared.UnRegisterAll();
_cancellationTokenSource = new CancellationTokenSource();
SceneManager.LoadScene(0, LoadSceneMode.Single);
RunAsync(_cancellationTokenSource.Token).Forget();
#if UNITY_EDITOR
static void OnPlayerModeState(UnityEditor.PlayModeStateChange state) {
if (state == UnityEditor.PlayModeStateChange.ExitingPlayMode) {
_cancellationTokenSource.Cancel();
}
}
UnityEditor.EditorApplication.playModeStateChanged -= OnPlayerModeState;
UnityEditor.EditorApplication.playModeStateChanged += OnPlayerModeState;
#endif
}
public static async UniTask RunAsync(CancellationToken cancellationToken) {
using RR instance = new RR();
RR.Shared = instance;
_logger.Debug("running");
await instance.Init(cancellationToken);
await instance.Run(cancellationToken);
_logger.Debug("stopped");
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 83bc8a98e30147babef3641863699cd9
timeCreated: 1740670775

View File

@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
namespace RebootKit.Engine.Foundation {
public interface IController : IDisposable {
UniTask OnStart(CancellationToken cancellationToken);
void OnStop();
void OnTick();
}
public class ControllersManager : IDisposable {
private readonly List<IController> _controllers = new();
private bool _isRunning;
private readonly CancellationToken _cancellationToken;
public ControllersManager(CancellationToken cancellationToken) {
_cancellationToken = cancellationToken;
}
public void Dispose() {
foreach (IController controller in _controllers) {
controller.Dispose();
}
}
public void Add(IController controller) {
_controllers.Add(controller);
if (_isRunning) {
controller.OnStart(_cancellationToken);
}
}
public void Remove(IController controller) {
if (_isRunning) {
controller.OnStop();
}
_controllers.Remove(controller);
}
public async UniTask Start(CancellationToken cancellationToken) {
if (_isRunning) {
return;
}
foreach (IController controller in _controllers) {
await controller.OnStart(cancellationToken);
}
_isRunning = true;
}
public void Stop() {
if (!_isRunning) {
return;
}
foreach (IController controller in _controllers) {
controller.OnStop();
}
_isRunning = false;
}
public void Tick() {
foreach (IController controller in _controllers) {
controller.OnTick();
}
}
}
public static class ControllersManagerUtils {
public static void Add(this ControllersManager manager, ControllerAsset controllerAsset) {
manager.Add(controllerAsset.Create());
}
public static void Add(this ControllersManager manager, List<ControllerAsset> controllerAsset) {
foreach (ControllerAsset asset in controllerAsset) {
manager.Add(asset.Create());
}
}
public static void Add(this ControllersManager manager, ControllerAsset[] controllerAsset) {
foreach (ControllerAsset asset in controllerAsset) {
manager.Add(asset.Create());
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9a78e7e501a24257a006967827a937ab
timeCreated: 1740757906

View File

@@ -0,0 +1,85 @@
using System;
using System.Diagnostics;
namespace RebootKit.Engine.Foundation {
public enum LogLevel {
Info,
Debug,
Warning,
Error
}
public readonly struct Logger {
private readonly string _name;
public Logger(string name) {
_name = name;
}
public void Log(LogLevel level, string message) {
switch (level) {
case LogLevel.Info:
UnityEngine.Debug.Log(FormatMessage(level, _name, message, true));
break;
case LogLevel.Debug:
UnityEngine.Debug.Log(FormatMessage(level, _name, message, true));
break;
case LogLevel.Warning:
UnityEngine.Debug.LogWarning(FormatMessage(level, _name, message, true));
break;
case LogLevel.Error:
UnityEngine.Debug.LogError(FormatMessage(level, _name, message, true));
break;
default:
throw new ArgumentOutOfRangeException(nameof(level), level, null);
}
}
[Conditional(RConsts.BuildFlagDebug)]
public void Info(string message) {
Log(LogLevel.Info, message);
}
[Conditional(RConsts.BuildFlagDebug)]
public void Debug(string message) {
Log(LogLevel.Debug, message);
}
[Conditional(RConsts.BuildFlagDebug)]
public void Warning(string message) {
Log(LogLevel.Warning, message);
}
public void Error(string message) {
Log(LogLevel.Error, message);
}
private static string FormatMessage(LogLevel level, string name, string message, bool richText) {
if (!richText) {
string prefix = level switch {
LogLevel.Info => "",
LogLevel.Debug => "[DEBUG]",
LogLevel.Warning => "[WARNING]",
LogLevel.Error => "[ERROR]",
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
};
return $"{prefix}[{name}] {message}";
}
switch (level) {
case LogLevel.Info:
return $"[<b>{name}</b>] {message}";
case LogLevel.Debug:
return $"[D][<b>{name}</b>] <color=#ffffff>{message}</color>";
case LogLevel.Warning:
return $"[W][<b>{name}</b>] <color=#ffffff>{message}</color>";
case LogLevel.Error:
return $"[<b><color=#ff0000>ERROR</color></b>][<b>{name}</b>] <color=#ffffff>{message}</color>";
default:
throw new ArgumentOutOfRangeException(nameof(level), level, null);
}
return "";
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f8a58e1bf60c4db6a35171b65538fcea
timeCreated: 1741027327

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace RebootKit.Engine.Foundation {
public interface IService : IDisposable {
UniTask OnInit(CancellationToken cancellationToken);
}
public abstract class ServiceMonoBehaviour : MonoBehaviour, IService {
public abstract UniTask OnInit(CancellationToken cancellationToken);
public abstract void Dispose();
}
public abstract class ServiceAsset<T> : ScriptableObject where T : IService {
public abstract T Create();
}
public abstract class ServiceAsset : ServiceAsset<IService> {
}
public class ServicesLocator {
private static readonly Logger _logger = new(nameof(ServicesLocator));
private static ServicesLocator _shared;
public static ServicesLocator Shared {
get {
if (_shared == null) {
_shared = new ServicesLocator();
}
return _shared;
}
}
private readonly Dictionary<Type, IService> _servicesMap = new();
public void Register(IService service, Type type) {
if (!_servicesMap.TryAdd(type, service)) {
_logger.Error($"There is already game service of type `{type}`");
return;
}
service.OnInit(default);
_logger.Info($"Registered service of type {type.Name}");
}
public void Register<T>(T service) where T : IService {
Register(service, service.GetType());
}
public async UniTask RegisterAsync<T>(T service, CancellationToken cancellationToken = default) where T : IService {
if (!_servicesMap.TryAdd(typeof(T), service)) {
_logger.Error($"There is already game service of type `{typeof(T).Name}`");
return;
}
await service.OnInit(cancellationToken);
_logger.Info($"Registered service of type {typeof(T).Name}");
}
public void UnRegister(Type type) {
if (_servicesMap.TryGetValue(type, out IService service)) {
service.Dispose();
_servicesMap.Remove(type);
_logger.Info($"Unregistered service of type {type.Name}");
}
}
public void UnRegisterAll() {
foreach (IService service in _servicesMap.Values) {
service.Dispose();
}
_servicesMap.Clear();
}
public IService Get(Type type) {
if (_servicesMap.TryGetValue(type, out IService service)) {
return service;
}
Debug.LogAssertionFormat($"Couldn't find service of type `{type}`");
return null;
}
public T Get<T>() where T : IService {
return (T) Get(typeof(T));
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: db549c154c4b413587c25dbfbadb7f49
timeCreated: 1741119753