Skip to content

Commit c2517bb

Browse files
committed
Added autosync save files through Newgrounds
Created method to save files to newgrounds if autosync is enabled Saving to Newgrounds automatically if autosync is enabled Space on condition Saving song scores Since slots load async, wait until it finishes to autosync Better way to check outdated data Ensure save slot is not null Used thx to do content comparasion Update NewgroundsClient.hx Ok, this was a funny one Syntax fix + check if file is empty too Introduce trace into function since saving save files is async too I forgot this was an error too lol
1 parent 353473a commit c2517bb

7 files changed

Lines changed: 116 additions & 7 deletions

File tree

source/funkin/InitState.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class InitState extends FlxState
7575
// Flixel has already loaded the save data, so we can just use it.
7676
Preferences.init();
7777

78+
NewgroundsClient.instance.autoSyncSavesFromNewgrounds();
79+
7880
// Load controls from save data.
7981
PlayerSettings.init();
8082

source/funkin/PlayerSettings.hx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import funkin.input.Controls;
55
import funkin.input.PreciseInputManager;
66
import flixel.input.gamepad.FlxGamepad;
77
import flixel.util.FlxSignal.FlxTypedSignal;
8+
#if FEATURE_NEWGROUNDS
9+
import funkin.api.newgrounds.NewgroundsClient;
10+
#end
811

912
/**
1013
* A core class which represents the current player(s) and their controls and other configuration.
@@ -180,5 +183,9 @@ class PlayerSettings
180183
Save.instance.setControls(id, Gamepad(controls.gamepadsAdded[0]), padData);
181184
}
182185
}
186+
187+
#if FEATURE_NEWGROUNDS
188+
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
189+
#end
183190
}
184191
}

source/funkin/Preferences.hx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,25 @@ class Preferences
594594
return value;
595595
}
596596

597+
/**
598+
* If enabled, save files uploaded to NG will sync automatically with the local ones if outdated.
599+
* @default `false`
600+
*/
601+
public static var autoSync(get, set):Bool;
602+
603+
static function get_autoSync():Bool
604+
{
605+
return Save?.instance?.options?.autoSync ?? false;
606+
}
607+
608+
static function set_autoSync(value:Bool):Bool
609+
{
610+
var save:Save = Save.instance;
611+
save.options.autoSync = value;
612+
Save.system.flush();
613+
return value;
614+
}
615+
597616
#if mobile
598617
/**
599618
* If enabled, device will be able to sleep on its own.

source/funkin/api/newgrounds/NGSaveSlot.hx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class NGSaveSlot
5050
* Saves `data` to the newgrounds save slot.
5151
* @param data The raw save data.
5252
*/
53-
public function save(data:RawSaveData):Void
53+
public function save(data:RawSaveData, ?onSuccess:Void->Void, ?onError:Void->Void):Void
5454
{
5555
var encodedData:String = haxe.Serializer.run(data);
5656

@@ -62,16 +62,21 @@ class NGSaveSlot
6262
{
6363
case SUCCESS:
6464
trace(' NEWGROUNDS '.bold().bg_orange() + ' Successfully saved save data to save slot!');
65+
if (onSuccess != null) onSuccess();
6566
case FAIL(error):
6667
trace(' NEWGROUNDS '.bold().bg_orange() + ' Failed to save data to save slot!');
6768
trace(error);
69+
70+
if (onError != null) onError();
6871
}
6972
});
7073
}
7174
catch (error:String)
7275
{
7376
trace(' NEWGROUNDS '.bold().bg_orange() + ' Failed to save data to save slot!');
7477
trace(error);
78+
79+
if (onError != null) onError();
7580
}
7681
}
7782

@@ -141,12 +146,16 @@ class NGSaveSlot
141146
}
142147
}
143148

144-
public function checkSlot():Void
149+
public function checkSlot(onFinish:(isNull:Bool, isEmpty:Bool)->Void):Void
145150
{
146151
trace(' NEWGROUNDS '.bold().bg_orange() + ' Checking save slot with the ID of ${ngSaveSlot?.id}...');
147152

148-
trace(' Is null? ${ngSaveSlot == null}');
149-
trace(' Is empty? ${ngSaveSlot?.isEmpty() ?? false}');
153+
var isNull:Bool = ngSaveSlot == null;
154+
var isEmpty:Bool = ngSaveSlot?.isEmpty() ?? false;
155+
trace(' Is null? $isNull');
156+
trace(' Is empty? $isEmpty');
157+
158+
if (onFinish != null) onFinish(isNull, isEmpty);
150159
}
151160
}
152161
#end

source/funkin/api/newgrounds/NewgroundsClient.hx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package funkin.api.newgrounds;
22

