Skip to content

Commit

Permalink
even cooler stuff for file attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
NexIsDumb committed Jun 18, 2024
1 parent a6c5b4d commit 6715e2b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 73 deletions.
2 changes: 1 addition & 1 deletion source/funkin/backend/system/MainState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ class MainState extends FlxState {
betaWarningShown = true;
}

CoolUtil.safeSetAttribute('./.temp/', HIDDEN);
CoolUtil.safeAddAttributes('./.temp/', NativeAPI.FileAttribute.HIDDEN);
}
}
50 changes: 33 additions & 17 deletions source/funkin/backend/utils/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sys.FileSystem;
import flixel.text.FlxText;
import funkin.backend.utils.XMLUtil.TextFormat;
import flixel.util.typeLimit.OneOfTwo;
import flixel.util.typeLimit.OneOfThree;
import flixel.tweens.FlxTween;
import flixel.system.frontEnds.SoundFrontEnd;
import flixel.sound.FlxSound;
Expand Down Expand Up @@ -99,30 +100,30 @@ class CoolUtil
}

/**
* Sets an attribute 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).
* Gets file attributes from a file or a folder adding eventual missing folders in the path
* (WARNING: Only works on `windows` for now. On other platforms the attributes' value it's always going to be `0` -thanks to the wrapper you can also use `isNothing` for checking- 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
* @return The attributes through the `FileAttributeWrapper`
*/
@:noUsing public static inline function safeSetAttribute(path:String, attrib:NativeAPI.FileAttribute, useAbsol:Bool = true) {
@:noUsing public static inline function safeGetAttributes(path:String, useAbsol:Bool = true):FileAttributeWrapper {
addMissingFolders(Path.directory(path));

var result = NativeAPI.setFileAttribute(path, attrib, useAbsol);
if(result == 0) Logs.trace('Failed to set attribute to $path with a code of: $result', WARNING);
var result = NativeAPI.getFileAttributes(path, useAbsol);
if(result.isNothing) Logs.trace('The file where it has been tried to get the attributes from, might be corrupted or inexistent (code: ${result.getValue()})', WARNING);
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 attrib The attribute(s) 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
* @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) {
@:noUsing public static inline function safeSetAttributes(path:String, attrib:OneOfThree<NativeAPI.FileAttribute, FileAttributeWrapper, Int>, useAbsol:Bool = true):Int {
// yes, i'm aware that FileAttribute is also an Int so need to include it too, but at least like this we don't have to make cast sometimes while passing the arguments - Nex
addMissingFolders(Path.directory(path));

var result = NativeAPI.setFileAttributes(path, attrib, useAbsol);
Expand All @@ -131,21 +132,36 @@ class CoolUtil
}

/**
* Gets the attributes of a file or a folder adding eventual missing folders in the path
* Adds one (or more) 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(s) to add (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
* @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) {
@:noUsing public static inline function safeAddAttributes(path:String, attrib:OneOfTwo<NativeAPI.FileAttribute, Int>, useAbsol:Bool = true):Int {
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);
var result = NativeAPI.addFileAttributes(path, attrib, useAbsol);
if(result == 0) Logs.trace('Failed to add attributes to $path with a code of: $result', WARNING);
return result;
}*/
}

/**
* Removes one (or more) 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(s) to remove (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 safeRemoveAttributes(path:String, attrib:OneOfTwo<NativeAPI.FileAttribute, Int>, useAbsol:Bool = true):Int {
addMissingFolders(Path.directory(path));

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

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

import flixel.util.FlxStringUtil;
import funkin.backend.utils.NativeAPI.FileAttribute;

/**
* Currently only for Windows, but planned to work on other platforms later.
*/
class FileAttributeWrapper
{
public var flags:Int;
private var flags:Int;
inline public function getValue():Int
{
return flags;
}

public function new(flags:Int)
{
this.flags = flags;
this.flags = flags == -1 ? 0 : flags;
}

public var isArchived(get, set):Bool;
/**
* Returns a string representation of the attributes.
*/
inline public function toString():String
{
return FlxStringUtil.getDebugString([
for (field in Reflect.fields(this))
{
LabelValuePair.weak(field, Reflect.getProperty(this, field));
}
]);
}

// Settables
public var isArchived(get, set):Bool;
private function get_isArchived():Bool
{
#if windows
Expand All @@ -24,7 +42,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isArchived(value:Bool):Bool
{
#if windows
Expand All @@ -39,7 +56,6 @@ class FileAttributeWrapper
}

public var isHidden(get, set):Bool;

private function get_isHidden():Bool
{
#if windows
Expand All @@ -48,7 +64,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isHidden(value:Bool):Bool
{
#if windows
Expand All @@ -63,7 +78,6 @@ class FileAttributeWrapper
}

public var isNormal(get, set):Bool;

private function get_isNormal():Bool
{
#if windows
Expand All @@ -72,7 +86,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isNormal(value:Bool):Bool
{
#if windows
Expand All @@ -87,7 +100,6 @@ class FileAttributeWrapper
}

public var isNotContentIndexed(get, set):Bool;

private function get_isNotContentIndexed():Bool
{
#if windows
Expand All @@ -96,7 +108,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isNotContentIndexed(value:Bool):Bool
{
#if windows
Expand All @@ -111,7 +122,6 @@ class FileAttributeWrapper
}

public var isOffline(get, set):Bool;

private function get_isOffline():Bool
{
#if windows
Expand All @@ -120,7 +130,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isOffline(value:Bool):Bool
{
#if windows
Expand All @@ -135,7 +144,6 @@ class FileAttributeWrapper
}

public var isReadOnly(get, set):Bool;

private function get_isReadOnly():Bool
{
#if windows
Expand All @@ -144,7 +152,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isReadOnly(value:Bool):Bool
{
#if windows
Expand All @@ -159,7 +166,6 @@ class FileAttributeWrapper
}

public var isSystem(get, set):Bool;

private function get_isSystem():Bool
{
#if windows
Expand All @@ -168,7 +174,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isSystem(value:Bool):Bool
{
#if windows
Expand All @@ -183,7 +188,6 @@ class FileAttributeWrapper
}

public var isTemporary(get, set):Bool;

private function get_isTemporary():Bool
{
#if windows
Expand All @@ -192,7 +196,6 @@ class FileAttributeWrapper
return false;
#end
}

private function set_isTemporary(value:Bool):Bool
{
#if windows
Expand All @@ -208,7 +211,6 @@ class FileAttributeWrapper

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

private function get_isCompressed():Bool
{
#if windows
Expand All @@ -219,7 +221,6 @@ class FileAttributeWrapper
}

public var isDevice(get, never):Bool;

private function get_isDevice():Bool
{
#if windows
Expand All @@ -230,7 +231,6 @@ class FileAttributeWrapper
}

public var isDirectory(get, never):Bool;

private function get_isDirectory():Bool
{
#if windows
Expand All @@ -241,7 +241,6 @@ class FileAttributeWrapper
}

public var isEncrypted(get, never):Bool;

private function get_isEncrypted():Bool
{
#if windows
Expand All @@ -252,7 +251,6 @@ class FileAttributeWrapper
}

public var isReparsePoint(get, never):Bool;

private function get_isReparsePoint():Bool
{
#if windows
Expand All @@ -263,7 +261,6 @@ class FileAttributeWrapper
}

public var isSparseFile(get, never):Bool;

private function get_isSparseFile():Bool
{
#if windows
Expand All @@ -273,20 +270,14 @@ class FileAttributeWrapper
#end
}

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

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

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

0 comments on commit 6715e2b

Please sign in to comment.