设置系统+新的RSOD

This commit is contained in:
2026-02-06 16:15:24 +08:00
parent 2124fea5e2
commit 8d6a43cd81
8 changed files with 337 additions and 74 deletions

103
Kernel.cs
View File

@@ -13,6 +13,7 @@ using System.Reflection.Metadata.Ecma335;
using System.Text; using System.Text;
using Sys = Cosmos.System; using Sys = Cosmos.System;
using CMLeonOS.Logger; using CMLeonOS.Logger;
using CMLeonOS.Settings;
namespace CMLeonOS namespace CMLeonOS
{ {
@@ -45,48 +46,6 @@ namespace CMLeonOS
Console.WriteLine("By LeonOS 2 Developement Team"); Console.WriteLine("By LeonOS 2 Developement Team");
Console.WriteLine(@"-------------------------------------------------"); Console.WriteLine(@"-------------------------------------------------");
// 记录系统启动时间用于uptime命令
SystemStartTime = DateTime.Now;
_logger.Info("Kernel", $"System started at: {SystemStartTime.ToString("yyyy-MM-dd HH:mm:ss")}");
// 初始化网络
_logger.Info("Kernel", "Starting network initialization");
try
{
if (Cosmos.HAL.NetworkDevice.Devices.Count == 0)
{
throw new Exception("No network devices are available.");
}
NetworkDevice = NetworkDevice.Devices[0];
_logger.Info("Kernel", $"Network device found: {NetworkDevice.Name}");
using var dhcp = new DHCPClient();
if (NetworkDevice.Ready == true) {
_logger.Success("Kernel", "Network device ready.");
}
else
{
_logger.Error("Kernel", "Network device is not ready");
}
dhcp.SendDiscoverPacket();
IPAddress = NetworkConfiguration.CurrentAddress.ToString();
_logger.Info("Kernel", $"Local IP: {IPAddress}");
string gateway = NetworkConfigManager.Instance.GetGateway();
_logger.Info("Kernel", $"Gateway: {gateway}");
string dns = NetworkConfigManager.Instance.GetDNS();
_logger.Info("Kernel", $"DNS Server: {dns}");
_logger.Success("Kernel", "Network started successfully");
}
catch (Exception ex)
{
_logger.Error("Kernel", $"Network initialization failed: {ex.Message}");
// ShowError($"Could not start network: {ex.ToString()}");
}
// 注册VFS // 注册VFS
_logger.Info("Kernel", "Starting VFS initialization"); _logger.Info("Kernel", "Starting VFS initialization");
try try
@@ -94,6 +53,9 @@ namespace CMLeonOS
Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs); Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);
_logger.Success("Kernel", "VFS initialized successfully"); _logger.Success("Kernel", "VFS initialized successfully");
Settings.SettingsManager.LoadSettings();
_logger.Info("Kernel", "Settings loaded successfully");
// 显示可用空间(动态单位) // 显示可用空间(动态单位)
var available_space = fs.GetAvailableFreeSpace(@"0:\"); var available_space = fs.GetAvailableFreeSpace(@"0:\");
string spaceWithUnit = FormatBytes(available_space); string spaceWithUnit = FormatBytes(available_space);
@@ -124,6 +86,47 @@ namespace CMLeonOS
_logger.Info("Kernel", "Created env.dat with Test=123"); _logger.Info("Kernel", "Created env.dat with Test=123");
} }
// 记录系统启动时间用于uptime命令
SystemStartTime = DateTime.Now;
_logger.Info("Kernel", $"System started at: {SystemStartTime.ToString("yyyy-MM-dd HH:mm:ss")}");
// 初始化网络
_logger.Info("Kernel", "Starting network initialization");
try
{
if (Cosmos.HAL.NetworkDevice.Devices.Count == 0)
{
throw new Exception("No network devices are available.");
}
NetworkDevice = NetworkDevice.Devices[0];
_logger.Info("Kernel", $"Network device found: {NetworkDevice.Name}");
using var dhcp = new DHCPClient();
if (NetworkDevice.Ready == true) {
_logger.Success("Kernel", "Network device ready.");
}
else
{
_logger.Error("Kernel", "Network device is not ready");
}
dhcp.SendDiscoverPacket();
IPAddress = NetworkConfiguration.CurrentAddress.ToString();
_logger.Info("Kernel", $"Local IP: {IPAddress}");
string gateway = NetworkConfigManager.Instance.GetGateway();
_logger.Info("Kernel", $"Gateway: {gateway}");
string dns = NetworkConfigManager.Instance.GetDNS();
_logger.Info("Kernel", $"DNS Server: {dns}");
_logger.Success("Kernel", "Network started successfully");
}
catch (Exception ex)
{
_logger.Error("Kernel", $"Network initialization failed: {ex.Message}");
}
// 输出系统启动-初始化完成后的时间 // 输出系统启动-初始化完成后的时间
TimeSpan uptime = DateTime.Now - Kernel.SystemStartTime; TimeSpan uptime = DateTime.Now - Kernel.SystemStartTime;
@@ -205,13 +208,15 @@ namespace CMLeonOS
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error("Kernel", $"System initialization error: {ex.Message}"); Console.Clear();
_logger.Error("Kernel", $"Please contact the developers, Email: leonmmcoset@outlook.com"); Console.BackgroundColor = ConsoleColor.Red;
// 这条横线居然和上一条信息的字符数是一样的 Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("-------------------------------------------------------------"); Console.Clear();
Console.WriteLine($"Maybe your disk isn't formatted, please format your disk using like PE or something else."); Console.WriteLine(":(");
Console.WriteLine("(CMLeonOS need more than 512 MB disk space and FAT32 file system.)"); Console.WriteLine("A problem has been detected and CMLeonOS has been shutdown to prevent damage to your computer.");
Console.WriteLine("Press any key to restart..."); Console.WriteLine($"Error information: {ex.Message}");
Console.WriteLine("If this is the first time you've seen this stop error screen, restart your computer and email to leonmmcoset@outlook.com WITH THE ERROR INFORMATION for technical support.");
Console.WriteLine("Press any keys to restart.");
Console.ReadKey(); Console.ReadKey();
Sys.Power.Reboot(); Sys.Power.Reboot();
} }

