Add dump/load for inventory, add dump for villager

This commit is contained in:
Kurt
2020-03-28 18:25:24 -07:00
parent d4a2a3a480
commit b493b44914
5 changed files with 138 additions and 7 deletions

View File

@@ -51,5 +51,21 @@ private static void Dump(string path, byte[] data, string name)
var file = Path.Combine(path, name);
File.WriteAllBytes(file, data);
}
/// <summary>
/// Dumps all villagers in their decrypted state to the requested <see cref="path"/>.
/// </summary>
/// <param name="sav">Save Data to dump from</param>
/// <param name="path">Path to dump to</param>
public static void DumpVillagers(this MainSave sav, string path)
{
for (int i = 0; i < 10; i++)
{
var v = sav.Offsets.ReadVillager(sav.Data, i);
var name = GameInfo.Strings.GetVillager(v.InternalName);
var dest = Path.Combine(path, $"{name}.nhv");
File.WriteAllBytes(dest, v.Data);
}
}
}
}

View File

@@ -67,6 +67,7 @@ private void InitializeComponent()
this.PB_Villager = new System.Windows.Forms.PictureBox();
this.L_VillagerID = new System.Windows.Forms.Label();
this.NUD_Villager = new System.Windows.Forms.NumericUpDown();
this.B_DumpVillager = new System.Windows.Forms.Button();
this.Menu_Editor.SuspendLayout();
this.TC_Editors.SuspendLayout();
this.Tab_Players.SuspendLayout();
@@ -321,6 +322,7 @@ private void InitializeComponent()
//
// Tab_Villagers
//
this.Tab_Villagers.Controls.Add(this.B_DumpVillager);
this.Tab_Villagers.Controls.Add(this.L_ExternalName);
this.Tab_Villagers.Controls.Add(this.L_InternalName);
this.Tab_Villagers.Controls.Add(this.TB_Catchphrase);
@@ -483,6 +485,16 @@ private void InitializeComponent()
0});
this.NUD_Villager.ValueChanged += new System.EventHandler(this.LoadVillager);
//
// B_DumpVillager
//
this.B_DumpVillager.Location = new System.Drawing.Point(6, 168);
this.B_DumpVillager.Name = "B_DumpVillager";
this.B_DumpVillager.Size = new System.Drawing.Size(100, 40);
this.B_DumpVillager.TabIndex = 24;
this.B_DumpVillager.Text = "Dump Villager";
this.B_DumpVillager.UseVisualStyleBackColor = true;
this.B_DumpVillager.Click += new System.EventHandler(this.B_DumpVillager_Click);
//
// Editor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -557,6 +569,7 @@ private void InitializeComponent()
private System.Windows.Forms.ContextMenuStrip CM_Picture;
private System.Windows.Forms.ToolStripMenuItem Menu_SavePNG;
private System.Windows.Forms.ToolStripMenuItem Menu_VerifyHashes;
private System.Windows.Forms.Button B_DumpVillager;
}
}

View File

@@ -222,7 +222,7 @@ private void Menu_SavePNG_Click(object sender, EventArgs e)
var bmp = pb.Image;
using var sfd = new SaveFileDialog
{
Filter = "png file (*.png) | *.png | All files (*.*) | *.* ",
Filter = "png file (*.png)|*.png|All files (*.*)|*.*",
FileName = $"{name}.png",
};
if (sfd.ShowDialog() != DialogResult.OK)
@@ -230,5 +230,33 @@ private void Menu_SavePNG_Click(object sender, EventArgs e)
bmp.Save(sfd.FileName, ImageFormat.Png);
}
private void B_DumpVillager_Click(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Shift)
{
using var fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() != DialogResult.OK)
return;
var dir = Path.GetDirectoryName(fbd.SelectedPath);
if (dir == null || !Directory.Exists(dir))
return;
SAV.Main.DumpVillagers(dir);
}
var name = L_ExternalName.Text;
using var sfd = new SaveFileDialog
{
Filter = "New Horizons Villager (*.nhv)|*.nhv|All files (*.*)|*.*",
FileName = $"{name}.nhv",
};
if (sfd.ShowDialog() != DialogResult.OK)
return;
SaveVillager(VillagerIndex);
var data = SAV.Main.Offsets.ReadVillager(SAV.Main.Data, VillagerIndex).Data;
File.WriteAllBytes(sfd.FileName, data);
}
}
}

View File

