Add automatic backup, add program settings editor

This commit is contained in:
Kurt
2020-04-21 12:11:31 -07:00
parent 3a68919325
commit eaa805a374
11 changed files with 365 additions and 24 deletions

View File

@@ -87,5 +87,12 @@ public string GetSaveTitle(string prefix)
return $"{prefix} - {townName} @ {timestamp}";
}
public string GetBackupFolderTitle()
{
var townName = Players[0].Personal.TownName;
var timestamp = Main.LastSaved.TimeStamp.Replace(':', '.');
return StringUtil.CleanFileName($"{townName} - {timestamp}");
}
}
}

View File

@@ -32,7 +32,6 @@ private void InitializeComponent()
this.components = new System.ComponentModel.Container();
this.Menu_Editor = new System.Windows.Forms.MenuStrip();
this.Menu_File = new System.Windows.Forms.ToolStripMenuItem();
this.Menu_Open = new System.Windows.Forms.ToolStripMenuItem();
this.Menu_Save = new System.Windows.Forms.ToolStripMenuItem();
this.Menu_Tools = new System.Windows.Forms.ToolStripMenuItem();
this.Menu_DumpDecrypted = new System.Windows.Forms.ToolStripMenuItem();
@@ -71,6 +70,7 @@ private void InitializeComponent()
this.NUD_PatternIndex = new System.Windows.Forms.NumericUpDown();
this.PB_Pattern = new System.Windows.Forms.PictureBox();
this.Tab_Map = new System.Windows.Forms.TabPage();
this.B_EditLandFlags = new System.Windows.Forms.Button();
this.L_PlayerHouse = new System.Windows.Forms.Label();
this.NUD_PlayerHouse = new System.Windows.Forms.NumericUpDown();
this.B_LoadHouse = new System.Windows.Forms.Button();
@@ -81,7 +81,9 @@ private void InitializeComponent()
this.B_EditTurnipExchange = new System.Windows.Forms.Button();
this.B_EditBuildings = new System.Windows.Forms.Button();
this.B_RecycleBin = new System.Windows.Forms.Button();
this.B_EditLandFlags = new System.Windows.Forms.Button();
this.Menu_Options = new System.Windows.Forms.ToolStripMenuItem();
this.Menu_Settings = new System.Windows.Forms.ToolStripMenuItem();
this.Menu_Language = new System.Windows.Forms.ToolStripComboBox();
this.Menu_Editor.SuspendLayout();
this.TC_Editors.SuspendLayout();
this.Tab_Players.SuspendLayout();
@@ -103,7 +105,8 @@ private void InitializeComponent()
this.Menu_Editor.BackColor = System.Drawing.SystemColors.Control;
this.Menu_Editor.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Menu_File,
this.Menu_Tools});
this.Menu_Tools,
this.Menu_Options});
this.Menu_Editor.Location = new System.Drawing.Point(0, 0);
this.Menu_Editor.Name = "Menu_Editor";
this.Menu_Editor.Size = new System.Drawing.Size(404, 24);
@@ -113,25 +116,16 @@ private void InitializeComponent()
// Menu_File
//
this.Menu_File.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Menu_Open,
this.Menu_Save});
this.Menu_File.Name = "Menu_File";
this.Menu_File.Size = new System.Drawing.Size(37, 20);
this.Menu_File.Text = "File";
//
// Menu_Open
//
this.Menu_Open.Name = "Menu_Open";
this.Menu_Open.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
this.Menu_Open.Size = new System.Drawing.Size(146, 22);
this.Menu_Open.Text = "Open";
this.Menu_Open.Click += new System.EventHandler(this.Menu_Open_Click);
//
// Menu_Save
//
this.Menu_Save.Name = "Menu_Save";
this.Menu_Save.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
this.Menu_Save.Size = new System.Drawing.Size(146, 22);
this.Menu_Save.Size = new System.Drawing.Size(180, 22);
this.Menu_Save.Text = "Save";
this.Menu_Save.Click += new System.EventHandler(this.Menu_Save_Click);
//
@@ -543,6 +537,16 @@ private void InitializeComponent()
this.Tab_Map.Text = "Map";
this.Tab_Map.UseVisualStyleBackColor = true;
//
// B_EditLandFlags
//
this.B_EditLandFlags.Location = new System.Drawing.Point(202, 76);
this.B_EditLandFlags.Name = "B_EditLandFlags";
this.B_EditLandFlags.Size = new System.Drawing.Size(92, 40);
this.B_EditLandFlags.TabIndex = 53;
this.B_EditLandFlags.Text = "Edit Flags";
this.B_EditLandFlags.UseVisualStyleBackColor = true;
this.B_EditLandFlags.Click += new System.EventHandler(this.B_EditLandFlags_Click);
//
// L_PlayerHouse
//
this.L_PlayerHouse.Location = new System.Drawing.Point(16, 6);
@@ -649,15 +653,31 @@ private void InitializeComponent()
this.B_RecycleBin.UseVisualStyleBackColor = true;
this.B_RecycleBin.Click += new System.EventHandler(this.B_RecycleBin_Click);
//
// B_EditLandFlags
// Menu_Options
//
this.B_EditLandFlags.Location = new System.Drawing.Point(202, 76);
this.B_EditLandFlags.Name = "B_EditLandFlags";
this.B_EditLandFlags.Size = new System.Drawing.Size(92, 40);
this.B_EditLandFlags.TabIndex = 53;
this.B_EditLandFlags.Text = "Edit Flags";
this.B_EditLandFlags.UseVisualStyleBackColor = true;
this.B_EditLandFlags.Click += new System.EventHandler(this.B_EditLandFlags_Click);
this.Menu_Options.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Menu_Language,
this.Menu_Settings});
this.Menu_Options.Name = "Menu_Options";
this.Menu_Options.Size = new System.Drawing.Size(61, 20);
this.Menu_Options.Text = "Options";
//
// Menu_Settings
//
this.Menu_Settings.Name = "Menu_Settings";
this.Menu_Settings.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.P)));
this.Menu_Settings.Size = new System.Drawing.Size(180, 22);
this.Menu_Settings.Text = "Settings";
this.Menu_Settings.Click += new System.EventHandler(this.Menu_Settings_Click);
//
// Menu_Language
//
this.Menu_Language.Enabled = false;
this.Menu_Language.Items.AddRange(new object[] {
"English"});
this.Menu_Language.Name = "Menu_Language";
this.Menu_Language.Size = new System.Drawing.Size(115, 23);
this.Menu_Language.Text = "Language";
//
// Editor
//
@@ -698,7 +718,6 @@ private void InitializeComponent()
private System.Windows.Forms.MenuStrip Menu_Editor;
private System.Windows.Forms.ToolStripMenuItem Menu_File;
private System.Windows.Forms.ToolStripMenuItem Menu_Open;
private System.Windows.Forms.ToolStripMenuItem Menu_Save;
private System.Windows.Forms.ToolStripMenuItem Menu_Tools;
private System.Windows.Forms.TabControl TC_Editors;
@@ -748,6 +767,9 @@ private void InitializeComponent()
private System.Windows.Forms.Button B_DumpHouse;
private System.Windows.Forms.Label L_PlayerHouse;
private System.Windows.Forms.Button B_EditLandFlags;
private System.Windows.Forms.ToolStripMenuItem Menu_Options;
private System.Windows.Forms.ToolStripComboBox Menu_Language;
private System.Windows.Forms.ToolStripMenuItem Menu_Settings;
}
}

