Ejemplo n.º 1
0
        /// <summary>Fixes null persistent fields in the module.</summary>
        /// <remarks>Used to prevent NREs in methods that persist KSP fields.</remarks>
        /// <param name="module">The module to fix.</param>
        static void CleanupFieldsInModule(PartModule module)
        {
            // HACK: Fix uninitialized fields in science lab module.
            ModuleScienceLab scienceModule = module as ModuleScienceLab;

            if (scienceModule != null)
            {
                scienceModule.ExperimentData = new List <string>();
                Core.Log("WORKAROUND. Fix null field in ModuleScienceLab module on the part prefab: " + module, LogLevel.Important);
            }

            // Ensure the module is awaken. Otherwise, any access to base fields list will result in NRE.
            // HACK: Accessing Fields property of a non-awaken module triggers NRE. If it happens then do
            // explicit awakening of the *base* module class.
            try
            {
                module.Fields.GetEnumerator();
            }
            catch
            {
                Core.Log("WORKAROUND. Module " + module + " on part prefab is not awaken. Call Awake on it", LogLevel.Important);
                module.Awake();
            }
            foreach (BaseField field in module.Fields)
            {
                BaseField baseField = field as BaseField;
                if (baseField.isPersistant && baseField.GetValue(module) == null)
                {
                    StandardOrdinaryTypesProto proto = new StandardOrdinaryTypesProto();
                    object defValue = proto.ParseFromString("", baseField.FieldInfo.FieldType);
                    Core.Log("WORKAROUND. Found null field " + baseField.name + " in module prefab " + module + ", fixing to default value of type " + baseField.FieldInfo.FieldType + ": " + defValue, LogLevel.Important);
                    baseField.SetValue(defValue, module);
                }
            }
        }
Ejemplo n.º 2
0
 public static bool Awaken(PartModule module)
 {
     if (module == null)
     {
         return(false);
     }
     module.Awake();
     return(true);
 }
Ejemplo n.º 3
0
        /// <summary>Fixes null persistent fields in the module.</summary>
        /// <remarks>Used to prevent NREs in methods that persist KSP fields.</remarks>
        /// <param name="module">The module to fix.</param>
        static void CleanupFieldsInModule(PartModule module)
        {
            // HACK: Fix uninitialized fields in science lab module.
            var scienceModule = module as ModuleScienceLab;

            if (scienceModule != null)
            {
                scienceModule.ExperimentData = new List <string>();
                DebugEx.Warning(
                    "WORKAROUND. Fix null field in ModuleScienceLab module on the part prefab: {0}", module);
            }

            // Ensure the module is awaken. Otherwise, any access to base fields list will result in NRE.
            // HACK: Accessing Fields property of a non-awaken module triggers NRE. If it happens then do
            // explicit awakening of the *base* module class.
            try {
                module.Fields.GetEnumerator();
            } catch {
                DebugEx.Warning(
                    "WORKAROUND. Module {0} on part prefab is not awaken. Call Awake on it", module);
                module.Awake();
            }
            foreach (var field in module.Fields)
            {
                var baseField = field as BaseField;
                if (baseField.isPersistant && baseField.GetValue(module) == null)
                {
                    var proto    = new StandardOrdinaryTypesProto();
                    var defValue = proto.ParseFromString("", baseField.FieldInfo.FieldType);
                    DebugEx.Warning("WORKAROUND. Found null field {0} in module prefab {1},"
                                    + " fixing to default value of type {2}: {3}",
                                    baseField.name, module, baseField.FieldInfo.FieldType, defValue);
                    baseField.SetValue(defValue, module);
                }
            }
        }