public static bool Prefix(DefMap <RecordDef, float> __instance) { var field = __instance.GetType().GetField("values", BindingFlags.GetField | BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance); var l = (field.GetValue(__instance) as List <float>); while (l.Count < DefDatabase <RecordDef> .DefCount) { l.Add(0f); } //field.SetValue(__instance, (int)() + 1); return(true); }
static public void Postfix <T, K>(DefMap <T, K> __instance) where T : Def, new() where K : new() { if (Scribe.mode == LoadSaveMode.Saving) { List <string> keys = new List <string>(); foreach (T def in DefDatabase <T> .AllDefsListForReading.OrderBy(e => e.index)) { keys.Add(def.defName); } Scribe_Collections.Look <string>(ref keys, "keys", LookMode.Undefined, new object[0]); } else { //grabbing the values via reflection. The llokup can be cached per load for performance. System.Reflection.FieldInfo fi = __instance.GetType().GetField("values", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); List <K> values = (List <K>)fi.GetValue(__instance); List <string> keys = new List <string>(); Scribe_Collections.Look <string>(ref keys, "keys", LookMode.Undefined, new object[0]); //we only need to save and calculate the keys once per save. To keep the patch simple we save it multiple times List <int> valueMapping = new List <int>(values.Count); if (keys.NullOrEmpty()) //old save without keys, we must assume the order is matching the database ordering { for (int index = 0; index < values.Count; index++) { valueMapping.Add(index); } } else { foreach (string defName in keys) { T def = DefDatabase <T> .GetNamed(defName, false); if (def == null) { //we no longer use the value specified at this index. valueMapping.Add(-1); } else { valueMapping.Add(def.index); } } } List <K> oldValues = new List <K>(values.Count); oldValues.AddRange(values); values.Clear(); //fill the list until it has enough entries foreach (T def in DefDatabase <T> .AllDefsListForReading.OrderBy(e => e.index)) { values.Add(default(K)); } //assign the saved values to the right spot for (int index = 0; index < valueMapping.Count; index++) { if (valueMapping[index] >= 0) { values[valueMapping[index]] = oldValues[index]; } } } }