Skip to content

Commit

Permalink
resolved the attrib +h crash in a cool way B)
Browse files Browse the repository at this point in the history
okay this was very funny to add
  • Loading branch information
NexIsDumb committed Jun 7, 2024
1 parent 1fffb48 commit dc94151
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 19 deletions.
5 changes: 1 addition & 4 deletions source/funkin/backend/system/MainState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class MainState extends FlxState {
betaWarningShown = true;
}

#if sys
sys.FileSystem.createDirectory('./.temp/');
#if windows new funkin.backend.utils.native.HiddenProcess("attrib +h .temp"); #end
#end
CoolUtil.safeSetAttribute('./.temp/', HIDDEN);
}
}
39 changes: 27 additions & 12 deletions source/funkin/backend/utils/CoolUtil.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package funkin.backend.utils;

#if sys
import sys.FileSystem;
#end
import flixel.text.FlxText;
import funkin.backend.utils.XMLUtil.TextFormat;
import flixel.util.typeLimit.OneOfTwo;
Expand Down Expand Up @@ -62,18 +65,15 @@ class CoolUtil
*/
@:noUsing public static function deleteFolder(delete:String) {
#if sys
if (!sys.FileSystem.exists(delete)) return;
var files:Array<String> = sys.FileSystem.readDirectory(delete);
if (!FileSystem.exists(delete)) return;
var files:Array<String> = FileSystem.readDirectory(delete);
for(file in files) {
if (sys.FileSystem.isDirectory(delete + "/" + file)) {
if (FileSystem.isDirectory(delete + "/" + file)) {
deleteFolder(delete + "/" + file);
sys.FileSystem.deleteDirectory(delete + "/" + file);
FileSystem.deleteDirectory(delete + "/" + file);
} else {
try {
sys.FileSystem.deleteFile(delete + "/" + file);
} catch(e) {
Logs.trace("Could not delete " + delete + "/" + file, WARNING);
}
try FileSystem.deleteFile(delete + "/" + file)
catch(e) Logs.trace("Could not delete " + delete + "/" + file, WARNING);
}
}
#end
Expand All @@ -98,6 +98,22 @@ class CoolUtil
#end
}

/**
* 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).
* @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 safeSetAttribute(path:String, attrib:NativeAPI.FileAttribute, useAbsol:Bool = true) {
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);
return result;
}

/**
* Creates eventual missing folders to the specified `path`
*
Expand All @@ -113,9 +129,8 @@ class CoolUtil

for (folder in folders) {
currentPath += folder + "/";
if (!sys.FileSystem.exists(currentPath)) {
sys.FileSystem.createDirectory(currentPath);
}
if (!FileSystem.exists(currentPath))
FileSystem.createDirectory(currentPath);
}
#end
return path;
Expand Down
48 changes: 45 additions & 3 deletions source/funkin/backend/utils/NativeAPI.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ class NativeAPI {
#end
}

/**
* Gets the specified file's (or folder) attribute.
*/
public static function getFileAttribute(path:String, useAbsol:Bool = true):FileAttribute {
#if windows
if(useAbsol) path = sys.FileSystem.absolutePath(path);
return Windows.getFileAttribute(path);
#else
return null;
#end
}

/**
* Sets the specified file's (or folder) attribute. If it fails, the return value is `0`.
*/
public static function setFileAttribute(path:String, attrib:FileAttribute, useAbsol:Bool = true):Int {
#if windows
if(useAbsol) path = sys.FileSystem.absolutePath(path);
return Windows.setFileAttribute(path, attrib);
#else
return 0;
#end
}

public static function setDarkMode(title:String, enable:Bool) {
#if windows
Windows.setDarkMode(title, enable);
Expand Down Expand Up @@ -61,15 +85,13 @@ class NativeAPI {
var fg = cast(foregroundColor, Int);
var bg = cast(backgroundColor, Int);
Windows.setConsoleColors((bg * 16) + fg);
#else
#if sys
#elseif sys
Sys.print("\x1b[0m");
if(foregroundColor != NONE)
Sys.print("\x1b[" + Std.int(consoleColorToANSI(foregroundColor)) + "m");
if(backgroundColor != NONE)
Sys.print("\x1b[" + Std.int(consoleColorToANSI(backgroundColor) + 10) + "m");
#end
#end
}

public static function consoleColorToANSI(color:ConsoleColor) {
Expand Down Expand Up @@ -115,6 +137,26 @@ class NativeAPI {
}
}

enum abstract FileAttribute(Int) {
// Settables
var ARCHIVE = 0x20;
var HIDDEN = 0x2;
var NORMAL = 0x80;
var NOT_CONTENT_INDEXED = 0x2000;
var OFFLINE = 0x1000;
var READONLY = 0x1;
var SYSTEM = 0x4;
var TEMPORARY = 0x100;

// Non Settables
var COMPRESSED = 0x800;
var DEVICE = 0x40;
var DIRECTORY = 0x10;
var ENCRYPTED = 0x4000;
var REPARSE_POINT = 0x400;
var SPARSE_FILE = 0x200;
}

enum abstract ConsoleColor(Int) {
var BLACK = 0;
var DARKBLUE = 1;
Expand Down
17 changes: 17 additions & 0 deletions source/funkin/backend/utils/native/Windows.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package funkin.backend.utils.native;

#if windows
import funkin.backend.utils.NativeAPI.FileAttribute;
import funkin.backend.utils.NativeAPI.MessageBoxIcon;
@:buildXml('
<target id="haxe">
Expand Down Expand Up @@ -153,6 +154,22 @@ class Windows {
public static function allocConsole() {
}

@:functionCode('
return GetFileAttributes(path);
')
public static function getFileAttribute(path:String):FileAttribute
{
return NORMAL;
}

@:functionCode('
return SetFileAttributes(path, attrib);
')
public static function setFileAttribute(path:String, attrib:FileAttribute):Int
{
return 0;
}


@:functionCode('
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
Expand Down

0 comments on commit dc94151

Please sign in to comment.