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

View File

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

View File

@ -21,22 +21,22 @@ namespace WelsonJS.Launcher
{
if (key != null)
{
object value = key.GetValue(RegistryKey);
if (value != null && value is int maxStatements)
string value = key.GetValue(RegistryKey)?.ToString();
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))
{
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);
}