Skip to content

Commit

Permalink
update SUtil.hx
Browse files Browse the repository at this point in the history
  • Loading branch information
Dxgamer7405 committed Oct 1, 2022
1 parent 88335e1 commit e1d723a
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions source/SUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package;
#if android
import android.Hardware;
import android.Permissions;
import android.os.Build.VERSION;
import android.os.Build;
import android.os.Environment;
#end
import flash.system.System;
import flixel.FlxG;
import haxe.CallStack.StackItem;
import haxe.CallStack;
import haxe.io.Path;
import lime.app.Application;
import openfl.Lib;
import openfl.events.UncaughtErrorEvent;
import openfl.utils.Assets;
#if (sys && !ios)
import sys.FileSystem;
import sys.io.File;
#end

using StringTools;

Expand All @@ -37,18 +38,18 @@ class SUtil
{
if (VERSION.SDK_INT >= VERSION_CODES.M)
{
Permissions.requestPermissions([PermissionsList.WRITE_EXTERNAL_STORAGE, Permissions.READ_EXTERNAL_STORAGE]);
Permissions.requestPermissions([Permissions.WRITE_EXTERNAL_STORAGE, Permissions.READ_EXTERNAL_STORAGE]);

/**
* Basically for now i can't force the app to stop while its requesting a android permission, so this makes the app to stop while its requesting the specific permission
*/
Application.current.window.alert('If you accepted the permissions you are all good!' + "\nIf you didn't then expect a crash"
Lib.application.window.alert('If you accepted the permissions you are all good!' + "\nIf you didn't then expect a crash"
+ 'Press Ok to see what happens',
'Permissions?');
}
else
{
Application.current.window.alert('Please grant the game storage permissions in app settings' + '\nPress Ok to close the app', 'Permissions?');
Lib.application.window.alert('Please grant the game storage permissions in app settings' + '\nPress Ok to close the app', 'Permissions?');
System.exit(1);
}
}
Expand All @@ -61,44 +62,44 @@ class SUtil

if (!FileSystem.exists(SUtil.getPath() + 'assets') && !FileSystem.exists(SUtil.getPath() + 'mods'))
{
Application.current.window.alert("Whoops, seems like you didn't extract the files from the .APK!\nPlease watch the tutorial by pressing OK.",
Lib.application.window.alert("Whoops, seems like you didn't extract the files from the .APK!\nPlease watch the tutorial by pressing OK.",
'Error!');
FlxG.openURL('https://youtu.be/zjvkTmdWvfU');
System.exit(1);
}
else if ((FileSystem.exists(SUtil.getPath() + 'assets') && !FileSystem.isDirectory(SUtil.getPath() + 'assets'))
&& (FileSystem.exists(SUtil.getPath() + 'mods') && !FileSystem.isDirectory(SUtil.getPath() + 'mods')))
{
Application.current.window.alert("Why did you create two files called assets and mods instead of copying the folders from the .APK?, expect a crash.",
Lib.application.window.alert("Why did you create two files called assets and mods instead of copying the folders from the .APK?, expect a crash.",
'Error!');
System.exit(1);
}
else
{
if (!FileSystem.exists(SUtil.getPath() + 'assets'))
{
Application.current.window.alert("Whoops, seems like you didn't extract the assets/assets folder from the .APK!\nPlease watch the tutorial by pressing OK.",
Lib.application.window.alert("Whoops, seems like you didn't extract the assets/assets folder from the .APK!\nPlease watch the tutorial by pressing OK.",
'Error!');
FlxG.openURL('https://youtu.be/zjvkTmdWvfU');
System.exit(1);
}
else if (FileSystem.exists(SUtil.getPath() + 'assets') && !FileSystem.isDirectory(SUtil.getPath() + 'assets'))
{
Application.current.window.alert("Why did you create a file called assets instead of copying the assets directory from the .APK?, expect a crash.",
Lib.application.window.alert("Why did you create a file called assets instead of copying the assets directory from the .APK?, expect a crash.",
'Error!');
System.exit(1);
}

if (!FileSystem.exists(SUtil.getPath() + 'mods'))
{
Application.current.window.alert("Whoops, seems like you didn't extract the assets/mods folder from the .APK!\nPlease watch the tutorial by pressing OK.",
Lib.application.window.alert("Whoops, seems like you didn't extract the assets/mods folder from the .APK!\nPlease watch the tutorial by pressing OK.",
'Error!');
FlxG.openURL('https://youtu.be/zjvkTmdWvfU');
System.exit(1);
}
else if (FileSystem.exists(SUtil.getPath() + 'mods') && !FileSystem.isDirectory(SUtil.getPath() + 'mods'))
{
Application.current.window.alert("Why did you create a file called mods instead of copying the mods directory from the .APK?, expect a crash.",
Lib.application.window.alert("Why did you create a file called mods instead of copying the mods directory from the .APK?, expect a crash.",
'Error!');
System.exit(1);
}
Expand All @@ -110,14 +111,8 @@ class SUtil
/**
* This returns the external storage path that the game will use
*/
public static function getPath():String
{
#if android
return Environment.getExternalStorageDirectory() + '/' + '.' + Application.current.meta.get('file') + '/';
#else
return '';
#end
}
public static function getPath():String #if android return Environment.getExternalStorageDirectory() + '/' + '.' + Lib.application.meta.get('file') +
'/'; #else return ''; #end

/**
* Uncaught error handler, original made by: sqirra-rng
Expand Down Expand Up @@ -148,14 +143,15 @@ class SUtil

errMsg += u.error;

#if (sys && !ios)
try
{
if (!FileSystem.exists(SUtil.getPath() + 'logs'))
FileSystem.createDirectory(SUtil.getPath() + 'logs');

File.saveContent(SUtil.getPath()
+ 'logs/'
+ Application.current.meta.get('file')
+ Lib.application.meta.get('file')
+ '-'
+ Date.now().toString().replace(' ', '-').replace(':', "'")
+ '.log',
Expand All @@ -166,14 +162,15 @@ class SUtil
catch (e:Dynamic)
Hardware.toast("Error!\nClouldn't save the crash dump because:\n" + e, ToastType.LENGTH_LONG);
#end
#end

Sys.println(errMsg);
Application.current.window.alert(errMsg, 'Error!');

println(errMsg);
Lib.application.window.alert(errMsg, 'Error!');
System.exit(1);
});
}

#if (sys && !ios)
public static function saveContent(fileName:String = 'file', fileExtension:String = '.json',
fileData:String = 'you forgot to add something in your code lol')
{
Expand All @@ -183,7 +180,9 @@ class SUtil
FileSystem.createDirectory(SUtil.getPath() + 'saves');

File.saveContent(SUtil.getPath() + 'saves/' + fileName + fileExtension, fileData);
#if android
Hardware.toast("File Saved Successfully!", ToastType.LENGTH_LONG);
#end
}
#if android
catch (e:Dynamic)
Expand All @@ -203,4 +202,15 @@ class SUtil
Hardware.toast("Error!\nClouldn't copy the file because:\n" + e, ToastType.LENGTH_LONG);
#end
}
}
#end

private static function println(msg:String):Void
{
#if sys
Sys.println(msg);
#else
// Pass null to exclude the position.
haxe.Log.trace(msg, null);
#end
}
}

0 comments on commit e1d723a

Please sign in to comment.