Exemplo n.º 1
0
        private static uint ImportBehavior(BehaviorXml details, AccessDatabase database)
        {
            var behaviorTable = database["BehaviorTemplate"];

            var entry = behaviorTable.Create();

            var behavior = new BehaviorTemplateTable(entry);

            behavior.effectID = (int)details.Effect;

            behavior.templateID = (int)details.Type;

            foreach (var detailsParameter in details.Parameters)
            {
                var parameterTable = database["BehaviorParameter"];

                var parameterEntry = parameterTable.Create(behavior.behaviorID);

                var parameter = new BehaviorParameterTable(parameterEntry);

                parameter.parameterID = detailsParameter.Name;

                if (detailsParameter.Behavior)
                {
                    parameter.value = detailsParameter.Next == null ? 0 : ImportBehavior(detailsParameter.Next, database);
                }
                else
                {
                    parameter.value = detailsParameter.Value;
                }
            }

            return((uint)behavior.behaviorID);
        }
Exemplo n.º 2
0
        public static BehaviorXml ExportBehavior(BehaviorBase behavior)
        {
            if (behavior == null)
            {
                return(null);
            }

            var details = new BehaviorXml
            {
                Effect = behavior.Effect
            };

            var type = behavior.GetType();

            var attribute = type.GetCustomAttribute <BehaviorAttribute>();

            if (attribute == null)
            {
                throw new Exception($"Invalid behavior: {behavior.GetType()}");
            }

            details.Type = (uint)attribute.Template;

            var properties = new List <ParameterXml>();

            foreach (var property in type.GetProperties())
            {
                if (property.Name == "Effect")
                {
                    continue;
                }

                var info = property.GetCustomAttribute <ParameterAttribute>();

                if (info == null)
                {
                    continue;
                }

                var propertyDetails = new ParameterXml
                {
                    Name = info.Name
                };

                if (property.PropertyType == typeof(BehaviorBase))
                {
                    propertyDetails.Behavior = true;

                    propertyDetails.Next = ExportBehavior(property.GetValue(behavior) as BehaviorBase);
                }
                else if (property.PropertyType == typeof(bool))
                {
                    propertyDetails.Value = (bool)property.GetValue(behavior) ? 1 : 0;
                }
                else
                {
                    propertyDetails.Value = (float)Convert.ChangeType(
                        property.GetValue(behavior) ?? 0f,
                        typeof(float)
                        );
                }

                properties.Add(propertyDetails);
            }

            details.Parameters = properties.ToArray();

            behavior.Export(details);

            return(details);
        }