Skip to content

Instantly share code, notes, and snippets.

@tabularelf
Last active February 17, 2022 05:26
Show Gist options
  • Save tabularelf/7556fb1cd47a5e39e78375088f8b24da to your computer and use it in GitHub Desktop.
Save tabularelf/7556fb1cd47a5e39e78375088f8b24da to your computer and use it in GitHub Desktop.
Gives the user a safe writeable place
#macro FILE_SYSTEM_SUB_FOLDER_LOCATION "My Games"
function __fileSystemGetSingleton() {
static _instance = undefined;
if (is_undefined(_instance)) {
_instance = {
filePath: game_save_id,
isSandboxed: true
}
// Determine if Sandbox is enabled/disabled
var _userPath = undefined;
var _usesBackSlash = false;
switch(os_type) {
// Windows
// There will always be a Documents folder
case os_windows:
_userPath = environment_get_variable("userprofile");
_usesBackSlash = true;
break;
// Mac and Linux use the same instructions, but lead to different paths.
// "/Users/<username>" on macOS and "/home/<username>" on Ubuntu (Linux).
// As Linux amd MacOS *may* have the Documents folder as it is.
// But in the event they don't (for whatever reason), we have a check to help in those cases.
// MacOSX/Linux
case os_macosx:
case os_linux:
_userPath = environment_get_variable("HOME");
break;
}
// We use this to determine whether or not Sandbox is enabled/disabled.
if (_userPath != undefined) && (os_browser == browser_not_a_browser) {
// Attempt to create a file
var _fpath = _userPath + (_usesBackSlash ? "\\" : "/") + ".gms2fstest";
var _file = file_text_open_append(_fpath);
file_text_close(_file);
// If writing our file was successful
if file_exists(_fpath) {
// Sandbox disabled
_instance.isSandboxed = false;
// Clean up
file_delete(_fpath);
if (_usesBackSlash) {
if (directory_exists(_userPath + "\\Documents")) {
_instance.filePath =_userPath + string("\\Documents\\") + FILE_SYSTEM_SUB_FOLDER_LOCATION + "\\" + game_project_name + "\\";
}
} else {
if (directory_exists(_userPath + "/Documents")) {
_instance.filePath = _userPath + string("/Documents/") + FILE_SYSTEM_SUB_FOLDER_LOCATION + "/" + game_project_name + "/";
}
}
}
}
}
return _instance;
}
/// @func fileSystem_is_sandboxed()
function fileSystem_is_sandboxed() {
return __fileSystemGetSingleton().isSandboxed;
}
/// @func fileSystem_get_path()
function fileSystem_get_path() {
return __fileSystemGetSingleton().filePath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment