git repos / blockattack-game

blame: source/code/os.cpp

normal view · raw

1899da11 sago007 2015-12-30 19:04 1
/*
1899da11 sago007 2015-12-30 19:04 2
===========================================================================
1899da11 sago007 2015-12-30 19:04 3
blockattack - Block Attack - Rise of the Blocks
1899da11 sago007 2015-12-30 19:04 4
Copyright (C) 2005-2015 Poul Sander
1899da11 sago007 2015-12-30 19:04 5
1899da11 sago007 2015-12-30 19:04 6
This program is free software: you can redistribute it and/or modify
1899da11 sago007 2015-12-30 19:04 7
it under the terms of the GNU General Public License as published by
1899da11 sago007 2015-12-30 19:04 8
the Free Software Foundation, either version 2 of the License, or
1899da11 sago007 2015-12-30 19:04 9
(at your option) any later version.
1899da11 sago007 2015-12-30 19:04 10
1899da11 sago007 2015-12-30 19:04 11
This program is distributed in the hope that it will be useful,
1899da11 sago007 2015-12-30 19:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
1899da11 sago007 2015-12-30 19:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1899da11 sago007 2015-12-30 19:04 14
GNU General Public License for more details.
1899da11 sago007 2015-12-30 19:04 15
1899da11 sago007 2015-12-30 19:04 16
You should have received a copy of the GNU General Public License
1899da11 sago007 2015-12-30 19:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
1899da11 sago007 2015-12-30 19:04 18
1899da11 sago007 2015-12-30 19:04 19
Source information and contacts persons can be found at
b372dcb1 Poul Sander 2019-02-13 18:53 20
https://blockattack.net
1899da11 sago007 2015-12-30 19:04 21
===========================================================================
1899da11 sago007 2015-12-30 19:04 22
*/
1899da11 sago007 2015-12-30 19:04 23
1899da11 sago007 2015-12-30 19:04 24
#include "os.hpp"
9dc80982 sago007 2016-01-02 21:08 25
#include <iostream>
cd724e02 sago007 2016-02-13 16:04 26
#include <physfs.h>
77531130 Poul Sander 2016-02-17 18:42 27
#include "sago/platform_folders.h"
11a99693 Poul Sander 2018-10-20 17:16 28
#include "version.h"
d50cee1b sago007 2019-03-02 14:50 29
#include "common.h"
1899da11 sago007 2015-12-30 19:04 30
b372dcb1 Poul Sander 2019-02-13 18:53 31
#if defined(__unix__)
b372dcb1 Poul Sander 2019-02-13 18:53 32
#include <pwd.h>
b372dcb1 Poul Sander 2019-02-13 18:53 33
#include <unistd.h>
b372dcb1 Poul Sander 2019-02-13 18:53 34
#endif
b372dcb1 Poul Sander 2019-02-13 18:53 35
77531130 Poul Sander 2016-02-17 18:42 36
static sago::PlatformFolders pf;
9dc80982 sago007 2016-01-02 21:08 37
58a2d7e1 sago007 2016-02-12 18:53 38
static std::string overrideSavePath = "";
db454a2d sago007 2016-02-11 18:03 39
1899da11 sago007 2015-12-30 19:04 40
/**
1899da11 sago007 2015-12-30 19:04 41
 * Returns the path to where all settings must be saved.
a76112fc Poul Sander 2018-10-20 16:22 42
 * On unix-like systems this is the home-folder under: ~/.local/share/GAMENAME
1899da11 sago007 2015-12-30 19:04 43
 * In Windows it is My Documents/My Games
1899da11 sago007 2015-12-30 19:04 44
 * Consider changing this for Vista that has a special save games folder
1899da11 sago007 2015-12-30 19:04 45
 */