@@ -32,6 +32,8 @@ private void InitializeComponent()
this.B_Save = new System.Windows.Forms.Button();
this.TC_Groups = new System.Windows.Forms.TabControl();
this.ItemEditor = new NHSE.WinForms.ItemEditor();
this.B_Dump = new System.Windows.Forms.Button();
this.B_Load = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// B_Cancel
@@ -78,11 +80,33 @@ private void InitializeComponent()
this.ItemEditor.Size = new System.Drawing.Size(150, 210);
this.ItemEditor.TabIndex = 6;
//
// B_Dump
//
this.B_Dump.Location = new System.Drawing.Point(9, 228);
this.B_Dump.Name = "B_Dump";
this.B_Dump.Size = new System.Drawing.Size(90, 23);
this.B_Dump.TabIndex = 7;
this.B_Dump.Text = "Dump";
this.B_Dump.UseVisualStyleBackColor = true;
this.B_Dump.Click += new System.EventHandler(this.B_Dump_Click);
//
// B_Load
//
this.B_Load.Location = new System.Drawing.Point(105, 228);
this.B_Load.Name = "B_Load";
this.B_Load.Size = new System.Drawing.Size(90, 23);
this.B_Load.TabIndex = 8;
this.B_Load.Text = "Load";
this.B_Load.UseVisualStyleBackColor = true;
this.B_Load.Click += new System.EventHandler(this.B_Load_Click);
//
// PlayerItemEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(584, 261);
this.Controls.Add(this.B_Load);
this.Controls.Add(this.B_Dump);
this.Controls.Add(this.ItemEditor);
this.Controls.Add(this.B_Cancel);
this.Controls.Add(this.B_Save);
@@ -103,5 +127,7 @@ private void InitializeComponent()
private System.Windows.Forms.Button B_Save;
private System.Windows.Forms.TabControl TC_Groups;
private ItemEditor ItemEditor;
private System.Windows.Forms.Button B_Dump;
private System.Windows.Forms.Button B_Load;
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using NHSE.Core;
@@ -11,6 +12,7 @@ public partial class PlayerItemEditor : Form
private readonly Player Player;
private readonly InventorySet[] Inventory;
private readonly string[] items;
private readonly ItemGridEditor[] Editors;
public PlayerItemEditor(Player player)
{
@@ -33,22 +35,27 @@ public PlayerItemEditor(Player player)
}
ItemEditor.Initialize(items);
CreateTabs();
Editors = CreateTabs();
}
private void CreateTabs()
private ItemGridEditor[] CreateTabs()
{
var i = items;
foreach (var p in Inventory)
var itemlist = items;
var editors = new ItemGridEditor[Inventory.Length];
for (var i = 0; i < Inventory.Length; i++)
{
var tab = new TabPage { Name = $"Tab_{p.Type}", Text = p.Type.ToString() };
var grid = new ItemGridEditor(ItemEditor, p.Items, i) { Dock = DockStyle.Fill };
var p = Inventory[i];
var tab = new TabPage {Name = $"Tab_{p.Type}", Text = p.Type.ToString()};
var grid = new ItemGridEditor(ItemEditor, p.Items, itemlist) {Dock = DockStyle.Fill};
grid.InitializeGrid(10, 4);
tab.Controls.Add(grid);
TC_Groups.TabPages.Add(tab);
TC_Groups.ShowToolTips = true;
tab.ToolTipText = p.Type.ToString();
editors[i] = grid;
}
return editors;
}
private static InventorySet[] GetInventory(Player player)
@@ -77,5 +84,46 @@ private void B_Save_Click(object sender, EventArgs e)
SetInventory(Player);
Close();
}
private void B_Dump_Click(object sender, EventArgs e)
{
var selected = TC_Groups.SelectedTab;
using var sfd = new SaveFileDialog
{
Filter = "New Horizons Inventory (*.nhi)|*.nhi|All files (*.*)|*.*",
FileName = $"{selected.Text}.nhi",
};
if (sfd.ShowDialog() != DialogResult.OK)
return;
var index = TC_Groups.SelectedIndex;
var inv = Inventory[index];
var content = inv.Items.ToArray();
var bytes = Item.SetArray(content);
File.WriteAllBytes(sfd.FileName, bytes);
}
private void B_Load_Click(object sender, EventArgs e)
{
var selected = TC_Groups.SelectedTab;
using var sfd = new OpenFileDialog
{
Filter = "New Horizons Inventory (*.nhi)|*.nhi|All files (*.*)|*.*",
FileName = $"{selected.Text}.nhi",
};
if (sfd.ShowDialog() != DialogResult.OK)
return;
var index = TC_Groups.SelectedIndex;
var inv = Inventory[index];
var data = File.ReadAllBytes(sfd.FileName);
var import = Item.GetArray(data);
var copyTo = inv.Items;
for (int i = 0; i < copyTo.Count && i < import.Length ; i++)
copyTo[i].CopyFrom(import[i]);
var grid = Editors[TC_Groups.SelectedIndex];
grid.LoadItems();
System.Media.SystemSounds.Asterisk.Play();
}
}
}