Skip to content

Commit 34f4b3c

Browse files
Major code clean-up and quality improvements
1 parent c3f1050 commit 34f4b3c

File tree

13 files changed

+808
-422
lines changed

13 files changed

+808
-422
lines changed

Runtime/Attributes/Asset.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,59 @@
11
namespace ElRaccoone.EntityComponentSystem {
2-
2+
/// <summary>
33
/// Describes an asset which should be assigned automatically.
4+
/// </summary>
45
[System.AttributeUsage (System.AttributeTargets.Field)]
56
public class Asset : System.Attribute {
6-
7+
/// <summary>
78
/// Defines whether the field name should be used for getting the asset from
89
/// the controller. When set to false, the asset name string will be used
910
/// instead.
10-
private bool useFieldNameAsAssetName = false;
11+
/// </summary>
12+
bool useFieldNameAsAssetName = false;
1113

14+
/// <summary>
1215
/// The asset name will be used when defined that the field name should not
1316
/// be used for finding the asset name. Thus using this asset name property
1417
/// instead.
15-
private string assetName = "";
18+
/// </summary>
19+
string assetName = "";
1620

21+
/// <summary>
1722
/// Defines the property as an asset, when the controller is done
1823
/// registering, the asset will be fetched from the controller using the
1924
/// name of the property field.
25+
/// </summary>
2026
public Asset () {
21-
this.useFieldNameAsAssetName = true;
22-
this.assetName = "";
27+
useFieldNameAsAssetName = true;
28+
assetName = "";
2329
}
2430

31+
/// <summary>
2532
/// Defines the property as an asset, when the controller is done
2633
/// registering, the asset will be fetched from the controller using the
2734
/// given asset name.
28-
public Asset(string assetName) {
29-
this.useFieldNameAsAssetName = false;
35+
/// </summary>
36+
/// <param name="assetName">The asset name.</param>
37+
public Asset (string assetName) {
38+
useFieldNameAsAssetName = false;
3039
this.assetName = assetName;
3140
}
3241

42+
/// <summary>
3343
/// Sets the attributes values on an object.
44+
/// </summary>
45+
/// <param name="target">The target object.</param>
3446
public static void SetAttributeValues (System.Object target) {
35-
var _fields = target.GetType ().GetFields (
36-
System.Reflection.BindingFlags.Instance |
37-
System.Reflection.BindingFlags.NonPublic);
38-
foreach (var _field in _fields) {
39-
var _assetFieldAttribute = System.Attribute.GetCustomAttribute (_field, typeof (Asset)) as Asset;
40-
if (_assetFieldAttribute != null)
41-
_field.SetValue (target, Controller.Instance.GetAsset(
42-
_assetFieldAttribute.useFieldNameAsAssetName == true ? _field.Name : _assetFieldAttribute.assetName));
47+
var targetType = target.GetType ();
48+
var fields = targetType.GetFields (System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
49+
foreach (var field in fields) {
50+
var assetFieldAttribute = System.Attribute.GetCustomAttribute (field, typeof (Asset)) as Asset;
51+
if (assetFieldAttribute == null) {
52+
continue;
53+
}
54+
var assetName = assetFieldAttribute.useFieldNameAsAssetName ? field.Name : assetFieldAttribute.assetName;
55+
var asset = Controller.Instance.GetAsset (assetName);
56+
field.SetValue (target, asset);
4357
}
4458
}
4559
}

Runtime/Attributes/Injected.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11
namespace ElRaccoone.EntityComponentSystem {
2-
2+
/// <summary>
33
/// Describes a injectedable system, service or controller.
4+
/// </summary>
45
[System.AttributeUsage (System.AttributeTargets.Field)]
56
public class Injected : System.Attribute {
7+
/// <summary>
8+
/// Type of an entity system.
9+
/// </summary>
10+
static readonly System.Type entitySystemType = typeof (IEntitySystem);
11+
12+
/// <summary>
13+
/// Type of a service.
14+
/// </summary>
15+
static readonly System.Type serviceType = typeof (IService);
16+
17+
/// <summary>
18+
/// Type of a controller.
19+
/// </summary>
20+
static readonly System.Type controllerType = typeof (IController);
621

22+
/// <summary>
723
/// Sets the attributes values on an object.
24+
/// </summary>
25+
/// <param name="target">The target object.</param>
826
public static void SetAttributeValues (System.Object target) {
9-
var _fields = target.GetType ().GetFields (
10-
System.Reflection.BindingFlags.Instance |
11-
System.Reflection.BindingFlags.NonPublic);
12-
foreach (var _field in _fields)
13-
if (System.Attribute.GetCustomAttribute (_field, typeof (Injected)) != null)
14-
foreach (var _fieldInterface in _field.FieldType.GetInterfaces ()) {
15-
if (_fieldInterface == typeof (IEntitySystem))
16-
_field.SetValue (target, Controller.Instance.GetSystem (_field.FieldType));
17-
else if (_fieldInterface == typeof (IService))
18-
_field.SetValue (target, Controller.Instance.GetService (_field.FieldType));
19-
else if (_fieldInterface == typeof (IController))
20-
_field.SetValue (target, Controller.Instance);
27+
var targetType = target.GetType ();
28+
var fields = targetType.GetFields (System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
29+
foreach (var field in fields) {
30+
if (System.Attribute.GetCustomAttribute (field, typeof (Injected)) == null) {
31+
continue;
32+
}
33+
var fieldInterfaces = field.FieldType.GetInterfaces ();
34+
foreach (var fieldInterface in fieldInterfaces) {
35+
if (fieldInterface == entitySystemType) {
36+
field.SetValue (target, Controller.Instance.GetSystem (field.FieldType));
37+
} else if (fieldInterface == serviceType) {
38+
field.SetValue (target, Controller.Instance.GetService (field.FieldType));
39+
} else if (fieldInterface == controllerType) {
40+
field.SetValue (target, Controller.Instance);
2141
}
42+
}
43+
}
2244
}
2345
}
2446
}

0 commit comments

Comments
 (0)