Skip to content

Commit

Permalink
Revamp config verification process
Browse files Browse the repository at this point in the history
- Config file is now verified within the Settings menu rather than through the main window
- Verification error message is now more specific
- You can no longer use the launcher while an invalid configuration is active
  • Loading branch information
WumboSpasm committed Dec 27, 2022
1 parent 39e530b commit 323e73b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 77 deletions.
30 changes: 28 additions & 2 deletions src/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public static class Config
public static string CLIFpPath { get; set; } = @".\CLIFp\CLIFp.exe";
public static string FlashpointServer { get; set; } = "http://infinity.unstable.life/Flashpoint";

public static bool NeedsRefresh { get; set; } = true;
public static bool Configured { get; set; } = false;
public static bool Initialized { get; set; } = false;

public static readonly object configJsonLock = new object();

Expand All @@ -39,7 +40,6 @@ public static void Read()
{
FlashpointServer = (string)readConfig["FlashpointServer"];
}

}
}
}
Expand All @@ -65,5 +65,31 @@ public static void Write()
}
}
}

public static bool[] Validate(string flashpointPath, string clifpPath)
{
bool pathValid = false;
bool clifpValid = false;

string databasePath = flashpointPath + @"\Data\flashpoint.sqlite";
byte[] header = new byte[16];

if (File.Exists(databasePath))
{
using (var fileStream = new FileStream(databasePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
fileStream.Read(header, 0, 16);
}

pathValid = Encoding.ASCII.GetString(header).Contains("SQLite format");
}

if (File.Exists(clifpPath) && clifpPath.ToLower().EndsWith(".exe"))
{
clifpValid = true;
}

return new bool[] { pathValid, clifpValid };
}
}
}
95 changes: 31 additions & 64 deletions src/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ private void Main_load(object sender, EventArgs e)
Config.Write();
}

// Validate the configuration file and open the settings menu if necessary.

if (Config.Validate(Config.FlashpointPath, Config.CLIFpPath).All(v => v))
{
Config.Configured = true;
}
else
{
MessageBox.Show(
"You must specify the paths to Flashpoint and CLIFp in order to proceed.",
"Notice", MessageBoxButtons.OK, MessageBoxIcon.Information
);

OpenSettings();

if (!Config.Configured)
{
Environment.Exit(0);
}
}

// Initialize list columns and their widths.

for (int i = 0; i < ArchiveList.Columns.Count; i++)
Expand Down Expand Up @@ -128,7 +149,7 @@ private void TabControl_tabChanged(object sender, EventArgs e)
{
if (((TabControl)sender).SelectedIndex == 1)
{
if (Config.NeedsRefresh)
if (!Config.Initialized)
{
InitializeDatabase();
}
Expand Down Expand Up @@ -168,7 +189,7 @@ private void SettingsMenu_formClosed(object sender, FormClosedEventArgs e)
{
Config.Read();

if (TabControl.SelectedIndex == 1 && Config.NeedsRefresh)
if (TabControl.SelectedIndex == 1 && !Config.Initialized)
{
InitializeDatabase();
}
Expand Down Expand Up @@ -284,11 +305,6 @@ private void ArchiveList_itemSelect(object sender, EventArgs e)
// Launch the selected entry.
private void ArchiveList_itemAccess(object sender, EventArgs e)
{
if (!InitializeCLIFp())
{
return;
}

string entryID;
lock (queryCacheLock)
{
Expand Down Expand Up @@ -338,11 +354,6 @@ private void AlternateButton_click(object sender, EventArgs e)
// Launch an additional application.
private void AlternateMenu_itemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (!InitializeCLIFp())
{
return;
}

var entry = (AddApp)e.ClickedItem.Tag;

LaunchEntry.StartInfo.Arguments = $"play -i {entry.ID}";
Expand Down Expand Up @@ -523,44 +534,15 @@ private void ArchiveImages_click(object sender, EventArgs e)

#region Form Component-changing Functions

// Check if database is valid and load if so
// Load the database and filters, and prepare the list display.
// Adapted from https://stackoverflow.com/a/70291358
private void InitializeDatabase()
{
if (Config.NeedsRefresh)
{
Config.NeedsRefresh = false;
}

string databasePath = Config.FlashpointPath + @"\Data\flashpoint.sqlite";
byte[] header = new byte[16];

if (File.Exists(databasePath))
{
using (var fileStream = new FileStream(databasePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
fileStream.Read(header, 0, 16);
}

if (Encoding.ASCII.GetString(header).Contains("SQLite format"))
{
LoadFilteredTags();
RefreshDatabase_Block();

prevWidth = ArchiveList.ClientSize.Width;

return;
}
}

ArchiveList.VirtualListSize = 0;
ClearInfoPanel();

MessageBox.Show("Database is either corrupted or missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
TabControl.SelectTab(0);
OpenSettings();
LoadFilteredTags();
RefreshDatabase_Block();

Config.NeedsRefresh = true;
prevWidth = ArchiveList.ClientSize.Width;
Config.Initialized = true;
}

// Generate a new cache and refresh list.
Expand Down Expand Up @@ -674,21 +656,6 @@ public void RefreshDatabase_OnThread_BlockBased(bool colChanged, bool playsCheck
}
}

private bool InitializeCLIFp()
{
if (File.Exists(Config.CLIFpPath))
{
LaunchEntry.StartInfo.FileName = Config.CLIFpPath;
return true;
}
else
{
MessageBox.Show("CLIFp not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
OpenSettings();
return false;
}
}

// TODO: mess with this, ensure it escapes stuff properly.
private void ExecuteSearchQuery()
{
Expand Down Expand Up @@ -978,9 +945,9 @@ private void LoadFilteredTags()
if (errorState)
{
MessageBox.Show(
"sharpFilters.json was not found, and as a result the archive will be unfiltered. Use at your own risk.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning
);
"sharpFilters.json was not found, and as a result the archive will be unfiltered. Use at your own risk.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning
);
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,21 @@ private void SettingsTabControl_tabChanged(object sender, EventArgs e)
SetDownloadedFileSizes();
}

// Write values to file and close the menu when the OK button is clicked.
// Verify and save the new configuration if the OK button is clicked, then close the menu.
private void OKButton_click(object sender, EventArgs e)
{
// Check if database and CLIFp exist under Flashpoint path
if (!File.Exists(PathInput.Text + @"\Data\flashpoint.sqlite")
|| !File.Exists(CLIFpInput.Text)
|| !CLIFpInput.Text.Contains(PathInput.Text))
bool[] valid = Config.Validate(PathInput.Text, CLIFpInput.Text);

if (!valid.All(v => v))
{
DialogResult warningResult = MessageBox.Show(
"One or more values are invalid. Continue?",
"Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation
"The following values are invalid:" +
(valid[0] ? "" : "\n- Path to Flashpoint folder") +
(valid[1] ? "" : "\n- Path to CLIFp"),
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Error
);

if (warningResult == DialogResult.No)
return;
return;
}

Config.FlashpointPath = PathInput.Text;
Expand Down Expand Up @@ -173,13 +173,14 @@ private void OKButton_click(object sender, EventArgs e)
}
}

Config.NeedsRefresh = true;
Config.Configured = true;
Config.Initialized = false;

Close();
}

// Close menu without saving if the Cancel button is clicked.
private void CancelButton_click(object sender, EventArgs e) { Close(); }
private void CancelButton_click(object sender, EventArgs e) => Close();

// Fill in total file sizes in the Data tab.
private void SetDownloadedFileSizes()
Expand Down

0 comments on commit 323e73b

Please sign in to comment.