Skip to content

Commit

Permalink
[SaveEdit] Add Save As.
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbed committed Jun 22, 2019
1 parent 445814b commit 41447d8
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,42 @@
<ApplicationIcon>Resources\Handsome Jack.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Resource Include="Resources\fugue\disk-black.png" />
<Resource Include="Resources\fugue\document-binary.png" />
<Resource Include="Resources\fugue\document.png" />
<Resource Include="Resources\fugue\folder-open-document.png" />
<Resource Include="Resources\fugue\luggage.png" />
<Resource Include="Resources\fugue\piggy-bank.png" />
<Resource Include="Resources\assaultrifle.png" />
<Resource Include="Resources\backpack.png" />
<Resource Include="Resources\bank.png" />
<Resource Include="Resources\stash.png" />
<Resource Include="Resources\Handsome Jack.ico" />
<Resource Include="Resources\Handsome Jack.png" />
<Resource Include="Resources\fugue\exclamation.png" />
<Resource Include="Resources\credits cropped.png" />
<Resource Include="Resources\credits.png" />
<Resource Include="Resources\eridium.png" />
<Resource Include="Resources\credits cropped.png" />
<Resource Include="Resources\assaultrifle.png" />
<Resource Include="Resources\shield.png" />
<Resource Include="Resources\pink seraph crystal.png" />
<Resource Include="Resources\seraph crystal.png" />
<Resource Include="Resources\fugue\scanner.png" />
<Resource Include="Resources\fugue\printer.png" />
<Resource Include="Resources\fugue\document-copy.png" />
<Resource Include="Resources\fugue\document-delete.png" />
<Resource Include="Resources\exclamation mark.png" />
<Resource Include="Resources\fast travel.png" />
<Resource Include="Resources\green vault symbol.png" />
<Resource Include="Resources\red skull.png" />
<Resource Include="Resources\fugue\computer.png" />
<Resource Include="Resources\fugue\control-090.png" />
<Resource Include="Resources\fugue\control-double-090.png" />
<Resource Include="Resources\exclamation mark.png" />
<Resource Include="Resources\question mark.png" />
<Resource Include="Resources\torgue token.png" />
<Resource Include="Resources\fugue\computer.png" />
<Resource Include="Resources\fugue\disk-rename.png" />
<Resource Include="Resources\fugue\disk.png" />
<Resource Include="Resources\fugue\document-binary.png" />
<Resource Include="Resources\fugue\document-copy.png" />
<Resource Include="Resources\fugue\document-delete.png" />
<Resource Include="Resources\fugue\document.png" />
<Resource Include="Resources\fugue\exclamation.png" />
<Resource Include="Resources\fugue\folder-open-document.png" />
<Resource Include="Resources\fugue\luggage.png" />
<Resource Include="Resources\fugue\piggy-bank.png" />
<Resource Include="Resources\fugue\printer.png" />
<Resource Include="Resources\fugue\scanner.png" />
<Resource Include="Resources\fugue\television-image.png" />
<Resource Include="Resources\fugue\television-test.png" />
<Resource Include="Resources\green vault symbol.png" />
<Resource Include="Resources\Handsome Jack.ico" />
<Resource Include="Resources\Handsome Jack.png" />
<Resource Include="Resources\pink seraph crystal.png" />
<Resource Include="Resources\question mark.png" />
<Resource Include="Resources\red skull.png" />
<Resource Include="Resources\scooter.png" />
<Resource Include="Resources\seraph crystal.png" />
<Resource Include="Resources\shield.png" />
<Resource Include="Resources\stash.png" />
<Resource Include="Resources\torgue token.png" />
<Resource Include="Resources\wheel.png" />
</ItemGroup>
<ItemGroup>
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions projects/Gibbed.Borderlands2.SaveEdit/ShellView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
[Shortcut Control+N] = [Action NewSave];
[Shortcut Control+O] = [Action ReadSave];
[Shortcut Control+S] = [Action WriteSave];
[Shortcut Control+Shift+S] = [Action WriteSaveAs];
</c:Message.Attach>
<ToolBar Name="ButtonBar"
DockPanel.Dock="Top">
Expand Down Expand Up @@ -86,16 +87,26 @@
</StackPanel>
<Button.ToolTip>Open (Ctrl+O)</Button.ToolTip>
</Button>
<Button IsEnabled="{Binding Path=SaveFile, Converter={StaticResource NullToBooleanConverter1}, ConverterParameter=true}">
<Button IsEnabled="{Binding Path=SavePath, Converter={StaticResource NullToBooleanConverter1}, ConverterParameter=true}">
<c:Message.Attach>[Event Click] = [Action WriteSave]</c:Message.Attach>
<StackPanel Orientation="Horizontal">
<Image Width="16"
Height="16"
Source="Resources\fugue\disk-black.png" />
Source="Resources\fugue\disk.png" />
<Label Content="Save" />
</StackPanel>
<Button.ToolTip>Save (Ctrl+S)</Button.ToolTip>
</Button>
<Button IsEnabled="{Binding Path=SaveFile, Converter={StaticResource NullToBooleanConverter1}, ConverterParameter=true}">
<c:Message.Attach>[Event Click] = [Action WriteSaveAs]</c:Message.Attach>
<StackPanel Orientation="Horizontal">
<Image Width="16"
Height="16"
Source="Resources\fugue\disk-rename.png" />
<Label Content="Save As" />
</StackPanel>
<Button.ToolTip>Save As (Ctrl+Shift+S)</Button.ToolTip>
</Button>
</ToolBar>
<TabControl TabStripPlacement="Top">
<TabItem IsSelected="{Binding Path=IsGeneralSelected}">
Expand Down
158 changes: 102 additions & 56 deletions projects/Gibbed.Borderlands2.SaveEdit/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public AboutViewModel About

