mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
GUI桌面环境
This commit is contained in:
104
utils/FileSecurity.cs
Normal file
104
utils/FileSecurity.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace CMLeonOS.Utils
|
||||
{
|
||||
public static class FileSecurity
|
||||
{
|
||||
public static bool IsSecure(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string fileName = Path.GetFileName(path);
|
||||
|
||||
string[] insecureExtensions = { ".exe", ".bat", ".cmd", ".sh", ".ps1" };
|
||||
string extension = Path.GetExtension(fileName).ToLower();
|
||||
|
||||
foreach (string insecureExt in insecureExtensions)
|
||||
{
|
||||
if (extension == insecureExt)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
string[] insecureNames = { "autoexec", "config", "system" };
|
||||
string nameWithoutExt = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
||||
|
||||
foreach (string insecureName in insecureNames)
|
||||
{
|
||||
if (nameWithoutExt.Contains(insecureName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string GetSecurityLevel(string path)
|
||||
{
|
||||
if (!IsSecure(path))
|
||||
{
|
||||
return "Insecure";
|
||||
}
|
||||
return "Secure";
|
||||
}
|
||||
|
||||
public static bool CanExecute(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string extension = Path.GetExtension(path).ToLower();
|
||||
string[] executableExtensions = { ".exe", ".bat", ".cmd", ".sh", ".ps1" };
|
||||
|
||||
foreach (string execExt in executableExtensions)
|
||||
{
|
||||
if (extension == execExt)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool CanRead(string path)
|
||||
{
|
||||
return File.Exists(path) || Directory.Exists(path);
|
||||
}
|
||||
|
||||
public static bool CanAccess(string path)
|
||||
{
|
||||
return CanRead(path) && CanWrite(path);
|
||||
}
|
||||
|
||||
public static bool CanWrite(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
string dir = Path.GetDirectoryName(path);
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string testFile = Path.Combine(dir, ".write_test");
|
||||
File.WriteAllText(testFile, "test");
|
||||
File.Delete(testFile);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
utils/FloatExtensions.cs
Normal file
12
utils/FloatExtensions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace CMLeonOS.Utils
|
||||
{
|
||||
public static class FloatExtensions
|
||||
{
|
||||
public static float Map(this float value, float inMin, float inMax, float outMin, float outMax)
|
||||
{
|
||||
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
|
||||
}
|
||||
}
|
||||
}
|
||||
118
utils/IniBuilder.cs
Normal file
118
utils/IniBuilder.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS.Utils
|
||||
{
|
||||
public class IniBuilder
|
||||
{
|
||||
private Dictionary<string, Dictionary<string, string>> data = new Dictionary<string, Dictionary<string, string>>();
|
||||
private string currentSection = "";
|
||||
|
||||
public IniBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
public IniBuilder BeginSection(string section)
|
||||
{
|
||||
currentSection = section;
|
||||
if (!data.ContainsKey(section))
|
||||
{
|
||||
data[section] = new Dictionary<string, string>();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public IniBuilder SetSection(string section)
|
||||
{
|
||||
currentSection = section;
|
||||
if (!data.ContainsKey(section))
|
||||
{
|
||||
data[section] = new Dictionary<string, string>();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public IniBuilder SetValue(string key, string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(currentSection))
|
||||
{
|
||||
currentSection = "General";
|
||||
if (!data.ContainsKey(currentSection))
|
||||
{
|
||||
data[currentSection] = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
||||
data[currentSection][key] = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IniBuilder SetValue(string section, string key, string value)
|
||||
{
|
||||
if (!data.ContainsKey(section))
|
||||
{
|
||||
data[section] = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
data[section][key] = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IniBuilder AddKey(string key, object value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(currentSection))
|
||||
{
|
||||
currentSection = "General";
|
||||
if (!data.ContainsKey(currentSection))
|
||||
{
|
||||
data[currentSection] = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
||||
data[currentSection][key] = value.ToString();
|
||||
return this;
|
||||
}
|
||||
|
||||
public IniBuilder AddKey(string section, string key, object value)
|
||||
{
|
||||
if (!data.ContainsKey(section))
|
||||
{
|
||||
data[section] = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
data[section][key] = value.ToString();
|
||||
return this;
|
||||
}
|
||||
|
||||
public string Build()
|
||||
{
|
||||
var lines = new List<string>();
|
||||
|
||||
foreach (var section in data.Keys)
|
||||
{
|
||||
lines.Add($"[{section}]");
|
||||
|
||||
foreach (var kvp in data[section])
|
||||
{
|
||||
lines.Add($"{kvp.Key}={kvp.Value}");
|
||||
}
|
||||
|
||||
lines.Add("");
|
||||
}
|
||||
|
||||
return string.Join("\n", lines);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Build();
|
||||
}
|
||||
|
||||
public void Save(string filePath)
|
||||
{
|
||||
string content = Build();
|
||||
File.WriteAllText(filePath, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
105
utils/IniReader.cs
Normal file
105
utils/IniReader.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS.Utils
|
||||
{
|
||||
public class IniReader
|
||||
{
|
||||
private Dictionary<string, Dictionary<string, string>> data = new Dictionary<string, Dictionary<string, string>>();
|
||||
|
||||
public IniReader(string filePath)
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
string currentSection = "";
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string trimmedLine = line.Trim();
|
||||
|
||||
if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]"))
|
||||
{
|
||||
currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2);
|
||||
if (!data.ContainsKey(currentSection))
|
||||
{
|
||||
data[currentSection] = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
else if (trimmedLine.Contains("=") && !string.IsNullOrWhiteSpace(currentSection))
|
||||
{
|
||||
string[] parts = trimmedLine.Split(new[] { '=' }, 2);
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
string key = parts[0].Trim();
|
||||
string value = parts[1].Trim();
|
||||
data[currentSection][key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(string section, string key, string defaultValue = "")
|
||||
{
|
||||
if (data.ContainsKey(section) && data[section].ContainsKey(key))
|
||||
{
|
||||
return data[section][key];
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public int GetInt(string section, string key, int defaultValue = 0)
|
||||
{
|
||||
string value = GetValue(section, key, defaultValue.ToString());
|
||||
if (int.TryParse(value, out int result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public bool GetBool(string section, string key, bool defaultValue = false)
|
||||
{
|
||||
string value = GetValue(section, key, defaultValue.ToString()).ToLower();
|
||||
return value == "true" || value == "1" || value == "yes";
|
||||
}
|
||||
|
||||
public List<string> GetSections()
|
||||
{
|
||||
return new List<string>(data.Keys);
|
||||
}
|
||||
|
||||
public List<string> GetKeys(string section)
|
||||
{
|
||||
if (data.ContainsKey(section))
|
||||
{
|
||||
return new List<string>(data[section].Keys);
|
||||
}
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
public bool TryReadBool(string key, out bool value, string section = "")
|
||||
{
|
||||
string sectionToUse = string.IsNullOrWhiteSpace(section) ? "General" : section;
|
||||
string stringValue = GetValue(sectionToUse, key, "false");
|
||||
value = stringValue.ToLower() == "true" || stringValue == "1" || stringValue == "yes";
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryReadInt(string key, out int value, string section = "")
|
||||
{
|
||||
string sectionToUse = string.IsNullOrWhiteSpace(section) ? "General" : section;
|
||||
string stringValue = GetValue(sectionToUse, key, "0");
|
||||
return int.TryParse(stringValue, out value);
|
||||
}
|
||||
|
||||
public bool TryReadFloat(string key, out float value, string section = "")
|
||||
{
|
||||
string sectionToUse = string.IsNullOrWhiteSpace(section) ? "General" : section;
|
||||
string stringValue = GetValue(sectionToUse, key, "0");
|
||||
return float.TryParse(stringValue, out value);
|
||||
}
|
||||
}
|
||||
}
|
||||
108
utils/MemoryStatisticsProvider.cs
Normal file
108
utils/MemoryStatisticsProvider.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
|
||||
namespace CMLeonOS.Utils
|
||||
{
|
||||
public static class MemoryStatisticsProvider
|
||||
{
|
||||
public static ulong GetAvailableMemory()
|
||||
{
|
||||
return Cosmos.Core.CPU.GetAmountOfRAM() * 1024 * 1024;
|
||||
}
|
||||
|
||||
public static ulong GetUsedMemory()
|
||||
{
|
||||
ulong totalMemory = GetAvailableMemory();
|
||||
ulong freeMemory = GetFreeMemory();
|
||||
return totalMemory - freeMemory;
|
||||
}
|
||||
|
||||
public static ulong GetFreeMemory()
|
||||
{
|
||||
ulong totalMemory = GetAvailableMemory();
|
||||
return totalMemory / 4;
|
||||
}
|
||||
|
||||
public static ulong GetTotalMemory()
|
||||
{
|
||||
return Cosmos.Core.CPU.GetAmountOfRAM() * 1024 * 1024;
|
||||
}
|
||||
|
||||
public static string FormatBytes(ulong bytes)
|
||||
{
|
||||
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
|
||||
int order = 0;
|
||||
double size = bytes;
|
||||
|
||||
while (size >= 1024 && order < sizes.Length - 1)
|
||||
{
|
||||
order++;
|
||||
size = size / 1024;
|
||||
}
|
||||
|
||||
return $"{size:0.##} {sizes[order]}";
|
||||
}
|
||||
|
||||
public static string GetMemoryUsagePercentage()
|
||||
{
|
||||
ulong used = GetUsedMemory();
|
||||
ulong total = GetTotalMemory();
|
||||
double percentage = (double)used / total * 100;
|
||||
return $"{percentage:0.1}%";
|
||||
}
|
||||
|
||||
public static MemoryStatistics GetMemoryStatistics()
|
||||
{
|
||||
return new MemoryStatistics
|
||||
{
|
||||
TotalMemory = GetTotalMemory(),
|
||||
UsedMemory = GetUsedMemory(),
|
||||
FreeMemory = GetFreeMemory(),
|
||||
UsagePercentage = GetMemoryUsagePercentage()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class MemoryStatistics
|
||||
{
|
||||
public ulong TotalMemory { get; set; }
|
||||
public ulong UsedMemory { get; set; }
|
||||
public ulong FreeMemory { get; set; }
|
||||
public string UsagePercentage { get; set; }
|
||||
|
||||
public ulong TotalMB
|
||||
{
|
||||
get { return TotalMemory / (1024 * 1024); }
|
||||
}
|
||||
|
||||
public ulong UnavailableMB
|
||||
{
|
||||
get { return 0; }
|
||||
}
|
||||
|
||||
public ulong UsedMB
|
||||
{
|
||||
get { return UsedMemory / (1024 * 1024); }
|
||||
}
|
||||
|
||||
public ulong FreeMB
|
||||
{
|
||||
get { return FreeMemory / (1024 * 1024); }
|
||||
}
|
||||
|
||||
public int PercentUsed
|
||||
{
|
||||
get
|
||||
{
|
||||
if (UsagePercentage.EndsWith("%"))
|
||||
{
|
||||
string percentage = UsagePercentage.TrimEnd('%');
|
||||
if (int.TryParse(percentage, out int result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
74
utils/PathUtil.cs
Normal file
74
utils/PathUtil.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace CMLeonOS.Utils
|
||||
{
|
||||
public static class PathUtil
|
||||
{
|
||||
public static string Sanitize(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
string sanitized = path;
|
||||
|
||||
sanitized = sanitized.Replace('\\', '/');
|
||||
sanitized = sanitized.Replace(':', '_');
|
||||
sanitized = sanitized.Replace('*', '_');
|
||||
sanitized = sanitized.Replace('?', '_');
|
||||
sanitized = sanitized.Replace('"', '_');
|
||||
sanitized = sanitized.Replace('<', '_');
|
||||
sanitized = sanitized.Replace('>', '_');
|
||||
sanitized = sanitized.Replace('|', '_');
|
||||
|
||||
sanitized = sanitized.Trim('/', '\\');
|
||||
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
public static string Combine(string path1, string path2)
|
||||
{
|
||||
return Path.Combine(path1, path2);
|
||||
}
|
||||
|
||||
public static string GetExtension(string path)
|
||||
{
|
||||
return Path.GetExtension(path);
|
||||
}
|
||||
|
||||
public static string GetFileName(string path)
|
||||
{
|
||||
return Path.GetFileName(path);
|
||||
}
|
||||
|
||||
public static string GetDirectoryName(string path)
|
||||
{
|
||||
return Path.GetDirectoryName(path);
|
||||
}
|
||||
|
||||
public static bool Exists(string path)
|
||||
{
|
||||
return File.Exists(path) || Directory.Exists(path);
|
||||
}
|
||||
|
||||
public static string Normalize(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
string normalized = path.Replace('\\', '/');
|
||||
|
||||
while (normalized.Contains("//"))
|
||||
{
|
||||
normalized = normalized.Replace("//", "/");
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,5 +29,10 @@ namespace CMLeonOS
|
||||
{
|
||||
get { return $"CMLeonOS v{ShortVersion} ({VersionType}) - Git: {GitCommit}"; }
|
||||
}
|
||||
|
||||
public static string GetVersion()
|
||||
{
|
||||
return ShortVersion;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user