Parse the new firmware options

This commit is contained in:
Jesse Talavera-Greenberg 2023-08-26 13:21:38 -04:00
parent 427fe29dd6
commit 40ffaf737b
3 changed files with 75 additions and 0 deletions

View File

@ -112,6 +112,18 @@ namespace melonds {
Auto = 8,
};
enum class AlarmMode {
Default,
Enabled,
Disabled,
};
enum class UsernameMode {
MelonDSDS,
Guess,
Firmware,
};
enum class Color {
Gray,
Brown,

View File

@ -16,10 +16,15 @@
#include "constants.hpp"
#include <string>
#include <net/net_compat.h>
#include <string/stdstring.h>
#include "environment.hpp"
using std::optional;
using std::nullopt;
using std::string;
optional<melonds::Renderer> melonds::config::ParseRenderer(const char* value) noexcept {
if (string_is_equal(value, values::SOFTWARE)) return melonds::Renderer::Software;
@ -54,6 +59,49 @@ optional<bool> melonds::config::ParseBoolean(const char* value) noexcept {
return nullopt;
}
optional<melonds::AlarmMode> melonds::config::ParseAlarmMode(const char* value) noexcept {
if (string_is_equal(value, values::DISABLED)) return AlarmMode::Disabled;
if (string_is_equal(value, values::ENABLED)) return AlarmMode::Enabled;
if (string_is_equal(value, values::DEFAULT)) return AlarmMode::Default;
return nullopt;
}
optional<melonds::UsernameMode> melonds::config::ParseUsernameMode(const char* value) noexcept {
if (string_is_empty(value) || string_is_equal(value, values::firmware::DEFAULT_USERNAME)) return UsernameMode::MelonDSDS;
if (string_is_equal(value, values::firmware::FIRMWARE_USERNAME)) return UsernameMode::Firmware;
if (string_is_equal(value, values::firmware::GUESS_USERNAME)) return UsernameMode::Guess;
return nullopt;
}
string melonds::config::GetUsername(melonds::UsernameMode mode) noexcept {
char result[DS_NAME_LIMIT];
switch (mode) {
case melonds::UsernameMode::Firmware:
return values::firmware::FIRMWARE_USERNAME;
case melonds::UsernameMode::Guess: {
if (optional<string> frontendGuess = retro::username(); frontendGuess && !frontendGuess->empty()) {
return *frontendGuess;
} else if (const char* user = getenv("USER"); !string_is_empty(user)) {
strncpy(result, user, DS_NAME_LIMIT);
} else if (const char* username = getenv("USERNAME"); !string_is_empty(username)) {
strncpy(result, username, DS_NAME_LIMIT);
} else if (const char* logname = getenv("LOGNAME"); !string_is_empty(logname)) {
strncpy(result, logname, DS_NAME_LIMIT);
} else {
strncpy(result, values::firmware::DEFAULT_USERNAME, DS_NAME_LIMIT);
}
return result;
}
case melonds::UsernameMode::MelonDSDS:
default:
return values::firmware::DEFAULT_USERNAME;
}
}
optional<melonds::ScreenLayout> melonds::config::ParseScreenLayout(const char* value) noexcept {
using melonds::ScreenLayout;
if (string_is_equal(value, values::TOP_BOTTOM)) return ScreenLayout::TopBottom;
@ -89,5 +137,15 @@ std::optional<melonds::FirmwareLanguage> melonds::config::ParseLanguage(std::str
if (value == values::ITALIAN) return melonds::FirmwareLanguage::Italian;
if (value == values::SPANISH) return melonds::FirmwareLanguage::Spanish;
return nullopt;
}
optional<SPI_Firmware::IpAddress> melonds::config::ParseIpAddress(const char* value) noexcept {
SPI_Firmware::IpAddress address;
if (inet_pton(AF_INET, value, &address) == 1) {
return address;
}
return nullopt;
}

View File

@ -207,7 +207,10 @@ namespace melonds::config {
}
std::optional<bool> ParseBoolean(const char *value) noexcept;
std::optional<AlarmMode> ParseAlarmMode(const char *value) noexcept;
std::optional<UsernameMode> ParseUsernameMode(const char* value) noexcept;
std::string GetUsername(UsernameMode mode) noexcept;
template<typename T>
std::optional<T> ParseIntegerInRange(const char *value, T min, T max) noexcept {
if (min > max) return std::nullopt;
@ -251,6 +254,8 @@ namespace melonds::config {
std::optional<melonds::FirmwareLanguage> ParseLanguage(std::string_view value) noexcept;
std::optional<SPI_Firmware::IpAddress> ParseIpAddress(const char* value) noexcept;
}