Edit the codes from code review result

This commit is contained in:
Namhyeon Go 2025-03-30 22:24:35 +09:00
parent b0e50d33bc
commit 5d717505f4
3 changed files with 97 additions and 89 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
namespace WelsonJS.Launcher namespace WelsonJS.Launcher
@ -9,6 +10,7 @@ namespace WelsonJS.Launcher
{ {
private Dictionary<string, string> userVariables = new Dictionary<string, string>(); private Dictionary<string, string> userVariables = new Dictionary<string, string>();
private string tempFilePath; private string tempFilePath;
private readonly Encoding defaultEncoding = Encoding.UTF8;
public EnvForm() public EnvForm()
{ {
@ -24,11 +26,11 @@ namespace WelsonJS.Launcher
// Initialize ListView // Initialize ListView
private void InitializeListView() private void InitializeListView()
{ {
listView1.View = View.Details; lvUserDefinedVariables.View = View.Details;
listView1.FullRowSelect = true; lvUserDefinedVariables.FullRowSelect = true;
listView1.Columns[0].Width = 150; lvUserDefinedVariables.Columns[0].Width = 150;
listView1.Columns[1].Width = 220; lvUserDefinedVariables.Columns[1].Width = 220;
listView1.SelectedIndexChanged += ListView1_SelectedIndexChanged; lvUserDefinedVariables.SelectedIndexChanged += ListView1_SelectedIndexChanged;
} }
// Load user-defined variables from the temporary folder in .env format // Load user-defined variables from the temporary folder in .env format
@ -86,21 +88,21 @@ namespace WelsonJS.Launcher
// Update ListView with current variables // Update ListView with current variables
private void UpdateListView() private void UpdateListView()
{ {
listView1.Items.Clear(); lvUserDefinedVariables.Items.Clear();
foreach (var variable in userVariables) foreach (var variable in userVariables)
{ {
var item = new ListViewItem(variable.Key); var item = new ListViewItem(variable.Key);
item.SubItems.Add(variable.Value); item.SubItems.Add(variable.Value);
listView1.Items.Add(item); lvUserDefinedVariables.Items.Add(item);
} }
} }
// Handle ListView selection change // Handle ListView selection change
private void ListView1_SelectedIndexChanged(object sender, EventArgs e) private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{ {
if (listView1.SelectedItems.Count > 0) if (lvUserDefinedVariables.SelectedItems.Count > 0)
{ {
var selectedItem = listView1.SelectedItems[0]; var selectedItem = lvUserDefinedVariables.SelectedItems[0];
textSetName.Text = selectedItem.Text; textSetName.Text = selectedItem.Text;
textSetValue.Text = selectedItem.SubItems[1].Text; textSetValue.Text = selectedItem.SubItems[1].Text;
checkDeleteVariable.Checked = false; checkDeleteVariable.Checked = false;
@ -166,7 +168,7 @@ namespace WelsonJS.Launcher
} }
// Write lines to the file // Write lines to the file
File.WriteAllLines(tempFilePath, lines); File.WriteAllLines(tempFilePath, lines, defaultEncoding);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -185,68 +187,74 @@ namespace WelsonJS.Launcher
// Handle "Open Directory" button click // Handle "Open Directory" button click
private void btnOpenDirectory_Click(object sender, EventArgs e) private void btnOpenDirectory_Click(object sender, EventArgs e)
{ {
var folderDialog = new FolderBrowserDialog(); using (var folderDialog = new FolderBrowserDialog())
if (folderDialog.ShowDialog() == DialogResult.OK)
{ {
textSetValue.Text = folderDialog.SelectedPath; if (folderDialog.ShowDialog() == DialogResult.OK)
{
textSetValue.Text = folderDialog.SelectedPath;
}
} }
} }
// Handle "Open File" button click // Handle "Open File" button click
private void btnOpenFile_Click(object sender, EventArgs e) private void btnOpenFile_Click(object sender, EventArgs e)
{ {
var fileDialog = new OpenFileDialog(); using (var fileDialog = new OpenFileDialog())
if (fileDialog.ShowDialog() == DialogResult.OK)
{ {
textSetName.Text = Path.GetFileName(fileDialog.FileName); if (fileDialog.ShowDialog() == DialogResult.OK)
textSetValue.Text = fileDialog.FileName; {
textSetName.Text = Path.GetFileName(fileDialog.FileName);
textSetValue.Text = fileDialog.FileName;
}
} }
} }
private void btnImport_Click(object sender, EventArgs e) private void btnImport_Click(object sender, EventArgs e)
{ {
OpenFileDialog openFileDialog = new OpenFileDialog(); using (var openFileDialog = new OpenFileDialog())
openFileDialog.Filter = "Env files (*.env)|*.env|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
try openFileDialog.Filter = "Env files (*.env)|*.env|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
// Load variables from the selected file try
string filePath = openFileDialog.FileName;
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{ {
// Skip empty lines // Load variables from the selected file
if (string.IsNullOrWhiteSpace(line)) continue; string filePath = openFileDialog.FileName;
string[] lines = File.ReadAllLines(filePath, defaultEncoding);
int indexOfEquals = line.IndexOf('='); foreach (string line in lines)
if (indexOfEquals != -1)
{ {
string key = line.Substring(0, indexOfEquals).Trim(); // Skip empty lines
string value = line.Substring(indexOfEquals + 1).Trim(); if (string.IsNullOrWhiteSpace(line)) continue;
// Remove surrounding quotes if present int indexOfEquals = line.IndexOf('=');
if (value.StartsWith("\"") && value.EndsWith("\"")) if (indexOfEquals != -1)
{ {
value = value.Substring(1, value.Length - 2); string key = line.Substring(0, indexOfEquals).Trim();
string value = line.Substring(indexOfEquals + 1).Trim();
// Remove surrounding quotes if present
if (value.StartsWith("\"") && value.EndsWith("\""))
{
value = value.Substring(1, value.Length - 2);
}
// Unescape double quotes in the value
value = value.Replace("\\\"", "\"");
// Update or add the key-value pair
userVariables[key] = value;
} }
// Unescape double quotes in the value
value = value.Replace("\\\"", "\"");
// Update or add the key-value pair
userVariables[key] = value;
} }
}
// Save the updated variables to the file // Save the updated variables to the file
SaveUserVariables(); SaveUserVariables();
UpdateListView(); // Refresh the display UpdateListView(); // Refresh the display
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"Error importing variable file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"Error importing variable file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
} }
} }
@ -275,7 +283,7 @@ namespace WelsonJS.Launcher
lines.Add($"{variable.Key}={value}"); lines.Add($"{variable.Key}={value}");
} }
File.WriteAllLines(filePath, lines); File.WriteAllLines(filePath, lines, defaultEncoding);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -28,62 +28,62 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.gbMaxScriptStatements = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button(); this.btnOkMaxScriptStatements = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox(); this.txtMaxScriptStatements = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout(); this.gbMaxScriptStatements.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBox1 // gbMaxScriptStatements
// //
this.groupBox1.Controls.Add(this.button1); this.gbMaxScriptStatements.Controls.Add(this.btnOkMaxScriptStatements);
this.groupBox1.Controls.Add(this.textBox1); this.gbMaxScriptStatements.Controls.Add(this.txtMaxScriptStatements);
this.groupBox1.Location = new System.Drawing.Point(12, 12); this.gbMaxScriptStatements.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1"; this.gbMaxScriptStatements.Name = "gbMaxScriptStatements";
this.groupBox1.Size = new System.Drawing.Size(290, 67); this.gbMaxScriptStatements.Size = new System.Drawing.Size(290, 67);
this.groupBox1.TabIndex = 0; this.gbMaxScriptStatements.TabIndex = 0;
this.groupBox1.TabStop = false; this.gbMaxScriptStatements.TabStop = false;
this.groupBox1.Text = "MaxScriptStatements (GUI only)"; this.gbMaxScriptStatements.Text = "MaxScriptStatements (GUI only)";
// //
// button1 // btnOkMaxScriptStatements
// //
this.button1.Location = new System.Drawing.Point(218, 30); this.btnOkMaxScriptStatements.Location = new System.Drawing.Point(218, 30);
this.button1.Name = "button1"; this.btnOkMaxScriptStatements.Name = "btnOkMaxScriptStatements";
this.button1.Size = new System.Drawing.Size(57, 21); this.btnOkMaxScriptStatements.Size = new System.Drawing.Size(57, 21);
this.button1.TabIndex = 1; this.btnOkMaxScriptStatements.TabIndex = 1;
this.button1.Text = "Ok"; this.btnOkMaxScriptStatements.Text = "Ok";
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText; this.btnOkMaxScriptStatements.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
this.button1.UseVisualStyleBackColor = true; this.btnOkMaxScriptStatements.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click); this.btnOkMaxScriptStatements.Click += new System.EventHandler(this.btnOkMaxScriptStatements_Click);
// //
// textBox1 // txtMaxScriptStatements
// //
this.textBox1.Location = new System.Drawing.Point(15, 30); this.txtMaxScriptStatements.Location = new System.Drawing.Point(15, 30);
this.textBox1.Name = "textBox1"; this.txtMaxScriptStatements.Name = "txtMaxScriptStatements";
this.textBox1.Size = new System.Drawing.Size(197, 21); this.txtMaxScriptStatements.Size = new System.Drawing.Size(197, 21);
this.textBox1.TabIndex = 1; this.txtMaxScriptStatements.TabIndex = 1;
// //
// GlobalSettingsForm // GlobalSettingsForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(315, 92); this.ClientSize = new System.Drawing.Size(315, 92);
this.Controls.Add(this.groupBox1); this.Controls.Add(this.gbMaxScriptStatements);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = global::WelsonJS.Launcher.Properties.Resources.favicon; this.Icon = global::WelsonJS.Launcher.Properties.Resources.favicon;
this.MaximizeBox = false; this.MaximizeBox = false;
this.Name = "GlobalSettingsForm"; this.Name = "GlobalSettingsForm";
this.Text = "Global settings..."; this.Text = "Global settings...";
this.groupBox1.ResumeLayout(false); this.gbMaxScriptStatements.ResumeLayout(false);
this.groupBox1.PerformLayout(); this.gbMaxScriptStatements.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
#endregion #endregion
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox gbMaxScriptStatements;
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox txtMaxScriptStatements;
private System.Windows.Forms.Button button1; private System.Windows.Forms.Button btnOkMaxScriptStatements;
} }
} }

View File

@ -21,22 +21,22 @@ namespace WelsonJS.Launcher
{ {
if (key != null) if (key != null)
{ {
object value = key.GetValue(RegistryKey); string value = key.GetValue(RegistryKey)?.ToString();
if (value != null && value is int maxStatements) if (value != null && int.TryParse(value, out int maxStatements))
{ {
textBox1.Text = maxStatements.ToString(); txtMaxScriptStatements.Text = maxStatements.ToString();
} }
} }
} }
} }
private void button1_Click(object sender, EventArgs e) private void btnOkMaxScriptStatements_Click(object sender, EventArgs e)
{ {
if (uint.TryParse(textBox1.Text, out uint maxStatements)) if (int.TryParse(txtMaxScriptStatements.Text, out int maxStatements))
{ {
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(RegistryPath)) using (RegistryKey key = Registry.CurrentUser.CreateSubKey(RegistryPath))
{ {
key.SetValue(RegistryKey, (int)maxStatements, RegistryValueKind.DWord); key.SetValue(RegistryKey, maxStatements, RegistryValueKind.DWord);
} }
MessageBox.Show($"MaxScriptStatements setting has been changed to {maxStatements}.", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show($"MaxScriptStatements setting has been changed to {maxStatements}.", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }