Empeld
Empeld plugin documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events
pluginbase.Dependencies.PluginDependencyExtensions Class Reference

Plugin dependency extensions. Included to maintain backwards compatibility More...

Static Public Member Functions

static IPluginLogger GetLogger< T > (this T asker)
 
static T GetDependency< T > (this object asker)
 Gets the dependency. More...
 
static void InjectDependencies< T > (this T cls)
 Find all dependency properties in the class and inject the dependency if available More...
 

Detailed Description

Plugin dependency extensions. Included to maintain backwards compatibility

Member Function Documentation

static T pluginbase.Dependencies.PluginDependencyExtensions.GetDependency< T > ( this object  asker)
static

Gets the dependency.

Returns
The dependency.
Parameters
askerAsker.
Template Parameters
TThe 1st type parameter.
Type Constraints
T :class 
33  : class
34  {
35  return Game.Services.GetDependency<T>(asker.GetType().Assembly);
36  }
static IPluginLogger pluginbase.Dependencies.PluginDependencyExtensions.GetLogger< T > ( this T  asker)
static
Type Constraints
T :class 
15  : class
16  {
17  return Game.Services.GetDependency<IPluginLogger>(typeof(T).Assembly);
18  }
static void pluginbase.Dependencies.PluginDependencyExtensions.InjectDependencies< T > ( this T  cls)
static

Find all dependency properties in the class and inject the dependency if available

Parameters
clsCls.
Template Parameters
TThe 1st type parameter.
Type Constraints
T :class 
48  : class
49  {
50  Type type = typeof(T);
51 
52 #if DEBUG
53  if (type.Assembly.FullName.Contains("empeld"))
54  {
55  throw new AccessViolationException("DEBUG: Don't use dependency injection in empeld core");
56  }
57 #endif
58 
59  var propDependencies = type
60  .GetProperties(DEPN_BINDING)
61  .Where(x => Attribute.IsDefined(x, typeof(DependencyAttribute)));
62 
63  foreach (var target in propDependencies)
64  {
65  if (target.GetValue(cls, null) == null)
66  {
67  var attr = (DependencyAttribute)target.GetCustomAttributes(typeof(DependencyAttribute), true).Single();
68  var injectionResource = Game.Services.GetDependency(type.Assembly, target.PropertyType);
69  if (injectionResource != null)
70  {
71  PropertyInfo declarer = target;
72  if (declarer.DeclaringType != type)
73  {
74  declarer = target.DeclaringType.GetProperty(target.Name, DEPN_BINDING);
75  }
76  declarer.SetValue(cls, injectionResource, null);
77  }
78  else if(!attr.Optional)
79  {
80  throw new Exception(string.Format("Failed to inject dependency for {0}. Dependency doesn't exist, or doesn't exist on both client and server.", target.PropertyType));
81  }
82  }
83  }
84 
85 
86  var fieldDependencies = type.GetFields(DEPN_BINDING)
87  .Where(x => Attribute.IsDefined(x, typeof(DependencyAttribute)));
88 
89  foreach(var target in fieldDependencies)
90  {
91  if (target.GetValue(cls) == null)
92  {
93  var attr = (DependencyAttribute)target.GetCustomAttributes(typeof(DependencyAttribute), true).Single();
94  var injection = Game.Services.GetDependency(type.Assembly, target.FieldType);
95  if (injection != null)
96  {
97  FieldInfo declarer = target;
98  if (declarer.DeclaringType != type)
99  {
100  declarer = target.DeclaringType.GetField(target.Name, DEPN_BINDING);
101  }
102  declarer.SetValue(cls, injection);
103  }
104  else if(!attr.Optional)
105  {
106  throw new Exception(string.Format("Failed to inject field dependency for {0}. Dependency doesn't exist, or doesn't exist on both client and server.", target.FieldType));
107  }
108  }
109  }
110 
111  }

The documentation for this class was generated from the following file: