Example #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);
        }
Example #2
0
        public static BehaviorBase Load(AccessDatabase database, uint id, out bool cached)
        {
            lock (Cache)
            {
                cached = Cache.TryGetValue(id, out var cache);

                if (cached)
                {
                    return(cache);
                }
            }

            var table = database["BehaviorTemplate"];

            if (!table.Seek((int)id, out var behavior))
            {
                throw new Exception($"Failed to find BehaviorTemplate for Behavior Id: {id}!");
            }

            var entry = new BehaviorTemplateTable(behavior);

            var template = (Template)entry.templateID;

            var instance = BehaviorRegistry.InquireBehavior(template);

            lock (Cache)
            {
                Cache[id] = instance;
            }

            instance.Id = id;

            instance.Effect = (uint)entry.effectID;

            instance.Load(database);

            foreach (var property in instance.GetType().GetProperties())
            {
                var attribute = property.GetCustomAttribute <ParameterAttribute>();

                if (attribute == null)
                {
                    continue;
                }

                object value;

                if (property.PropertyType == typeof(BehaviorBase))
                {
                    value = instance.Branch(database, attribute.Name);
                }
                else if (property.PropertyType == typeof(bool))
                {
                    value = instance.GetParameter <int>(database, attribute.Name) == 1;
                }
                else
                {
                    value = instance.GetParameter(database, attribute.Name, property.PropertyType);

                    if (value == null)
                    {
                        continue;
                    }
                }

                property.SetValue(instance, value);
            }

            return(instance);
        }
Example #3
0
        public void Apply(AccessDatabase database)
        {
            foreach (var property in GetType().GetProperties())
            {
                if (property.PropertyType != typeof(BehaviorBase))
                {
                    continue;
                }

                var behavior = (BehaviorBase)property.GetValue(this);

                if (behavior == null)
                {
                    continue;
                }

                if (!Branches.Contains(behavior))
                {
                    Branches.Add(behavior);
                }
            }

            if (Id == default)
            {
                var table = database["BehaviorTemplate"];

                var entry = table.Create();

                var template = new BehaviorTemplateTable(entry);

                Id = (uint)template.behaviorID;

                template.effectID = (int)Effect;

                var type = GetType();

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

                if (attribute == null)
                {
                    throw new Exception($"Invalid behavior template: {type}!");
                }

                template.templateID = (int)attribute.Template;
            }

            foreach (var branch in Branches)
            {
                branch.Apply(database);
            }

            foreach (var property in GetType().GetProperties())
            {
                var attribute = property.GetCustomAttribute <ParameterAttribute>();

                if (attribute == null)
                {
                    continue;
                }

                if (property.PropertyType == typeof(BehaviorBase))
                {
                    var behavior = (BehaviorBase)property.GetValue(this);

                    SetParameter(database, attribute.Name, behavior);
                }
                else if (property.PropertyType == typeof(bool))
                {
                    var boolean = (bool)property.GetValue(this);

                    SetParameter(database, attribute.Name, boolean ? 1 : 0);
                }
                else
                {
                    SetParameter(database, attribute.Name, (object)property.GetValue(this));
                }
            }

            ApplyChanges(database);
        }