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
1899da11 sago007 2015-12-30 19:04 20
http://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>
1899da11 sago007 2015-12-30 19:04 26
9dc80982 sago007 2016-01-02 21:08 27
#ifdef __unix__
9dc80982 sago007 2016-01-02 21:08 28
#include <pwd.h>
9dc80982 sago007 2016-01-02 21:08 29
#include <unistd.h>
9dc80982 sago007 2016-01-02 21:08 30
#include <stdexcept>
9dc80982 sago007 2016-01-02 21:08 31
9dc80982 sago007 2016-01-02 21:08 32
9dc80982 sago007 2016-01-02 21:08 33
/**
335635ec sago007 2016-02-06 13:51 34
 * Retrives the effective user's home dir.
335635ec sago007 2016-02-06 13:51 35
 * If the user is running as root we ignore the HOME environment. It works badly with sudo.
9dc80982 sago007 2016-01-02 21:08 36
 * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
335635ec sago007 2016-02-06 13:51 37
 * @return The home directory. HOME environment is respected for non-root users if it exists.
9dc80982 sago007 2016-01-02 21:08 38
 */
9dc80982 sago007 2016-01-02 21:08 39
static std::string getHome() {
9dc80982 sago007 2016-01-02 21:08 40
	std::string res;
9dc80982 sago007 2016-01-02 21:08 41
	int uid = getuid();
9dc80982 sago007 2016-01-02 21:08 42
	const char* homeEnv = getenv("HOME");
9dc80982 sago007 2016-01-02 21:08 43
	if ( uid != 0 && homeEnv) {
9dc80982 sago007 2016-01-02 21:08 44
		//We only acknowlegde HOME if not root.
9dc80982 sago007 2016-01-02 21:08 45
		res = homeEnv;
9dc80982 sago007 2016-01-02 21:08 46
		return res;
9dc80982 sago007 2016-01-02 21:08 47
	}
335635ec sago007 2016-02-06 13:51 48
	struct passwd* pw = getpwuid(uid);
9dc80982 sago007 2016-01-02 21:08 49
	if (!pw) {
9dc80982 sago007 2016-01-02 21:08 50
		throw std::runtime_error("Unable to get passwd struct.");
9dc80982 sago007 2016-01-02 21:08 51
	}
9dc80982 sago007 2016-01-02 21:08 52
	const char* tempRes = pw->pw_dir;
9dc80982 sago007 2016-01-02 21:08 53
	if (!tempRes) {
9dc80982 sago007 2016-01-02 21:08 54
		throw std::runtime_error("User has no home directory");
9dc80982 sago007 2016-01-02 21:08 55
	}
9dc80982 sago007 2016-01-02 21:08 56
	res = tempRes;
9dc80982 sago007 2016-01-02 21:08 57
	return res;
9dc80982 sago007 2016-01-02 21:08 58
}
9dc80982 sago007 2016-01-02 21:08 59
#endif
1899da11 sago007 2015-12-30 19:04 60
1899da11 sago007 2015-12-30 19:04 61
/*
1899da11 sago007 2015-12-30 19:04 62
 *Files will be saved in:
1899da11 sago007 2015-12-30 19:04 63
 * HOME/.gamesaves/"+GAMENAME (unix)
1899da11 sago007 2015-12-30 19:04 64
 *or DOCUMENTS/My Games/GAMENAME (Windows)
1899da11 sago007 2015-12-30 19:04 65
 */
1899da11 sago007 2015-12-30 19:04 66
#define GAMENAME "blockattack"
1899da11 sago007 2015-12-30 19:04 67
9dc80982 sago007 2016-01-02 21:08 68
using namespace std;
9dc80982 sago007 2016-01-02 21:08 69
1899da11 sago007 2015-12-30 19:04 70
#ifdef _WIN32
1899da11 sago007 2015-12-30 19:04 71
//Returns path to "my Documents" in windows:
1899da11 sago007 2015-12-30 19:04 72
string getMyDocumentsPath() {
9dc80982 sago007 2016-01-02 21:08 73
	char pszPath[MAX_PATH];
1899da11 sago007 2015-12-30 19:04 74
	//if (SUCCEEDED(SHGetSpecialFolderPath(nullptr, pszPath, CSIDL_PERSONAL, FALSE))) {
9dc80982 sago007 2016-01-02 21:08 75
	if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_PERSONAL, nullptr, 0, pszPath))) {
1899da11 sago007 2015-12-30 19:04 76
		// pszPath is now the path that you want
1899da11 sago007 2015-12-30 19:04 77
#if DEBUG
1899da11 sago007 2015-12-30 19:04 78
		cout << "MyDocuments Located: " << pszPath << endl;
1899da11 sago007 2015-12-30 19:04 79
#endif
1899da11 sago007 2015-12-30 19:04 80
		string theResult= pszPath;
1899da11 sago007 2015-12-30 19:04 81
		return theResult;
1899da11 sago007 2015-12-30 19:04 82
	}
1899da11 sago007 2015-12-30 19:04 83
	else {
1899da11 sago007 2015-12-30 19:04 84
		cout << "Warning: My Documents not found!" << endl;
1899da11 sago007 2015-12-30 19:04 85
		string theResult ="";
1899da11 sago007 2015-12-30 19:04 86
		return theResult;
1899da11 sago007 2015-12-30 19:04 87
	}
