diff --git a/src/common/repository/sqlite/StellaDb.cxx b/src/common/repository/sqlite/StellaDb.cxx index 6fd87d85f..ce65f0c64 100644 --- a/src/common/repository/sqlite/StellaDb.cxx +++ b/src/common/repository/sqlite/StellaDb.cxx @@ -29,6 +29,10 @@ #include "SqliteStatement.hxx" #include "FSNode.hxx" +#ifdef BSPF_MACOS +#include "SettingsRepositoryMACOS.hxx" +#endif + namespace { constexpr Int32 CURRENT_VERSION = 1; @@ -87,22 +91,13 @@ const string StellaDb::databaseFileName() const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void StellaDb::initializeDb() const +void StellaDb::initializeDb() { - FilesystemNode legacyConfigFile{myDatabaseDirectory}; - legacyConfigFile /= LEGACY_SETTINGS_FILE; - - FilesystemNode legacyConfigDatabase{myDatabaseDirectory}; - legacyConfigDatabase /= "settings.sqlite3"; - + importOldSettings(); + FilesystemNode legacyPropertyFile{myDatabaseDirectory}; legacyPropertyFile /= "stella.pro"; - - if (legacyConfigDatabase.exists() && legacyConfigDatabase.isFile()) - importOldStellaDb(legacyConfigDatabase); - else if (legacyConfigFile.exists() && legacyConfigFile.isFile()) - importStellarc(legacyConfigFile); - + if (legacyPropertyFile.exists() && legacyPropertyFile.isFile()) importOldPropset(legacyPropertyFile); @@ -110,15 +105,37 @@ void StellaDb::initializeDb() const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void StellaDb::importStellarc(const FilesystemNode& node) const +void StellaDb::importOldSettings() { - Logger::info("importing old settings from " + node.getPath()); + #ifdef BSPF_MACOS + Logger::info("importing old settings"); + + mySettingsRepository->save(SettingsRepositoryMACOS().load()); + #else + FilesystemNode legacyConfigFile{myDatabaseDirectory}; + legacyConfigFile /= LEGACY_SETTINGS_FILE; - mySettingsRepository->save(KeyValueRepositoryConfigfile(node).load()); + FilesystemNode legacyConfigDatabase{myDatabaseDirectory}; + legacyConfigDatabase /= "settings.sqlite3"; + + if (legacyConfigDatabase.exists() && legacyConfigDatabase.isFile()) + importOldStellaDb(legacyConfigDatabase); + else if (legacyConfigFile.exists() && legacyConfigFile.isFile()) + importStellarc(legacyConfigFile); + #endif } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void StellaDb::importOldStellaDb(const FilesystemNode& node) const { +void StellaDb::importStellarc(const FilesystemNode& node) +{ + Logger::info("importing old settings from " + node.getPath()); + + mySettingsRepository->save(KeyValueRepositoryConfigfile(node).load()); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void StellaDb::importOldStellaDb(const FilesystemNode& node) +{ Logger::info("importing old settings from " + node.getPath()); try { @@ -138,7 +155,7 @@ void StellaDb::importOldStellaDb(const FilesystemNode& node) const { } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void StellaDb::importOldPropset(const FilesystemNode& node) const +void StellaDb::importOldPropset(const FilesystemNode& node) { Logger::info("importing old game properties from " + node.getPath()); @@ -169,7 +186,7 @@ void StellaDb::importOldPropset(const FilesystemNode& node) const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void StellaDb::migrate() const +void StellaDb::migrate() { Int32 version = myDb->getUserVersion(); switch (version) { diff --git a/src/common/repository/sqlite/StellaDb.hxx b/src/common/repository/sqlite/StellaDb.hxx index ce75395bc..ba0d59362 100644 --- a/src/common/repository/sqlite/StellaDb.hxx +++ b/src/common/repository/sqlite/StellaDb.hxx @@ -40,12 +40,13 @@ class StellaDb private: - void initializeDb() const; - void importStellarc(const FilesystemNode& node) const; - void importOldStellaDb(const FilesystemNode& node) const; - void importOldPropset(const FilesystemNode& node) const; + void initializeDb(); + void importOldSettings(); + void importStellarc(const FilesystemNode& node); + void importOldStellaDb(const FilesystemNode& node); + void importOldPropset(const FilesystemNode& node); - void migrate() const; + void migrate(); private: diff --git a/src/macos/SettingsRepositoryMACOS.hxx b/src/macos/SettingsRepositoryMACOS.hxx new file mode 100644 index 000000000..641aace3e --- /dev/null +++ b/src/macos/SettingsRepositoryMACOS.hxx @@ -0,0 +1,31 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2020 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#ifndef SETTINGS_REPOSITORY_MACOS_HXX +#define SETTINGS_REPOSITORY_MACOS_HXX + +#include "repository/KeyValueRepository.hxx" + +class SettingsRepositoryMACOS : public KeyValueRepository +{ + public: + std::map load() override; + + bool save(const std::map& values) override; +}; + +#endif // SETTINGS_REPOSITORY_MACOS_HXX diff --git a/src/macos/SettingsRepositoryMACOS.mm b/src/macos/SettingsRepositoryMACOS.mm new file mode 100644 index 000000000..3c8136926 --- /dev/null +++ b/src/macos/SettingsRepositoryMACOS.mm @@ -0,0 +1,57 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2020 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#import + +#include "SettingsRepositoryMACOS.hxx" + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +std::map SettingsRepositoryMACOS::load() +{ + std::map values; + + @autoreleasepool { + NSString* bundleId = [[NSBundle mainBundle] bundleIdentifier]; + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + NSArray* keys = [[defaults persistentDomainForName:bundleId] allKeys]; + + for (NSString* key in keys) { + NSString* value = [defaults stringForKey:key]; + if (value != nil) + values[[key UTF8String]] = string([value UTF8String]); + } + } + + return values; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool SettingsRepositoryMACOS::save(const std::map& values) +{ + @autoreleasepool { + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + + for (const auto& [key, value]: values) + [defaults + setObject:[NSString stringWithUTF8String:value.toCString()] + forKey:[NSString stringWithUTF8String:key.c_str()] + ]; + } + + return true; +} + diff --git a/src/macos/stella.xcodeproj/project.pbxproj b/src/macos/stella.xcodeproj/project.pbxproj index 1035878b3..a9e2aa020 100644 --- a/src/macos/stella.xcodeproj/project.pbxproj +++ b/src/macos/stella.xcodeproj/project.pbxproj @@ -741,6 +741,8 @@ E0A3841E2589741A0062AA93 /* SqliteTransaction.hxx in Headers */ = {isa = PBXBuildFile; fileRef = E0A3840C2589741A0062AA93 /* SqliteTransaction.hxx */; }; E0A755782244294600101889 /* CartCDFInfoWidget.hxx in Headers */ = {isa = PBXBuildFile; fileRef = E0A755762244294600101889 /* CartCDFInfoWidget.hxx */; }; E0A755792244294600101889 /* CartCDFInfoWidget.cxx in Sources */ = {isa = PBXBuildFile; fileRef = E0A755772244294600101889 /* CartCDFInfoWidget.cxx */; }; + E0D4153C25A120340031A8D6 /* SettingsRepositoryMACOS.hxx in Headers */ = {isa = PBXBuildFile; fileRef = E0D4153A25A120340031A8D6 /* SettingsRepositoryMACOS.hxx */; }; + E0D4153D25A120340031A8D6 /* SettingsRepositoryMACOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = E0D4153B25A120340031A8D6 /* SettingsRepositoryMACOS.mm */; }; E0DCD3A720A64E96000B614E /* LanczosResampler.hxx in Headers */ = {isa = PBXBuildFile; fileRef = E0DCD3A320A64E95000B614E /* LanczosResampler.hxx */; }; E0DCD3A820A64E96000B614E /* LanczosResampler.cxx in Sources */ = {isa = PBXBuildFile; fileRef = E0DCD3A420A64E95000B614E /* LanczosResampler.cxx */; }; E0DCD3A920A64E96000B614E /* ConvolutionBuffer.hxx in Headers */ = {isa = PBXBuildFile; fileRef = E0DCD3A520A64E96000B614E /* ConvolutionBuffer.hxx */; }; @@ -1544,6 +1546,8 @@ E0A3840C2589741A0062AA93 /* SqliteTransaction.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SqliteTransaction.hxx; sourceTree = ""; }; E0A755762244294600101889 /* CartCDFInfoWidget.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = CartCDFInfoWidget.hxx; sourceTree = ""; }; E0A755772244294600101889 /* CartCDFInfoWidget.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CartCDFInfoWidget.cxx; sourceTree = ""; }; + E0D4153A25A120340031A8D6 /* SettingsRepositoryMACOS.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SettingsRepositoryMACOS.hxx; sourceTree = ""; }; + E0D4153B25A120340031A8D6 /* SettingsRepositoryMACOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SettingsRepositoryMACOS.mm; sourceTree = ""; }; E0DCD3A320A64E95000B614E /* LanczosResampler.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = LanczosResampler.hxx; path = audio/LanczosResampler.hxx; sourceTree = ""; }; E0DCD3A420A64E95000B614E /* LanczosResampler.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LanczosResampler.cxx; path = audio/LanczosResampler.cxx; sourceTree = ""; }; E0DCD3A520A64E96000B614E /* ConvolutionBuffer.hxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ConvolutionBuffer.hxx; path = audio/ConvolutionBuffer.hxx; sourceTree = ""; }; @@ -1918,6 +1922,8 @@ 2D6050C60898771C00C6DE89 /* macos */ = { isa = PBXGroup; children = ( + E0D4153A25A120340031A8D6 /* SettingsRepositoryMACOS.hxx */, + E0D4153B25A120340031A8D6 /* SettingsRepositoryMACOS.mm */, DC21E5B921CA903E007D0E1A /* OSystemMACOS.cxx */, DC21E5BA21CA903E007D0E1A /* OSystemMACOS.hxx */, DC21E5BB21CA903E007D0E1A /* SerialPortMACOS.cxx */, @@ -2807,6 +2813,7 @@ DCC527D310B9DA19005E1287 /* M6502.hxx in Headers */, DC3EE8661E2C0E6D00905161 /* inflate.h in Headers */, DC21E5C221CA903E007D0E1A /* SerialPortMACOS.hxx in Headers */, + E0D4153C25A120340031A8D6 /* SettingsRepositoryMACOS.hxx in Headers */, DCC527D510B9DA19005E1287 /* NullDev.hxx in Headers */, DCC527D710B9DA19005E1287 /* System.hxx in Headers */, CFE3F6161E84A9CE00A8204E /* CartCDF.hxx in Headers */, @@ -3380,6 +3387,7 @@ DCAACB12188D636F00A4D282 /* CartBFWidget.cxx in Sources */, DCAACB14188D636F00A4D282 /* CartDFSCWidget.cxx in Sources */, DCAACB16188D636F00A4D282 /* CartDFWidget.cxx in Sources */, + E0D4153D25A120340031A8D6 /* SettingsRepositoryMACOS.mm in Sources */, DC368F5818A2FB710084199C /* SoundSDL2.cxx in Sources */, DCFF14CD18B0260300A20364 /* EventHandlerSDL2.cxx in Sources */, DC3EE8561E2C0E6D00905161 /* adler32.c in Sources */,