public static bool LoadOptions() { Options = new MultiplayerOptions(); try { configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, OPTION_SAVE_FILE), true); List <PropertyInfo> properties = AccessTools.GetDeclaredProperties(typeof(MultiplayerOptions)); MethodInfo configBindMethod = typeof(ConfigFile) .GetMethods() .Where(m => m.Name == nameof(ConfigFile.Bind)) .First(m => m.IsGenericMethod && m.GetParameters().Length == 4); foreach (PropertyInfo prop in properties) { object entry = configBindMethod.MakeGenericMethod(prop.PropertyType).Invoke(configFile, new object[] { SECTION_NAME, prop.Name, prop.GetValue(Options), null }); Type entryType = typeof(ConfigEntry <>).MakeGenericType(prop.PropertyType); prop.SetValue(Options, AccessTools.Property(entryType, "Value").GetValue(entry)); } } catch (Exception e) { Log.Error($"Could not load {OPTION_SAVE_FILE}", e); return(false); } return(true); }
public static bool LoadOptions() { Options = new MultiplayerOptions(); string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), GameConfig.gameName, OPTION_SAVE_FILE); if (File.Exists(path)) { try { var formatter = new BinaryFormatter(); using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)) { Options = (MultiplayerOptions)formatter.Deserialize(stream); } } catch (System.Exception e) { Log.Error($"Could not load {OPTION_SAVE_FILE}", e); return(false); } } return(true); }