Skip to content

Commit ff0e4c5

Browse files
committed
da cool save system WIPPPP!!!!!!
still doesnt load the save files correctly, prob its due to a stupid thing
1 parent cc62021 commit ff0e4c5

12 files changed

Lines changed: 781 additions & 143 deletions

File tree

building/libs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<git name="hxdiscord_rpc" url="https://github.com/CodenameCrew/cne-hxdiscord_rpc" skipDeps="true" />
1919
<lib name="funkin-modchart" version="1.2.4" skipDeps="true" />
2020
<lib name="hxvlc" version="1.9.3" skipDeps="true" />
21+
<git name="bin-serializer" url="https://github.com/CodenameCrew/bin-serializer" />
2122

2223
<!-- Documentation and other features -->
2324
<git name="away3d" url="https://github.com/CodenameCrew/away3d" />

project.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
version="1.0.1" company="CodenameCrew" />
99

1010
<!--
11-
CHANGE THE SAVE PATH & NAME FOR YOUR MOD HERE!
11+
CHANGE THE SAVE PATH & NAME FOR YOUR HARDCODED MOD HERE!
1212
-->
13-
<haxedef name="SAVE_PATH" value="CodenameEngine"/>
13+
<haxedef name="SAVE_PATH" value="CodenameCrew/CodenameEngine"/>
1414
<haxedef name="SAVE_NAME" value="save-default"/>
1515

16-
<haxedef name="SAVE_OPTIONS_PATH" value="CodenameEngine"/>
17-
<haxedef name="SAVE_OPTIONS_NAME" value="options"/>
18-
1916
<!--Switch Export with Unique ApplicationID and Icon-->
2017
<set name="APP_ID" value="0x0100f6c013bbc000" />
2118

@@ -131,6 +128,7 @@
131128
<haxelib name="away3d" if="THREE_D_SUPPORT" />
132129
<haxelib name="format" />
133130
<haxelib name="flxanimate" />
131+
<haxelib name="bin-serializer" />
134132
<haxelib name="nape-haxe4" if="NAPE_ENABLED"/>
135133

136134
<haxelib name="hscript-improved" />

