From 97791b36dc88d9b67e2911f8c15e79cd7bebdbd3 Mon Sep 17 00:00:00 2001 From: NeeEoo Date: Fri, 24 Mar 2023 01:30:29 +0100 Subject: [PATCH 1/6] Fix compiling on mac --- hmm.json | 2 +- source/funkin/utils/MemoryUtil.hx | 90 ++++++++++++++++--------------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/hmm.json b/hmm.json index 52aea2a2..06defb1c 100644 --- a/hmm.json +++ b/hmm.json @@ -22,7 +22,7 @@ "type": "git", "dir": "flxanimate", "ref": "master", - "url": "https://github.com/Dot-Stuff/flxanimate/" + "url": "https://github.com/YoshiCrafter29/flxanimate/" }, { "name": "format", diff --git a/source/funkin/utils/MemoryUtil.hx b/source/funkin/utils/MemoryUtil.hx index 66f5c603..db6d2685 100644 --- a/source/funkin/utils/MemoryUtil.hx +++ b/source/funkin/utils/MemoryUtil.hx @@ -12,53 +12,20 @@ import neko.vm.Gc; #end import openfl.system.System; +using StringTools; + #if cpp @:cppFileCode(" #if defined(HX_WINDOWS) -#include - -long long totalMem() { - unsigned long long allocatedRAM = 0; - GetPhysicallyInstalledSystemMemory(&allocatedRAM); - return (allocatedRAM / 1024); -} + #include #endif -#if define(HX_MAC) -#include -long long totalMem() { - int mib [] = { CTL_HW, HW_MEMSIZE }; - int64_t value = 0; - size_t length = sizeof(value); - - if(-1 == sysctl(mib, 2, &value, &length, NULL, 0)) - return -1; // An error occurred - - return value / 1024 / 1024; -} +#if defined(HX_MAC) + #include #endif + #if defined(HX_LINUX) -#include - -long long totalMem() { - FILE *meminfo = fopen('/proc/meminfo', 'r'); - - if(meminfo == NULL) return -1; - - char line[256]; - while(fgets(line, sizeof(line), meminfo)) - { - int ram; - if(sscanf(line, 'MemTotal: %d kB', &ram) == 1) - { - fclose(meminfo); - return (ram / 1024); - } - } - - fclose(meminfo); - return -1; -} + #include #endif ") #end @@ -112,7 +79,44 @@ class MemoryUtil { } #if cpp - @:functionCode("return totalMem();") + @:functionCode(" +#if defined(HX_WINDOWS) + unsigned long long allocatedRAM = 0; + GetPhysicallyInstalledSystemMemory(&allocatedRAM); + return (allocatedRAM / 1024); +#endif + +#if defined(HX_MAC) + int mib [] = { CTL_HW, HW_MEMSIZE }; + int64_t value = 0; + size_t length = sizeof(value); + + if(-1 == sysctl(mib, 2, &value, &length, NULL, 0)) + return -1; // An error occurred + + return value / 1024 / 1024; +#endif + +#if defined(HX_LINUX) + FILE *meminfo = fopen('/proc/meminfo', 'r'); + + if(meminfo == NULL) return -1; + + char line[256]; + while(fgets(line, sizeof(line), meminfo)) + { + int ram; + if(sscanf(line, 'MemTotal: %d kB', &ram) == 1) + { + fclose(meminfo); + return (ram / 1024); + } + } + + fclose(meminfo); + return -1; +#endif + ") public static function getTotalMem():Float { return 0; @@ -167,7 +171,9 @@ class MemoryUtil { if (memoryOutput != -1) return memoryMap[memoryOutput]; #elseif mac var process = new HiddenProcess("system_profiler", ["SPMemoryDataType"]); - if (process.exitCode() == 0) return process.stdout.readAll().toString().match(/Type: (.+)/)[1]; + var reg = ~/Type: (.+)/; + reg.match(process.stdout.readAll().toString()); + if (process.exitCode() == 0) return reg.matched(1); #elseif linux var process = HiddenProcess("sudo", ["dmidecode", "--type", "17"]); if (process.exitCode() != 0) return "Unknown"; From 44a459895806a43cd71acee4626bd2a0a46a8ea7 Mon Sep 17 00:00:00 2001 From: lunarcleint <88809950+lunarcleint@users.noreply.github.com> Date: Thu, 23 Mar 2023 17:44:54 -0700 Subject: [PATCH 2/6] hopefully working mac total mem --- source/funkin/system/framerate/SystemInfo.hx | 2 +- source/funkin/utils/MemoryUtil.hx | 62 +++++++++++--------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/source/funkin/system/framerate/SystemInfo.hx b/source/funkin/system/framerate/SystemInfo.hx index fe9ffb26..cd8d3b50 100644 --- a/source/funkin/system/framerate/SystemInfo.hx +++ b/source/funkin/system/framerate/SystemInfo.hx @@ -14,7 +14,7 @@ class SystemInfo extends FramerateCategory { public static var memType:String = "Unknown"; public static inline function init() { - osInfo = '${lime.system.System.platformLabel.replace(lime.system.System.platformVersion, "")} ${lime.system.System.platformVersion}'; + osInfo = '${lime.system.System.platformLabel.replace(lime.system.System.platformVersion, "").trim()} ${lime.system.System.platformVersion}'; #if windows var process = new HiddenProcess("wmic", ["cpu", "get", "name"]); diff --git a/source/funkin/utils/MemoryUtil.hx b/source/funkin/utils/MemoryUtil.hx index db6d2685..78a3584d 100644 --- a/source/funkin/utils/MemoryUtil.hx +++ b/source/funkin/utils/MemoryUtil.hx @@ -80,42 +80,48 @@ class MemoryUtil { #if cpp @:functionCode(" -#if defined(HX_WINDOWS) - unsigned long long allocatedRAM = 0; - GetPhysicallyInstalledSystemMemory(&allocatedRAM); - return (allocatedRAM / 1024); -#endif + #if defined(HX_WINDOWS) + unsigned long long allocatedRAM = 0; + GetPhysicallyInstalledSystemMemory(&allocatedRAM); + return (allocatedRAM / 1024); + #endif -#if defined(HX_MAC) - int mib [] = { CTL_HW, HW_MEMSIZE }; - int64_t value = 0; - size_t length = sizeof(value); + #if defined(HX_MAC) + vm_size_t page_size; + host_page_size(mach_host_self(), &page_size); + vm_statistics64_data_t vm_stats; + mach_msg_type_number_t info_count = HOST_VM_INFO64_COUNT; + if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stats, &info_count) == KERN_SUCCESS) { + unsigned long long total_memory = ((unsigned long long)vm_stats.active_count + + (unsigned long long)vm_stats.inactive_count + + (unsigned long long)vm_stats.wire_count + + (unsigned long long)vm_stats.free_count) * (unsigned long long)page_size; - if(-1 == sysctl(mib, 2, &value, &length, NULL, 0)) - return -1; // An error occurred + return total_memory / (1024*1024); + } else { + return -1024; + } + #endif - return value / 1024 / 1024; -#endif + #if defined(HX_LINUX) + FILE *meminfo = fopen('/proc/meminfo', 'r'); -#if defined(HX_LINUX) - FILE *meminfo = fopen('/proc/meminfo', 'r'); + if(meminfo == NULL) return -1; - if(meminfo == NULL) return -1; - - char line[256]; - while(fgets(line, sizeof(line), meminfo)) - { - int ram; - if(sscanf(line, 'MemTotal: %d kB', &ram) == 1) + char line[256]; + while(fgets(line, sizeof(line), meminfo)) { - fclose(meminfo); - return (ram / 1024); + int ram; + if(sscanf(line, 'MemTotal: %d kB', &ram) == 1) + { + fclose(meminfo); + return (ram / 1024); + } } - } - fclose(meminfo); - return -1; -#endif + fclose(meminfo); + return -1; + #endif ") public static function getTotalMem():Float { From 9ed1d82952c63d4eb6262f8d15e5bf218a9622f4 Mon Sep 17 00:00:00 2001 From: lunarcleint <88809950+lunarcleint@users.noreply.github.com> Date: Thu, 23 Mar 2023 17:45:31 -0700 Subject: [PATCH 3/6] useless else --- source/funkin/utils/MemoryUtil.hx | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/funkin/utils/MemoryUtil.hx b/source/funkin/utils/MemoryUtil.hx index 78a3584d..bc59dea3 100644 --- a/source/funkin/utils/MemoryUtil.hx +++ b/source/funkin/utils/MemoryUtil.hx @@ -98,8 +98,6 @@ class MemoryUtil { (unsigned long long)vm_stats.free_count) * (unsigned long long)page_size; return total_memory / (1024*1024); - } else { - return -1024; } #endif From f63a0d7eaa479af9673540267a3bc8ebf341ada1 Mon Sep 17 00:00:00 2001 From: lunarcleint <88809950+lunarcleint@users.noreply.github.com> Date: Thu, 23 Mar 2023 17:47:30 -0700 Subject: [PATCH 4/6] i forgot includes --- source/funkin/utils/MemoryUtil.hx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/funkin/utils/MemoryUtil.hx b/source/funkin/utils/MemoryUtil.hx index bc59dea3..47f4ab69 100644 --- a/source/funkin/utils/MemoryUtil.hx +++ b/source/funkin/utils/MemoryUtil.hx @@ -21,7 +21,8 @@ using StringTools; #endif #if defined(HX_MAC) - #include + #include + #include #endif #if defined(HX_LINUX) From e0e59d449513af8719da0984073ccac040d4d911 Mon Sep 17 00:00:00 2001 From: lunarcleint <88809950+lunarcleint@users.noreply.github.com> Date: Thu, 23 Mar 2023 17:50:54 -0700 Subject: [PATCH 5/6] grahhh --- source/funkin/utils/MemoryUtil.hx | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/source/funkin/utils/MemoryUtil.hx b/source/funkin/utils/MemoryUtil.hx index 47f4ab69..6974ee72 100644 --- a/source/funkin/utils/MemoryUtil.hx +++ b/source/funkin/utils/MemoryUtil.hx @@ -21,12 +21,11 @@ using StringTools; #endif #if defined(HX_MAC) - #include - #include + #include #endif #if defined(HX_LINUX) - #include + #include #endif ") #end @@ -88,18 +87,14 @@ class MemoryUtil { #endif #if defined(HX_MAC) - vm_size_t page_size; - host_page_size(mach_host_self(), &page_size); - vm_statistics64_data_t vm_stats; - mach_msg_type_number_t info_count = HOST_VM_INFO64_COUNT; - if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stats, &info_count) == KERN_SUCCESS) { - unsigned long long total_memory = ((unsigned long long)vm_stats.active_count + - (unsigned long long)vm_stats.inactive_count + - (unsigned long long)vm_stats.wire_count + - (unsigned long long)vm_stats.free_count) * (unsigned long long)page_size; + int mib [] = { CTL_HW, HW_MEMSIZE }; + int64_t value = 0; + size_t length = sizeof(value); - return total_memory / (1024*1024); - } + if(-1 == sysctl(mib, 2, &value, &length, NULL, 0)) + return -1; // An error occurred + + return value / 1024 / 1024; #endif #if defined(HX_LINUX) From aeacb0fb3ef3f0e13a821a4e0d5d3bea8601cd8e Mon Sep 17 00:00:00 2001 From: NeeEoo Date: Fri, 24 Mar 2023 02:06:15 +0100 Subject: [PATCH 6/6] Working total ram, but needs restructuring --- source/ExternalCode.hx | 55 ++++++++++++++++++++ source/funkin/system/framerate/SystemInfo.hx | 7 ++- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 source/ExternalCode.hx diff --git a/source/ExternalCode.hx b/source/ExternalCode.hx new file mode 100644 index 00000000..70575c83 --- /dev/null +++ b/source/ExternalCode.hx @@ -0,0 +1,55 @@ +package; + +#if windows +@:headerCode("#include ") +#elseif linux +@:headerCode("#include ") +#elseif mac +@:headerCode("#include ") +#end +class ExternalCode { + #if windows + @:functionCode(" + // simple but effective code + unsigned long long allocatedRAM = 0; + GetPhysicallyInstalledSystemMemory(&allocatedRAM); + return (allocatedRAM / 1024); + ") + #elseif linux + @:functionCode(' + FILE *meminfo = fopen("/proc/meminfo", "r"); + + if(meminfo == NULL) + return -1; + + char line[256]; + while(fgets(line, sizeof(line), meminfo)) + { + int ram; + if(sscanf(line, "MemTotal: %d kB", &ram) == 1) + { + fclose(meminfo); + return (ram / 1024); + } + } + + fclose(meminfo); + return -1; + ') + #elseif mac + @:functionCode(' + int mib [] = { CTL_HW, HW_MEMSIZE }; + int64_t value = 0; + size_t length = sizeof(value); + + if(-1 == sysctl(mib, 2, &value, &length, NULL, 0)) + return -1; // An error occurred + + return value / 1024 / 1024; + ') + #end + public static function getTotalRam():Float + { + return 0; + } +} \ No newline at end of file diff --git a/source/funkin/system/framerate/SystemInfo.hx b/source/funkin/system/framerate/SystemInfo.hx index cd8d3b50..2e4a182b 100644 --- a/source/funkin/system/framerate/SystemInfo.hx +++ b/source/funkin/system/framerate/SystemInfo.hx @@ -1,5 +1,6 @@ package funkin.system.framerate; +import ExternalCode; import native.HiddenProcess; import funkin.utils.MemoryUtil; @@ -39,8 +40,10 @@ class SystemInfo extends FramerateCategory { vRAM = CoolUtil.getSizeString(cast(flixel.FlxG.stage.context3D.gl.getParameter(openfl.display3D.Context3D.__glMemoryTotalAvailable), UInt) * 1000); } - #if cpp - totalMem = Std.string(MemoryUtil.getTotalMem() / 1024) + " GB"; + #if cpp + var total = ExternalCode.getTotalRam(); + trace("Total Ram: " + total); + totalMem = Std.string(total / 1024) + " GB"; #end memType = MemoryUtil.getMemType(); }