Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 19869a9412597152daac567e415a51d16c13d81c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
   filesystem/filesystem.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <stdlib.h>

#include <list>

#ifdef _WIN32

#include <windows.h>
#include <shlobj.h>

#endif

#include "filesystem/filesystem.h"
#include "filesystem/diskfile.h"
#include "filesystem/file.h"
#include "sys/sys.h"

namespace filesystem 
{

SearchPath filesystem_searchpath;
std::string filesystem_basename;
std::string filesystem_modname;

std::string filesystem_homedir;
std::string filesystem_writedir;
std::string filesystem_datadir;

std::string const & writedir()
{
	return filesystem_writedir;
}

std::string const & homedir()
{
	return filesystem_homedir;
}

SearchPath & searchpath()
{
	return filesystem_searchpath;
}

void init(std::string const & basename, std::string const & modname)
{
	con_print << "^BInitializing filesystem..." << std::endl;

	// clear everything
	filesystem_searchpath.clear();
	filesystem_basename.assign(basename);
	if (!filesystem_basename.size())
		filesystem_basename.assign("base");

	filesystem_modname.assign(modname);
#ifndef _WIN32
	filesystem_homedir.assign(getenv("HOME"));
	filesystem_homedir.append("/.osirion/");
#else
	char mydocuments[512];
	memset(mydocuments, 0, sizeof(mydocuments));

	SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, mydocuments);
	filesystem_homedir.assign(mydocuments);

	if (filesystem_homedir.size()) {
		filesystem_homedir.append("\\My Games");
		if (!sys::isdirectory(filesystem_homedir))
			sys::mkdir(filesystem_homedir);

		filesystem_homedir.append("\\Osirion");
		if (!sys::isdirectory(filesystem_homedir))
			sys::mkdir(filesystem_homedir);

		filesystem_homedir.append("\\");
	} else {
		filesystem_homedir.assign("home/");
	}
#endif
	
	std::string current_datadir("data/");
	std::string package_datadir(PACKAGE_DATADIR);
	std::string dir;
	
	// set writedir depending on modname and add home paths to the searchpath
	filesystem_writedir.assign(filesystem_homedir);
	if (filesystem_modname.size()) {
		// append modname to writedir
		filesystem_writedir.append(filesystem_modname);	
		filesystem_writedir += '/';
	} else {
		// append basename to writedir
		filesystem_writedir.append(filesystem_basename);
		filesystem_writedir += '/';
	}

	sys::mkdir(filesystem_homedir);
	sys::mkdir(filesystem_writedir);

	// modname search path
	if (filesystem_modname.size()) {
		// HOME/modname
		dir.assign(filesystem_homedir + filesystem_modname);
		if (sys::isdirectory(dir)) {
			dir += '/';
			filesystem_searchpath.push_back(dir);
		}

		// CURRENT/data/modname
		dir.assign(current_datadir + filesystem_modname);
		if (sys::isdirectory(dir)) {
			dir += '/';
			filesystem_searchpath.push_back(dir);
		}

		// PACKAGE_DATADIR/modname
		std::string dir(package_datadir + '/' + filesystem_modname);
		if (sys::isdirectory(dir)) {
			dir += '/';
			filesystem_searchpath.push_back(dir);
		}
	}

	// basename search path
	// HOME/basename
	dir.assign(filesystem_homedir + filesystem_basename);
	if (sys::isdirectory(dir)) {
		dir += '/';
		filesystem_searchpath.push_back(dir);
	}

	// PACKAGE_DATADIR/basename
	dir.assign(package_datadir + '/' + filesystem_basename);
	if (sys::isdirectory(dir)) {
		dir += '/';
		filesystem_searchpath.push_back(dir);
		filesystem_datadir.assign(dir);
	} else {
		// CURRENT/data/basename
		dir.assign(current_datadir + filesystem_basename);
		if (sys::isdirectory(dir)) {
			dir += '/';
			filesystem_searchpath.push_back(dir);
			filesystem_datadir.assign(dir);
		} else {
			con_warn << "data/" << basename << " not found!" << std::endl;
		}
	}

	// print search path
	for (SearchPath::iterator path = filesystem_searchpath.begin(); path != filesystem_searchpath.end(); ++path) {
#ifdef _WIN32
		for (size_t i = 0; i < (*path).size(); i++)
			if ((*path)[i] == '/') (*path)[i] = '\\';
#endif
		con_print << "  directory " << (*path) << std::endl;
	}

	// create writedir
	con_print << "  home " << filesystem_writedir << std::endl;
}

void shutdown()
{
	con_print << "^BShutting down filesystem..." << std::endl;

	filesystem_searchpath.clear();
	filesystem_basename.clear();

	filesystem_modname.clear();
	filesystem_homedir.clear();

	filesystem_writedir.clear();
}

File *open(const char *filename) 
{
	// for now, File is always a DiskFile
	DiskFile *f = new DiskFile();
	if (!f->open(filename)) {
		delete f;
		f = 0;
		return 0;
	}
	return f;
}

void close(File *file)
{
	if (!file)
		return;

	file->close();
	delete file;
}

}