mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 11:37:01 +00:00
设置系统+新的RSOD
This commit is contained in:
103
Kernel.cs
103
Kernel.cs
@@ -13,6 +13,7 @@ using System.Reflection.Metadata.Ecma335;
|
||||
using System.Text;
|
||||
using Sys = Cosmos.System;
|
||||
using CMLeonOS.Logger;
|
||||
using CMLeonOS.Settings;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
@@ -45,48 +46,6 @@ namespace CMLeonOS
|
||||
Console.WriteLine("By LeonOS 2 Developement Team");
|
||||
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
|
||||
_logger.Info("Kernel", "Starting VFS initialization");
|
||||
try
|
||||
@@ -94,6 +53,9 @@ namespace CMLeonOS
|
||||
Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);
|
||||
_logger.Success("Kernel", "VFS initialized successfully");
|
||||
|
||||
Settings.SettingsManager.LoadSettings();
|
||||
_logger.Info("Kernel", "Settings loaded successfully");
|
||||
|
||||
// 显示可用空间(动态单位)
|
||||
var available_space = fs.GetAvailableFreeSpace(@"0:\");
|
||||
string spaceWithUnit = FormatBytes(available_space);
|
||||
@@ -124,6 +86,47 @@ namespace CMLeonOS
|
||||
_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;
|
||||
|
||||
@@ -205,13 +208,15 @@ namespace CMLeonOS
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("Kernel", $"System initialization error: {ex.Message}");
|
||||
_logger.Error("Kernel", $"Please contact the developers, Email: leonmmcoset@outlook.com");
|
||||
// 这条横线居然和上一条信息的字符数是一样的
|
||||
Console.WriteLine("-------------------------------------------------------------");
|
||||
Console.WriteLine($"Maybe your disk isn't formatted, please format your disk using like PE or something else.");
|
||||
Console.WriteLine("(CMLeonOS need more than 512 MB disk space and FAT32 file system.)");
|
||||
Console.WriteLine("Press any key to restart...");
|
||||
Console.Clear();
|
||||
Console.BackgroundColor = ConsoleColor.Red;
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Clear();
|
||||
Console.WriteLine(":(");
|
||||
Console.WriteLine("A problem has been detected and CMLeonOS has been shutdown to prevent damage to your computer.");
|
||||
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();
|
||||
Sys.Power.Reboot();
|
||||
}
|
||||
|
||||
@@ -81,29 +81,31 @@ namespace CMLeonOS.Logger
|
||||
|
||||
private void WriteToConsole(LogEntry entry)
|
||||
{
|
||||
ConsoleColor originalColor = Console.ForegroundColor;
|
||||
|
||||
switch (entry.Level)
|
||||
{
|
||||
case LogLevel.Debug:
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
break;
|
||||
case LogLevel.Info:
|
||||
Console.ForegroundColor = ConsoleColor.DarkCyan;
|
||||
break;
|
||||
case LogLevel.Warning:
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
break;
|
||||
case LogLevel.Success:
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
break;
|
||||
}
|
||||
if (Settings.SettingsManager.LoggerEnabled) {
|
||||
ConsoleColor originalColor = Console.ForegroundColor;
|
||||
|
||||
switch (entry.Level)
|
||||
{
|
||||
case LogLevel.Debug:
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
break;
|
||||
case LogLevel.Info:
|
||||
Console.ForegroundColor = ConsoleColor.DarkCyan;
|
||||
break;
|
||||
case LogLevel.Warning:
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
break;
|
||||
case LogLevel.Success:
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine(entry.ToString());
|
||||
Console.ForegroundColor = originalColor;
|
||||
Console.WriteLine(entry.ToString());
|
||||
Console.ForegroundColor = originalColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
156
Settings/Settings.cs
Normal file
156
Settings/Settings.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -213,9 +213,18 @@ namespace CMLeonOS
|
||||
List<string> lines = new List<string>();
|
||||
foreach (User user in users)
|
||||
{
|
||||
// 使用SHA256加密密码
|
||||
string hashedPassword = HashPasswordSha256(user.Password);
|
||||
string line = $"{user.Username}|{hashedPassword}|{(user.IsAdmin ? "admin" : "user")}|{user.Hostname}";
|
||||
string passwordToSave;
|
||||
|
||||
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);
|
||||
}
|
||||
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
|
||||
{
|
||||
get { return users.Count > 0; }
|
||||
|
||||
@@ -85,6 +85,9 @@ namespace CMLeonOS.shell
|
||||
case "version":
|
||||
shell.ProcessVersion();
|
||||
break;
|
||||
case "settings":
|
||||
shell.ProcessSettings(args);
|
||||
break;
|
||||
case "about":
|
||||
shell.ProcessAbout();
|
||||
break;
|
||||
|
||||
@@ -72,6 +72,8 @@ namespace CMLeonOS.Commands
|
||||
" lua <file> - Execute Lua script",
|
||||
" version - Show OS version",
|
||||
" about - Show about information",
|
||||
" settings <key> [value] - View or modify system settings",
|
||||
" settings - List all settings",
|
||||
" help <page> - Show help page (1-3)",
|
||||
" help all - Show all help pages",
|
||||
"",
|
||||
|
||||
56
shell/Commands/System/SettingsCommand.cs
Normal file
56
shell/Commands/System/SettingsCommand.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -501,6 +501,11 @@ namespace CMLeonOS
|
||||
Commands.VersionCommand.ProcessVersion();
|
||||
}
|
||||
|
||||
public void ProcessSettings(string args)
|
||||
{
|
||||
Commands.SettingsCommand.ProcessSettings(args);
|
||||
}
|
||||
|
||||
public void ProcessAbout()
|
||||
{
|
||||
Commands.AboutCommand.ProcessAbout();
|
||||
|
||||
Reference in New Issue
Block a user