Skip to content

Commit a81e28d

Browse files
Copy settings and scenarios when first using a new release. (#101)
When first using a new version of Bridge Command (for example moving from 5.10 to 5.11), this will ask the user if they want to copy their existing settings and scenarios. When this has been done, any new scenarios (present in the installation but not in the user's folder) are copied in, and the same is done with any new settings. This is only carried out if the user folder for the previous version is available (so in this case 5.10->5.11).
1 parent dc182bd commit a81e28d

9 files changed

Lines changed: 197 additions & 39 deletions

File tree

bin/languageLauncher-en.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ startINIMH = "Settings: Multiplayer Hub "
1515
startDOC = "Documentation "
1616
user = "Open user folder "
1717
leave = "Exit "
18+
copy = "Copy"
19+
copyUserFolder = "Do you want to copy\nscenarios and settings\nfrom the previous version?"

bin/languageLauncher-es.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ startINIMH = "Configuración: Hub multijugador "
1515
startDOC = "Manuales "
1616
user = "Carpeta del usuario "
1717
leave = "Salir "
18+
copy = "Copiar"
19+
copyUserFolder = "¿Desea copiar los escenario\ny la configuración de la\nversión anterior?"

bin/languageLauncher-fr.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ startINIMH = "Paramètres : Hub multijoueurs "
1515
startDOC = "Documentation "
1616
user = "Ouvrir le répertoire utilisateur "
1717
leave = "Quitter "
18+
copy = "Copier"
19+
copyUserFolder = "Souhaitez-vous copier les\nscénarios et les paramètres\ndel la version précédente?"

src/Constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const irr::f32 RAD_PER_S_IN_DEG_PER_MINUTE = 180.0/PI * 60 ;
4545

4646
//general definitions
4747
const std::string LONGNAME = "Bridge Command 5.11.0-alpha.1";
48+
const std::string PREV_VERSION = "5.10";
4849
const std::string VERSION = "5.11";
4950
const std::string LONGVERSION = "5.11.0-alpha.1";
5051
#endif

src/Utilities.cpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@
4545
#include <windows.h>
4646
#include <Shellapi.h>
4747
#else // _WIN32
48-
#ifdef __APPLE__
49-
#include <copyfile.h>
50-
#include <sys/stat.h>
51-
#else
5248
#include <dirent.h>
5349
#include <sys/stat.h>
5450
#include <fstream>
55-
#endif
5651
#endif // __APPLE__
5752

5853
namespace Utilities
@@ -225,6 +220,18 @@ namespace Utilities
225220
return userFolder;
226221
}
227222

223+
std::string getPrevUserDir() {
224+
std::string userFolder = getUserDirBase();
225+
226+
if (userFolder.length() > 0) {
227+
userFolder.append(PREV_VERSION);
228+
userFolder.append("/");
229+
}
230+
231+
return userFolder;
232+
}
233+
234+
228235
bool pathExists(std::string filePath) {
229236

230237
if (filePath.empty()) {
@@ -249,6 +256,10 @@ namespace Utilities
249256
int copyDir(std::string source, std::string dest)
250257
{
251258

259+
if (source.empty() || dest.empty()) {
260+
return -1;
261+
}
262+
252263
//Copy contents of source dir into dest dir
253264

254265
#ifdef _WIN32
@@ -265,19 +276,22 @@ namespace Utilities
265276

266277
return SHFileOperation(&fileOp);
267278
#else
268-
#ifdef __APPLE__
269-
//Apple version: Requires that dest dir exists
270-
copyfile_state_t s;
271-
s = copyfile_state_alloc();
272-
//use copyfile here to do recursive copy
273-
int returnValue = copyfile(source.c_str(), dest.c_str(), s, COPYFILE_DATA | COPYFILE_RECURSIVE);
274-
copyfile_state_free(s);
275-
return returnValue;
276-
#else // __APPLE__
277-
//Other posix
278-
//Note: Not implemented yet for other posix: need to implement recursive directory copy.
279+
// Strip trailing slash if present
280+
if (dest.back() == '/') {
281+
dest.pop_back();
282+
}
283+
if (source.back() == '/') {
284+
source.pop_back();
285+
}
286+
287+
// Try to make dest dir if it doesn't exist.
288+
// Won't help if the parent doesn't exist, but will help in many cases
289+
if (!pathExists(dest)) {
290+
mkdir(dest.c_str(), 0755);
291+
}
292+
279293
//Requires that dest dir exists
280-
//std::cout << "Copying from:" << source << " to:" << dest << std::endl;
294+
std::cout << "Copying dir from:" << source << " to:" << dest << std::endl;
281295
if (!Utilities::pathExists(dest)) {
282296
return -1;
283297
}
@@ -290,7 +304,7 @@ namespace Utilities
290304
if (entry->d_type == DT_DIR && entry->d_name[0] != '.') {
291305
std::string newDir = dest;
292306

293-
newDir.append(source);
307+
//newDir.append(source);
294308
newDir.append("/");
295309
newDir.append(entry->d_name);
296310
//newDir.append("/");
@@ -303,9 +317,7 @@ namespace Utilities
303317
fromDir.append("/");
304318
fromDir.append(entry->d_name);
305319

306-
std::string toDir = dest;
307-
308-
copyDir(fromDir, toDir);
320+
copyDir(fromDir, newDir);
309321
}
310322
else {
311323
return -1;
@@ -315,7 +327,7 @@ namespace Utilities
315327
//Copy file
316328
//entry->d_name;
317329
std::string newFile = dest;
318-
newFile.append(source);
330+
//newFile.append(source);
319331
newFile.append("/");
320332
newFile.append(entry->d_name);
321333

@@ -327,6 +339,7 @@ namespace Utilities
327339

328340
std::ifstream fromStream(fromFile.c_str(), std::ios::binary);
329341
std::ofstream destStream(newFile.c_str(), std::ios::binary);
342+
std::cout << "Copying from " << fromFile << " to " << newFile << std::endl;
330343
if (fromStream && destStream) {
331344
destStream << fromStream.rdbuf();
332345
}
@@ -338,11 +351,9 @@ namespace Utilities
338351

339352
//For each file at root level, create the file and copy contents
340353

341-
342-
#endif // __APPLE__
343354
#endif // _WIN32
344355

345-
return -1;
356+
return 0;
346357
}
347358

348359
ScenarioData getScenarioDataFromFile(std::string scenarioPath, std::string scenarioName) //Read a scenario from ini files

src/Utilities.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace Utilities
4747
std::vector<std::string> split(const std::string &inputString, char delim);
4848
std::string getUserDirBase(); //Returns the directory path (absolute, with trailing slash) for a user read/writable directory, the first level folder in the user's filesystem (eg %appdata%/Bridge Command/ on windows)
4949
std::string getUserDir(); //Returns the directory path (absolute, with trailing slash) for a user read/writable directory (eg %appdata%/Bridge Command/VERSIONUMBER/ on windows)
50+
std::string getPrevUserDir(); // Returns the same a getUserDir, but for the previous program version
5051
bool pathExists(std::string filePath);
5152
int copyDir(std::string source, std::string dest);
5253
ScenarioData getScenarioDataFromFile(std::string scenarioPath, std::string scenarioName); //Read a scenario from ini files

src/editor/main.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ void checkUserScenarioDir(void)
262262

263263
if (!Utilities::pathExists(userFolder + scenarioPath)) {
264264

265-
#ifdef _WIN32
266265
std::cout << "Copying scenario files into " << userFolder + scenarioPath << std::endl;
266+
#ifdef _WIN32
267267
Utilities::copyDir("Scenarios", userFolder + scenarioPath);
268268
#else
269269
//Make sure destination folder for scenarios exists. Not needed on windows as the copy method creates the output folder and directories above it.
@@ -281,11 +281,8 @@ void checkUserScenarioDir(void)
281281
std::string pathToMake = Utilities::getUserDir() + "Scenarios";
282282
mkdir(pathToMake.c_str(),0755);
283283
}
284-
std::cout << "Copying scenario files into " << userFolder << std::endl;
285-
Utilities::copyDir("Scenarios", userFolder);
284+
Utilities::copyDir("Scenarios", userFolder + scenarioPath);
286285
#endif // __APPLE__
287-
288-
289286
}
290287
}
291288

src/iniEditor/main.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ int main (int argc, char ** argv)
216216
iniFilename = "repeater.ini";
217217
}
218218

219+
bool autoMode = false;
220+
if (((argc > 1) && (strcmp(argv[1], "-auto") == 0)) ||
221+
((argc > 2) && (strcmp(argv[2], "-auto") == 0))) {
222+
autoMode = true;
223+
}
224+
219225
//Mac OS:
220226
//Find starting folder
221227
#ifdef __APPLE__
@@ -388,16 +394,25 @@ int main (int argc, char ** argv)
388394
}
389395
}
390396

