Skip to content

Commit

Permalink
Add MacOS support (#132)
Browse files Browse the repository at this point in the history
* Add MacOS support

* Fix data dir parsing on MacOS
  • Loading branch information
KilianSteenman committed Sep 15, 2024
1 parent 96f6b54 commit e7c1ef0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ ClientBin/
*.publishsettings
node_modules/
bower_components/
*.idea/

# RIA/Silverlight projects
Generated_Code/
Expand All @@ -243,4 +244,5 @@ UpgradeLog*.htm
# Microsoft Fakes
FakesAssemblies/


# MacOS
*.DS_Store
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ source_group("" FILES ${GTA3SC_SRC_MAIN})

target_link_libraries(gta3sc cppformat)

if(CMAKE_COMPILER_IS_GNUXX OR CMAKE_COMPILER_IS_CLANGXX)
target_link_libraries(gta3sc stdc++fs)
endif()

if(MSVC) # idk how to setup this in GCC/Clang
add_precompiled_header(gta3sc stdinc.h SOURCE_CXX src/stdinc.cpp)
endif(MSVC)
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4100.pdf
///
#pragma once
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#include <filesystem>
namespace fs = std::filesystem;
9 changes: 8 additions & 1 deletion src/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,20 @@ auto load_dat(const fs::path& filepath, bool is_default_dat) -> std::map<std::st

fs::path gamedir = filepath;
gamedir.remove_filename(); // remove gta.dat
gamedir.remove_filename(); // remove data/
gamedir = gamedir.parent_path(); // remove trailing /
gamedir = gamedir.parent_path(); // remove /data

for(const char* it = file_data.c_str();
nextline(it, buffer, std::size(buffer)); )
{
if(!strncmp(buffer, "IDE", 3))
{
for(auto& c : buffer) {
if(c == '\\') {
c = fs::path::preferred_separator;
}
}

load_ide(gamedir / (buffer+4), is_default_dat, output);
}
}
Expand Down
30 changes: 29 additions & 1 deletion src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#elif defined(__unix__)
#include <unistd.h>
#include <fcntl.h>
#elif defined(__APPLE__)
#include <sys/stat.h>
#include <sys/syslimits.h>
#include <unistd.h>
#include <mach-o/dyld.h>
#endif

static fs::path find_config_path()
Expand All @@ -22,7 +27,7 @@ static fs::path find_config_path()
return path;
}
throw std::runtime_error("find_config_path failed");
#elif defined(__unix__)
#elif defined(__unix__) || defined(__APPLE__)
const char* home_path = std::getenv("HOME");
std::vector<fs::path> search_path;

Expand All @@ -48,6 +53,20 @@ static fs::path find_config_path()
search_path.emplace_back(std::move(path));
}
}
#elif defined(__APPLE__)
{
const size_t bufSize = PATH_MAX + 1;
char exe_path[bufSize];
uint32_t size = bufSize;

ssize_t outlen = _NSGetExecutablePath(exe_path, &size);
if(outlen != -1)
{
fs::path path(exe_path);
path.replace_filename("config");
search_path.emplace_back(std::move(path));
}
}
#endif

// Home folder
Expand Down Expand Up @@ -98,6 +117,15 @@ bool allocate_file(FILE* f, uint64_t size)

#elif defined(__unix__)
return !posix_fallocate(fileno(f), 0, size);
#elif defined(__APPLE__)
struct stat st;
if (fstat(fileno(f), &st) == -1) {
return false;
}
if (ftruncate(fileno(f), size) == -1) {
return false;
}
return true;
#else
# error allocate_file not implemented for this platform.
#endif
Expand Down

0 comments on commit e7c1ef0

Please sign in to comment.