#region Fields
private SaveLoad _SaveLoad;
private string _SavePath;
private SaveFile _SaveFile;
private readonly ICommand _NewSaveFromPlayerClass;

Expand All @@ -172,6 +173,19 @@ public IEnumerable<PlayerClassDefinition> PlayerClasses
}
}

public string SavePath
{
get { return this._SavePath; }
private set
{
if (this._SavePath != value)
{
this._SavePath = value;
this.NotifyOfPropertyChange(nameof(SavePath));
}
}
}

public SaveFile SaveFile
{
get { return this._SaveFile; }
Expand Down Expand Up @@ -350,6 +364,7 @@ public IEnumerable<IResult> ReadSave()
this.Backpack.ImportData(saveFile.SaveGame, saveFile.Platform);
this.Bank.ImportData(saveFile.SaveGame, saveFile.Platform);
this.FastTravel.ImportData(saveFile.SaveGame);
this.SavePath = fileName;
this.SaveFile = saveFile;
this.MaybeSwitchToGeneral();
}
Expand Down Expand Up @@ -388,7 +403,6 @@ public IEnumerable<IResult> ReadSave()
.WithIcon(MessageBoxImage.Information);
}


if (this.SaveFile != null &&
this.Backpack.BrokenWeapons.Count > 0)
{
Expand Down Expand Up @@ -416,11 +430,12 @@ public IEnumerable<IResult> ReadSave()
else if (result == MessageBoxResult.Cancel)
{
var sb = new StringBuilder();
this.Backpack.BrokenWeapons.ForEach(kv =>
{
sb.AppendLine(kv.Value.ToString());
sb.AppendLine();
});
this.Backpack.BrokenWeapons.ForEach(
kv =>
{
sb.AppendLine(kv.Value.ToString());
sb.AppendLine();
});
if (MyClipboard.SetText(sb.ToString()) != MyClipboard.Result.Success)
{
MessageBox.Show("Clipboard failure.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Expand Down Expand Up @@ -457,11 +472,12 @@ public IEnumerable<IResult> ReadSave()
else if (result == MessageBoxResult.Cancel)
{
var sb = new StringBuilder();
this.Backpack.BrokenItems.ForEach(kv =>
{
sb.AppendLine(kv.Value.ToString());
sb.AppendLine();
});
this.Backpack.BrokenItems.ForEach(
kv =>
{
sb.AppendLine(kv.Value.ToString());
sb.AppendLine();
});
if (MyClipboard.SetText(sb.ToString()) != MyClipboard.Result.Success)
{
MessageBox.Show("Clipboard failure.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Expand Down Expand Up @@ -498,11 +514,12 @@ public IEnumerable<IResult> ReadSave()
else if (result == MessageBoxResult.Cancel)
{
var sb = new StringBuilder();
this.Bank.BrokenSlots.ForEach(kv =>
{
sb.AppendLine(kv.Value.ToString());
sb.AppendLine();
});
this.Bank.BrokenSlots.ForEach(
kv =>
{
sb.AppendLine(kv.Value.ToString());
sb.AppendLine();
});
if (MyClipboard.SetText(sb.ToString()) != MyClipboard.Result.Success)
{
MessageBox.Show("Clipboard failure.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Expand All @@ -514,20 +531,41 @@ public IEnumerable<IResult> ReadSave()
}

public IEnumerable<IResult> WriteSave()
{
if (this.SaveFile == null || string.IsNullOrEmpty(this.SavePath) == true)
{
yield break;
}

var savePath = this.SavePath;
var saveFile = this.SaveFile;

yield return new DelegateResult(
() => WriteSave(savePath, saveFile))
.Rescue()
.Execute(
x =>
new MyMessageBox(
$"An exception was thrown (press Ctrl+C to copy):\n\n{x.ToString()}",
"Error")
.WithIcon(MessageBoxImage.Error).AsCoroutine());
}

public IEnumerable<IResult> WriteSaveAs()
{
if (this.SaveFile == null)
{
yield break;
}

string fileName = null;
string savePath = null;

foreach (var result in this.SaveLoad.SaveFile(s => fileName = s))
foreach (var result in this.SaveLoad.SaveFile(s => savePath = s))
{
yield return result;
}

if (fileName == null)
if (savePath == null)
{
yield break;
}
Expand All @@ -537,48 +575,56 @@ public IEnumerable<IResult> WriteSave()
yield return new DelegateResult(
() =>
{
this.General.ExportData(saveFile.SaveGame, out var platform);
this.Character.ExportData(saveFile.SaveGame);
this.Vehicle.ExportData(saveFile.SaveGame);
this.CurrencyOnHand.ExportData(saveFile.SaveGame);
this.Backpack.ExportData(saveFile.SaveGame, platform);
this.Bank.ExportData(saveFile.SaveGame, platform);
this.FastTravel.ExportData(saveFile.SaveGame);

if (saveFile.SaveGame != null &&
saveFile.SaveGame.WeaponData != null)
{
saveFile.SaveGame.WeaponData.RemoveAll(
wd =>
Blacklisting.IsBlacklistedType(wd.Type) == true ||
Blacklisting.IsBlacklistedBalance(wd.Balance) == true);
}

if (saveFile.SaveGame != null &&
saveFile.SaveGame.ItemData != null)
{
saveFile.SaveGame.ItemData.RemoveAll(
wd =>
Blacklisting.IsBlacklistedType(wd.Type) == true ||
Blacklisting.IsBlacklistedBalance(wd.Balance) == true);
}

using (var output = File.Create(fileName))
{
FileFormats.SaveExpansion.AddExpansionSavedataToUnloadableItemData(
saveFile.SaveGame);
saveFile.Platform = platform;
saveFile.Serialize(output);
FileFormats.SaveExpansion
.ExtractExpansionSavedataFromUnloadableItemData(
saveFile.SaveGame);
}
}).Rescue().Execute(
this.WriteSave(savePath, saveFile);
this.SavePath = savePath;
})
.Rescue()
.Execute(
x =>
new MyMessageBox(
$"An exception was thrown (press Ctrl+C to copy):\n\n{x.ToString()}",
"Error")
.WithIcon(MessageBoxImage.Error).AsCoroutine());
}

private void WriteSave(string savePath, SaveFile saveFile)
{
this.General.ExportData(saveFile.SaveGame, out var platform);
this.Character.ExportData(saveFile.SaveGame);
this.Vehicle.ExportData(saveFile.SaveGame);
this.CurrencyOnHand.ExportData(saveFile.SaveGame);
this.Backpack.ExportData(saveFile.SaveGame, platform);
this.Bank.ExportData(saveFile.SaveGame, platform);
this.FastTravel.ExportData(saveFile.SaveGame);

if (saveFile.SaveGame != null &&
saveFile.SaveGame.WeaponData != null)
{
saveFile.SaveGame.WeaponData.RemoveAll(
wd =>
Blacklisting.IsBlacklistedType(wd.Type) == true ||
Blacklisting.IsBlacklistedBalance(wd.Balance) == true);
}

if (saveFile.SaveGame != null &&
saveFile.SaveGame.ItemData != null)
{
saveFile.SaveGame.ItemData.RemoveAll(
wd =>
Blacklisting.IsBlacklistedType(wd.Type) == true ||
Blacklisting.IsBlacklistedBalance(wd.Balance) == true);
}

using (var output = File.Create(savePath))
{
FileFormats.SaveExpansion.AddExpansionSavedataToUnloadableItemData(
saveFile.SaveGame);
saveFile.Platform = platform;
saveFile.Serialize(output);
FileFormats.SaveExpansion
.ExtractExpansionSavedataFromUnloadableItemData(
saveFile.SaveGame);
}
}
}
}

0 comments on commit 41447d8

Please sign in to comment.