391-
//If not, find the corresponding tab, or fall back to the first tab
397+
//If not, find the corresponding tab, or add a new tab and add there
392398
if (!found) {
393399
//Add to corresponding tab
394-
int whichTab = 0;
400+
int whichTab = -1;
395401
for (int i = 0; i < iniFileStructure.size(); i++) {
396402
if (currentTabName.compare(iniFileStructure.at(i).tabName) == 0) {
397403
whichTab = i;
398404
}
399405
}
400-
iniFileStructure.at(whichTab).settings.push_back(thisEntry);
406+
if (whichTab < 0) {
407+
// Tab not found, create a new one
408+
IniFileTab newTab;
409+
newTab.tabName = currentTabName;
410+
newTab.settings.push_back(thisEntry);
411+
iniFileStructure.push_back(newTab);
412+
} else {
413+
// Found existing tab, use this
414+
iniFileStructure.at(whichTab).settings.push_back(thisEntry);
415+
}
401416
}
402417

403418
}
@@ -525,10 +540,16 @@ int main (int argc, char ** argv)
525540
Receiver receiver(device, environment, tabbedPane, iniFilename);
526541
device->setEventReceiver(&receiver);
527542

528-
while (device->run()) {
529-
driver->beginScene();
530-
device->getGUIEnvironment()->drawAll();
531-
driver->endScene();
543+
if (autoMode) {
544+
// Automatically save and close. This mode is used to ensure we have user settings file updated with any new global ini settings
545+
saveFile(device, iniFilename, tabbedPane);
546+
}
547+
else {
548+
while (device->run()) {
549+
driver->beginScene();
550+
device->getGUIEnvironment()->drawAll();
551+
driver->endScene();
552+
}
532553
}
533554
return(0);
534555
}

0 commit comments

Comments
 (0)