Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/funkin/InitState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class InitState extends FlxState
// Flixel has already loaded the save data, so we can just use it.
Preferences.init();

NewgroundsClient.instance.autoSyncSavesFromNewgrounds();

// Load controls from save data.
PlayerSettings.init();

Expand Down
7 changes: 7 additions & 0 deletions source/funkin/PlayerSettings.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import funkin.input.Controls;
import funkin.input.PreciseInputManager;
import flixel.input.gamepad.FlxGamepad;
import flixel.util.FlxSignal.FlxTypedSignal;
#if FEATURE_NEWGROUNDS
import funkin.api.newgrounds.NewgroundsClient;
#end

/**
* A core class which represents the current player(s) and their controls and other configuration.
Expand Down Expand Up @@ -180,5 +183,9 @@ class PlayerSettings
Save.instance.setControls(id, Gamepad(controls.gamepadsAdded[0]), padData);
}
}

#if FEATURE_NEWGROUNDS
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
#end
}
}
19 changes: 19 additions & 0 deletions source/funkin/Preferences.hx
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,25 @@ class Preferences
return value;
}

/**
* If enabled, save files uploaded to NG will sync automatically with the local ones if outdated.
* @default `false`
*/
public static var autoSync(get, set):Bool;

static function get_autoSync():Bool
{
return Save?.instance?.options?.autoSync ?? false;
}

static function set_autoSync(value:Bool):Bool
{
var save:Save = Save.instance;
save.options.autoSync = value;
Save.system.flush();
return value;
}

/**
* Controls Scheme for the hitbox.
* @default `4 Lanes`
Expand Down
17 changes: 13 additions & 4 deletions source/funkin/api/newgrounds/NGSaveSlot.hx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class NGSaveSlot
* Saves `data` to the newgrounds save slot.
* @param data The raw save data.
*/
public function save(data:RawSaveData):Void
public function save(data:RawSaveData, ?onSuccess:Void->Void, ?onError:Void->Void):Void
{
var encodedData:String = haxe.Serializer.run(data);

Expand All @@ -64,16 +64,21 @@ class NGSaveSlot
{
case SUCCESS:
trace(' NEWGROUNDS '.bold().bg_orange() + ' Successfully saved save data to save slot!');
if (onSuccess != null) onSuccess();
case FAIL(error):
trace(' NEWGROUNDS '.bold().bg_orange() + ' Failed to save data to save slot!');
trace(error);

if (onError != null) onError();
}
});
}
catch (error:String)
{
trace(' NEWGROUNDS '.bold().bg_orange() + ' Failed to save data to save slot!');
trace(error);

if (onError != null) onError();
}
}

Expand Down Expand Up @@ -146,12 +151,16 @@ class NGSaveSlot
}
}

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

trace(' Is null? ${ngSaveSlot == null}');
trace(' Is empty? ${ngSaveSlot?.isEmpty() ?? false}');
var isNull:Bool = ngSaveSlot == null;
var isEmpty:Bool = ngSaveSlot?.isEmpty() ?? false;
trace(' Is null? $isNull');
trace(' Is empty? $isEmpty');

if (onFinish != null) onFinish(isNull, isEmpty);
}
}
#end
36 changes: 35 additions & 1 deletion source/funkin/api/newgrounds/NewgroundsClient.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package funkin.api.newgrounds;

import funkin.save.Save;
import funkin.save.migrator.SaveDataMigrator;
import funkin.api.newgrounds.Medals.Medal;
#if FEATURE_NEWGROUNDS
import io.newgrounds.Call.CallError;
Expand Down Expand Up @@ -356,7 +357,40 @@ class NewgroundsClient
{
trace(' NEWGROUNDS '.bold().bg_orange() + ' Fetched save slots!');

NGSaveSlot.instance.checkSlot();
NGSaveSlot.instance.checkSlot(autoSyncSavesFromNewgrounds);
}

public function autoSyncSavesFromNewgrounds(slotIsNull:Bool = false, slotIsEmpty:Bool = false)
{
if (!Preferences.autoSync) return;
if (slotIsNull || slotIsEmpty) return;

@:privateAccess
var rawSaveData:RawSaveData = Save.instance.data;

NGSaveSlot.instance.load((data:RawSaveData) ->
{
var gameSave = SaveDataMigrator.migrate(data);
@:privateAccess
if (thx.Dynamics.equals(rawSaveData, gameSave.data))
{
trace(' NEWGROUNDS '.bold().bg_orange() + 'Save files are already synced');

}
else
{
Save.loadFromNewgrounds(() -> {
trace(' NEWGROUNDS '.bold().bg_orange() + ' Synced with Newgrounds (loaded!)');
});
}
});
}

public function autoSyncSavesToNewgrounds()
{
if (!Preferences.autoSync) return;

Save.saveToNewgrounds(() -> trace(' NEWGROUNDS '.bold().bg_orange() + ' Synced with Newgrounds (saved!)'));
}

function get_user():Null<User>
Expand Down
24 changes: 22 additions & 2 deletions source/funkin/save/Save.hx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import funkin.mobile.ui.FunkinHitbox;
import thx.semver.Version;
import funkin.util.tools.ISerializable;
#if FEATURE_NEWGROUNDS
import funkin.api.newgrounds.NewgroundsClient;
import funkin.api.newgrounds.Medals;
import funkin.api.newgrounds.Leaderboards;
#end
Expand Down Expand Up @@ -146,6 +147,7 @@ class Save implements ConsoleClass implements ISerializable
audioVisualOffset: 0,
unlockedFramerate: false,
enabledDiscordRPC: true,
autoSync: false,
screenshot: {
shouldHideMouse: true,
fancyPreview: true,
Expand Down Expand Up @@ -617,6 +619,10 @@ class Save implements ConsoleClass implements ISerializable
}
song.set(difficultyId, score);
Save.system.flush();

#if FEATURE_NEWGROUNDS
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
#end
}

/**
Expand Down Expand Up @@ -861,6 +867,10 @@ class Save implements ConsoleClass implements ISerializable
{
data.favoriteSongs.push(id);
Save.system.flush();

#if FEATURE_NEWGROUNDS
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
#end
}
}

Expand All @@ -874,6 +884,10 @@ class Save implements ConsoleClass implements ISerializable
{
data.favoriteSongs.remove(id);
Save.system.flush();

#if FEATURE_NEWGROUNDS
NewgroundsClient.instance.autoSyncSavesToNewgrounds();
#end
}
}

Expand Down Expand Up @@ -1119,11 +1133,11 @@ class Save implements ConsoleClass implements ISerializable
/**
* Save the current save data to the cloud on Newgrounds.
*/
public static function saveToNewgrounds():Void
public static function saveToNewgrounds(?onSuccess:Void->Void, ?onError:Void->Void):Void
{
if (_instance == null) return;
trace('[SAVE] Saving Save Data to Newgrounds...');
funkin.api.newgrounds.NGSaveSlot.instance.save(_instance.data);
funkin.api.newgrounds.NGSaveSlot.instance.save(_instance.data, onSuccess, onError);
}

/**
Expand Down Expand Up @@ -1458,6 +1472,12 @@ typedef SaveDataOptions =
*/
var enabledDiscordRPC:Bool;

/**
* If files are synced automatically from NG
* @default `false`
*/
var autoSync:Bool;

/**
* Screenshot options
* @param shouldHideMouse Should the mouse be hidden when taking a screenshot? Default: `true`
Expand Down
14 changes: 14 additions & 0 deletions source/funkin/ui/options/PreferencesMenu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import funkin.util.SwipeUtil;
#end
import funkin.util.HapticUtil;
import lime.ui.WindowVSyncMode;
#if FEATURE_NEWGROUNDS
import funkin.api.newgrounds.NewgroundsClient;
#end

class PreferencesMenu extends Page<OptionsState.OptionsMenuPageName>
{
Expand Down Expand Up @@ -255,6 +258,17 @@ class PreferencesMenu extends Page<OptionsState.OptionsMenuPageName>
Preferences.enabledDiscordRPC = value;
}, Preferences.enabledDiscordRPC);
#end

#if FEATURE_NEWGROUNDS
createPrefItemCheckbox('NG Save Files Auto Sync', 'When enabled, save files uploaded to NG will sync automatically with local.', function(value:Bool):Void
{
Preferences.autoSync = value;
if(Preferences.autoSync)
{
NewgroundsClient.instance.autoSyncSavesFromNewgrounds();
}
}, Preferences.autoSync);
#end
}

override function update(elapsed:Float):Void
Expand Down
Loading