View File

@@ -25,11 +25,13 @@ public Editor(HorizonSave file)
Villagers = LoadVillagers();
Text = SAV.GetSaveTitle("NHSE");
Menu_Language.SelectedIndex = 0; // en
}
private void Menu_Open_Click(object sender, EventArgs e)
private void Menu_Settings_Click(object sender, EventArgs e)
{
WinFormsUtil.Alert("I don't do anything yet!");
using var editor = new SettingsEditor();
editor.ShowDialog();
}
private void Menu_Save_Click(object sender, EventArgs e)

View File

@@ -12,6 +12,10 @@ namespace NHSE.WinForms
/// </summary>
public partial class Main : Form
{
public const string BackupFolderName = "bak";
public static readonly string WorkingDirectory = Application.StartupPath;
public static readonly string BackupPath = Path.Combine(WorkingDirectory, BackupFolderName);
public Main()
{
InitializeComponent();
@@ -127,9 +131,30 @@ private static void OpenSaveFile(string path)
var settings = Settings.Default;
settings.LastFilePath = path;
if (!settings.BackupPrompted)
{
settings.BackupPrompted = true;
var line1 = string.Format(MessageStrings.MsgBackupCreateLocation, BackupFolderName);
var line2 = MessageStrings.MsgBackupCreateQuestion;
var prompt = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, line1, line2);
settings.AutomaticBackup = prompt == DialogResult.Yes;
}
if (settings.AutomaticBackup)
BackupSaveFile(file, path, BackupPath);
settings.Save();
}
private static void BackupSaveFile(HorizonSave file, string path, string bak)
{
Directory.CreateDirectory(bak);
var dest = Path.Combine(bak, file.GetBackupFolderTitle());
if (!Directory.Exists(dest))
FileUtil.CopyFolder(path, dest);
}
private void Main_KeyDown(object sender, KeyEventArgs e)
{
if (ModifierKeys != Keys.Control)
@@ -157,6 +182,12 @@ private void Main_KeyDown(object sender, KeyEventArgs e)
editor.ShowDialog();
break;
}
case Keys.P:
{
using var editor = new SettingsEditor();
editor.ShowDialog();
break;
}
}
}
}

