19namespace fs = std::filesystem;
28 if (fs::exists(
"assets") && fs::is_directory(
"assets")) {
32#ifdef ENIGMA_INSTALL_ASSETS_PATH
34 if (fs::exists(ENIGMA_INSTALL_ASSETS_PATH) && fs::is_directory(ENIGMA_INSTALL_ASSETS_PATH)) {
35 return ENIGMA_INSTALL_ASSETS_PATH;
53 std::vector<RotorConfig> rotors;
59 rotorBox = std::make_unique<RotorBox>(std::vector<AlphabetIndex>{0, 0, 0}, rotors, reflector,
logger);
60 plugBoard = std::make_unique<PlugBoard>();
62 }
catch (
const std::exception& e) {
64 this->logger->log(LogLevel::Error,
"Failed to initialize default EnigmaMachine: " + std::string(e.what()));
75 : rotorBox(std::make_unique<
RotorBox>(config.rotorPositions, config.rotors, config.reflector, logger)),
76 plugBoard(std::make_unique<
PlugBoard>(config.plugBoardPairs)),
82 : rotorBox(std::make_unique<
RotorBox>(std::move(config.rotorPositions), std::move(config.rotors),
83 std::move(config.reflector), logger)),
84 plugBoard(std::make_unique<
PlugBoard>(config.plugBoardPairs)),
95 : rotorBox(std::move(other.rotorBox)),
96 plugBoard(std::move(other.plugBoard)),
97 observers(std::move(other.observers)),
98 logger(other.logger) {
101 rotorBox->registerObserver(
this);
106 if (
this != &other) {
107 rotorBox = std::move(other.rotorBox);
108 plugBoard = std::move(other.plugBoard);
109 observers = std::move(other.observers);
110 logger = other.logger;
113 rotorBox->removeObserver(&other);
114 rotorBox->registerObserver(
this);
130 AlphabetIndex originalInput = input;
132 input =
rotorBox->keyTransform(input);
133 AlphabetIndex output =
plugBoard->swap(input);
135 this->
onCharEncrypted(
static_cast<char>(
'A' + originalInput),
static_cast<char>(
'A' + output));
141 std::ranges::for_each(buffer, [
this](
auto& item) { item =
keyTransform(item); });
154 auto iterator = std::ranges::remove(
observers, observer).begin();
162 obs->onRotorStepped(rotorIndex, position);
168 obs->onCharEncrypted(input, output);
static std::string resolveDefaultAssetPath()
Resolves the default asset path based on execution context. Checks for a local 'assets/' folder first...
EnigmaConfigLoader::FileName FileName
Header file for the EnigmaMachine class.
Header file for the PlugBoard class.
Header file for the RotorBox class.
Factory class for loading Enigma Machine configurations. Handles the parsing of TOML files and the cr...
static ReflectorConfig loadReflector(const IAssetProvider &provider, const FileName &fileName)
Loads a single reflector configuration from a TOML file.
static RotorConfig loadRotor(const IAssetProvider &provider, const FileName &fileName)
Loads a single rotor configuration from a TOML file.
Class representing the Enigma machine.
void onCharEncrypted(char input, char output) override
Called when a character is encrypted/decrypted.
std::unique_ptr< RotorBox > rotorBox
EnigmaMachine & operator=(const EnigmaMachine &)=delete
EnigmaMachine(const EnigmaMachineConfig &config, ILogger *logger=nullptr)
Internal constructor using the configuration struct. Accessible to tests and benchmarks,...
void onRotorStepped(int rotorIndex, AlphabetIndex position) override
Called when a rotor steps.
void setLogger(ILogger *logger)
Sets the logger for the machine.
std::unique_ptr< PlugBoard > plugBoard
void registerObserver(IEnigmaObserver *observer)
Registers an observer to receive notifications.
~EnigmaMachine() override
void processBuffer(std::span< AlphabetIndex > buffer)
Processes a buffer of alphabet indexes in-place.
void removeObserver(IEnigmaObserver *observer)
Removes an observer.
AlphabetIndex keyTransform(AlphabetIndex input)
Transforms the input key through the rotor box.
std::vector< IEnigmaObserver * > observers
Implementation of IAssetProvider that reads from the local filesystem. This is the standard provider ...
Interface for providing configuration/asset data. Abstracts the source of assets (filesystem,...
Class representing the PlugBoard (Steckerbrett) of the Enigma machine.
Class representing a box of rotors in the Enigma machine.
Global configuration constants for the Enigma machine.
constexpr std::string_view defaultRotor2File
constexpr std::string_view assetsDir
Default assets base directory.
constexpr std::string_view defaultRotor3File
constexpr std::string_view defaultRotor1File
Default configuration files.
constexpr std::string_view defaultReflectorFile
Strongly typed wrapper for asset paths. Used to distinguish between general file names and base asset...
Strongly typed wrapper for file names. Used to distinguish between general file names and base asset ...
Configuration Data Transfer Object (DTO) for the Enigma Machine. Holds the raw configuration data req...
Interface for observing Enigma Machine events. Implement this interface to receive notifications abou...