AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SyncSettings.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Reflection;
8 
9 namespace HoloToolkit.Sharing.SyncModel
10 {
16  public class SyncSettings
17  {
18 #if UNITY_WSA && !UNITY_EDITOR
19  private readonly Dictionary<TypeInfo, string> dataModelTypeToName = new Dictionary<TypeInfo, string>();
20  private readonly Dictionary<string, TypeInfo> dataModelNameToType = new Dictionary<string, TypeInfo>();
21 #else
22  private readonly Dictionary<Type, string> dataModelTypeToName = new Dictionary<Type, string>();
23  private readonly Dictionary<string, Type> dataModelNameToType = new Dictionary<string, Type>();
24 #endif
25 
26  private static SyncSettings instance;
27  public static SyncSettings Instance
28  {
29  get
30  {
31  if (instance == null)
32  {
33  instance = new SyncSettings();
34  }
35  return instance;
36  }
37  }
38 
39  public string GetDataModelName(Type type)
40  {
41  var typeInfo = type.GetTypeInfo();
42  string retVal;
43  dataModelTypeToName.TryGetValue(typeInfo, out retVal);
44  return retVal;
45  }
46 
47 #if UNITY_WSA && !UNITY_EDITOR
48  public TypeInfo GetDataModelType(string name)
49  {
50  TypeInfo retVal;
51 #else
52  public Type GetDataModelType(string name)
53  {
54  Type retVal;
55 #endif
56 
57  dataModelNameToType.TryGetValue(name, out retVal);
58  return retVal;
59  }
60 
61  public void Initialize()
62  {
63  dataModelNameToType.Clear();
64  dataModelTypeToName.Clear();
65 
66  foreach (var assembly in GetAssemblies())
67  {
68  // We currently skip all assemblies except Unity-generated ones
69  // This could be modified to be customizable by the user
70  if (!assembly.FullName.StartsWith("Assembly-"))
71  {
72  continue;
73  }
74 
75 #if UNITY_WSA && !UNITY_EDITOR
76  foreach (TypeInfo type in assembly.GetTypes())
77 #else
78  foreach (Type type in assembly.GetTypes())
79 #endif
80  {
81  object customAttribute = type.GetCustomAttributes(typeof(SyncDataClassAttribute), false).FirstOrDefault();
82  SyncDataClassAttribute attribute = customAttribute as SyncDataClassAttribute;
83 
84  if (attribute != null)
85  {
86  string dataModelName = type.Name;
87 
88  // Override the class name if provided
89  if (!string.IsNullOrEmpty(attribute.CustomClassName))
90  {
91  dataModelName = attribute.CustomClassName;
92  }
93 
94  dataModelNameToType.Add(dataModelName, type);
95  dataModelTypeToName.Add(type, dataModelName);
96  }
97  }
98  }
99  }
100 
101  private static Assembly[] GetAssemblies()
102  {
103 #if UNITY_WSA && !UNITY_EDITOR
104  return new Assembly[]
105  {
106  typeof(SyncSettings).GetTypeInfo().Assembly
107  };
108 #else
109  return AppDomain.CurrentDomain.GetAssemblies();
110 #endif
111  }
112  }
113 }
Collection of sharing sync settings, used by the HoloToolkit Sharing sync system to figure out which ...
Definition: SyncSettings.cs:16
Used to markup SyncObject classes, so that they properly get instantiated when using a hierarchical d...