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
3affd2d6 Poul Sander 2020-12-27 15:56 36
#include <dirent.h>
3affd2d6 Poul Sander 2020-12-27 15:56 37
77531130 Poul Sander 2016-02-17 18:42 38
static sago::PlatformFolders pf;
9dc80982 sago007 2016-01-02 21:08 39
58a2d7e1 sago007 2016-02-12 18:53 40
static std::string overrideSavePath = "";
db454a2d sago007 2016-02-11 18:03 41
1899da11 sago007 2015-12-30 19:04 42
/**
1899da11 sago007 2015-12-30 19:04 43
 * Returns the path to where all settings must be saved.
a76112fc Poul Sander 2018-10-20 16:22 44
 * On unix-like systems this is the home-folder under: ~/.local/share/GAMENAME
1899da11 sago007 2015-12-30 19:04 45
 * In Windows it is My Documents/My Games
1899da11 sago007 2015-12-30 19:04 46
 * Consider changing this for Vista that has a special save games folder
1899da11 sago007 2015-12-30 19:04 47
 */
1899da11 sago007 2015-12-30 19:04 48
std::string getPathToSaveFiles() {
db454a2d sago007 2016-02-11 18:03 49
	if (overrideSavePath.length() > 0) {
db454a2d sago007 2016-02-11 18:03 50
		return overrideSavePath;
db454a2d sago007 2016-02-11 18:03 51
	}
77531130 Poul Sander 2016-02-17 18:42 52
	return pf.getSaveGamesFolder1()+"/"+GAMENAME;
1f50b970 sago007 2015-12-30 21:00 53
}
1f50b970 sago007 2015-12-30 21:00 54
877e52e0 Poul Sander 2022-02-15 13:21 55
std::string getPathToStateFiles() {
877e52e0 Poul Sander 2022-02-15 13:21 56
	return sago::getStateDir()+"/"+GAMENAME+"/state";
877e52e0 Poul Sander 2022-02-15 13:21 57
}
877e52e0 Poul Sander 2022-02-15 13:21 58
db454a2d sago007 2016-02-11 18:03 59
void setPathToSaveFiles(const std::string& path) {
db454a2d sago007 2016-02-11 18:03 60
	overrideSavePath = path;
db454a2d sago007 2016-02-11 18:03 61
}
db454a2d sago007 2016-02-11 18:03 62
88b5bd84 sago007 2019-03-09 14:18 63
#if defined(_WIN32)
e19eddb8 sago007 2019-05-05 16:21 64
#include "shlwapi.h"
e19eddb8 sago007 2019-05-05 16:21 65
8b477c41 sago007 2019-05-05 14:40 66
static std::wstring win32_utf8_to_utf16(const char* str) {
88b5bd84 sago007 2019-03-09 14:18 67
	std::wstring res;
88b5bd84 sago007 2019-03-09 14:18 68
	// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
88b5bd84 sago007 2019-03-09 14:18 69
	int actualSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
88b5bd84 sago007 2019-03-09 14:18 70
	if (actualSize > 0) {
88b5bd84 sago007 2019-03-09 14:18 71
		//If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it.
88b5bd84 sago007 2019-03-09 14:18 72
		std::vector<wchar_t> buffer(actualSize);
88b5bd84 sago007 2019-03-09 14:18 73
		actualSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, &buffer[0], buffer.size());
88b5bd84 sago007 2019-03-09 14:18 74
		res = buffer.data();
88b5bd84 sago007 2019-03-09 14:18 75
	}
88b5bd84 sago007 2019-03-09 14:18 76
	if (actualSize == 0) {
88b5bd84 sago007 2019-03-09 14:18 77
		// WideCharToMultiByte return 0 for errors.
88b5bd84 sago007 2019-03-09 14:18 78
		const std::string errorMsg = "UTF8 to UTF16 failed with error code: " + GetLastError();
88b5bd84 sago007 2019-03-09 14:18 79
		throw std::runtime_error(errorMsg.c_str());
88b5bd84 sago007 2019-03-09 14:18 80
	}
88b5bd84 sago007 2019-03-09 14:18 81
	return res;