View File

@@ -94,5 +94,29 @@ internal sealed partial class Settings : global::System.Configuration.Applicatio
this["SysBotGenericOffset"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool AutomaticBackup {
get {
return ((bool)(this["AutomaticBackup"]));
}
set {
this["AutomaticBackup"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool BackupPrompted {
get {
return ((bool)(this["BackupPrompted"]));
}
set {
this["BackupPrompted"] = value;
}
}
}
}

View File

@@ -20,5 +20,11 @@
<Setting Name="SysBotGenericOffset" Type="System.UInt32" Scope="User">
<Value Profile="(Default)">2889584832</Value>
</Setting>
<Setting Name="AutomaticBackup" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="BackupPrompted" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@@ -0,0 +1,65 @@
namespace NHSE.WinForms
{
partial class SettingsEditor
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.PG_Settings = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// PG_Settings
//
this.PG_Settings.Dock = System.Windows.Forms.DockStyle.Fill;
this.PG_Settings.Location = new System.Drawing.Point(0, 0);
this.PG_Settings.Name = "PG_Settings";
this.PG_Settings.Size = new System.Drawing.Size(322, 310);
this.PG_Settings.TabIndex = 0;
//
// SettingsEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(322, 310);
this.Controls.Add(this.PG_Settings);
this.Icon = global::NHSE.WinForms.Properties.Resources.icon;
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SettingsEditor";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Settings Editor";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SettingsEditor_FormClosing);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SettingsEditor_KeyDown);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PropertyGrid PG_Settings;
}
}

View File

@@ -0,0 +1,28 @@
using System.Windows.Forms;
using NHSE.WinForms.Properties;
namespace NHSE.WinForms
{
public partial class SettingsEditor : Form
{
private readonly Settings obj;
public SettingsEditor()
{
obj = Settings.Default;
InitializeComponent();
PG_Settings.SelectedObject = obj;
}
private void SettingsEditor_FormClosing(object sender, FormClosingEventArgs e)
{
obj.Save();
}
private void SettingsEditor_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W && ModifierKeys == Keys.Control)
Close();
}
}
}

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -0,0 +1,28 @@
using System.IO;
namespace NHSE.WinForms
{
public static class FileUtil
{
public static void CopyFolder(string source, string dest, bool subfolders = true)
{
Directory.CreateDirectory(dest);
var dir = new DirectoryInfo(source);
foreach (var file in dir.EnumerateFiles())
{
var path = Path.Combine(dest, file.Name);
file.CopyTo(path, false);
}
if (!subfolders)
return;
foreach (var folder in dir.EnumerateDirectories())
{
var path = Path.Combine(dest, folder.Name);
CopyFolder(folder.FullName, path);
}
}
}
}

View File

@@ -0,0 +1,8 @@
namespace NHSE.WinForms
{
public static class MessageStrings
{
public static string MsgBackupCreateLocation { get; set; } = "NHSE can perform automatic backups if you create a folder with the name '{0}' in the same folder as NHSE's executable.";
public static string MsgBackupCreateQuestion { get; set; } = "Would you NHSE to automatically keep a backup of your save data?";
}
}