Skip to content

Commit a9160ef

Browse files
show commit hash in info
1 parent 32bdef4 commit a9160ef

File tree

7 files changed

+56
-10
lines changed

7 files changed

+56
-10
lines changed

assets/preload/data/defaultOptions.json

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"option": "versionDisplay",
1717
"value": true
1818
},
19+
{
20+
"option": "showCommitHash",
21+
"value": true
22+
},
1923
{
2024
"option": "uiSkin",
2125
"value": "default"

source/Main.hx

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class Main extends Sprite {
107107
display.infoDisplayed[3] = logsEnabled;
108108
}
109109

110+
public static inline function toggleCommitHash(commitHashEnabled:Bool):Void {
111+
display.infoDisplayed[4] = commitHashEnabled;
112+
}
113+
110114
public static inline function changeFont(font:String):Void {
111115
display.defaultTextFormat = new TextFormat(font, (font == "_sans" ? 12 : 14), display.textColor);
112116
}

source/macros/GithubCommitHash.hx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package macros;
2+
3+
import haxe.macro.Context;
4+
import haxe.macro.Expr;
5+
import sys.io.Process;
6+
7+
/**
8+
* Macro class for getting the current git commit hash.
9+
* @see https://code.haxe.org/category/macros/add-git-commit-hash-in-build.html
10+
*/
11+
class GithubCommitHash {
12+
public static macro function getGitCommitHash():ExprOf<String> {
13+
#if !display
14+
var process:Process = new Process('git', ['rev-parse', 'HEAD']);
15+
if (process.exitCode() != 0) {
16+
var message:String = process.stderr.readAll().toString();
17+
var pos:Position = Context.currentPos();
18+
Context.error("Cannot execute `git rev-parse HEAD`. " + message, pos);
19+
}
20+
21+
// read the output of the process
22+
var commitHash:String = process.stdout.readLine();
23+
24+
// Generates a string expression
25+
return macro $v{commitHash};
26+
#else
27+
// `#if display` is used for code completion. In this case returning an
28+
// empty string is good enough; We don't want to call git on every hint.
29+
var commitHash:String = "";
30+
return macro $v{commitHash};
31+
#end
32+
}
33+
}

source/states/OptionsMenu.hx

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class OptionsMenu extends MusicBeatState {
9696
"infoDisplayFont"),
9797
new BoolOption("FPS Counter", "fpsCounter"),
9898
new BoolOption("Memory Counter", "memoryCounter"),
99-
new BoolOption("Version Display", "versionDisplay")
99+
new BoolOption("Version Display", "versionDisplay"),
100+
new BoolOption("Commit Hash", "showCommitHash")
100101
],
101102
"Judgements" => [
102103
new PageOption("Back", "Gameplay"),

source/states/TitleState.hx

+1-2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class TitleState extends MusicBeatState {
170170
Main.toggleVers(Options.getData("versionDisplay"));
171171
Main.toggleLogs(Options.getData("developer"));
172172
Main.changeFont(Options.getData("infoDisplayFont"));
173+
Main.toggleCommitHash(Options.getData("showCommitHash"));
173174

174175
call("startIntroPost");
175176
}
@@ -307,8 +308,6 @@ class TitleState extends MusicBeatState {
307308
var http:Http = new Http("https://raw.githubusercontent.com/Vortex2Oblivion/LeatherEngine/main/version.txt");
308309
http.onData = (data:String) -> {
309310
data = 'v' + data;
310-
trace(data);
311-
312311
if (CoolUtil.getCurrentVersion() != data) {
313312
trace('Outdated Version Detected! ' + data.trim() + ' != ' + CoolUtil.getCurrentVersion(), WARNING);
314313
Main.display.version += ' - UPDATE AVALIABLE (${data.trim()})';

source/ui/Option.hx

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class BoolOption extends Option {
101101
Main.toggleVers(optionChecked);
102102
case "developer":
103103
Main.toggleLogs(optionChecked);
104+
case "showCommitHash":
105+
Main.toggleCommitHash(optionChecked);
104106
}
105107
}
106108
}

source/ui/SimpleInfoDisplay.hx

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package ui;
22

3+
import macros.GithubCommitHash;
34
import flixel.util.FlxStringUtil;
45
import flixel.FlxG;
56
import openfl.utils.Assets;
67
import openfl.text.TextField;
78
import openfl.text.TextFormat;
89
import external.memory.Memory;
10+
import macros.GithubCommitHash;
11+
import haxe.macro.Compiler;
912

1013
/**
1114
* Shows basic info about the game.
@@ -35,7 +38,7 @@ class SimpleInfoDisplay extends TextField {
3538
height = FlxG.height;
3639
}
3740

38-
private function update():Void {
41+
public function update():Void {
3942
framerateTimer += FlxG.elapsed;
4043

4144
if (framerateTimer >= 1) {
@@ -59,16 +62,16 @@ class SimpleInfoDisplay extends TextField {
5962

6063
switch (i) {
6164
case 0: // FPS
62-
text += '${framerate}fps';
65+
text += '${framerate}fps\n';
6366
case 1: // Memory
64-
text += '${FlxStringUtil.formatBytes(Memory.getCurrentUsage())} / ${FlxStringUtil.formatBytes(Memory.getPeakUsage())}';
67+
text += '${FlxStringUtil.formatBytes(Memory.getCurrentUsage())} / ${FlxStringUtil.formatBytes(Memory.getPeakUsage())}\n';
6568
case 2: // Version
66-
text += version;
69+
text += '$version\n';
6770
case 3: // Console
68-
text += '${Main.logsOverlay.logs.length} traced lines. F3 to view.';
71+
text += Main.logsOverlay.logs.length > 0 ? '${Main.logsOverlay.logs.length} traced lines. F3 to view.\n' : '';
72+
case 4:
73+
text += 'Commit ${GithubCommitHash.getGitCommitHash().substring(0, 7)}';
6974
}
70-
71-
text += '\n';
7275
}
7376
}
7477
}

0 commit comments

Comments
 (0)