88b5bd84 sago007 2019-03-09 14:18 82
}
88b5bd84 sago007 2019-03-09 14:18 83
#endif
88b5bd84 sago007 2019-03-09 14:18 84
b372dcb1 Poul Sander 2019-02-13 18:53 85
std::string defaultPlayerName() {
b372dcb1 Poul Sander 2019-02-13 18:53 86
	std::string ret;
d50cee1b sago007 2019-03-02 14:50 87
#if defined(_WIN32)
d50cee1b sago007 2019-03-02 14:50 88
	wchar_t win_buffer[50];
d50cee1b sago007 2019-03-02 14:50 89
	DWORD win_buffer_size = sizeof(win_buffer);
d50cee1b sago007 2019-03-02 14:50 90
	bool success = GetUserNameW(win_buffer, &win_buffer_size);
d50cee1b sago007 2019-03-02 14:50 91
	if (success) {
d50cee1b sago007 2019-03-02 14:50 92
		ret = sago::internal::win32_utf16_to_utf8(win_buffer);
d50cee1b sago007 2019-03-02 14:50 93
	}
d50cee1b sago007 2019-03-02 14:50 94
#endif
d50cee1b sago007 2019-03-02 14:50 95
#if defined(__unix__)
b372dcb1 Poul Sander 2019-02-13 18:53 96
	int uid = getuid();
b372dcb1 Poul Sander 2019-02-13 18:53 97
	struct passwd* pw = getpwuid(uid);
b372dcb1 Poul Sander 2019-02-13 18:53 98
	if (pw && pw->pw_gecos) {
b372dcb1 Poul Sander 2019-02-13 18:53 99
		ret = pw->pw_gecos;
b372dcb1 Poul Sander 2019-02-13 18:53 100
		ret = ret.substr(0, ret.find_first_of(',',0));
b372dcb1 Poul Sander 2019-02-13 18:53 101
	}
b372dcb1 Poul Sander 2019-02-13 18:53 102
	if (pw && pw->pw_name && ret.empty()) {
b372dcb1 Poul Sander 2019-02-13 18:53 103
		ret = pw->pw_name;
b372dcb1 Poul Sander 2019-02-13 18:53 104
	}
d50cee1b sago007 2019-03-02 14:50 105
#endif
d50cee1b sago007 2019-03-02 14:50 106
	if (ret.empty()) {
d50cee1b sago007 2019-03-02 14:50 107
		ret = _("Player 1");
d50cee1b sago007 2019-03-02 14:50 108
	}
b372dcb1 Poul Sander 2019-02-13 18:53 109
	return ret;
b372dcb1 Poul Sander 2019-02-13 18:53 110
}
b372dcb1 Poul Sander 2019-02-13 18:53 111
e19eddb8 sago007 2019-05-05 16:21 112
bool OsPathIsRelative(const std::string& path) {
f634c96f sago007 2019-05-05 15:50 113
#if defined(_WIN32)
e19eddb8 sago007 2019-05-05 16:21 114
	return PathIsRelativeW(win32_utf8_to_utf16(path.c_str()).c_str());
f634c96f sago007 2019-05-05 15:50 115
#else
f634c96f sago007 2019-05-05 15:50 116
	return path[0] != '/';
f634c96f sago007 2019-05-05 15:50 117
#endif
f634c96f sago007 2019-05-05 15:50 118
}
f634c96f sago007 2019-05-05 15:50 119
f634c96f sago007 2019-05-05 15:50 120
void OsCreateFolder(const std::string& path) {
877e52e0 Poul Sander 2022-02-15 13:21 121
	//Once all supported systems works with C++17 then we can use "std::filesystem::create_directories" instead
877e52e0 Poul Sander 2022-02-15 13:21 122
#if defined(_WIN32)
877e52e0 Poul Sander 2022-02-15 13:21 123
	//Now for Windows Vista+
877e52e0 Poul Sander 2022-02-15 13:21 124
	int retcode = SHCreateDirectoryExW(NULL, win32_utf8_to_utf16(path.c_str()).c_str(), NULL);
877e52e0 Poul Sander 2022-02-15 13:21 125
	if (retcode != ERROR_SUCCESS) {
877e52e0 Poul Sander 2022-02-15 13:21 126
		std::cerr << "Failed to create: " << path+"/" << "\n";
877e52e0 Poul Sander 2022-02-15 13:21 127
	}
877e52e0 Poul Sander 2022-02-15 13:21 128
#else
8b477c41 sago007 2019-05-05 14:40 129
	std::string cmd = "mkdir -p '"+path+"/'";
77531130 Poul Sander 2016-02-17 18:42 130
	int retcode = system(cmd.c_str());
77531130 Poul Sander 2016-02-17 18:42 131
	if (retcode != 0) {
8b477c41 sago007 2019-05-05 14:40 132
		std::cerr << "Failed to create: " << path+"/" << "\n";
77531130 Poul Sander 2016-02-17 18:42 133
	}
9dc80982 sago007 2016-01-02 21:08 134
#endif
2824f1ad sago007 2016-01-09 22:34 135
}
8b477c41 sago007 2019-05-05 14:40 136
8b477c41 sago007 2019-05-05 14:40 137
void OsCreateSaveFolder() {
8b477c41 sago007 2019-05-05 14:40 138
	std::string path = getPathToSaveFiles();
8b477c41 sago007 2019-05-05 14:40 139
	OsCreateFolder(path);
8b477c41 sago007 2019-05-05 14:40 140
}
3affd2d6 Poul Sander 2020-12-27 15:56 141
3affd2d6 Poul Sander 2020-12-27 15:56 142
std::vector<std::string> OsGetDirFileList(const std::string& path) {
3affd2d6 Poul Sander 2020-12-27 15:56 143
	//At the moment dirent.h is used for all OS. Should have a Unicode version on Windows.
3affd2d6 Poul Sander 2020-12-27 15:56 144
	std::vector<std::string> ret;
3affd2d6 Poul Sander 2020-12-27 15:56 145
	DIR* dir = opendir(path.c_str());
3affd2d6 Poul Sander 2020-12-27 15:56 146
	if (dir) {
84003a0e Poul Sander 2021-10-20 21:47 147
		struct dirent* ent;
3affd2d6 Poul Sander 2020-12-27 15:56 148
		while ((ent = readdir (dir)) ) {
3affd2d6 Poul Sander 2020-12-27 15:56 149
			if (ent->d_name[0] == '.') {
3affd2d6 Poul Sander 2020-12-27 15:56 150
				continue;
3affd2d6 Poul Sander 2020-12-27 15:56 151
			}
3affd2d6 Poul Sander 2020-12-27 15:56 152
			ret.push_back(ent->d_name);
3affd2d6 Poul Sander 2020-12-27 15:56 153
		}
3affd2d6 Poul Sander 2020-12-27 15:56 154
		closedir (dir);
3affd2d6 Poul Sander 2020-12-27 15:56 155
	}
3affd2d6 Poul Sander 2020-12-27 15:56 156
	else {
3affd2d6 Poul Sander 2020-12-27 15:56 157
		std::cerr << "Failed to list: " << path+"/" << "\n";
3affd2d6 Poul Sander 2020-12-27 15:56 158
	}
3affd2d6 Poul Sander 2020-12-27 15:56 159
	return ret;
3affd2d6 Poul Sander 2020-12-27 15:56 160
}
1970-01-01 00:00 161