1899da11 sago007 2015-12-30 19:04 46
std::string getPathToSaveFiles() {
db454a2d sago007 2016-02-11 18:03 47
	if (overrideSavePath.length() > 0) {
db454a2d sago007 2016-02-11 18:03 48
		return overrideSavePath;
db454a2d sago007 2016-02-11 18:03 49
	}
77531130 Poul Sander 2016-02-17 18:42 50
	return pf.getSaveGamesFolder1()+"/"+GAMENAME;
1f50b970 sago007 2015-12-30 21:00 51
}
1f50b970 sago007 2015-12-30 21:00 52
db454a2d sago007 2016-02-11 18:03 53
void setPathToSaveFiles(const std::string& path) {
db454a2d sago007 2016-02-11 18:03 54
	overrideSavePath = path;
db454a2d sago007 2016-02-11 18:03 55
}
db454a2d sago007 2016-02-11 18:03 56
b372dcb1 Poul Sander 2019-02-13 18:53 57
std::string defaultPlayerName() {
b372dcb1 Poul Sander 2019-02-13 18:53 58
	std::string ret;
d50cee1b sago007 2019-03-02 14:50 59
#if defined(_WIN32)
d50cee1b sago007 2019-03-02 14:50 60
	wchar_t win_buffer[50];
d50cee1b sago007 2019-03-02 14:50 61
	DWORD win_buffer_size = sizeof(win_buffer);
d50cee1b sago007 2019-03-02 14:50 62
	bool success = GetUserNameW(win_buffer, &win_buffer_size);
d50cee1b sago007 2019-03-02 14:50 63
	if (success) {
d50cee1b sago007 2019-03-02 14:50 64
		ret = sago::internal::win32_utf16_to_utf8(win_buffer);
d50cee1b sago007 2019-03-02 14:50 65
	}
d50cee1b sago007 2019-03-02 14:50 66
#endif
d50cee1b sago007 2019-03-02 14:50 67
#if defined(__unix__)
b372dcb1 Poul Sander 2019-02-13 18:53 68
	int uid = getuid();
b372dcb1 Poul Sander 2019-02-13 18:53 69
	struct passwd* pw = getpwuid(uid);
b372dcb1 Poul Sander 2019-02-13 18:53 70
	if (pw && pw->pw_gecos) {
b372dcb1 Poul Sander 2019-02-13 18:53 71
		ret = pw->pw_gecos;
b372dcb1 Poul Sander 2019-02-13 18:53 72
		ret = ret.substr(0, ret.find_first_of(',',0));
b372dcb1 Poul Sander 2019-02-13 18:53 73
	}
b372dcb1 Poul Sander 2019-02-13 18:53 74
	if (pw && pw->pw_name && ret.empty()) {
b372dcb1 Poul Sander 2019-02-13 18:53 75
		ret = pw->pw_name;
b372dcb1 Poul Sander 2019-02-13 18:53 76
	}
d50cee1b sago007 2019-03-02 14:50 77
#endif
d50cee1b sago007 2019-03-02 14:50 78
	if (ret.empty()) {
d50cee1b sago007 2019-03-02 14:50 79
		ret = _("Player 1");
d50cee1b sago007 2019-03-02 14:50 80
	}
b372dcb1 Poul Sander 2019-02-13 18:53 81
	return ret;
b372dcb1 Poul Sander 2019-02-13 18:53 82
}
b372dcb1 Poul Sander 2019-02-13 18:53 83
77531130 Poul Sander 2016-02-17 18:42 84
void OsCreateSaveFolder() {
9dc80982 sago007 2016-01-02 21:08 85
#if defined(__unix__)
d24a5159 Poul Sander 2016-04-13 15:36 86
	std::string cmd = "mkdir -p '"+getPathToSaveFiles()+"/'";
77531130 Poul Sander 2016-02-17 18:42 87
	int retcode = system(cmd.c_str());
77531130 Poul Sander 2016-02-17 18:42 88
	if (retcode != 0) {
161a010c sago007 2016-05-06 14:00 89
		std::cerr << "Failed to create: " << getPathToSaveFiles()+"/" << "\n";
77531130 Poul Sander 2016-02-17 18:42 90
	}
9dc80982 sago007 2016-01-02 21:08 91
#elif defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 92
	//Now for Windows NT/2k/xp/2k3 etc.
fd42d602 sago007 2017-06-05 18:51 93
	CreateDirectory(pf.getSaveGamesFolder1().c_str(), nullptr);
77531130 Poul Sander 2016-02-17 18:42 94
	std::string tempA = getPathToSaveFiles();
77531130 Poul Sander 2016-02-17 18:42 95
	CreateDirectory(tempA.c_str(),nullptr);
9dc80982 sago007 2016-01-02 21:08 96
#endif
2824f1ad sago007 2016-01-09 22:34 97
}
1970-01-01 00:00 98