Files
CodenameEngine/source/funkin/game/cutscenes/VideoCutscene.hx
T
ItsLJcoolandGitHub a7b040152d Fixed / Refactored SysZip to actually work, and store compressed entries without uncompressing an entire zip (#761)
* well boys I fixed it. you can now have Zipped Mods!!!!!

* comment update

* quick fixes + optimizations

* fixed stream audio bug????

* found the issue ffs

* removing traces oops

* fixed videos from not being able to play. Inspired from VideoCutscene's fix for ZIP videos, and also removed that fix in favor for just fixing the issue entirely

* oop just in case no special characters that kill themself on other shit

* Implemented preloading every video (caching) when loading a ZipFolderLibrary.

Since videos have to be decompressed and be saved as a file anyways this reduces the time to do that, and what not.

* [UNTESTED] Allowed Zip Extensions for allowing any extension in the list to be passed as a `.zip`

* After some more testing loading Zip Addons works as well.

* Added the long awaited .cnemod file type!!!!! (it's just a folder with .cnemod with a .zip inside LMAO)

* Chalking up the Streamed Audio Bug to a mod issue, and also added capabilites to detect if a library is considered compressed

* Added warning to those who try to use the editor with compressed libraries

* ok instead of making it a .cnemod, the zip name will be `cnemod.zip` or what not.

* removing unnessesary traces oops

* clean Library accessor ig then

* fixed small issues and removed an idiot idea

* writing todos and fixing some context that left the room in comments

* Fixed an issue where if you had a file of 0 bytes (nothing) in a zipped library it would FUCKING CRASH the game

* Actually fixed the issue of decompressing 0 byte files

Also stopped saving the data for Folders in Zipped files, as they are 0 bytes anyways.

* Added the features that needed to exist, and removed a stupid line I saw in `ModsFolder` lol

For some reason animation offsets for characters are breaking and I can't tell if its my drive killing itself or cne so uh

* fix an issue that will force the game to crash if first ever mod being loaded is a ZipModLibrary
2026-03-15 14:24:47 +07:00

257 lines
7.0 KiB
Haxe

package funkin.game.cutscenes;
import flixel.tweens.FlxTween;
import flixel.addons.display.FlxBackdrop;
import flixel.util.FlxColor;
import flixel.util.FlxTimer;
import funkin.backend.FunkinText;
import haxe.Int64;
import haxe.io.FPHelper;
import haxe.io.Path;
import haxe.xml.Access;
#if sys
import sys.io.File;
#end
#if VIDEO_CUTSCENES
import hxvlc.flixel.FlxVideoSprite;
#end
/**
* Substate made for video cutscenes. To use it in a scripted cutscene, call `startVideo`.
*/
class VideoCutscene extends Cutscene {
private static var curVideo:Int = 0; // internal for zip videos
var path:String;
var localPath:String;
#if VIDEO_CUTSCENES
var video:FlxVideoSprite;
final mutex = new sys.thread.Mutex();
var cutsceneCamera:FlxCamera;
var text:FunkinText;
var loadingBackdrop:FlxBackdrop;
private var __loaded:Bool = false;
var bg:FlxSprite;
var subtitle:FunkinText;
public var subtitles:Array<CutsceneSubtitle> = [];
var curSubtitle:Int = 0;
#end
public function new(path:String, callback:Void->Void) {
super(callback, false);
localPath = Assets.getPath(this.path = path);
}
public override function create() {
super.create();
// TODO: get vlc to stop autoloading those goddamn subtitles (use different file ext??)
#if VIDEO_CUTSCENES
cutsceneCamera = new FlxCamera();
cutsceneCamera.bgColor = 0;
FlxG.cameras.add(cutsceneCamera, false);
parseSubtitles();
add(video = new FlxVideoSprite());
video.antialiasing = true;
#if (hxvlc < version("2.0.0"))
video.autoPause = false; // Imma handle it better inside this class, mainly because of the pause menu - Nex
#end
video.bitmap.onEndReached.add(close);
video.bitmap.onFormatSetup.add(function() if (video.bitmap != null && video.bitmap.bitmapData != null) {
final width = video.bitmap.bitmapData.width;
final height = video.bitmap.bitmapData.height;
final scale:Float = Math.min(FlxG.width / width, FlxG.height / height);
video.setGraphicSize(Std.int(width * scale), Std.int(height * scale));
video.updateHitbox();
video.screenCenter();
});
//cover = new FlxSprite(0, FlxG.height * 0.85).makeSolid(FlxG.width + 50, FlxG.height + 50, 0xFF000000);
//cover.scrollFactor.set(0, 0);
//cover.screenCenter();
//add(cover);
bg = new FlxSprite(0, FlxG.height * 0.85).makeGraphic(1, 1, 0xFF000000);
bg.alpha = 0.5;
bg.visible = false;
subtitle = new FunkinText(0, FlxG.height * 0.875, 0, "", 20);
subtitle.alignment = CENTER;
subtitle.visible = false;
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.velocity.x = 70;
loadingBackdrop.cameras = [cutsceneCamera];
add(loadingBackdrop);
loadingBackdrop.alpha = 0;
FlxTween.tween(loadingBackdrop, {alpha: 1}, 0.5, {ease: FlxEase.sineInOut});
Main.execAsync(function() {
if (video.load(localPath)) new FlxTimer().start(0.001, function(_) {
mutex.acquire(); onReady(); mutex.release();
});
else { mutex.acquire(); close(); mutex.release(); }
});
add(bg);
add(subtitle);
cameras = [cutsceneCamera];
#else
close();
#end
}
#if VIDEO_CUTSCENES
public function parseSubtitles() {
var subtitlesPath = '${Path.withoutExtension(path)}.srt';
if (Assets.exists(subtitlesPath)) {
var text = Assets.getText('$subtitlesPath').split("\n");
while(text.length > 0) {
var head = text.shift();
if (head == null || head.trim() == "")
continue; // no head (EOF or empty line), skipping
if (Std.parseInt(head) == null)
continue; // invalid index, skipping
var id = head;
var time = text.shift();
if (time == null) continue; // no time (EOF), skipping
var arrowIndex = time.indexOf('-->');
if (arrowIndex < 0) continue; // no -->, skipping
var beginTime = splitTime(time.substr(0, arrowIndex).trim());
var endTime = splitTime(time.substr(arrowIndex + 3).trim());
if (beginTime < 0 || endTime < 0) continue; // invalid timestamps
var subtitleText:Array<String> = [];
var t:String = text.shift();
while(t != null && t.trim() != "") {
subtitleText.push(t);
t = text.shift();
}
if (subtitleText.length <= 0) continue; // empty subtitle, skipping
var lastSub = subtitles.last();
if (lastSub != null && lastSub.subtitle == "" && lastSub.time >= beginTime)
subtitles.pop(); // remove last subtitle auto reset to prevent bugs
subtitles.push({
subtitle: subtitleText.join(""),
time: beginTime * 1000
});
subtitles.push({
subtitle: "",
time: endTime * 1000
});
}
}
}
public static function splitTime(str:String):Float {
if (str == null || str.trim() == "") return -1;
var multipliers:Array<Float> = [1, 60, 3600, 86400, 31536000]; // no way a cutscene will last longer than years
var timeSplit:Array<Null<Float>> = [for(e in str.split(":")) Std.parseFloat(e.replace(",", "."))];
var time:Float = 0;
for(k=>i in timeSplit) {
var mul = multipliers[timeSplit.length - 1 - k];
if (i != null)
time += i * mul;
}
return time;
}
public inline function onReady() {
trace("VideoCutscene: Ready");
FlxTween.cancelTweensOf(loadingBackdrop);
FlxTween.tween(loadingBackdrop, {alpha: 0}, 0.7, {ease: FlxEase.sineInOut, onComplete: function(_) {
loadingBackdrop.destroy();
text.destroy();
}});
pausable = __loaded = true;
video.play();
}
public override function update(elapsed:Float) {
super.update(elapsed);
if(loadingBackdrop != null) return;
@:privateAccess
var time:Int64 = video.bitmap.time;
var time:Float = FPHelper.i64ToDouble(time.low, time.high);
if(curSubtitle < subtitles.length && subtitles[curSubtitle].time < time) {
setSubtitle(subtitles[curSubtitle]);
curSubtitle++;
}
}
#if (hxvlc < version("2.0.0"))
@:dox(hide) override public function onFocus() {
if(FlxG.autoPause && !paused) video.resume();
super.onFocus();
}
@:dox(hide) override public function onFocusLost() {
if(FlxG.autoPause && !paused) video.pause();
super.onFocusLost();
}
#end
public override function pauseCutscene() {
video.pause();
super.pauseCutscene();
}
public override function onResumeCutscene(event) {
video.resume();
super.onResumeCutscene(event);
}
public override function onRestartCutscene(event) {
closeSubState();
video.bitmap.position = curSubtitle = 0;
setSubtitle({time: -1, subtitle: ""});
video.resume();
}
public function setSubtitle(sub:CutsceneSubtitle) {
if (bg.visible = subtitle.visible = (sub.subtitle.length > 0)) {
subtitle.text = sub.subtitle;
subtitle.screenCenter(X);
bg.scale.set(subtitle.width + 8, subtitle.height + 8);
bg.updateHitbox();
bg.setPosition(subtitle.x - 4, subtitle.y - 4);
}
}
public override function destroy() {
FlxG.cameras.remove(cutsceneCamera, true);
video.destroy();
super.destroy();
}
#end
}
typedef CutsceneSubtitle = {
var time:Float; // time in ms
var subtitle:String; // subtitle text
}