1899da11 sago007 2015-12-30 19:04 88
}
1899da11 sago007 2015-12-30 19:04 89
1f50b970 sago007 2015-12-30 21:00 90
1899da11 sago007 2015-12-30 19:04 91
#endif
1899da11 sago007 2015-12-30 19:04 92
58a2d7e1 sago007 2016-02-12 18:53 93
static std::string overrideSavePath = "";
db454a2d sago007 2016-02-11 18:03 94
1899da11 sago007 2015-12-30 19:04 95
/**
1899da11 sago007 2015-12-30 19:04 96
 * Returns the path to where all settings must be saved.
1899da11 sago007 2015-12-30 19:04 97
 * On unix-like systems this is the home-folder under: ~/.gamesaves/GAMENAME
1899da11 sago007 2015-12-30 19:04 98
 * In Windows it is My Documents/My Games
1899da11 sago007 2015-12-30 19:04 99
 * Consider changing this for Vista that has a special save games folder
1899da11 sago007 2015-12-30 19:04 100
 */
1899da11 sago007 2015-12-30 19:04 101
std::string getPathToSaveFiles() {
db454a2d sago007 2016-02-11 18:03 102
	if (overrideSavePath.length() > 0) {
db454a2d sago007 2016-02-11 18:03 103
		return overrideSavePath;
db454a2d sago007 2016-02-11 18:03 104
	}
1899da11 sago007 2015-12-30 19:04 105
#ifdef __unix__
1899da11 sago007 2015-12-30 19:04 106
	return (std::string)getenv("HOME")+(std::string)"/.gamesaves/"+GAMENAME;
1899da11 sago007 2015-12-30 19:04 107
#elif _WIN32
1899da11 sago007 2015-12-30 19:04 108
	return getMyDocumentsPath()+(string)"/My Games/"+GAMENAME;
1899da11 sago007 2015-12-30 19:04 109
#else
1899da11 sago007 2015-12-30 19:04 110
	return ".";
1899da11 sago007 2015-12-30 19:04 111
#endif
1f50b970 sago007 2015-12-30 21:00 112
}
1f50b970 sago007 2015-12-30 21:00 113
db454a2d sago007 2016-02-11 18:03 114
void setPathToSaveFiles(const std::string& path) {
db454a2d sago007 2016-02-11 18:03 115
	overrideSavePath = path;
db454a2d sago007 2016-02-11 18:03 116
}
db454a2d sago007 2016-02-11 18:03 117
9dc80982 sago007 2016-01-02 21:08 118
std::string getPuzzleSetSavePath() {
9dc80982 sago007 2016-01-02 21:08 119
	std::string ret;
9dc80982 sago007 2016-01-02 21:08 120
#if defined(__unix__)
9dc80982 sago007 2016-01-02 21:08 121
	ret = getHome()+"/.gamesaves/blockattack/puzzle.levels.save";
9dc80982 sago007 2016-01-02 21:08 122
#elif defined(_WIN32)
9dc80982 sago007 2016-01-02 21:08 123
	ret = getMyDocumentsPath()+"/My Games/blockattack/puzzle.levels.save";
9dc80982 sago007 2016-01-02 21:08 124
#else
9dc80982 sago007 2016-01-02 21:08 125
	ret = "puzzle.levels.save";
9dc80982 sago007 2016-01-02 21:08 126
#endif
9dc80982 sago007 2016-01-02 21:08 127
	return ret;
2824f1ad sago007 2016-01-09 22:34 128
}
2824f1ad sago007 2016-01-09 22:34 129
58a2d7e1 sago007 2016-02-12 18:53 130
static void OsCreateFolderInSaveGames(const string& path) {
58a2d7e1 sago007 2016-02-12 18:53 131
	string cmd = "mkdir -p "+getPathToSaveFiles()+"/"+path;
2824f1ad sago007 2016-01-09 22:34 132
#if defined(__unix__)
58a2d7e1 sago007 2016-02-12 18:53 133
	int retcode = system(cmd.c_str());
58a2d7e1 sago007 2016-02-12 18:53 134
	if (retcode != 0) {
58a2d7e1 sago007 2016-02-12 18:53 135
		cerr << "Failed to create: " << getPathToSaveFiles()+"/"+path << endl;
2824f1ad sago007 2016-01-09 22:34 136
	}
2824f1ad sago007 2016-01-09 22:34 137
#elif defined(_WIN32)
2824f1ad sago007 2016-01-09 22:34 138
	//Now for Windows NT/2k/xp/2k3 etc.
2824f1ad sago007 2016-01-09 22:34 139
	string tempA = getMyDocumentsPath()+"\\My Games";
2824f1ad sago007 2016-01-09 22:34 140
	CreateDirectory(tempA.c_str(),nullptr);
58a2d7e1 sago007 2016-02-12 18:53 141
	tempA += "/"+path;
2824f1ad sago007 2016-01-09 22:34 142
	CreateDirectory(tempA.c_str(),nullptr);
2824f1ad sago007 2016-01-09 22:34 143
#endif
58a2d7e1 sago007 2016-02-12 18:53 144
}
58a2d7e1 sago007 2016-02-12 18:53 145
58a2d7e1 sago007 2016-02-12 18:53 146
void OsCreateFolders() {
58a2d7e1 sago007 2016-02-12 18:53 147
	//We first create the folder there we will save (only on UNIX systems)
58a2d7e1 sago007 2016-02-12 18:53 148
	//we call the external command "mkdir"... the user might have renamed this, but we hope he hasn't
58a2d7e1 sago007 2016-02-12 18:53 149
	OsCreateFolderInSaveGames("screenshots");
58a2d7e1 sago007 2016-02-12 18:53 150
	OsCreateFolderInSaveGames("replays");
58a2d7e1 sago007 2016-02-12 18:53 151
	OsCreateFolderInSaveGames("puzzles");
1899da11 sago007 2015-12-30 19:04 152
}