/// <summary>Fixes null persistent fields in the module.</summary> /// <remarks>Used to prevent NREs in methods that persist KSP fields.</remarks> /// <param name="module">Module to fix.</param> public static void CleanupFieldsInModule(PartModule 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 { var unused = module.Fields.GetEnumerator(); } catch { Logger.logWarning("WORKAROUND. Module {0} on part prefab {1} is not awaken. Call Awake on it", module.GetType(), module.part); AwakePartModule(module); } 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); Logger.logWarning("WORKAROUND. Found null field {0} in module prefab {1}," + " fixing to default value of type {2}: {3}", baseField.name, module.moduleName, baseField.FieldInfo.FieldType, defValue); baseField.SetValue(defValue, module); } } }
/// <summary>Fixes null persistent fields in the module.</summary> /// <remarks>Used to prevent NREs in methods that persist KSP fields.</remarks> /// <param name="module">A module to fix.</param> public static void CleanupFieldsInModule(PartModule module) { 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); Logger.logWarning("WORKAROUND. Found null field {0} in module prefab {1}," + " fixing to default value of type {2}: {3}", baseField.name, module.moduleName, baseField.FieldInfo.FieldType, defValue); baseField.SetValue(defValue, module); } } }
/// <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); } } }