| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Components; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Models; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class RuntimeComponentFactory : IRuntimeComponentFactory |
| | 13 | | { |
| | 14 | | // Temporal delegate for special behaviours. Should be deleted or refactored later. |
| 938 | 15 | | public Dictionary<int, IRuntimeComponentFactory.CreateCondition> createConditions { get; set; } = |
| 772 | 16 | | new Dictionary<int, IRuntimeComponentFactory.CreateCondition>(); |
| | 17 | |
|
| 1386 | 18 | | public Dictionary<int, IRuntimeComponentFactory.CreateOverride> createOverrides { get; set; } = |
| 772 | 19 | | new Dictionary<int, IRuntimeComponentFactory.CreateOverride>(); |
| | 20 | |
|
| | 21 | | protected delegate IComponent ComponentBuilder(int classId); |
| | 22 | |
|
| 772 | 23 | | protected Dictionary<int, ComponentBuilder> builders = new Dictionary<int, ComponentBuilder>(); |
| | 24 | |
|
| | 25 | | public void RegisterBuilder(int classId, Func<IComponent> builder) |
| | 26 | | { |
| 12404 | 27 | | if (builders.ContainsKey(classId)) |
| 0 | 28 | | builders[classId] = (id) => builder(); |
| | 29 | | else |
| 13399 | 30 | | builders.Add(classId, (id) => builder()); |
| 12404 | 31 | | } |
| | 32 | |
|
| | 33 | | public void UnregisterBuilder(int classId) |
| | 34 | | { |
| 13435 | 35 | | if (!builders.ContainsKey(classId)) |
| 1191 | 36 | | return; |
| | 37 | |
|
| 12244 | 38 | | builders.Remove(classId); |
| 12244 | 39 | | } |
| | 40 | |
|
| | 41 | | public IComponent CreateComponent(int classId) |
| | 42 | | { |
| 995 | 43 | | if (!builders.ContainsKey(classId)) |
| | 44 | | { |
| 0 | 45 | | Debug.LogError( |
| | 46 | | $"Unknown classId: {classId} - Make sure the component is registered! (You forgot to add a plugin?)" |
| 0 | 47 | | return null; |
| | 48 | | } |
| | 49 | |
|
| 995 | 50 | | IComponent newComponent = builders[classId](classId); |
| | 51 | |
|
| 995 | 52 | | return newComponent; |
| | 53 | | } |
| | 54 | |
|
| 772 | 55 | | public RuntimeComponentFactory() |
| | 56 | | { |
| 772 | 57 | | } |
| | 58 | |
|
| | 59 | | public void Initialize() |
| | 60 | | { |
| 772 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Dispose() |
| | 64 | | { |
| 772 | 65 | | } |
| | 66 | | } |
| | 67 | | } |