2026-01-31 19:14:13 +08:00
|
|
|
|
using System;
|
2026-01-30 21:55:35 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using Sys = Cosmos.System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace CMLeonOS
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Kernel : Sys.Kernel
|
|
|
|
|
|
{
|
|
|
|
|
|
// 创建全局CosmosVFS实例
|
|
|
|
|
|
Sys.FileSystem.CosmosVFS fs = new Sys.FileSystem.CosmosVFS();
|
|
|
|
|
|
|
|
|
|
|
|
private Shell shell;
|
|
|
|
|
|
private UserSystem userSystem;
|
2026-01-30 23:36:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 修复模式变量(硬编码,用于控制是否启用修复模式)
|
|
|
|
|
|
public static bool FixMode = false;
|
2026-01-30 21:55:35 +08:00
|
|
|
|
|
|
|
|
|
|
protected override void BeforeRun()
|
|
|
|
|
|
{
|
2026-01-31 19:14:13 +08:00
|
|
|
|
// Console.Clear();
|
|
|
|
|
|
Console.WriteLine(@"-------------------------------------------------");
|
2026-01-30 21:55:35 +08:00
|
|
|
|
Console.WriteLine(@" ____ __ __ _ ___ ____ ");
|
|
|
|
|
|
Console.WriteLine(@" / ___| \/ | | ___ ___ _ __ / _ \/ ___| ");
|
|
|
|
|
|
Console.WriteLine(@" | | | |\/| | | / _ \/ _ \| '_ \| | | \___ \ ");
|
|
|
|
|
|
Console.WriteLine(@" | |___| | | | |__| __/ (_) | | | | |_| |___) |");
|
2026-01-30 23:36:08 +08:00
|
|
|
|
Console.WriteLine(@" \____|_| |_|_____\___|\___/|_| |_|____/|____/ ");
|
2026-01-30 21:55:35 +08:00
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
Console.WriteLine("CMLeonOS Test Project");
|
|
|
|
|
|
Console.WriteLine("By LeonOS 2 Developement Team");
|
|
|
|
|
|
|
|
|
|
|
|
// 注册VFS
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);
|
|
|
|
|
|
Console.WriteLine("VFS initialized successfully");
|
|
|
|
|
|
|
2026-01-31 14:01:50 +08:00
|
|
|
|
// 显示可用空间(动态单位)
|
2026-01-30 21:55:35 +08:00
|
|
|
|
var available_space = fs.GetAvailableFreeSpace(@"0:\");
|
2026-01-31 14:01:50 +08:00
|
|
|
|
string spaceWithUnit = FormatBytes(available_space);
|
|
|
|
|
|
Console.WriteLine("Available Free Space: " + spaceWithUnit);
|
2026-01-30 21:55:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 显示文件系统类型
|
|
|
|
|
|
var fs_type = fs.GetFileSystemType(@"0:\");
|
|
|
|
|
|
Console.WriteLine("File System Type: " + fs_type);
|
|
|
|
|
|
|
2026-01-31 19:14:13 +08:00
|
|
|
|
// 检查并创建system文件夹
|
|
|
|
|
|
string systemFolderPath = @"0:\system";
|
|
|
|
|
|
if (!System.IO.Directory.Exists(systemFolderPath))
|
2026-01-30 21:55:35 +08:00
|
|
|
|
{
|
2026-01-31 19:14:13 +08:00
|
|
|
|
System.IO.Directory.CreateDirectory(systemFolderPath);
|
|
|
|
|
|
Console.WriteLine("Created system folder.");
|
2026-01-30 21:55:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化用户系统
|
|
|
|
|
|
userSystem = new UserSystem();
|
|
|
|
|
|
|
2026-01-31 19:14:13 +08:00
|
|
|
|
// 循环直到登录成功或退出
|
|
|
|
|
|
while (true)
|
2026-01-30 21:55:35 +08:00
|
|
|
|
{
|
2026-01-31 19:14:13 +08:00
|
|
|
|
// 第一次启动,设置管理员账户
|
|
|
|
|
|
if (!userSystem.HasUsers)
|
|
|
|
|
|
{
|
|
|
|
|
|
userSystem.FirstTimeSetup();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 后续启动,需要登录
|
|
|
|
|
|
else
|
2026-01-30 21:55:35 +08:00
|
|
|
|
{
|
2026-01-31 19:14:13 +08:00
|
|
|
|
// 循环直到登录成功
|
|
|
|
|
|
while (!userSystem.Login())
|
|
|
|
|
|
{
|
|
|
|
|
|
// 登录失败,继续尝试
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 登录成功后,初始化Shell
|
|
|
|
|
|
shell = new Shell();
|
|
|
|
|
|
|
|
|
|
|
|
// 检查并执行启动脚本
|
|
|
|
|
|
ExecuteStartupScript();
|
|
|
|
|
|
|
|
|
|
|
|
// 运行Shell(用户可以输入exit退出)
|
|
|
|
|
|
shell.Run();
|
|
|
|
|
|
|
|
|
|
|
|
// 如果用户输入了exit,Shell.Run()会返回,继续循环
|
2026-01-30 21:55:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"Error initializing system: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-31 14:01:50 +08:00
|
|
|
|
private void ExecuteStartupScript()
|
|
|
|
|
|
{
|
2026-01-31 19:14:13 +08:00
|
|
|
|
string startupFilePath = @"0:\system\startup.cm";
|
2026-01-31 14:01:50 +08:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 检查启动脚本文件是否存在
|
|
|
|
|
|
if (System.IO.File.Exists(startupFilePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 读取启动脚本内容
|
|
|
|
|
|
string[] lines = System.IO.File.ReadAllLines(startupFilePath);
|
|
|
|
|
|
|
|
|
|
|
|
// 检查文件是否为空
|
|
|
|
|
|
if (lines.Length == 0 || (lines.Length == 1 && string.IsNullOrWhiteSpace(lines[0])))
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Startup script is empty, skipping...");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Executing startup script...");
|
|
|
|
|
|
Console.WriteLine("--------------------------------");
|
|
|
|
|
|
|
|
|
|
|
|
// 逐行执行命令
|
|
|
|
|
|
foreach (string line in lines)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 跳过空行和注释行
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("#"))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 执行命令
|
2026-01-31 20:50:07 +08:00
|
|
|
|
// Console.WriteLine($"Executing: {line}");
|
2026-01-31 14:01:50 +08:00
|
|
|
|
shell.ExecuteCommand(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("--------------------------------");
|
|
|
|
|
|
Console.WriteLine("Startup script execution completed.");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 启动脚本不存在,创建空文件
|
|
|
|
|
|
Console.WriteLine("Startup script not found, creating empty file...");
|
|
|
|
|
|
System.IO.File.WriteAllText(startupFilePath, "");
|
|
|
|
|
|
Console.WriteLine("Created empty startup script at: " + startupFilePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"Error executing startup script: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string FormatBytes(long bytes)
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] units = { "B", "KB", "MB", "GB", "TB" };
|
|
|
|
|
|
int unitIndex = 0;
|
|
|
|
|
|
double size = bytes;
|
|
|
|
|
|
|
|
|
|
|
|
while (size >= 1024 && unitIndex < units.Length - 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
size /= 1024;
|
|
|
|
|
|
unitIndex++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $"{size:F2} {units[unitIndex]}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-30 21:55:35 +08:00
|
|
|
|
protected override void Run()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (shell != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
shell.Run();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|