Ejemplo n.º 1
0
    /// <summary> Use Reflection to check if all systems have proper attributes defined and Debug.Log everything is used, but not specified </summary>
    public void ValidateSystems()
    {
        // Validate Gun Components, map them to GunAspects for later use
        Dictionary <ushort, Type> component_map = new Dictionary <ushort, Type>();

        foreach (Type component_type in typeof(GunComponent).GetAllDerivedTypes())
        {
            GunDataAttribute att = component_type.GetCustomAttribute <GunDataAttribute>();
            if (att == null)
            {
                Debug.LogError($"{component_type} doesn't have a GunDataAttribute assigned. Every GunComponent needs to specify one!");
                continue;
            }

            if (component_map.ContainsKey((ushort)att.gun_aspect))  // TODO might need to override GunAspect operator==
            {
                Debug.LogError($"{component_type} links to {att.gun_aspect}, but {component_map[(ushort)att.gun_aspect]} has already linked to the same aspect!");
            }
            else
            {
                component_map.Add((ushort)att.gun_aspect, component_type);
            }
        }

        // Go over every system
        foreach (Type system_type in typeof(GunSystemBase).GetAllDerivedTypes(systems.GetType()))
        {
            List <Type> registered = new List <Type>();

            // Gather types the system says it wants to use
            InclusiveAspectsAttribute inc_att = system_type.GetCustomAttribute <InclusiveAspectsAttribute>();
            if (inc_att != null)
            {
                foreach (GunAspect gun_aspect in inc_att.inclusive_aspects.value)
                {
                    if (component_map.ContainsKey((ushort)gun_aspect))
                    {
                        registered.Add(component_map[(ushort)gun_aspect]);
                    }
                }
            }

            // Gather types the system actually uses as fields
            foreach (FieldInfo field in system_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (field.FieldType.IsSubclassOf(typeof(GunComponent)) && !registered.Contains(field.FieldType))
                {
                    Debug.LogWarning($"{system_type} uses {field.FieldType} but doesn't include it as inclusive aspect!");
                }
            }
        }

        Debug.Log("System validation completed!");
    }
Ejemplo n.º 2
0
 public GunComponent GetComponentFromAspect(GunAspect aspect)
 {
     foreach (Type type in possible_components)
     {
         GunDataAttribute gun_data = type.GetCustomAttribute <GunDataAttribute>();
         if (gun_data != null && gun_data.gun_aspect.HasFlag(aspect))
         {
             return((GunComponent)gun_script.GetComponent(type));
         }
     }
     return(null);
 }