Skip to content

Commit 0e5fa6c

Browse files
committed
e
1 parent 455642d commit 0e5fa6c

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

source/backend/MusicBeatState.hx

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package backend;
22

3+
import haxe.ds.HashMap;
34
import backend.window.CppAPI;
45
import flixel.FlxState;
56
import backend.PsychCamera;
@@ -310,24 +311,49 @@ class MusicBeatState extends FlxState
310311

311312
}
312313
}
314+
var preloadFunctions:Map<String, (FlxState)->Void> = [
315+
"PlayState" => function(state:FlxState) {
316+
if (state is PlayState) {
317+
@:privateAccess
318+
(cast state:PlayState).generateSong();
319+
}
320+
}
321+
];
313322

314-
var preloadFunction:()->Void = null;
323+
public function hashCode():Int
324+
{
325+
return Type.getClassName(Type.getClass(this)).hashcode();
326+
}
315327

316-
public function preloadState(switchState:Bool = false):Void
328+
public function preloadState(switchState:Bool = false, state:FlxState, ?proceedOnError:Bool = true)
317329
{
330+
var stateClassName = Type.getClassName(Type.getClass(state)).split(".")[Lambda.count(Type.getClassName(Type.getClass(state)).split(".")) - 1];
331+
var preloadFunction = preloadFunctions.get(stateClassName);
332+
var errored = false;
333+
if (preloadFunction == null)
334+
{
335+
trace('No preload function for state: ' + stateClassName);
336+
}
318337
var preloader = function()
319338
{
320339
if (preloadFunction != null)
321340
{
322-
preloadFunction();
341+
trace('Preloading state: ' + stateClassName);
342+
try {
343+
preloadFunction(state);
344+
} catch (e:Dynamic) {
345+
trace('Error during state preloading: ' + e);
346+
}
323347
preloadFunction = null;
324348
}
325-
if (switchState)
349+
if (switchState && (!errored || proceedOnError))
326350
return MusicBeatState.switchState(this);
327-
}
351+
};
328352

329-
yutautil.Threader.runInThread(preloader(), 1, 'State Preloader - ${Type.getClassName(Type.getClass(this))}');
330-
}
353+
var stateName = Type.getClassName(Type.getClass(state));
354+
355+
yutautil.Threader.runInThread(preloader(), 1, 'State Preloader');
356+
}
331357

332358

333359
public static function preloadAndSwitchState(state:MusicBeatState)
@@ -340,7 +366,7 @@ class MusicBeatState extends FlxState
340366
return;
341367
}
342368

343-
state.preloadState(true);
369+
state.preloadState(true, state);
344370
}
345371

346372
public static function switchState(nextState:FlxState = null)

source/states/PlayState.hx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ class PlayState extends MusicBeatState
137137

138138
public var comboOffsetCustom:Null<Array<Int>> = null;
139139

140+
// override function preloadFunction():Void {
141+
// generateSong();
142+
// }
143+
140144
public var BF_X:Float = 770;
141145
public var BF_Y:Float = 100;
142146
public var BF2_X:Float = 770;
@@ -456,6 +460,12 @@ class PlayState extends MusicBeatState
456460

457461
override public function create()
458462
{
463+
464+
// if (SONG != null)
465+
// {
466+
// preloadFunction = generateSong;
467+
// }
468+
459469
if (SONG == null) {
460470
var songLowercase:String = Paths.formatToSongPath('tutorial');
461471
var poop:String = Highscore.formatSong(songLowercase, storyDifficulty);
@@ -2288,6 +2298,7 @@ class PlayState extends MusicBeatState
22882298

22892299
private function generateSong():Void
22902300
{
2301+
trace('Generating Song: ${SONG.song}');
22912302
// FlxG.log.add(ChartParser.parse());
22922303
songSpeed = PlayState.SONG.speed;
22932304
songSpeedType = ClientPrefs.getGameplaySetting('scrolltype');

source/states/freeplay/FreeplayState.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ class FreeplayState extends MusicBeatState
13131313
alreadyClicked = true;
13141314
MusicBeatState.reopen = false; //Fix a sticker bug
13151315
LoadingState.prepareToSong();
1316-
LoadingState.loadAndSwitchState(APEntryState.inArchipelagoMode ? new archipelago.APPlayState() : new states.PlayState());
1316+
MusicBeatState.preloadAndSwitchState(APEntryState.inArchipelagoMode ? new archipelago.APPlayState() : new states.PlayState());
13171317
}
13181318
#if !SHOW_LOADING_SCREEN FlxG.sound.music.stop(); #end
13191319
stopMusicPlay = true;

source/yutautil/CollectionUtils.hx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,35 @@ class CollectionUtils
520520
return Type.createInstance(CLASS, args != null ? args : []);
521521
}
522522

523+
public static inline function hashcode(input:Dynamic):Int
524+
{
525+
if (Std.is(input, String))
526+
{
527+
var hash = 0;
528+
for (i in 0...input.length) {
529+
var char = input.charCodeAt(i);
530+
hash = (hash * 31 + char) & 0xffffffff;
531+
}
532+
return hash;
533+
}
534+
else if (Std.is(input, Int) || Std.is(input, Float))
535+
{
536+
return Std.int(input);
537+
}
538+
else if (Std.is(input, Array))
539+
{
540+
return input.hashCode();
541+
}
542+
else if (Std.is(input, IMap))
543+
{
544+
return input.hashCode();
545+
}
546+
else
547+
{
548+
return 0;
549+
}
550+
}
551+
523552
public static inline function callOn<T>(item:T, func:T->Dynamic):Dynamic
524553
{
525554
return func(item);

0 commit comments

Comments
 (0)