| 1 |
/* |
| 2 |
Copyright (C) 2009-2010 wxLauncher Team |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <wx/wx.h> |
| 20 |
#include <wx/dir.h> |
| 21 |
#include <wx/filename.h> |
| 22 |
#include <wx/wfstream.h> |
| 23 |
#include "generated/configure_launcher.h" |
| 24 |
#include "apis/PlatformProfileManager.h" |
| 25 |
#include "controls/LightingPresets.h" |
| 26 |
#include "global/ProfileKeys.h" |
| 27 |
|
| 28 |
ProMan::RegistryCodes PushCmdlineFSO(wxFileConfig *cfg) { |
| 29 |
wxString modLine, flagLine, tcPath; |
| 30 |
cfg->Read(PRO_CFG_TC_CURRENT_MODLINE, &modLine); |
| 31 |
cfg->Read(PRO_CFG_TC_CURRENT_FLAG_LINE, &flagLine); |
| 32 |
cfg->Read(PRO_CFG_TC_ROOT_FOLDER, &tcPath); |
| 33 |
|
| 34 |
wxString presetName; |
| 35 |
wxString lightingPresetFlagSet; |
| 36 |
if (cfg->Read(PRO_CFG_LIGHTING_PRESET, &presetName)) { |
| 37 |
lightingPresetFlagSet = LightingPresets::PresetNameToPresetFlagSet(presetName); |
| 38 |
} |
| 39 |
|
| 40 |
extern wxFileName GetPlatformDefaultConfigFilePath(const wxString&); |
| 41 |
wxString cmdLineString = GetPlatformDefaultConfigFilePath(tcPath).GetFullPath(); |
| 42 |
|
| 43 |
cmdLineString += _T("data"); |
| 44 |
|
| 45 |
#if IS_LINUX // try to rename file in root folder if exists |
| 46 |
wxFileName tcCfgFile(tcPath + wxFileName::GetPathSeparator()); |
| 47 |
tcCfgFile.AppendDir(_T("data")); |
| 48 |
tcCfgFile.SetFullName(_T("cmdline_fso.cfg")); |
| 49 |
|
| 50 |
if (tcCfgFile.IsOk() && ::wxFileExists(tcCfgFile.GetFullPath())) { |
| 51 |
wxFileName tcCfgRenameFile(tcCfgFile); |
| 52 |
tcCfgRenameFile.SetFullName(_T("cmdline_fso.old.cfg")); |
| 53 |
|
| 54 |
// rename target exists; attempt to delete it |
| 55 |
if (tcCfgRenameFile.IsOk() && ::wxFileExists(tcCfgRenameFile.GetFullPath())) { |
| 56 |
wxLogWarning(_T("Backup cmdline_fso.old.cfg file %s exists, deleting it"), |
| 57 |
tcCfgRenameFile.GetFullPath().c_str()); |
| 58 |
|
| 59 |
if (!::wxRemoveFile(tcCfgRenameFile.GetFullPath())) { |
| 60 |
wxLogError(_T("Could not remove backup cmdline_fso.old.cfg file %s"), |
| 61 |
tcCfgRenameFile.GetFullPath().c_str()); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// now try the rename |
| 66 |
if (!::wxRenameFile(tcCfgFile.GetFullPath(), |
| 67 |
tcCfgRenameFile.GetFullPath(), false)) { |
| 68 |
wxLogError(_T("Could not rename root folder copy %s to %s"), |
| 69 |
tcCfgFile.GetFullPath().c_str(), |
| 70 |
tcCfgRenameFile.GetFullPath().c_str()); |
| 71 |
} else { |
| 72 |
wxLogInfo(_T("Renamed root folder copy of cmdline_fso.cfg")); |
| 73 |
} |
| 74 |
} |
| 75 |
#endif |
| 76 |
|
| 77 |
// if data folder does not exist in cmdline folder, attempt to create it first |
| 78 |
if (!wxDir::Exists(cmdLineString)) { |
| 79 |
if (!::wxMkdir(cmdLineString)) { |
| 80 |
wxLogError(_T("Couldn't create 'data' folder %s"), |
| 81 |
cmdLineString.c_str()); |
| 82 |
return ProMan::UnknownError; |
| 83 |
} |
| 84 |
wxLogDebug(_T("'data' folder %s created"), |
| 85 |
cmdLineString.c_str()); |
| 86 |
} else { |
| 87 |
wxLogDebug(_T("'data' folder %s found"), |
| 88 |
cmdLineString.c_str()); |
| 89 |
} |
| 90 |
|
| 91 |
cmdLineString += wxFileName::GetPathSeparator(); |
| 92 |
cmdLineString += _T("cmdline_fso.cfg"); |
| 93 |
wxFileName cmdLineFileName(cmdLineString); |
| 94 |
wxFFileOutputStream outStream(cmdLineFileName.GetFullPath(), _T("w+b")); |
| 95 |
if ( !outStream.IsOk() ) { |
| 96 |
return ProMan::UnknownError; |
| 97 |
} |
| 98 |
if ( !modLine.IsEmpty()) { |
| 99 |
outStream.Write("-mod ", 5); |
| 100 |
|
| 101 |
// Enclose the mod parameter in quotes to escape spaces |
| 102 |
outStream.Write("\"", 1); |
| 103 |
outStream.Write(modLine.char_str(), modLine.size()); |
| 104 |
outStream.Write("\"", 1); |
| 105 |
} |
| 106 |
if ( !flagLine.IsEmpty() ) { |
| 107 |
outStream.Write(" ", 1); |
| 108 |
outStream.Write(flagLine.char_str(), flagLine.size()); |
| 109 |
} |
| 110 |
if ( !lightingPresetFlagSet.IsEmpty()) { |
| 111 |
outStream.Write(" ", 1); |
| 112 |
outStream.Write(lightingPresetFlagSet.char_str(), lightingPresetFlagSet.size()); |
| 113 |
} |
| 114 |
if ( !outStream.Close() ) { |
| 115 |
return ProMan::UnknownError; |
| 116 |
} |
| 117 |
|
| 118 |
return ProMan::NoError; |
| 119 |
} |