View File

@@ -81,29 +81,31 @@ namespace CMLeonOS.Logger
private void WriteToConsole(LogEntry entry) private void WriteToConsole(LogEntry entry)
{ {
ConsoleColor originalColor = Console.ForegroundColor; if (Settings.SettingsManager.LoggerEnabled) {
ConsoleColor originalColor = Console.ForegroundColor;
switch (entry.Level)
{ switch (entry.Level)
case LogLevel.Debug: {
Console.ForegroundColor = ConsoleColor.Gray; case LogLevel.Debug:
break; Console.ForegroundColor = ConsoleColor.Gray;
case LogLevel.Info: break;
Console.ForegroundColor = ConsoleColor.DarkCyan; case LogLevel.Info:
break; Console.ForegroundColor = ConsoleColor.DarkCyan;
case LogLevel.Warning: break;
Console.ForegroundColor = ConsoleColor.Yellow; case LogLevel.Warning:
break; Console.ForegroundColor = ConsoleColor.Yellow;
case LogLevel.Error: break;
Console.ForegroundColor = ConsoleColor.Red; case LogLevel.Error:
break; Console.ForegroundColor = ConsoleColor.Red;
case LogLevel.Success: break;
Console.ForegroundColor = ConsoleColor.Green; case LogLevel.Success:
break; Console.ForegroundColor = ConsoleColor.Green;
} break;
}
Console.WriteLine(entry.ToString()); Console.WriteLine(entry.ToString());
Console.ForegroundColor = originalColor; Console.ForegroundColor = originalColor;
}
} }
} }
} }

156
Settings/Settings.cs Normal file
View File

@@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace CMLeonOS.Settings
{
public static class SettingsManager
{
private static string settingsFilePath = @"0:\system\settings.dat";
private static Dictionary<string, string> settings = new Dictionary<string, string>();
private static Dictionary<string, string> defaultSettings = new Dictionary<string, string>
{
{ "LoggerEnabled", "true" }
};
public static bool LoggerEnabled
{
get
{
if (settings.TryGetValue("LoggerEnabled", out string value))
{
return value.ToLower() == "true";
}
return true;
}
set
{
settings["LoggerEnabled"] = value ? "true" : "false";
SaveSettings();
}
}
public static void LoadSettings()
{
settings.Clear();
try
{
if (File.Exists(settingsFilePath))
{
string[] lines = File.ReadAllLines(settingsFilePath);
foreach (string line in lines)
{
if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))
{
int separatorIndex = line.IndexOf('=');
if (separatorIndex > 0)
{
string key = line.Substring(0, separatorIndex).Trim();
string value = line.Substring(separatorIndex + 1).Trim();
settings[key] = value;
}
}
}
foreach (var defaultSetting in defaultSettings)
{
if (!settings.ContainsKey(defaultSetting.Key))
{
settings[defaultSetting.Key] = defaultSetting.Value;
}
}
}
else
{
foreach (var defaultSetting in defaultSettings)
{
settings[defaultSetting.Key] = defaultSetting.Value;
}
SaveSettings();
}
}
catch (Exception e)
{
Console.WriteLine($"Error loading settings: {e.Message}");
}
}
public static void SaveSettings()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(settingsFilePath));
using (StreamWriter writer = new StreamWriter(settingsFilePath))
{
writer.WriteLine("# CMLeonOS Settings Configuration");
writer.WriteLine("# Format: setting_name=value");
writer.WriteLine();
foreach (var setting in settings)
{
writer.WriteLine($"{setting.Key}={setting.Value}");
}
}
}
catch (Exception e)
{
Console.WriteLine($"Error saving settings: {e.Message}");
}
}
public static string GetSetting(string key)
{
if (settings.TryGetValue(key, out string value))
{
return value;
}
return null;
}
public static void SetSetting(string key, string value)
{
settings[key] = value;
SaveSettings();
}
public static bool GetBoolSetting(string key, bool defaultValue)
{
if (settings.TryGetValue(key, out string value))
{
return value.ToLower() == "true";
}
return defaultValue;
}
public static int GetIntSetting(string key, int defaultValue)
{
if (settings.TryGetValue(key, out string value))
{
if (int.TryParse(value, out int result))
{
return result;
}
}
return defaultValue;
}
public static void ListSettings()
{
if (settings.Count == 0)
{
Console.WriteLine("No settings defined");
return;
}
Console.WriteLine("Current settings:");
foreach (var setting in settings)
{
Console.WriteLine($" {setting.Key} = {setting.Value}");
}
}
}
}