33
#if FEATURE_NEWGROUNDS
44
import funkin.save.Save;
5+
import funkin.save.migrator.SaveDataMigrator;
56
import io.newgrounds.Call.CallError;
67
import io.newgrounds.NG;
78
import io.newgrounds.NGLite;
@@ -350,7 +351,40 @@ class NewgroundsClient
350351
{
351352
trace(' NEWGROUNDS '.bold().bg_orange() + ' Fetched save slots!');
352353

353-
NGSaveSlot.instance.checkSlot();
354+
NGSaveSlot.instance.checkSlot(autoSyncSavesFromNewgrounds);
355+
}
356+
357+
public function autoSyncSavesFromNewgrounds(slotIsNull:Bool = false, slotIsEmpty:Bool = false)
358+
{
359+
if (!Preferences.autoSync) return;
360+
if (slotIsNull || slotIsEmpty) return;
361+
362+
@:privateAccess
363+
var rawSaveData:RawSaveData = Save.instance.data;
364+
365+
NGSaveSlot.instance.load((data:RawSaveData) ->
366+
{
367+
var gameSave = SaveDataMigrator.migrate(data);
368+
@:privateAccess
369+
if (thx.Dynamics.equals(rawSaveData, gameSave.data))
370+
{
371+
trace(' NEWGROUNDS '.bold().bg_orange() + 'Save files are already synced');
372+
373+
}
374+
else
375+
{
376+
Save.loadFromNewgrounds(() -> {
377+
trace(' NEWGROUNDS '.bold().bg_orange() + ' Synced with Newgrounds (loaded!)');
378+
});
379+
}
380+
});
381+
}
382+
383+
public function autoSyncSavesToNewgrounds()
384+
{
385+
if (!Preferences.autoSync) return;
386+
387+
Save.saveToNewgrounds(() -> trace(' NEWGROUNDS '.bold().bg_orange() + ' Synced with Newgrounds (saved!)'));
354388
}
355389

356390
function get_user():Null<User>

source/funkin/save/Save.hx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import funkin.util.SerializerUtil;
1717
import funkin.mobile.ui.FunkinHitbox;
1818
import thx.semver.Version;
1919
#if FEATURE_NEWGROUNDS
20+
import funkin.api.newgrounds.NewgroundsClient;
2021
import funkin.api.newgrounds.Medals;
2122
import funkin.api.newgrounds.Leaderboards;
2223
#end
@@ -115,6 +116,7 @@ class Save implements ConsoleClass
115116
audioVisualOffset: 0,
116117
unlockedFramerate: false,
117118
enabledDiscordRPC: true,
119+
autoSync: false,
118120
screenshot: {
119121
shouldHideMouse: true,
120122
fancyPreview: true,
@@ -433,6 +435,10 @@ class Save implements ConsoleClass
433435
}
434436
level.set(difficultyId, score);
435437
Save.system.flush();
438+
439+
#if FEATURE_NEWGROUNDS
440+
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
441+
#end
436442
}
437443

438444
public function isLevelHighScore(levelId:String, difficultyId:String = 'normal', score:SaveScoreData):Bool
@@ -525,6 +531,10 @@ class Save implements ConsoleClass
525531
}
526532
song.set(difficultyId, score);
527533
Save.system.flush();
534+
535+
#if FEATURE_NEWGROUNDS
536+
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
537+
#end
528538
}
529539

530540
/**
@@ -674,6 +684,10 @@ class Save implements ConsoleClass
674684
{
675685
data.favoriteSongs.push(id);
676686
Save.system.flush();
687+
688+
#if FEATURE_NEWGROUNDS
689+
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
690+
#end
677691
}
678692
}
679693

@@ -683,6 +697,10 @@ class Save implements ConsoleClass
683697
{
684698
data.favoriteSongs.remove(id);
685699
Save.system.flush();
700+
701+
#if FEATURE_NEWGROUNDS
702+
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
703+
#end
686704
}
687705
}
688706

@@ -907,11 +925,11 @@ class Save implements ConsoleClass
907925
}
908926

909927
#if FEATURE_NEWGROUNDS
910-
public static function saveToNewgrounds():Void
928+
public static function saveToNewgrounds(?onSuccess:Void->Void, ?onError:Void->Void):Void
911929
{
912930
if (_instance == null) return;
913931
trace('[SAVE] Saving Save Data to Newgrounds...');
914-
funkin.api.newgrounds.NGSaveSlot.instance.save(_instance.data);
932+
funkin.api.newgrounds.NGSaveSlot.instance.save(_instance.data, onSuccess, onError);
915933
}
916934

917935
public static function loadFromNewgrounds(onFinish:Void->Void):Void
@@ -1213,6 +1231,12 @@ typedef SaveDataOptions =
12131231
*/
12141232
var enabledDiscordRPC:Bool;
12151233

1234+
/**
1235+
* If files are synced automatically from NG
1236+
* @default `false`
1237+
*/
1238+
var autoSync:Bool;
1239+
12161240
/**
12171241
* Screenshot options
12181242
* @param shouldHideMouse Should the mouse be hidden when taking a screenshot? Default: `true`

source/funkin/ui/options/PreferencesMenu.hx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import funkin.util.SwipeUtil;
2626
#end
2727
import funkin.util.HapticUtil;
2828
import lime.ui.WindowVSyncMode;
29+
#if FEATURE_NEWGROUNDS
30+
import funkin.api.newgrounds.NewgroundsClient;
31+
#end
2932

3033
class PreferencesMenu extends Page<OptionsState.OptionsMenuPageName>
3134
{
@@ -237,6 +240,17 @@ class PreferencesMenu extends Page<OptionsState.OptionsMenuPageName>
237240
Preferences.enabledDiscordRPC = value;
238241
}, Preferences.enabledDiscordRPC);
239242
#end
243+
244+
#if FEATURE_NEWGROUNDS
245+
createPrefItemCheckbox('NG Save Files Auto Sync', 'When enabled, save files uploaded to NG will sync automatically with local.', function(value:Bool):Void
246+
{
247+
Preferences.autoSync = value;
248+
if(Preferences.autoSync)
249+
{
250+
NewgroundsClient.instance.autoSyncSavesFromNewgrounds();
251+
}
252+
}, Preferences.autoSync);
253+
#end
240254
}
241255

242256
override function update(elapsed:Float):Void

0 commit comments

Comments
 (0)