Skip to content

Commit cccbcd1

Browse files
committed
- More fixes to cross-platform folder detection.
- Makefile updates and Unix/OpenBSD folder detection by iku.
1 parent 78a0e04 commit cccbcd1

2 files changed

Lines changed: 66 additions & 25 deletions

File tree

‎src/Engine/CrossPlatform.cpp‎

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,25 @@
1818
*/
1919
#include "CrossPlatform.h"
2020
#include <iostream>
21+
#include <algorithm>
2122
#include <sys/stat.h>
2223
#include "Exception.h"
2324
#include "Options.h"
2425
#ifdef _WIN32
2526
#include <windows.h>
2627
#include <shlobj.h>
2728
#include <shlwapi.h>
29+
#ifndef SHGFP_TYPE_CURRENT
30+
#define SHGFP_TYPE_CURRENT 0
31+
#endif
32+
#pragma comment(lib, "advapi32.lib")
33+
#pragma comment(lib, "shell32.lib")
2834
#pragma comment(lib, "shlwapi.lib")
2935
#else
36+
#include <stdio.h>
37+
#include <stdlib.h>
38+
#include <sys/param.h>
39+
#include <sys/types.h>
3040
#endif
3141

3242
namespace OpenXcom
@@ -44,7 +54,11 @@ namespace CrossPlatform
4454
*/
4555
std::string getDataFile(const std::string &filename)
4656
{
57+
// Correct path endings
4758
std::string newName = filename;
59+
#ifdef _WIN32
60+
std::replace(newName.begin(), newName.end(), '/', '\\');
61+
#endif
4862
std::string newPath = Options::getDataFolder() + filename;
4963

5064
// Try lowercase and uppercase names
@@ -55,17 +69,15 @@ std::string getDataFile(const std::string &filename)
5569
}
5670
else
5771
{
58-
for (std::string::iterator c = newName.begin(); c != newName.end(); ++c)
59-
*c = toupper(*c);
72+
std::transform(newName.begin(), newName.end(), newName.begin(), toupper);
6073
newPath = Options::getDataFolder() + newName;
6174
if (stat(newPath.c_str(), &info) == 0)
6275
{
6376
return newPath;
6477
}
6578
else
6679
{
67-
for (std::string::iterator c = newName.begin(); c != newName.end(); ++c)
68-
*c = tolower(*c);
80+
std::transform(newName.begin(), newName.end(), newName.begin(), tolower);
6981
newPath = Options::getDataFolder() + newName;
7082
if (stat(newPath.c_str(), &info) == 0)
7183
{
@@ -111,6 +123,16 @@ std::wstring findDataFolder()
111123
#ifdef _WIN32
112124
WCHAR path[MAX_PATH];
113125

126+
// Check in AppData folder
127+
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, path)))
128+
{
129+
PathAppendW(path, L"OpenXcom");
130+
if ((GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) != 0)
131+
{
132+
return path;
133+
}
134+
}
135+
114136
// Check in binary directory
115137
if (GetModuleFileNameW(NULL, path, MAX_PATH) != 0)
116138
{
@@ -131,33 +153,33 @@ std::wstring findDataFolder()
131153
return path;
132154
}
133155
}
134-
135-
return L"";
136156
#else
157+
char path[MAXPATHLEN];
158+
struct stat info;
137159

160+
// Check shared directory
161+
path = "/usr/share/openxcom/"
162+
if (stat(path, &info) == 0 && S_ISDIR(info.st_mode))
163+
{
164+
return path;
165+
}
166+
167+
// Check working directory
168+
path = "./DATA/";
169+
if (stat(path, &info) == 0 && S_ISDIR(info.st_mode))
170+
{
171+
return path;
172+
}
138173
#endif
174+
return L"";
139175
}
140176

141177
std::wstring findUserFolder()
142178
{
143179
#ifdef _WIN32
144180
WCHAR path[MAX_PATH];
145181

146-
#if (NTDDI_VERSION >= NTDDI_VISTA)
147-
// Check in Saved Games folder (Vista and newer)
148-
PWSTR *folder;
149-
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_SavedGames, 0, NULL, folder)))
150-
{
151-
PathAppendW(*folder, L"OpenXcom");
152-
CoTaskMemFree(folder);
153-
if ((GetFileAttributesW(*folder) & FILE_ATTRIBUTE_DIRECTORY) != 0)
154-
{
155-
return *folder;
156-
}
157-
}
158-
#endif
159-
160-
// Check in Documents folder (XP and older)
182+
// Check in Documents folder
161183
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path)))
162184
{
163185
PathAppendW(path, L"OpenXcom");
@@ -187,11 +209,29 @@ std::wstring findUserFolder()
187209
return path;
188210
}
189211
}
190-
191-
return L"";
192212
#else
213+
char path[MAXPATHLEN];
214+
struct stat info;
193215

216+
// Check HOME directory
217+
char *homedir = getenv("HOME");
218+
if (homedir)
219+
{
220+
snprintf(path, MAXPATHLEN, "%s/.openxcom/", homedir);
221+
if (stat(path, &info) == 0 && S_ISDIR(info.st_mode))
222+
{
223+
return path;
224+
}
225+
}
226+
227+
// Check working directory
228+
path = "./USER/";
229+
if (stat(path, &info) == 0 && S_ISDIR(info.st_mode))
230+
{
231+
return path;
232+
}
194233
#endif
234+
return L"";
195235
}
196236

197237
}

‎src/Makefile‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ BIN = openxcom
2020
endif
2121

2222
# Compiler settings
23-
CXXFLAGS = -Wall -O2 $(addprefix -D,$(TARGET))
23+
CXXFLAGS ?= -Wall -O2
24+
CXXFLAGS += $(addprefix -D,$(TARGET))
2425
CXXFLAGS += `$(PKG-CONFIG) --cflags sdl yaml-cpp`
2526

26-
LDFLAGS = -lSDL_gfx -lSDL_mixer
27+
LDFLAGS ?= -lSDL_gfx -lSDL_mixer
2728
LDFLAGS += `$(PKG-CONFIG) --libs sdl yaml-cpp`
2829

2930
# Rules

0 commit comments

Comments
 (0)