Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
Coloryr committed Jan 14, 2025
1 parent 2b75301 commit 5eab1dd
Show file tree
Hide file tree
Showing 38 changed files with 830 additions and 3,361 deletions.
5 changes: 5 additions & 0 deletions log.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
## 已知问题

## 已完成 Done
### 2025.1.14
- 添加内存大小检测
- 添加检测开关
- 添加内存大小显示

### 2025.1.13
- 修复自定义加载器获取模组文件列表错误
- 修复模组升级时信息被删除
Expand Down
2 changes: 1 addition & 1 deletion src/ColorMC.Core/CoreMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class ColorMCCore
{
public const int VersionNum = 34;
public const string TopVersion = "A34";
public const string DateVersion = "20250113";
public const string DateVersion = "20250114";

/// <summary>
/// 版本号
Expand Down
7 changes: 5 additions & 2 deletions src/ColorMC.Core/Game/GameLaunch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,8 +1366,11 @@ public static async Task<CreateCmdRes> CreateGameCmd(this GameSettingObj obj, Ga
{
if (larg.Request == null)
{
return new() { Message =
string.Format(LanguageHelper.Get("Core.Launch.Info16"), obj.Name) };
return new()
{
Message =
string.Format(LanguageHelper.Get("Core.Launch.Info16"), obj.Name)
};
}
var res2 = await larg.Request(string.Format(LanguageHelper.Get("Core.Launch.Info15"), obj.Name));
if (!res2)
Expand Down
4 changes: 2 additions & 2 deletions src/ColorMC.Core/Game/GameLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public static List<string> GetLogFiles(this GameSettingObj obj)
return Encoding.GetEncoding("gbk").GetString(output.ToArray());
}
catch
{
{

}
}
return Encoding.UTF8.GetString(output.ToArray());
Expand Down
2 changes: 1 addition & 1 deletion src/ColorMC.Core/Game/Mods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static void Disable(this ModObj mod)

var info = mod.Game.Mods.Values.FirstOrDefault(item => item.Sha1 == mod.Sha1);
if (info != null)
{
{
info.File = Path.ChangeExtension(info.File, Name3);
mod.Game.SaveModInfo();
}
Expand Down
1 change: 0 additions & 1 deletion src/ColorMC.Core/Hook/Win32.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using Microsoft.Win32.SafeHandles;

Expand Down
4 changes: 2 additions & 2 deletions src/ColorMC.Gui/ColorMCGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public static void Main(string[] args)
ProcessUtils.LaunchAdmin(args);
}
catch
{
{

}
return;
}
Expand Down
68 changes: 68 additions & 0 deletions src/ColorMC.Gui/Hook/HookUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Diagnostics;
using ColorMC.Core.Objs;
using ColorMC.Core.Utils;

namespace ColorMC.Gui.Hook;

public static class HookUtils
{
public static ulong GetMemorySize()
{
if (SystemInfo.Os == OsType.Windows)
{
return Win32.GetTotalMemory() / 1024 / 1024;
}
else if (SystemInfo.Os == OsType.Linux)
{
return Linux.GetTotalMemory() / 1024;
}
else if (SystemInfo.Os == OsType.MacOS)
{
return Macos.GetTotalMemory() / 1024;
}

return 0;
}

public static ulong GetFreeSize()
{
if (SystemInfo.Os == OsType.Windows)
{
return Win32.GetFreeMemory() / 1024 / 1024;
}
else if (SystemInfo.Os == OsType.Linux)
{
return Linux.GetFreeMemory() / 1024;
}
else if (SystemInfo.Os == OsType.MacOS)
{
return Macos.GetFreeMemory() / 1024;
}

return 0;
}

public static void WaitWindowDisplay(Process pr)
{
if (SystemInfo.Os == OsType.Windows)
{
Win32.WaitWindowDisplay(pr);
}
else if (SystemInfo.Os == OsType.Linux)
{
X11Native.WaitWindowDisplay(pr);
}
}

public static void SetTitle(Process pr, string title)
{
if (SystemInfo.Os == OsType.Windows)
{
Win32.SetTitle(pr, title);
}
else if (SystemInfo.Os == OsType.Linux)
{
X11Native.SetTitle(pr, title);
}
}
}
51 changes: 51 additions & 0 deletions src/ColorMC.Gui/Hook/Linux.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;

namespace ColorMC.Gui.Hook;

internal static class Linux
{
public static ulong GetTotalMemory()
{
const string memInfoPath = "/proc/meminfo";
if (File.Exists(memInfoPath))
{
var lines = File.ReadAllLines(memInfoPath);
foreach (var line in lines)
{
if (line.StartsWith("MemTotal:"))
{
var parts = line.Split([' '], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2 && ulong.TryParse(parts[1], out ulong memKb))
{
return memKb;
}
}
}
}

return 0;
}

public static ulong GetFreeMemory()
{
const string memInfoPath = "/proc/meminfo";
if (File.Exists(memInfoPath))
{
var lines = File.ReadAllLines(memInfoPath);
foreach (var line in lines)
{
if (line.StartsWith("MemFree:"))
{
var parts = line.Split([' '], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2 && ulong.TryParse(parts[1], out ulong memKb))
{
return memKb;
}
}
}
}

return 0;
}
}
86 changes: 86 additions & 0 deletions src/ColorMC.Gui/Hook/Macos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Diagnostics;

namespace ColorMC.Gui.Hook;

internal static class Macos
{
public static ulong GetTotalMemory()
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "sysctl",
Arguments = "hw.memsize",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};

process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

if (output.StartsWith("hw.memsize:"))
{
var parts = output.Split([' '], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2 && ulong.TryParse(parts[1], out ulong memBytes))
{
return memBytes;
}
}

return 0;
}

public static ulong GetFreeMemory()
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "vm_stat",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};

process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

var lines = output.Split('\n');
ulong freePages = 0;
ulong pageSize = 4096;

foreach (var line in lines)
{
if (line.StartsWith("Pages free:"))
{
var parts = line.Split([' '], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 3 && ulong.TryParse(parts[2].TrimEnd('.'), out ulong pages))
{
freePages = pages;
}
}
else if (line.StartsWith("page size of"))
{
var parts = line.Split([' '], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 4 && ulong.TryParse(parts[3], out ulong size))
{
pageSize = size;
}
}
}

if (freePages > 0)
{
return freePages * pageSize;
}

return 0;
}
}
Loading

0 comments on commit 5eab1dd

Please sign in to comment.