Prevent overwriting existing replays by appending count in parentheses

This commit is contained in:
Terry Hearst 2023-02-05 22:16:08 -05:00
parent 7f000d8021
commit d2323a3811

View file

@ -1994,33 +1994,42 @@ class MarbleWorld extends Scheduler {
public function saveReplay() { public function saveReplay() {
this.replay.name = MarbleGame.instance.recordingName; this.replay.name = MarbleGame.instance.recordingName;
var replayBytes = this.replay.write();
#if hl #if hl
// hxd.File.saveAs(replayBytes, {
// title: 'Save Replay',
// fileTypes: [
// {
// name: "Replay (*.mbr)",
// extensions: ["mbr"]
// }
// ],
// defaultPath: 'data/replay/${this.mission.title}${this.timeState.gameplayClock}.mbr'
// });
sys.FileSystem.createDirectory(haxe.io.Path.join([Settings.settingsDir, "data", "replays"])); sys.FileSystem.createDirectory(haxe.io.Path.join([Settings.settingsDir, "data", "replays"]));
var replayPath = haxe.io.Path.join([ var replayPath = haxe.io.Path.join([
Settings.settingsDir, Settings.settingsDir,
"data", "data",
"replays", "replays",
'${this.mission.title}${this.timeState.gameplayClock}.mbr' '${this.replay.name}.mbr'
]); ]);
if (sys.FileSystem.exists(replayPath)) {
var count = 1;
var found = false;
while (!found) {
replayPath = haxe.io.Path.join([
Settings.settingsDir,
"data",
"replays",
'${this.replay.name} (${count}).mbr'
]);
if (!sys.FileSystem.exists(replayPath)) {
this.replay.name += ' (${count})';
found = true;
} else {
count++;
}
}
}
var replayBytes = this.replay.write();
sys.io.File.saveBytes(replayPath, replayBytes); sys.io.File.saveBytes(replayPath, replayBytes);
#end #end
#if js #if js
var replayBytes = this.replay.write();
var blob = new js.html.Blob([replayBytes.getData()], { var blob = new js.html.Blob([replayBytes.getData()], {
type: 'application/octet-stream' type: 'application/octet-stream'
}); });
var url = js.html.URL.createObjectURL(blob); var url = js.html.URL.createObjectURL(blob);
var fname = '${this.mission.title}${this.timeState.gameplayClock}.mbr'; var fname = '${this.replay.name}.mbr';
var element = js.Browser.document.createElement('a'); var element = js.Browser.document.createElement('a');
element.setAttribute('href', url); element.setAttribute('href', url);
element.setAttribute('download', fname); element.setAttribute('download', fname);