/// <summary> /// Set given setting /// </summary> /// <returns></returns> private object SetSetting() { if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting) { return(APIStatus.BadRequest("You may only do this before server init")); } // TODO Refactor Settings to a POCO that is serialized, and at runtime, build a dictionary of types to validate against try { Settings setting = this.Bind(); if (string.IsNullOrEmpty(setting.setting)) { return(APIStatus.BadRequest("An invalid setting was passed")); } if (setting.value == null) { return(APIStatus.BadRequest("An invalid value was passed")); } var property = typeof(ServerSettings).GetProperty(setting.setting); if (property == null) { return(APIStatus.BadRequest("An invalid setting was passed")); } if (!property.CanWrite) { return(APIStatus.BadRequest("An invalid setting was passed")); } var settingType = property.PropertyType; try { var converter = TypeDescriptor.GetConverter(settingType); if (!converter.CanConvertFrom(typeof(string))) { return(APIStatus.BadRequest("An invalid value was passed")); } var value = converter.ConvertFromInvariantString(setting.value); if (value == null) { return(APIStatus.BadRequest("An invalid value was passed")); } property.SetValue(null, value); } catch { // ignore, we are returning the error below } return(APIStatus.BadRequest("An invalid value was passed")); } catch { return(APIStatus.InternalError()); } }
/// <summary> /// Return given setting /// </summary> /// <returns></returns> private object GetSetting() { if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting) { return(APIStatus.BadRequest("You may only do this before server init")); } try { // TODO Refactor Settings to a POCO that is serialized, and at runtime, build a dictionary of types to validate against Settings setting = this.Bind(); if (string.IsNullOrEmpty(setting?.setting)) { return(APIStatus.BadRequest("An invalid setting was passed")); } try { var value = typeof(ServerSettings).GetProperty(setting.setting)?.GetValue(null, null); if (value == null) { return(APIStatus.BadRequest("An invalid setting was passed")); } Settings return_setting = new Settings { setting = setting.setting, value = value.ToString() }; return(return_setting); } catch { return(APIStatus.BadRequest("An invalid setting was passed")); } } catch { return(APIStatus.InternalError()); } }