The Utilities library provides foundational infrastructure for Nexen, including serialization, file I/O, string handling, compression, and platform abstraction.
Utilities/
├── Core Utilities
│ ├── Serializer.h/cpp - State serialization (save states, rewind)
│ ├── BitUtilities.h - Bit manipulation (templated, zero-cost)
│ ├── HexUtilities.h/cpp - Hex string conversion
│ ├── StringUtilities.h - String manipulation helpers
│ └── FastString.h - Stack-allocated string builder
│
├── File & Archive I/O
│ ├── VirtualFile.h/cpp - Unified file abstraction
│ ├── FolderUtilities.h/cpp - Directory operations
│ ├── ArchiveReader.h/cpp - Archive extraction base
│ ├── ZipReader.h/cpp - ZIP file reading
│ ├── ZipWriter.h/cpp - ZIP file writing
│ └── (UI managed path) - ZIP/7z runtime archive read via SharpCompress
│
├── Compression
│ ├── CompressionHelper.h - Compression API wrapper
│ └── miniz.h/cpp - DEFLATE implementation
│
├── Hashing & Validation
│ ├── CRC32.h/cpp - CRC32 checksum
│ ├── md5.h/cpp - MD5 hash
│ └── sha1.h/cpp - SHA-1 hash
│
├── Platform Abstraction
│ ├── PlatformUtilities.h/cpp - OS-specific helpers
│ ├── UTF8Util.h/cpp - UTF-8 string encoding
│ ├── PathUtil.h - Path manipulation
│ └── Socket.h/cpp - Network socket wrapper
│
├── Threading & Synchronization
│ ├── SimpleLock.h/cpp - Mutex wrapper
│ └── AutoResetEvent.h/cpp - Event signaling
│
├── Graphics Filters
│ ├── HQX/ - HQnX filter
│ ├── KreedSaiEagle/ - Kreed/SAI/Eagle filters
│ ├── Scale2x/ - Scale2x filter
│ ├── xBRZ/ - xBRZ filter
│ ├── NTSC/ - NTSC composite simulation
│ └── Video/ - Video output helpers
│
├── Audio
│ └── Audio/ - Audio processing
│
├── Images
│ ├── PNGHelper.h/cpp - PNG loading/saving
│ └── spng.h/c - Simple PNG library
│
├── Patching
│ └── Patches/ - IPS/BPS patch support
│
└── Third-Party
├── magic_enum.hpp - Enum reflection
├── kissfft.h - FFT library (audio)
└── safe_ptr.h - Thread-safe smart pointers
The Serializer class provides a key-value based serialization system used for save states and rewind functionality.
Key Features:
- Bidirectional: Same code for saving and loading
- Multiple formats: Binary (compact), Text (debug), Map (Lua API)
- Versioning support for backwards compatibility
- Nested object support via ISerializable interface
Usage Example:
class MyState : public ISerializable {
uint32_t _value;
uint8_t _data[256];
void Serialize(Serializer& s) override {
SV(_value); // Stream variable with name
SVArray(_data, 256); // Stream array with count
}
};
// Saving
Serializer saver(1, true, SerializeFormat::Binary);
myState.Serialize(saver);
saver.SaveTo(outputStream);
// Loading
Serializer loader(1, false, SerializeFormat::Binary);
loader.LoadFrom(inputStream);
myState.Serialize(loader);Macros:
SV(var)- Stream variable with automatic nameSVArray(arr, count)- Stream array with countSVI(var)- Stream indexed variableSVVector(var)- Stream std::vector
Template-based bit manipulation utilities with compile-time optimization.
Key Methods:
// Set 8 bits at position 16 (bits 16-23)
BitUtilities::SetBits<16>(dst, 0x42);
// Get 8 bits from position 8 (bits 8-15)
uint8_t value = BitUtilities::GetBits<8>(src);Benefits:
- Zero runtime overhead (compile-time templates)
__forceinlineensures no function call overhead- Type-safe with templates
Cached hex string conversion for performance.
Key Methods:
string ToHex(uint8_t value); // "AB"
string ToHex(uint16_t value); // "1234"
string ToHex(uint32_t value); // Variable length
string ToHex24(int32_t value); // "00FFFF" (24-bit addresses)
int FromHex(string hex); // Parse hex stringStack-allocated string builder for high-performance string concatenation.
Features:
- No heap allocation for typical strings (< ~400 chars)
- StringBuilder-like API
- Automatic overflow to heap if needed
Common string operations:
Split(string, delimiter)- Split stringTrim(string)- Remove whitespaceToLower(string)/ToUpper(string)Contains(string, substring)
Unified file abstraction supporting:
- Regular filesystem files
- Files inside ZIP archives
- Files inside 7-Zip archives
- Memory buffers
Usage:
VirtualFile file("game.zip/rom.nes");
vector<uint8_t> data;
file.ReadAllBytes(data);- ZipReader/ZipWriter - ZIP file handling (miniz-based)
- ArchiveReader - Native archive abstraction for ZIP-oriented utility paths
- UI managed archive path - ZIP/7z entry listing and extraction handled by
UI/Utilities/ArchiveHelper.cs(SharpCompress)
uint32_t crc = CRC32::GetCRC(data, size);
uint32_t crc = CRC32::GetCRC(filename);string hash = MD5::GetMd5Hash(data, size);
string hash = SHA1::GetSha1Hash(data, size);Cross-platform mutex wrapper with RAII pattern:
SimpleLock lock;
void ThreadSafeMethod() {
auto guard = lock.AcquireSafe();
// Protected code
} // Automatically releasedThread signaling for producer/consumer patterns:
AutoResetEvent event;
event.Wait(); // Block until signaled
event.Signal(); // Wake waiting threadSupported upscaling filters:
- HQ2x/HQ3x/HQ4x - High Quality Magnification
- Scale2x/Scale3x - Edge-preserving scaling
- xBRZ - xBR-based scaling
- Kreed SAI/Eagle - Classic filters
- NTSC - Composite video simulation
OS-specific functionality:
- File dialogs
- Clipboard access
- System paths
- Display information
UTF-8/UTF-16/UTF-32 conversion for cross-platform text handling.
Third-Party Libraries:
miniz- DEFLATE compression (embedded)spng- Simple PNG (embedded)magic_enum- Enum reflection (header-only)kissfft- FFT (header-only)
Most utilities are not thread-safe by default. Use SimpleLock for synchronization when needed. Key exceptions:
CRC32::GetCRCis thread-safeMD5/SHA1hash functions are thread-safe
- HexUtilities: Uses cached strings for byte values
- BitUtilities: Templates ensure compile-time optimization
- FastString: Stack allocation avoids heap for small strings
- Serializer: Binary format is optimized for size and speed
- CODE-DOCUMENTATION-STYLE.md - Documentation standards
- CPP-DEVELOPMENT-GUIDE.md - C++ coding standards