public static void GenerateEntityClass(FoxEntity foxEntity) { var fileName = classDirectory + foxEntity.ClassName + ".cs"; if (File.Exists(fileName)) { throw new ArgumentException("Class " + foxEntity.ClassName + " already exists."); } using (var outfile = new StreamWriter(fileName)) { // Write using statements foreach (var statement in MakeUsingStatements()) { outfile.WriteLine(statement); } outfile.WriteLine(""); outfile.WriteLine(MakeNamespaceStatement()); // Opening brace for namespace declaration. outfile.WriteLine("{"); var parentClass = DetermineParentClass(foxEntity); outfile.WriteLine(" " + MakeClassNameDeclaration(foxEntity.ClassName, GetRealTypeName(parentClass))); // Opening brace for class declaration. outfile.WriteLine(" {"); // Write properties. var inheritedFields = GetInheritedPropertyNames(parentClass).Select(field => field.ToLower()); var hasWrittenFirstProperty = false; foreach (var property in foxEntity.StaticProperties.Where(property => !inheritedFields.Contains(property.Name.ToLower()))) { if (hasWrittenFirstProperty) { outfile.WriteLine(""); } outfile.WriteLine(MakeFieldAttribute(property.Name, property.DataType, property.ContainerType, false)); outfile.WriteLine(MakeFieldDeclaration(property.Name, GetNativeType(property.DataType), property.ContainerType)); hasWrittenFirstProperty = true; } foreach (var property in foxEntity.DynamicProperties.Where(property => !inheritedFields.Contains(property.Name.ToLower()))) { if (hasWrittenFirstProperty) { outfile.WriteLine(""); } outfile.WriteLine(MakeFieldAttribute(property.Name, property.DataType, property.ContainerType, true)); outfile.WriteLine(MakeFieldDeclaration(property.Name, GetNativeType(property.DataType), property.ContainerType)); hasWrittenFirstProperty = true; } // Closing brace for class declaration. outfile.WriteLine(" }"); // Closing brace for namespace declaration. outfile.WriteLine("}"); } // Need to dispatch this to main thread //AssetDatabase.Refresh(); }
public static Entity MakeEntity(FoxEntity foxData, Dictionary <string, Type> entityTypes, ConcurrentDictionary <string, FoxEntity> unimplementedTypeTable, IUnityThreadCommandDispatcher unityThreadDispatcher, IEntityReferenceResolver entityReferenceResolver) { // Get Entity type. Type entityType; if (!entityTypes.TryGetValue(foxData.ClassName, out entityType) && !unimplementedTypeTable.ContainsKey(foxData.ClassName)) { Debug.LogWarning("Class '" + foxData.ClassName + "' not found in entityTypes."); unimplementedTypeTable.TryAdd(foxData.ClassName, foxData); return(null); } // Get new Entity instance. // We can't make new GameObjects outside the main thread, so dispatch a request for one to the Unity thread. Entity entityInstance = null; var waitEvent = unityThreadDispatcher.DispatchCommand(new CreateEntity(entityType, newEntity => entityInstance = newEntity)); // Wait for our command to be executed by the Unity thread. waitEvent.WaitOne(); entityReferenceResolver.RegisterReference(foxData.Address, entityInstance); // Load properties. ImportEntityProperties(entityInstance, foxData, entityReferenceResolver); return(entityInstance); }
private static void ImportEntityProperties(Entity entityInstance, FoxEntity foxEntityData, IEntityReferenceResolver entityReferenceResolver) { foreach (var foxProperty in foxEntityData.StaticProperties) //.Concat(foxEntityData.DynamicProperties)) -- Dynamic properties don't work yet. { try { ImportEntityProperty(entityInstance, foxProperty, entityReferenceResolver); } catch (Exception e) { Debug.LogError(e); } } }
private static Type DetermineParentClass(FoxEntity foxEntity) { const string namePropertyName = "name"; const string dataSetPropertyName = "dataSet"; const string ownerPropertyName = "owner"; const string parentPropertyName = "parent"; const string transformPropertyName = "transform"; const string shearTransformPropertyName = "shearTransform"; const string pivotTransformPropertyName = "pivotTransform"; const string childrenPropertyName = "children"; const string flagsPropertyName = "flags"; const string sizePropertyName = "size"; var propertiesToCheck = new Dictionary <string, bool> { { namePropertyName, false }, { dataSetPropertyName, false }, { ownerPropertyName, false }, { parentPropertyName, false }, { transformPropertyName, false }, { shearTransformPropertyName, false }, { pivotTransformPropertyName, false }, { childrenPropertyName, false }, { flagsPropertyName, false }, { sizePropertyName, false }, }; foreach (var property in foxEntity.StaticProperties) { if (propertiesToCheck.ContainsKey(property.Name)) { propertiesToCheck[property.Name] = true; } } // An Entity is a subclass of Data if it has a "name" and "dataSet" property. if (propertiesToCheck[namePropertyName] && propertiesToCheck[dataSetPropertyName]) { // An Entity is a subclass of TransformData if it has a "parent," "transform," "shearTransform," "pivotTransform," "children," and "flags" property. if (propertiesToCheck[parentPropertyName] && propertiesToCheck[transformPropertyName] && propertiesToCheck[shearTransformPropertyName] && propertiesToCheck[pivotTransformPropertyName] && propertiesToCheck[childrenPropertyName] && propertiesToCheck[flagsPropertyName]) { // An Entity is a subclass of Locator if it has a "size" property. if (propertiesToCheck[sizePropertyName]) { return(typeof(Locator)); } return(typeof(TransformData)); } return(typeof(Data)); } // An Entity is a subclass of DataElement<T> if it has an "owner" property. if (propertiesToCheck[ownerPropertyName]) { // TODO: Is there a way to determine the actual generic type argument? return(typeof(DataElement <Entity>)); } return(typeof(Entity)); }