Files
SaveWizard_rewritten/MainForm.cs

242 lines
9.5 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SaveWizard_rewritten
{
public partial class MainForm: Form
{
private readonly string config_file_name = "update.cfg";
[DllImport("SII_Decrypt.dll")]
public static extern int GetFileFormat(string FileName);
[DllImport("SII_Decrypt.dll")]
public static extern int DecryptAndDecodeFile(string InputFile, string OutputFile);
private Dictionary<TextBox, WindowText> items = null;
private string opened_file_path, selected_game;
private Dictionary<string, bool> owns = null;
public MainForm()
{
InitializeComponent();
// Test stuff
//string path = Path.Combine(Environment.CurrentDirectory, "configs");
//if (Directory.Exists(path))
// Directory.Delete(path, true);
//path = Path.Combine(Environment.CurrentDirectory, "update.cfg");
//if (File.Exists(path))
// File.Delete(path);
}
private class WindowText
{
private readonly CheckBox Check;
private readonly string Str;
public WindowText(CheckBox _check, string _str)
{
Check = _check;
Str = _str;
}
public CheckBox GetCheck()
{
return Check;
}
public string GetString()
{
return Str;
}
}
private void boxTextChanged(object sender, EventArgs e)
{
TextBox _sender = sender as TextBox;
items[_sender].GetCheck().Checked = false;
}
private void ValidateBasic(object sender, KeyPressEventArgs e)
{
if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && !char.IsControl(e.KeyChar))
e.Handled = true;
}
private void ValidateSkills(object sender, KeyPressEventArgs e)
{
if (!(e.KeyChar >= '0' && e.KeyChar <= '6') && !char.IsControl(e.KeyChar))
e.Handled = true;
}
private void GetFileData(string filename)
{
RegexHandler.FillLines(File.ReadAllLines(filename).ToList());
foreach (KeyValuePair<TextBox, WindowText> item in items)
{
item.Key.Text = RegexHandler.GetValue(RegexHandler.SearchLine(item.Value.GetString()));
item.Value.GetCheck().Checked = true;
}
if (RegexHandler.SearchLine("company.volatile.stokes.calais") != 0)
selected_game = "ets2";
else if (RegexHandler.SearchLine("company.volatile.ed_mkt.elko") != 0)
selected_game = "ats";
else
selected_game = null;
if (selected_game != null)
{
string config_path = $"configs/{selected_game}/dlc.json";
if (File.Exists(config_path))
{
Dictionary<string, string> dlc = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(config_path));
owns = new Dictionary<string, bool>
{
{ "base", true }
};
List<string> companies = RegexHandler.GetArrayItems(RegexHandler.SearchLine("companies:"));
foreach (string company in companies)
foreach (KeyValuePair<string, string> item in dlc)
if (company.Contains(item.Value))
owns[item.Key] = true;
}
else
MessageBox.Show($"'dlc.json' from '{selected_game}' have errors or not found, functionality has been limited", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
btn_unlock_garages.Enabled = true;
btn_recover_backup.Enabled = true;
btn_apply_changes.Enabled = true;
}
private void ClearData()
{
opened_file_path = null;
RegexHandler.FillLines(null);
selected_game = null;
owns = null;
foreach (KeyValuePair<TextBox, WindowText> item in items)
{
item.Key.Text = "";
item.Value.GetCheck().Checked = true;
}
btn_unlock_garages.Enabled = false;
btn_recover_backup.Enabled = false;
btn_apply_changes.Enabled = false;
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void MainForm_Load(object sender, EventArgs e)
{
items = new Dictionary<TextBox, WindowText>
{
{ txt_money, new WindowText(chk_money, "money_account:") },
{ txt_experience, new WindowText(chk_experience, "experience_points:") },
{ txt_loan_limit, new WindowText(chk_loan_limit, "loan_limit:") },
{ txt_long_distance, new WindowText(chk_long_distance, "long_dist:") },
{ txt_high_value_cargo, new WindowText(chk_high_value_cargo, "heavy:") },
{ txt_fragile_cargo, new WindowText(chk_fragile_cargo, "fragile:") },
{ txt_urgent_delivery, new WindowText(chk_urgent_delivery, "urgent:") },
{ txt_ecodriving, new WindowText(chk_ecodriving, "mechanical:") },
};
KeyPressEventHandler basic = new KeyPressEventHandler(ValidateBasic);
txt_money.KeyPress += basic;
txt_experience.KeyPress += basic;
txt_loan_limit.KeyPress += basic;
//txt_adr.KeyPress += new KeyPressEventHandler(ValidateADR);
KeyPressEventHandler skills = new KeyPressEventHandler(ValidateSkills);
txt_long_distance.KeyPress += skills;
txt_high_value_cargo.KeyPress += skills;
txt_fragile_cargo.KeyPress += skills;
txt_urgent_delivery.KeyPress += skills;
txt_ecodriving.KeyPress += skills;
foreach (TextBox txtbox in items.Keys)
txtbox.TextChanged += boxTextChanged;
Dictionary<string, bool> update_conf = new Dictionary<string, bool>
{
{ "update_on_start", false }
};
string path = Path.Combine(Environment.CurrentDirectory, config_file_name);
if (File.Exists(path))
update_conf = JsonConvert.DeserializeObject<Dictionary<string, bool>>(File.ReadAllText(path));
if (update_conf["update_on_start"])
{
chk_update_on_start.Checked = true;
UpdateForm window = new UpdateForm();
window.ShowDialog();
window.Focus();
}
}
private void chk_update_on_start_CheckedChanged(object sender, EventArgs e)
{
File.WriteAllText(
Path.Combine(Environment.CurrentDirectory, config_file_name),
JsonConvert.SerializeObject(new Dictionary<string, bool>
{
{ "update_on_start", chk_update_on_start.Checked }
})
);
}
private void btn_open_save_Click(object sender, EventArgs e)
{
ClearData();
OpenFileDialog ofd = new OpenFileDialog()
{
Title = "Выберите файл сохранения",
Filter = "Сохранение в формате SII|game.sii",
Multiselect = false,
};
if (ofd.ShowDialog() == DialogResult.OK)
{
if (GetFileFormat(ofd.FileName) == 2)
{
if (DecryptAndDecodeFile(ofd.FileName, ofd.FileName) != 0)
{
MessageBox.Show("Something went wrong with decrypting file. Try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
GetFileData(ofd.FileName);
opened_file_path = ofd.FileName;
}
}
private void btn_recover_backup_Click(object sender, EventArgs e)
{
string backup_path = opened_file_path + ".swbak";
if (!File.Exists(backup_path))
{
MessageBox.Show("Backup not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
File.WriteAllLines(opened_file_path, File.ReadAllLines(backup_path));
File.Delete(backup_path);
MessageBox.Show("Backup successfully recovered.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetFileData(opened_file_path);
}
private void btn_apply_changes_Click(object sender, EventArgs e)
{
foreach (KeyValuePair<TextBox, WindowText> item in items)
if (!item.Value.GetCheck().Checked)
RegexHandler.SetValue(RegexHandler.SearchLine(item.Value.GetString()), item.Key.Text);
File.WriteAllLines(opened_file_path + ".swbak", RegexHandler.GetBackup());
File.WriteAllLines(opened_file_path, RegexHandler.GetAllLines());
MessageBox.Show("Changes successfully applied.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetFileData(opened_file_path);
}
}
}