View File

@@ -213,9 +213,18 @@ namespace CMLeonOS
List<string> lines = new List<string>(); List<string> lines = new List<string>();
foreach (User user in users) foreach (User user in users)
{ {
// 使用SHA256加密密码 string passwordToSave;
string hashedPassword = HashPasswordSha256(user.Password);
string line = $"{user.Username}|{hashedPassword}|{(user.IsAdmin ? "admin" : "user")}|{user.Hostname}"; if (IsPasswordAlreadyHashed(user.Password))
{
passwordToSave = user.Password;
}
else
{
passwordToSave = HashPasswordSha256(user.Password);
}
string line = $"{user.Username}|{passwordToSave}|{(user.IsAdmin ? "admin" : "user")}|{user.Hostname}";
lines.Add(line); lines.Add(line);
} }
File.WriteAllLines(userFilePath, lines.ToArray()); File.WriteAllLines(userFilePath, lines.ToArray());
@@ -226,6 +235,31 @@ namespace CMLeonOS
} }
} }
private bool IsPasswordAlreadyHashed(string password)
{
if (string.IsNullOrWhiteSpace(password))
{
return false;
}
string trimmedPassword = password.Trim();
if (trimmedPassword.Length < 32)
{
return false;
}
foreach (char c in trimmedPassword)
{
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/' || c == '='))
{
return false;
}
}
return true;
}
public bool HasUsers public bool HasUsers
{ {
get { return users.Count > 0; } get { return users.Count > 0; }

View File

@@ -85,6 +85,9 @@ namespace CMLeonOS.shell
case "version": case "version":
shell.ProcessVersion(); shell.ProcessVersion();
break; break;
case "settings":
shell.ProcessSettings(args);
break;
case "about": case "about":
shell.ProcessAbout(); shell.ProcessAbout();
break; break;

View File

@@ -72,6 +72,8 @@ namespace CMLeonOS.Commands
" lua <file> - Execute Lua script", " lua <file> - Execute Lua script",
" version - Show OS version", " version - Show OS version",
" about - Show about information", " about - Show about information",
" settings <key> [value] - View or modify system settings",
" settings - List all settings",
" help <page> - Show help page (1-3)", " help <page> - Show help page (1-3)",
" help all - Show all help pages", " help all - Show all help pages",
"", "",

View File

@@ -0,0 +1,56 @@
using System;
using CMLeonOS.Settings;
namespace CMLeonOS.Commands
{
public static class SettingsCommand
{
public static void ProcessSettings(string args)
{
if (string.IsNullOrWhiteSpace(args))
{
SettingsManager.ListSettings();
return;
}
string[] parts = args.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 1)
{
string key = parts[0];
string value = SettingsManager.GetSetting(key);
if (value != null)
{
Console.WriteLine($"{key} = {value}");
}
else
{
Console.WriteLine($"Error: Setting '{key}' not found");
}
}
else if (parts.Length == 2)
{
string key = parts[0];
string value = parts[1];
if (key.ToLower() == "loggerenabled")
{
if (value.ToLower() == "true" || value.ToLower() == "false")
{
SettingsManager.LoggerEnabled = value.ToLower() == "true";
Console.WriteLine($"LoggerEnabled set to {value.ToLower()}");
}
else
{
Console.WriteLine("Error: LoggerEnabled must be 'true' or 'false'");
}
}
else
{
SettingsManager.SetSetting(key, value);
Console.WriteLine($"{key} set to {value}");
}
}
}
}
}

View File

@@ -501,6 +501,11 @@ namespace CMLeonOS
Commands.VersionCommand.ProcessVersion(); Commands.VersionCommand.ProcessVersion();
} }
public void ProcessSettings(string args)
{
Commands.SettingsCommand.ProcessSettings(args);
}
public void ProcessAbout() public void ProcessAbout()
{ {
Commands.AboutCommand.ProcessAbout(); Commands.AboutCommand.ProcessAbout();