Skip to content

Commit

Permalink
fixin some stuff about file attribs
Browse files Browse the repository at this point in the history
Co-Authored-By: Ne_Eo <NeeEoo@users.noreply.github.com>
  • Loading branch information
NexIsDumb and NeeEoo committed Jun 17, 2024
1 parent 576448a commit a6c5b4d
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 3 deletions.
35 changes: 34 additions & 1 deletion source/funkin/backend/utils/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class CoolUtil

/**
* Sets an attribute to a file or a folder adding eventual missing folders in the path
* (WARNING: Only works on `windows`. On other platforms the return code it's always going to be `0` but still creates eventual missing folders if the platforms allows it to).
* (WARNING: Only works on `windows` for now. On other platforms the return code it's always going to be `0` but still creates eventual missing folders if the platforms allows it to).
* @param path Path to the file or folder
* @param attrib The attribute to set (WARNING: There are some non settable attributes, such as the `COMPRESSED` one)
* @param useAbsol If it should use the absolute path (By default it's `true` but if it's `false` you can use files outside from this program's directory for example)
Expand All @@ -114,6 +114,39 @@ class CoolUtil
return result;
}

/**
* Sets file attributes to a file or a folder adding eventual missing folders in the path
* (WARNING: Only works on `windows` for now. On other platforms the return code it's always going to be `0` but still creates eventual missing folders if the platforms allows it to).
* @param path Path to the file or folder
* @param attrib The attribute to set (WARNING: There are some non settable attributes, such as the `COMPRESSED` one)
* @param useAbsol If it should use the absolute path (By default it's `true` but if it's `false` you can use files outside from this program's directory for example)
* @return The result code: 0 means that it failed setting
*/
@:noUsing public static inline function safeSetAttributes(path:String, attrib:OneOfTwo<FileAttributeWrapper, Int>, useAbsol:Bool = true) {
addMissingFolders(Path.directory(path));

var result = NativeAPI.setFileAttributes(path, attrib, useAbsol);
if(result == 0) Logs.trace('Failed to set attributes to $path with a code of: $result', WARNING);
return result;
}

/**
* Gets the attributes of a file or a folder adding eventual missing folders in the path
* (WARNING: Only works on `windows` for now. On other platforms the return code it's always going to be `0` but still creates eventual missing folders if the platforms allows it to).
* @param path Path to the file or folder
* @param useAbsol If it should use the absolute path (By default it's `true` but if it's `false` you can use files outside from this program's directory for example)
* @return The result code: 0 means that it failed setting
*/
// uncomment if it crashes when using it normall
/*@:noUsing public static inline function safeGetAttribute(path:String, useAbsol:Bool = true) {
addMissingFolders(Path.directory(path));
var result = NativeAPI.getFileAttribute(path, useAbsol);
if(result == 0) Logs.trace('Failed to get attribute to $path with a code of: $result', WARNING);
return result;
}*/


/**
* Creates eventual missing folders to the specified `path`
*
Expand Down
292 changes: 292 additions & 0 deletions source/funkin/backend/utils/FileAttributeWrapper.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
package funkin.backend.utils;

import funkin.backend.utils.NativeAPI.FileAttribute;

/**
* Currently only for Windows, but planned to work on other platforms later.
*/
class FileAttributeWrapper
{
public var flags:Int;

public function new(flags:Int)
{
this.flags = flags;
}

public var isArchived(get, set):Bool;

private function get_isArchived():Bool
{
#if windows
return (flags & FileAttribute.ARCHIVE) != 0;
#else
return false;
#end
}

private function set_isArchived(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.ARCHIVE;
else
flags &= ~FileAttribute.ARCHIVE;
return value;
#else
return false;
#end
}

public var isHidden(get, set):Bool;

private function get_isHidden():Bool
{
#if windows
return (flags & FileAttribute.HIDDEN) != 0;
#else
return false;
#end
}

private function set_isHidden(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.HIDDEN;
else
flags &= ~FileAttribute.HIDDEN;
return value;
#else
return false;
#end
}

public var isNormal(get, set):Bool;

private function get_isNormal():Bool
{
#if windows
return (flags & FileAttribute.NORMAL) != 0;
#else
return false;
#end
}

private function set_isNormal(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.NORMAL;
else
flags &= ~FileAttribute.NORMAL;
return value;
#else
return false;
#end
}

public var isNotContentIndexed(get, set):Bool;

private function get_isNotContentIndexed():Bool
{
#if windows
return (flags & FileAttribute.NOT_CONTENT_INDEXED) != 0;
#else
return false;
#end
}

private function set_isNotContentIndexed(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.NOT_CONTENT_INDEXED;
else
flags &= ~FileAttribute.NOT_CONTENT_INDEXED;
return value;
#else
return false;
#end
}

public var isOffline(get, set):Bool;

private function get_isOffline():Bool
{
#if windows
return (flags & FileAttribute.OFFLINE) != 0;
#else
return false;
#end
}

private function set_isOffline(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.OFFLINE;
else
flags &= ~FileAttribute.OFFLINE;
return value;
#else
return false;
#end
}

public var isReadOnly(get, set):Bool;

private function get_isReadOnly():Bool
{
#if windows
return (flags & FileAttribute.READONLY) != 0;
#else
return false;
#end
}

private function set_isReadOnly(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.READONLY;
else
flags &= ~FileAttribute.READONLY;
return value;
#else
return false;
#end
}

public var isSystem(get, set):Bool;

private function get_isSystem():Bool
{
#if windows
return (flags & FileAttribute.SYSTEM) != 0;
#else
return false;
#end
}

private function set_isSystem(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.SYSTEM;
else
flags &= ~FileAttribute.SYSTEM;
return value;
#else
return false;
#end
}

public var isTemporary(get, set):Bool;

private function get_isTemporary():Bool
{
#if windows
return (flags & FileAttribute.TEMPORARY) != 0;
#else
return false;
#end
}

private function set_isTemporary(value:Bool):Bool
{
#if windows
if (value)
flags |= FileAttribute.TEMPORARY;
else
flags &= ~FileAttribute.TEMPORARY;
return value;
#else
return false;
#end
}

// Non Settables
public var isCompressed(get, never):Bool;

private function get_isCompressed():Bool
{
#if windows
return (flags & FileAttribute.COMPRESSED) != 0;
#else
return false;
#end
}

public var isDevice(get, never):Bool;

private function get_isDevice():Bool
{
#if windows
return (flags & FileAttribute.DEVICE) != 0;
#else
return false;
#end
}

public var isDirectory(get, never):Bool;

private function get_isDirectory():Bool
{
#if windows
return (flags & FileAttribute.DIRECTORY) != 0;
#else
return false;
#end
}

public var isEncrypted(get, never):Bool;

private function get_isEncrypted():Bool
{
#if windows
return (flags & FileAttribute.ENCRYPTED) != 0;
#else
return false;
#end
}

public var isReparsePoint(get, never):Bool;

private function get_isReparsePoint():Bool
{
#if windows
return (flags & FileAttribute.REPARSE_POINT) != 0;
#else
return false;
#end
}

public var isSparseFile(get, never):Bool;

private function get_isSparseFile():Bool
{
#if windows
return (flags & FileAttribute.SPARSE_FILE) != 0;
#else
return false;
#end
}

// For checking (mainly)
public var isNothing(get, never):Bool;

private function get_isNothing():Bool
{
#if windows
return (flags & FileAttribute.NOTHING) == 0;
#else
return false;
#end
}

public function getValue():Int
{
return flags;
}
}
Loading

0 comments on commit a6c5b4d

Please sign in to comment.