Skip to content

Commit

Permalink
Update CopyState.hx
Browse files Browse the repository at this point in the history
  • Loading branch information
mcagabe19 authored Jun 23, 2024
1 parent 536e560 commit 4958d63
Showing 1 changed file with 92 additions and 89 deletions.
181 changes: 92 additions & 89 deletions source/mobile/funkin/backend/system/CopyState.hx
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package mobile.funkin.backend.system;

import funkin.backend.MusicBeatState;
#if mobile
import lime.utils.Assets as LimeAssets;
import openfl.utils.Assets as OpenflAssets;
import openfl.utils.Assets as OpenFLAssets;
import flixel.addons.util.FlxAsyncLoop;
import openfl.utils.ByteArray;
import haxe.io.Path;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.text.FlxText;
import flixel.FlxSprite;
import flixel.util.FlxColor;
import openfl.utils.ByteArray;
import haxe.io.Path;
import mobile.funkin.backend.utils.SUtil;
import funkin.backend.assets.Paths;
import funkin.backend.utils.NativeAPI;
import sys.FileSystem;
import funkin.menus.TitleState;

#if sys
import sys.io.File;
import mobile.funkin.backend.utils.SUtil;
import flixel.util.FlxColor;
#if (target.threaded)
import sys.thread.Thread;
import sys.FileSystem;
#end

using StringTools;