source/funkin/backend/system/Flags.hx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package funkin.backend.system;
33
import flixel.util.FlxColor;
44
import funkin.backend.assets.ModsFolder;
55
import funkin.backend.assets.IModsAssetLibrary;
6-
import funkin.backend.assets.ScriptedAssetLibrary;
76
import funkin.backend.system.macros.GitCommitMacro;
87
import funkin.backend.utils.IniUtil;
98
import lime.app.Application;
@@ -13,7 +12,7 @@ import lime.utils.AssetType;
1312
/**
1413
* A class that reads the `flags.ini` file, allowing to read settable Flags (customs too).
1514
*/
16-
@:build(funkin.backend.system.macros.FlagMacro.build())
15+
@:build(funkin.backend.system.macros.FlagMacro.build("onReset"))
1716
class Flags {
1817
public static var overridenFlags:Map<String, Bool> = [];
1918

@@ -157,7 +156,7 @@ class Flags {
157156
public static var DEFAULT_HUD_ZOOM_LERP:Float = 0.05;
158157

159158
public static var USE_LEGACY_ZOOM_FACTOR:Null<Bool> = null;
160-
159+
161160
// Font configuration
162161
public static var DEFAULT_FONT:String = "vcr.ttf";
163162
public static var DEFAULT_FONT_SIZE:Int = 16;
@@ -312,6 +311,12 @@ class Flags {
312311
if (SUSTAINS_AS_ONE_NOTE == null) SUSTAINS_AS_ONE_NOTE = MOD_API_VERSION >= 2;
313312
}
314313

314+
public static function onReset() {
315+
var currentModFolder = ModsFolder.currentModFolder;
316+
if (currentModFolder != null) SAVE_NAME = currentModFolder;
317+
trace("resetted");
318+
}
319+
315320
public static function loadFromDatas(datas:Array<String>):Map<String, String> {
316321
var flags:Map<String, String> = [];
317322
for (data in datas) {
@@ -344,7 +349,7 @@ class Flags {
344349
libs = Paths.assetsTree.libraries.copy();
345350
libs.reverse();
346351
}
347-
for(lib in libs) {
352+
for (lib in libs) {
348353
var l = lib;
349354
if (l is openfl.utils.AssetLibrary) {
350355
@:privateAccess

source/funkin/backend/system/Main.hx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import flixel.addons.transition.TransitionData;
66
import flixel.graphics.FlxGraphic;
77
import flixel.math.FlxPoint;
88
import flixel.math.FlxRect;
9-
import flixel.system.ui.FlxSoundTray;
10-
import funkin.backend.assets.AssetSource;
119
import funkin.backend.assets.AssetsLibraryList;
1210
import funkin.backend.assets.ModsFolder;
1311
import funkin.backend.system.framerate.Framerate;
@@ -16,10 +14,10 @@ import funkin.backend.system.modules.*;
1614
import funkin.backend.utils.ThreadUtil;
1715
import funkin.editors.SaveWarning;
1816
import funkin.options.PlayerSettings;
17+
import funkin.savedata.FunkinSave;
1918
import openfl.Assets;
2019
import openfl.Lib;
2120
import openfl.display.Sprite;
22-
import openfl.text.TextFormat;
2321
import openfl.utils.AssetLibrary;
2422
import sys.FileSystem;
2523
import sys.io.File;
@@ -70,6 +68,7 @@ class Main extends Sprite
7068
instance = this;
7169

7270
CrashHandler.init();
71+
FunkinSave.reloadSaves();
7372

7473
addChild(game = new FunkinGame(gameWidth, gameHeight, MainState, Options.framerate, Options.framerate, skipSplash, startFullscreen));
7574

@@ -127,6 +126,7 @@ class Main extends Sprite
127126
Assets.registerLibrary('default', lib);
128127

129128
funkin.options.PlayerSettings.init();
129+
FunkinSave.init();
130130
Options.load();
131131

132132
FlxG.fixedTimestep = false;
@@ -164,10 +164,10 @@ class Main extends Sprite
164164
var daSndTray = Type.createInstance(game._customSoundTray = funkin.menus.ui.FunkinSoundTray, []);
165165
var index:Int = game.numChildren - 1;
166166

167-
if(game.soundTray != null)
167+
if (game.soundTray != null)
168168
{
169169
var newIndex:Int = game.getChildIndex(game.soundTray);
170-
if(newIndex != -1) index = newIndex;
170+
if (newIndex != -1) index = newIndex;
171171
game.removeChild(game.soundTray);
172172
game.soundTray.__cleanup();
173173
}

source/funkin/backend/system/MainState.hx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ class MainState extends FlxState {
2626
public static var initiated:Bool = false;
2727
public override function create() {
2828
super.create();
29-
if (!initiated) {
30-
Main.loadGameSettings();
31-
}
3229

30+
if (!initiated) Main.loadGameSettings();
3331
initiated = true;
3432

3533
#if sys
@@ -113,7 +111,7 @@ class MainState extends FlxState {
113111

114112
Flags.reset();
115113
Flags.load();
116-
funkin.savedata.FunkinSave.init();
114+
funkin.savedata.FunkinSave.reloadSaves();
117115

118116
TranslationUtil.findAllLanguages();
119117
TranslationUtil.setLanguage(Flags.DISABLE_LANGUAGES ? Flags.DEFAULT_LANGUAGE : null);

source/funkin/backend/system/macros/FlagMacro.macro.hx

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FlagMacro {
1919
return false;
2020
}
2121

22-
public static function build():Array<Field> {
22+
public static function build(resetCallback:String):Array<Field> {
2323
var fields = Context.getBuildFields();
2424

2525
var clRef = Context.getLocalClass();
@@ -32,7 +32,7 @@ class FlagMacro {
3232
var parserExprs:Array<Expr> = [];
3333
resetExprs.push(macro $i{"customFlags"} = $v{[]});
3434

35-
for (field in fields) {
35+
for (field in fields.copy()) {
3636
if (field.meta.hasMeta(":bypass")) continue;
3737
var hasLazy = field.meta.hasMeta(":lazy");
3838

@@ -47,9 +47,9 @@ class FlagMacro {
4747
}
4848

4949
var alsos:Array<Expr> = [];
50-
for(meta in field.meta) {
51-
if(meta.name == ":also") {
52-
for(param in meta.params) {
50+
for (meta in field.meta) {
51+
if (meta.name == ":also") {
52+
for (param in meta.params) {
5353
switch(param.expr) {
5454
case EField(_, _):
5555
alsos.push(param);
@@ -60,6 +60,9 @@ class FlagMacro {
6060
}
6161
}
6262

63+
if (alsos.length > 0 && hasLazy)
64+
Context.error("Flag " + field.name + " cannot have both :also and :lazy", field.pos);
65+
6366
if (expr == null) Context.error('Flag ' + field.name + ' must have a default value', field.pos);
6467
if (type == null) {
6568
switch(expr.expr) {
@@ -208,33 +211,56 @@ class FlagMacro {
208211
if(customCheck != null) {
209212
parserExprs.push(customCheck);
210213
} else {
211-
var alsoExpr = alsos.length > 0 ? macro @:mergeBlock $b{alsos.map((e) -> return macro $e = val)} : macro {};
212-
213214
if(isNullable) {
214215
parserExprs.push(macro @:mergeBlock {
215216
if(name == $v{field.name}) {
216-
var val = value == "NULL" ? null : $parser;
217-
$i{field.name} = val;
218-
$alsoExpr;
217+
$i{field.name} = value == "NULL" ? null : $parser;
219218
return true;
220219
}
221220
});
222221
} else {
223222
parserExprs.push(macro @:mergeBlock {
224223
if(name == $v{field.name}) {
225-
var val = $parser;
226-
$i{field.name} = val;
227-
$alsoExpr;
224+
$i{field.name} = $parser;
228225
return true;
229226
}
230227
});
231228
}
232229
}
230+
231+
if (alsos.length > 0) {
232+
var aliasExpr = macro @:mergeBlock $b{alsos.map((e) -> return macro $e = val)};
233+
234+
var setter = macro {
235+
$i{field.name} = val;
236+
$aliasExpr;
237+
return val;
238+
};
239+
240+
switch(field.kind) {
241+
case FVar(t, expr):
242+
field.kind = FProp("default", "set", t, expr);
243+
fields.push({
244+
name: "set_" + field.name,
245+
access: [APublic, AStatic, AInline],
246+
kind: FFun({
247+
args: [{name: "val", type: t}],
248+
expr: setter,
249+
ret: t
250+
}),
251+
pos: Context.currentPos(),
252+
doc: null,
253+
meta: []
254+
});
255+
default:
256+
Context.error("Flag " + field.name + " must be a variable, or something else happened", field.pos);
257+
}
258+
}
233259
default:
234260
// nothing
235261
}
236262

237-
if(hasLazy) {
263+
if (hasLazy) {
238264
switch(field.kind) {
239265
case FVar(t, expr):
240266
field.kind = FVar(t, null);
@@ -248,7 +274,10 @@ class FlagMacro {
248274
access: [APublic, AStatic],
249275
kind: FFun({
250276
args: [],
251-
expr: macro $b{resetExprs},
277+
expr: macro {
278+
$b{resetExprs};
279+
$i{resetCallback}();
280+
},
252281
ret: macro: Void
253282
}),
254283
pos: Context.currentPos(),

source/funkin/backend/system/macros/FunkinSaveMacro.hx

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,75 @@ package funkin.backend.system.macros;
44
import haxe.macro.Context;
55
import haxe.macro.Expr;
66

7+
class FieldName
8+
{
9+
public var name:String;
10+
public var saveField:String;
11+
12+
public function new(name:String, saveField:String)
13+
{
14+
this.name = name;
15+
this.saveField = saveField;
16+
}
17+
}
18+
719
/**
820
* Macro that automatically generates flush and load functions.
921
*/
1022
class FunkinSaveMacro {
1123
/**
1224
* Generates flush and load functions.
13-
* @param saveFieldName Name of the save field (`save`)
25+
* @param saveFieldName Name of the save field (`save`) or `null` to not have a default save field
1426
* @param saveFuncName Name of the save func (`flush`)
1527
* @param loadFuncName Name of the load func (`load`)
1628
* @return Array<Field>
1729
*/
1830
public static function build(saveFieldName:String = "save", saveFuncName:String = "flush", loadFuncName:String = "load"):Array<Field> {
1931
var fields:Array<Field> = Context.getBuildFields();
2032

21-
var fieldNames:Array<String> = [];
22-
for(field in fields) {
23-
if (!field.access.contains(AStatic)) continue;
33+
var fieldNames:Array<FieldName> = [];
34+
for (field in fields) {
35+
if (!field.access.contains(AStatic))
36+
continue;
2437

2538
switch(field.kind) {
2639
case FVar(type, expr):
27-
if (field.name == saveFieldName) continue;
40+
if (saveFieldName != null && field.name == saveFieldName)
41+
continue;
42+
2843
var valid:Bool = true;
29-
if (field.meta != null)
30-
for(m in field.meta)
31-
if (m.name == ":doNotSave") {
32-
valid = false;
33-
break;
34-
}
35-
if (valid)
36-
fieldNames.push(field.name);
44+
var customSaveFieldName:Null<String> = saveFieldName;
45+
if (field.meta != null) for (m in field.meta) {
46+
if (m.name == ":doNotSave") valid = false;
47+
else if (m.name == ":saveField") customSaveFieldName = meta_extractIdent(m);
48+
}
49+
if (valid) {
50+
if (customSaveFieldName == null)
51+
Context.error("Field " + field.name + " is not marked with @:saveField", field.pos);
52+
53+
fieldNames.push(new FieldName(field.name, customSaveFieldName));
54+
}
3755
default:
3856
continue;
3957
}
4058
}
4159

60+
var _allSaveFields:Array<String> = [for (f in fieldNames) f.saveField];
61+
if (saveFieldName != null) _allSaveFields.push(saveFieldName);
62+
var __allSaveFields:Map<String, Bool> = [];
63+
for (f in _allSaveFields) __allSaveFields.set(f, true);
64+
var allSaveFields:Array<String> = [];
65+
for (f in __allSaveFields.keys()) allSaveFields.push(f);
66+
4267
/**
4368
* SAVE FUNCTION
4469
*/
45-
var saveFuncBlocks:Array<Expr> = [for(f in fieldNames) macro $i{saveFieldName}.data.$f = $i{f}];
46-
saveFuncBlocks.push(macro $i{saveFieldName}.flush());
70+
var saveFuncBlocks:Array<Expr> = [for(f in fieldNames) {
71+
var name:String = f.name;
72+
macro $i{f.saveField}.data.$name = $i{name};
73+
}];
74+
for (f in allSaveFields)
75+
saveFuncBlocks.push(macro $i{f}.flush());
4776

4877
fields.push({
4978
pos: Context.currentPos(),
@@ -58,7 +87,10 @@ class FunkinSaveMacro {
5887
access: [APublic, AStatic]
5988
});
6089

61-
var loadFuncBlocks:Array<Expr> = [for(f in fieldNames) macro if ($i{saveFieldName}.data.$f != null) $i{f} = $i{saveFieldName}.data.$f];
90+
var loadFuncBlocks:Array<Expr> = [for(f in fieldNames) {
91+
var name:String = f.name;
92+
macro if ($i{saveFieldName}.data.$name != null) $i{name} = $i{saveFieldName}.data.$name;
93+
}];
6294

6395
/**
6496
* LOAD FUNCTION
@@ -78,5 +110,16 @@ class FunkinSaveMacro {
78110

79111
return fields;
80112
}
113+
114+
public static function meta_extractIdent(meta:MetadataEntry):Null<String> {
115+
if (meta == null || meta.params == null || meta.params.length == 0)
116+
throw "Expected an identifier";
117+
switch (meta.params[0].expr) {
118+
case EConst(CIdent(s)):
119+
return s;
120+
default:
121+
}
122+
throw "Expected an identifier";
123+
}
81124
}
82125
#end

0 commit comments

Comments
 (0)