From d9ca22bbda48e9b2c0cd7d482dc6a79ddae2da00 Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Sun, 2 Apr 2023 19:39:50 +0200 Subject: [PATCH] video fix for zips --- source/funkin/cutscenes/VideoCutscene.hx | 49 ++++++++++++++++++++++-- source/funkin/game/PlayState.hx | 2 +- source/funkin/mods/ZipFolderLibrary.hx | 8 +++- source/funkin/system/MainState.hx | 7 ++++ source/funkin/utils/CoolUtil.hx | 23 +++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/source/funkin/cutscenes/VideoCutscene.hx b/source/funkin/cutscenes/VideoCutscene.hx index ebb7ee24..c863e2fb 100644 --- a/source/funkin/cutscenes/VideoCutscene.hx +++ b/source/funkin/cutscenes/VideoCutscene.hx @@ -1,10 +1,17 @@ package funkin.cutscenes; +import flixel.addons.display.FlxBackdrop; +#if sys +import sys.io.File; +#end +import funkin.ui.FunkinText; + /** * Substate made for video cutscenes. To use it in a scripted cutscene, call `startVideo`. */ class VideoCutscene extends Cutscene { + public static var curVideo:Int = 0; var path:String; var localPath:String; @@ -13,6 +20,10 @@ class VideoCutscene extends Cutscene { var videoSprite:FlxSprite; #end var cutsceneCamera:FlxCamera; + + var text:FunkinText; + var loadingBackdrop:FlxBackdrop; + var videoReady:Bool = false; public function new(path:String, callback:Void->Void) { super(callback); @@ -35,9 +46,31 @@ class VideoCutscene extends Cutscene { videoSprite = new FlxSprite(); videoSprite.cameras = [cutsceneCamera]; videoSprite.antialiasing = true; - add(videoSprite); - video.playMP4(localPath, false, videoSprite); + if (localPath.startsWith("[ZIP]")) { + text = new FunkinText(10, 10, Std.int(FlxG.width / 2), "Loading video..."); + text.cameras = [cutsceneCamera]; + text.visible = false; + @:privateAccess + text.regenGraphic(); + add(text); + + loadingBackdrop = new FlxBackdrop(text.graphic, X); + loadingBackdrop.y = FlxG.height - 20 - loadingBackdrop.height; + loadingBackdrop.cameras = [cutsceneCamera]; + add(loadingBackdrop); + + // ZIP PATH: EXPORT + // TODO: this but better and more ram friendly + localPath = './.temp/video-${curVideo++}.mp4'; + Main.execAsync(function() { + File.saveBytes(localPath, Assets.getBytes(path)); + videoReady = true; + }); + } else { + video.playMP4(localPath, false, videoSprite); + } + add(videoSprite); #end cameras = [cutsceneCamera]; @@ -45,7 +78,17 @@ class VideoCutscene extends Cutscene { public override function update(elapsed:Float) { super.update(elapsed); - #if !VIDEO_CUTSCENES + #if VIDEO_CUTSCENES + if (videoReady) { + videoReady = false; + video.playMP4(localPath, false, videoSprite); + if (loadingBackdrop != null) + loadingBackdrop.visible = false; + } + if (loadingBackdrop != null) { + loadingBackdrop.x -= elapsed * FlxG.width * 0.5; + } + #else close(); #end } diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index 82a63791..9ac25ee6 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -721,7 +721,7 @@ class PlayState extends MusicBeatState var videoCutsceneAlt = Paths.file('songs/${PlayState.SONG.meta.name.toLowerCase()}/${prefix}cutscene.mp4'); var dialogue = Paths.file('songs/${PlayState.SONG.meta.name.toLowerCase()}/${prefix}dialogue.xml'); persistentUpdate = true; - if (cutsceneScriptPath != null) { + if (cutsceneScriptPath != null && Assets.exists(cutsceneScriptPath)) { openSubState(new ScriptedCutscene(cutsceneScriptPath, function() { callback(); })); diff --git a/source/funkin/mods/ZipFolderLibrary.hx b/source/funkin/mods/ZipFolderLibrary.hx index ffef98cc..8c8e7bd3 100644 --- a/source/funkin/mods/ZipFolderLibrary.hx +++ b/source/funkin/mods/ZipFolderLibrary.hx @@ -65,6 +65,11 @@ class ZipFolderLibrary extends AssetLibrary implements ModsAssetLibrary { return Image.fromBytes(unzip(assets[_parsedAsset])); } + public override function getPath(id:String):String { + if (!__parseAsset(id)) return null; + return getAssetPath(); + } + public inline function unzip(f:SysZipEntry) @@ -88,7 +93,8 @@ class ZipFolderLibrary extends AssetLibrary implements ModsAssetLibrary { } private function getAssetPath() { - return _parsedAsset; + trace('[ZIP]$zipPath/$_parsedAsset'); + return '[ZIP]$zipPath/$_parsedAsset'; } public function getFiles(folder:String):Array { diff --git a/source/funkin/system/MainState.hx b/source/funkin/system/MainState.hx index e55c3fe7..3b5e1999 100644 --- a/source/funkin/system/MainState.hx +++ b/source/funkin/system/MainState.hx @@ -18,6 +18,9 @@ class MainState extends FlxState { Main.loadGameSettings(); initiated = true; + #if sys + CoolUtil.deleteFolder('./.temp/'); // delete temp folder + #end Options.save(); FlxG.bitmap.reset(); @@ -46,5 +49,9 @@ class MainState extends FlxState { FlxG.switchState(new BetaWarningState()); betaWarningShown = true; } + + #if sys + sys.FileSystem.createDirectory('./.temp/'); + #end } } \ No newline at end of file diff --git a/source/funkin/utils/CoolUtil.hx b/source/funkin/utils/CoolUtil.hx index 0e4953b9..688221c9 100644 --- a/source/funkin/utils/CoolUtil.hx +++ b/source/funkin/utils/CoolUtil.hx @@ -50,6 +50,29 @@ class CoolUtil return Json.parse(Assets.getText(assetPath)); } + /** + * Deletes a folder recursively + * @param delete Path to the folder. + */ + public static function deleteFolder(delete:String) { + #if sys + if (!sys.FileSystem.exists(delete)) return; + var files:Array = sys.FileSystem.readDirectory(delete); + for(file in files) { + if (sys.FileSystem.isDirectory(delete + "/" + file)) { + deleteFolder(delete + "/" + file); + sys.FileSystem.deleteDirectory(delete + "/" + file); + } else { + try { + sys.FileSystem.deleteFile(delete + "/" + file); + } catch(e) { + Logs.trace("Could not delete " + delete + "/" + file, WARNING); + } + } + } + #end + } + /** * Shortcut to parse a JSON string * @param str Path to the JSON string