From 2efd94d099a0f8599eef07146c16f7795af969f6 Mon Sep 17 00:00:00 2001
From: Stephen Anthony <sa666666@gmail.com>
Date: Wed, 8 Apr 2020 11:59:02 -0230
Subject: [PATCH] Fix 'saveconfig' not saving file correctly (fixes #602).

---
 Changes.txt                |  2 ++
 src/debugger/CartDebug.cxx | 22 ++++++++++++----------
 src/debugger/CartDebug.hxx |  2 +-
 src/emucore/OSystem.cxx    |  4 ++++
 src/emucore/OSystem.hxx    |  8 ++++++++
 5 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/Changes.txt b/Changes.txt
index 2de2987ee..60fd29f4e 100644
--- a/Changes.txt
+++ b/Changes.txt
@@ -28,6 +28,8 @@
 
   * Added detection of color and audio data in DiStella.
 
+  * Restored 'cfg' directory for Distella config files.
+
 -Have fun!
 
 
diff --git a/src/debugger/CartDebug.cxx b/src/debugger/CartDebug.cxx
index 612f51542..a52732cfe 100644
--- a/src/debugger/CartDebug.cxx
+++ b/src/debugger/CartDebug.cxx
@@ -843,12 +843,13 @@ string CartDebug::loadSymbolFile()
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 string CartDebug::loadConfigFile()
 {
-  // The default naming/location for config files is the ROM dir based on the
-  // actual ROM filename
+  // The default naming/location for config files is the CFG dir and based
+  // on the actual ROM filename
 
   if(myCfgFile == "")
   {
-    FilesystemNode cfg(myOSystem.romFile().getPathWithExt("") + ".cfg");
+    FilesystemNode romNode(myOSystem.romFile().getPathWithExt("") + ".cfg");
+    FilesystemNode cfg(myOSystem.cfgDir() + romNode.getName());
     if(cfg.isFile() && cfg.isReadable())
       myCfgFile = cfg.getPath();
     else
@@ -964,14 +965,14 @@ string CartDebug::loadConfigFile()
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 string CartDebug::saveConfigFile()
 {
-  // The default naming/location for config files is the ROM dir based on the
-  // actual ROM filename
+  // The default naming/location for config files is the CFG dir and based
+  // on the actual ROM filename
 
-  FilesystemNode cfg;
   if(myCfgFile == "")
   {
-    cfg = FilesystemNode(myOSystem.romFile().getPathWithExt("") + ".cfg");
-    if(cfg.isFile() && cfg.isWritable())
+    FilesystemNode romNode(myOSystem.romFile().getPathWithExt("") + ".cfg");
+    FilesystemNode cfg(myOSystem.cfgDir() + romNode.getName());
+    if(cfg.getParent().isWritable())
       myCfgFile = cfg.getPath();
     else
       return DebuggerParser::red("config file \'" + cfg.getShortPath() + "\' not writable");
@@ -980,13 +981,14 @@ string CartDebug::saveConfigFile()
   const string& name = myConsole.properties().get(PropType::Cart_Name);
   const string& md5 = myConsole.properties().get(PropType::Cart_MD5);
 
+  FilesystemNode cfg(myCfgFile);
   ofstream out(cfg.getPath());
   if(!out.is_open())
     return "Unable to save directives to " + cfg.getShortPath();
 
   // Store all bank information
-  out << "//Stella.pro: \"" << name << "\"" << endl
-      << "//MD5: " << md5 << endl
+  out << "// Stella.pro: \"" << name << "\"" << endl
+      << "// MD5: " << md5 << endl
       << endl;
   for(uInt32 b = 0; b < myConsole.cartridge().bankCount(); ++b)
   {
diff --git a/src/debugger/CartDebug.hxx b/src/debugger/CartDebug.hxx
index 7fff9c8a6..c2bd03cae 100644
--- a/src/debugger/CartDebug.hxx
+++ b/src/debugger/CartDebug.hxx
@@ -343,7 +343,7 @@ class CartDebug : public DebuggerSystem
     uInt16 myLabelLength{8};  // longest pre-defined label
 
     // Filenames to use for various I/O (currently these are hardcoded)
-    string myListFile, mySymbolFile, myCfgFile, myDisasmFile, myRomFile;
+    string myListFile, mySymbolFile, myCfgFile, myDisasmFile;
 
     /// Table of instruction mnemonics
     static std::array<const char*, 16>  ourTIAMnemonicR; // read mode
diff --git a/src/emucore/OSystem.cxx b/src/emucore/OSystem.cxx
index 29359d6ba..832b1ce35 100644
--- a/src/emucore/OSystem.cxx
+++ b/src/emucore/OSystem.cxx
@@ -268,6 +268,9 @@ void OSystem::setConfigPaths()
 
   buildDirIfRequired(myStateDir, myBaseDir + "state");
   buildDirIfRequired(myNVRamDir, myBaseDir + "nvram");
+#ifdef DEBUGGER_SUPPORT
+  buildDirIfRequired(myCfgDir, myBaseDir + "cfg");
+#endif
 
 #ifdef PNG_SUPPORT
   mySnapshotSaveDir = mySettings->getString("snapsavedir");
@@ -292,6 +295,7 @@ void OSystem::setConfigPaths()
   dbgPath("base dir  ", myBaseDir);
   dbgPath("state dir ", myStateDir);
   dbgPath("nvram dir ", myNVRamDir);
+  dbgPath("cfg dir   ", myCfgDir);
   dbgPath("ssave dir ", mySnapshotSaveDir);
   dbgPath("sload dir ", mySnapshotLoadDir);
   dbgPath("cheat file", myCheatFile);
diff --git a/src/emucore/OSystem.hxx b/src/emucore/OSystem.hxx
index 123bc92cb..b23cbdb83 100644
--- a/src/emucore/OSystem.hxx
+++ b/src/emucore/OSystem.hxx
@@ -270,6 +270,13 @@ class OSystem
     const string& cheatFile() const { return myCheatFile; }
   #endif
 
+  #ifdef DEBUGGER_SUPPORT
+    /**
+      Return the full/complete directory name for storing Distella cfg files.
+    */
+    const string& cfgDir() const { return myCfgDir; }
+  #endif
+
   #ifdef PNG_SUPPORT
     /**
       Return the full/complete directory name for saving and loading
@@ -529,6 +536,7 @@ class OSystem
     string mySnapshotSaveDir;
     string mySnapshotLoadDir;
     string myNVRamDir;
+    string myCfgDir;
     string myDefaultSaveDir;
     string myDefaultLoadDir;