-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
830 additions
and
3,361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,8 +81,8 @@ public static void Main(string[] args) | |
ProcessUtils.LaunchAdmin(args); | ||
} | ||
catch | ||
{ | ||
{ | ||
|
||
} | ||
return; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.