riversong code showcase
This commit is contained in:
9
Source/Engine/GameData/GameDataAsset.cs
Normal file
9
Source/Engine/GameData/GameDataAsset.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public abstract class GameDataAsset : ScriptableObject, IGameDataRuntimeId
|
||||
{
|
||||
public int RuntimeId { get; set; }
|
||||
}
|
||||
}
|
||||
61
Source/Engine/GameData/GameDatabase.cs
Normal file
61
Source/Engine/GameData/GameDatabase.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class GameDatabase : IGameDatabase
|
||||
{
|
||||
private Dictionary<int, object> _idLookup = new();
|
||||
|
||||
private Dictionary<Type, object> _typeLookup = new();
|
||||
|
||||
public void Add<T>(int id, T asset) where T : class
|
||||
{
|
||||
_idLookup.Add(id, asset);
|
||||
|
||||
InvokeAddToTypeLookupWithType(asset, asset.GetType());
|
||||
|
||||
if (asset is IGameDataRuntimeId runtimeId) runtimeId.RuntimeId = id;
|
||||
}
|
||||
|
||||
private void InvokeAddToTypeLookupWithType(object asset, Type type)
|
||||
{
|
||||
var bindingAttr = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
var method = GetType().GetMethod(nameof(AddToTypeLookup), bindingAttr)!.MakeGenericMethod(type);
|
||||
method.Invoke(this, new[] { asset });
|
||||
}
|
||||
|
||||
private void AddToTypeLookup<T>(T asset)
|
||||
{
|
||||
var type = typeof(T);
|
||||
|
||||
if (!_typeLookup.TryGetValue(type, out var list))
|
||||
{
|
||||
list = new List<T>();
|
||||
_typeLookup.Add(type, list);
|
||||
}
|
||||
((List<T>)list).Add(asset);
|
||||
|
||||
var nextType = type.BaseType;
|
||||
if (nextType == typeof(object)) return;
|
||||
|
||||
InvokeAddToTypeLookupWithType(asset, nextType);
|
||||
}
|
||||
|
||||
public T WithId<T>(int id) where T : class
|
||||
{
|
||||
return _idLookup.TryGetValue(id, out var asset) ? (T)asset : null;
|
||||
}
|
||||
|
||||
public List<T> OfType<T>() where T : class
|
||||
{
|
||||
if (!_typeLookup.TryGetValue(typeof(T), out var assets))
|
||||
{
|
||||
assets = new List<T>();
|
||||
_typeLookup.Add(typeof(T), assets);
|
||||
}
|
||||
return (List<T>)assets;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Source/Engine/GameData/GameDatabaseSystem.cs
Normal file
37
Source/Engine/GameData/GameDatabaseSystem.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[GameSystemGroup(typeof(EarlyGameSystemGroup))]
|
||||
public class GameDatabaseSystem : GameSystem, IInitializable, IServiceProvider
|
||||
{
|
||||
public const string GameDataAddressablesKey = "GameData";
|
||||
|
||||
private GameDatabase _gameDatabase;
|
||||
|
||||
public GameDatabaseSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public void RegisterServices(IServiceLocator serviceLocator)
|
||||
{
|
||||
_gameDatabase = new GameDatabase();
|
||||
ServiceLocator.RegisterService<IGameDatabase>(_gameDatabase);
|
||||
}
|
||||
|
||||
public async UniTask InitializeAsync()
|
||||
{
|
||||
await Addressables.LoadAssetsAsync<Object>(GameDataAddressablesKey, OnAssetLoaded);
|
||||
}
|
||||
|
||||
private void OnAssetLoaded(Object asset)
|
||||
{
|
||||
var id = asset.GetInstanceID();
|
||||
id = unchecked(id + 0x40000000);
|
||||
|
||||
_gameDatabase.Add(id, asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Source/Engine/GameData/IGameDataRuntimeId.cs
Normal file
7
Source/Engine/GameData/IGameDataRuntimeId.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public interface IGameDataRuntimeId
|
||||
{
|
||||
int RuntimeId { get; set; }
|
||||
}
|
||||
}
|
||||
13
Source/Engine/GameData/IGameDatabase.cs
Normal file
13
Source/Engine/GameData/IGameDatabase.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public interface IGameDatabase
|
||||
{
|
||||
void Add<T>(int id, T asset) where T : class;
|
||||
|
||||
T WithId<T>(int id) where T : class;
|
||||
|
||||
List<T> OfType<T>() where T : class;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user