Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssetItemAccessor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using SiliconStudio.Core.Reflection;
6 
7 namespace SiliconStudio.Assets
8 {
9  /// <summary>
10  /// An <see cref="AssetItem"/> accessor to get member value and overrides.
11  /// </summary>
12  public class AssetItemAccessor
13  {
14  private readonly List<AssetItem> baseItems;
15 
16  /// <summary>
17  /// Initializes a new instance of the <see cref="AssetItemAccessor"/> class.
18  /// </summary>
19  /// <param name="item">The item.</param>
20  /// <exception cref="System.ArgumentNullException">item</exception>
22  {
23  if (item == null) throw new ArgumentNullException("item");
24  Item = item;
25  baseItems = new List<AssetItem>();
26 
27  var nextBaseItem = Item;
28  // Process the hierarchy and try to find
29  while ((nextBaseItem = nextBaseItem.FindBase()) != null)
30  {
31  baseItems.Add(nextBaseItem);
32  }
33  }
34 
35  /// <summary>
36  /// Gets the item associated to this instance.
37  /// </summary>
38  /// <value>The item associated to this instance.</value>
39  public AssetItem Item { get; private set; }
40 
41  /// <summary>
42  /// Try to gets the value of an asset member and provides the assets that
43  /// <see cref="OverrideType" /> information for this particular
44  /// member.
45  /// </summary>
46  /// <param name="path">The path to the member.</param>
47  /// <returns>AssetMemberValue.</returns>
48  /// <exception cref="System.ArgumentNullException">path</exception>
50  {
51  if (path == null) throw new ArgumentNullException("path");
52 
53  object value;
54  OverrideType overrideType;
55 
56  if (path.TryGetValue(Item.Asset, out value, out overrideType))
57  {
58  // If the member is new, we don't need to check further the inheritance.
59  if (overrideType.IsNew())
60  {
61  return new AssetMemberValue(value, overrideType, null);
62  }
63 
64  AssetItem overriderItem = null;
65 
66  // Else check bases and find the first new or sealed base
67  foreach (var nextBaseItem in baseItems)
68  {
69  object parentValue;
70  OverrideType parentOverrideType;
71  if (path.TryGetValue(nextBaseItem.Asset, out parentValue, out parentOverrideType))
72  {
73  overriderItem = nextBaseItem;
74 
75  // If we found a base asset with a sealed member, sets the orriderItem to this item and
76  // make sure the overrideType of the current asset item instance is set to Base|Sealed
77  if (parentOverrideType.IsSealed())
78  {
79  overrideType = OverrideType.Base | OverrideType.Sealed;
80  break;
81  }
82 
83  // Check if value is coming from a member with a new value
84  if (parentOverrideType.IsNew())
85  {
86  break;
87  }
88  }
89  }
90  return new AssetMemberValue(value, overrideType, overriderItem);
91  }
92 
93  // Not valid, return an empty value
94  return new AssetMemberValue();
95  }
96  }
97 }
bool TryGetValue(object rootObject, out object value)
Gets the value from the specified root object following this instance path.
Definition: MemberPath.cs:257
AssetItemAccessor(AssetItem item)
Initializes a new instance of the AssetItemAccessor class.
An asset item part of a Package accessible through SiliconStudio.Assets.Package.Assets.
Definition: AssetItem.cs:17
Contains the value of an asset member returned by AssetItemAccessor.TryGetMemberValue ...
AssetMemberValue TryGetMemberValue(MemberPath path)
Try to gets the value of an asset member and provides the assets that OverrideType information for th...
An AssetItem accessor to get member value and overrides.
Allows to get/set a property/field value on a deeply nested object instance (supporting members...
Definition: MemberPath.cs:14
OverrideType
A Type of override used on a member value.
Definition: OverrideType.cs:11