class CopyState extends MusicBeatState
class CopyState extends funkin.backend.MusicBeatState
{
public static var locatedFiles:Array<String> = [];
public static var maxLoopTimes:Int = 0;
public static var to:String = '';
public static final IGNORE_FOLDER_FILE_NAME:String = "ignore.txt";

public var loadingImage:FlxSprite;
public var bottomBG:FlxSprite;
Expand All @@ -34,52 +35,51 @@ class CopyState extends MusicBeatState

var loopTimes:Int = 0;
var failedFiles:Array<String> = [];
var failedFilesStack:Array<String> = [];
var canUpdate:Bool = true;
var shouldCopy:Bool = false;

static final textFilesExtensions:Array<String> = ['ini', 'txt', 'xml', 'hxs', 'hx', 'lua', 'json', 'frag', 'vert'];
private static final textFilesExtensions:Array<String> = ['ini', 'txt', 'xml', 'hxs', 'hx', 'lua', 'json', 'frag', 'vert'];

override function create()
{
locatedFiles = [];
maxLoopTimes = 0;
checkExistingFiles();
if (maxLoopTimes > 0)
{
shouldCopy = true;
NativeAPI.showMessageBox("Notice!", "Seems like you have some missing files that are necessary to run the game\nPress OK so the game can generate those files!");

add(new FlxSprite(0, 0).makeGraphic(FlxG.width, FlxG.height, 0xffcaff4d));

loadingImage = new FlxSprite(0, 0, Paths.image('funkay'));
loadingImage.setGraphicSize(0, FlxG.height);
loadingImage.updateHitbox();
loadingImage.screenCenter();
add(loadingImage);

bottomBG = new FlxSprite(0, FlxG.height - 26).makeGraphic(FlxG.width, 26, 0xFF000000);
bottomBG.alpha = 0.6;
add(bottomBG);

loadedText = new FlxText(bottomBG.x, bottomBG.y + 4, FlxG.width, '', 16);
loadedText.setFormat(Paths.font("vcr.ttf"), 16, FlxColor.WHITE, CENTER);
add(loadedText);

#if (target.threaded)
Thread.createWithEventLoop(() -> {
#end
copyLoop = new FlxAsyncLoop(maxLoopTimes, copyAsset, maxLoopTimes <= 15 ? 1 : 15);
add(copyLoop);
copyLoop.start();
#if (target.threaded)
});
#end
}
else
if (maxLoopTimes <= 0)
{
FlxG.resetGame();
FlxG.switchState(new TitleState());
return;
}

NativeAPI.showMessageBox("Notice", "Seems like you have some missing files that are necessary to run the game\nPress OK to begin the copy process");

shouldCopy = true;

add(new FlxSprite(0, 0).makeGraphic(FlxG.width, FlxG.height, 0xffcaff4d));

loadingImage = new FlxSprite(0, 0, Paths.image('funkay'));
loadingImage.setGraphicSize(0, FlxG.height);
loadingImage.updateHitbox();
loadingImage.screenCenter();
add(loadingImage);

bottomBG = new FlxSprite(0, FlxG.height - 26).makeGraphic(FlxG.width, 26, 0xFF000000);
bottomBG.alpha = 0.6;
add(bottomBG);

loadedText = new FlxText(bottomBG.x, bottomBG.y + 4, FlxG.width, '', 16);
loadedText.setFormat(Paths.font("vcr.ttf"), 16, FlxColor.WHITE, CENTER);
add(loadedText);

var ticks:Int = 15;
if (maxLoopTimes <= 15)
ticks = 1;

copyLoop = new FlxAsyncLoop(maxLoopTimes, copyAsset, ticks);
add(copyLoop);
copyLoop.start();

super.create();
}

Expand All @@ -91,17 +91,17 @@ class CopyState extends MusicBeatState
{
if (failedFiles.length > 0)
{
NativeAPI.showMessageBox('Failed To Get ${failedFiles.length} File.', failedFiles.join('\n'));
NativeAPI.showMessageBox('Failed To Copy ${failedFiles.length} File.', failedFiles.join('\n'));
if (!FileSystem.exists('logs'))
FileSystem.createDirectory('logs');
File.saveContent('logs/' + Date.now().toString().replace(' ', '-').replace(':', "'") + '-CopyState' + '.txt', failedFiles.join('\n'));
File.saveContent('logs/' + Date.now().toString().replace(' ', '-').replace(':', "'") + '-CopyState' + '.txt', failedFilesStack.join('\n'));
}
canUpdate = false;
loadedText.text = "Completed!";
FlxG.sound.play(Paths.sound('menu/confirm')).onComplete = () -> {
FlxG.resetGame();
FlxG.sound.play(Paths.sound('confirmMenu')).onComplete = () -> {
FlxG.switchState(new TitleState());
};
}

if (maxLoopTimes == 0)
loadedText.text = "Completed!";
else
Expand All @@ -113,91 +113,93 @@ class CopyState extends MusicBeatState
public function copyAsset()
{
var file = locatedFiles[loopTimes];
var toFile = Path.join([to, file]);
loopTimes++;
if (!FileSystem.exists(toFile))
if (!FileSystem.exists(file))
{
var directory = Path.directory(toFile);
var directory = Path.directory(file);
if (!FileSystem.exists(directory))
SUtil.mkDirs(directory);
try
{
if (OpenflAssets.exists(getFile(file)))
if (OpenFLAssets.exists(getFile(file)))
{
if (textFilesExtensions.contains(Path.extension(file)))
createContentFromInternal(file);
else
File.saveBytes(toFile, getFileBytes(getFile(file)));
File.saveBytes(file, getFileBytes(getFile(file)));
}
else
{
failedFiles.push(getFile(file) + " (File Dosen't Exist)");
failedFilesStack.push('Asset ${getFile(file)} does not exist.');
}
}
catch (err:Dynamic)
catch (e:haxe.Exception)
{
failedFiles.push('${getFile(file)} ($err)');
failedFiles.push('${getFile(file)} (${e.message})');
failedFilesStack.push('${getFile(file)} (${e.stack})');
}
}
}

public static function getFileBytes(file:String):ByteArray
public function createContentFromInternal(file:String)
{
var fileName = Path.withoutDirectory(file);
var directory = Path.directory(file);
try
{
var fileData:String = OpenFLAssets.getText(getFile(file));
if (fileData == null)
fileData = '';
if (!FileSystem.exists(directory))
SUtil.mkDirs(directory);
File.saveContent(Path.join([directory, fileName]), fileData);
}
catch (e:haxe.Exception)
{
failedFiles.push('${getFile(file)} (${e.message})');
failedFilesStack.push('${getFile(file)} (${e.stack})');
}
}

public function getFileBytes(file:String):ByteArray
{
switch (Path.extension(file))
{
case 'otf' | 'ttf':
return ByteArray.fromFile(file);
default:
return OpenflAssets.getBytes(file);
return OpenFLAssets.getBytes(file);
}
}

public static function getFile(file:String):String
{
if(OpenFLAssets.exists(file)) return file;

@:privateAccess
for(library in LimeAssets.libraries.keys()){
if(OpenflAssets.exists('$library:$file') && library != 'default')
if(OpenFLAssets.exists('$library:$file') && library != 'default')
return '$library:$file';
}
return file;
}

public function createContentFromInternal(file:String = 'assets/file.txt')
{
var fileName = Path.withoutDirectory(file);
var directory = Path.directory(Path.join([to, file]));
try
{
var fileData:String = OpenflAssets.getText(getFile(file));
if (fileData == null)
fileData = '';
if (!FileSystem.exists(directory))
SUtil.mkDirs(directory);
File.saveContent(Path.join([directory, fileName]), fileData);
}
catch (error:Dynamic)
{
failedFiles.push('${getFile(file)} ($error)');
}
return file;
}

public static function checkExistingFiles():Bool
{
locatedFiles = Paths.assetsTree.list(null);
locatedFiles = OpenFLAssets.list();

// removes unwanted assets
#if MOD_SUPPORT
var assets = locatedFiles.filter(folder -> folder.startsWith('assets/'));
var mods = locatedFiles.filter(folder -> folder.startsWith('mods/'));
locatedFiles = assets.concat(mods);
#else
locatedFiles = locatedFiles.filter(folder -> folder.startsWith('assets/'));
#end

var filesToRemove:Array<String> = [];

for (file in locatedFiles)
{
var toFile = Path.join([to, file]);
if (FileSystem.exists(toFile))
if (FileSystem.exists(file) || OpenFLAssets.exists(getFile(Path.join([Path.directory(getFile(file)), IGNORE_FOLDER_FILE_NAME]))))
{
filesToRemove.push(file);
}
Expand All @@ -207,7 +209,8 @@ class CopyState extends MusicBeatState
locatedFiles.remove(file);

maxLoopTimes = locatedFiles.length;
return maxLoopTimes > 0;

return (maxLoopTimes < 0);
}
}
#end

0 comments on commit 4958d63

Please sign in to comment.