Repository navigation
Expand file tree
/
Copy pathproject.hxp
More file actions
865 lines (711 loc) · 28 KB
/
Copy pathproject.hxp
File metadata and controls
865 lines (711 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
package;
import hxp.*;
import lime.tools.*;
import sys.FileSystem;
import sys.io.File;
import sys.io.Process;
import haxe.io.Path;
using StringTools;
/**
* This HXP performs the functions of a Lime `project.xml` file,
* but it's written in Haxe rather than XML!
*
* This makes it far easier to organize, reuse, and refactor,
* and improves management of feature flag logic.
*/
@:nullSafety
class Project extends HXProject {
//
// METADATA
//
/**
* The game's version number, as a Semantic Versioning string with no prefix.
* REMEMBER TO CHANGE THIS WHEN THE GAME UPDATES!
* You only have to change it here, the rest of the game will query this value.
*/
static final VERSION:String = "0.1.5";
/**
* The game's name. Used as the default window title.
*/
static final TITLE:String = "AlterEngine";
/**
* A package name used for identifying the app on various app stores.
*/
static final PACKAGE_NAME:String = "com.altertoriel.alterengine";
/**
* The name of the generated executable file.
* For example, `"Funkin"` will create a file called `Funkin.exe`.
*/
static final EXECUTABLE_NAME:String = "AlterEngine";
/**
* The relative location of the source code.
*/
static final SOURCE_DIR:String = "source";
/**
* The fully qualified class path for the game's preloader.
* Particularly important on HTML5 but we use it on all platforms.
*/
static final PRELOADER:String = "flixel.system.FlxPreloader";
/**
* The fully qualified class path for the entry point class to execute when launching the game.
* It's where `public static function main():Void` goes.
*/
static final MAIN_CLASS:String = "Main";
/**
* The company name for the game.
* This appears in metadata in places I think.
*/
static final COMPANY:String = "Altertoriel";
/**
* Asset path globs to always exclude from asset libraries.
*/
static final EXCLUDE_ASSETS:Array<String> = [".*", "cvs", "thumbs.db", "desktop.ini", "*.hash", "*.md", "*.fla", "*/.git*"];
//
// FEATURES
//
static final MODS_ALLOWED:FeatureFlag = "MODS_ALLOWED";
static final VIDEOS_ALLOWED:FeatureFlag = "VIDEOS_ALLOWED";
static final HSCRIPT_ALLOWED:FeatureFlag = "HSCRIPT_ALLOWED";
static final DISCORD_ALLOWED:FeatureFlag = "DISCORD_ALLOWED";
static final AWARDS_ALLOWED:FeatureFlag = "AWARDS_ALLOWED";
static final CRASH_HANDLER:FeatureFlag = "CRASH_HANDLER";
static final CHECK_FOR_UPDATES:FeatureFlag = "CHECK_FOR_UPDATES";
static final NDLLS_ALLOWED:FeatureFlag = "NDLLS_ALLOWED";
static final TRANSLATIONS_ALLOWED:FeatureFlag = "TRANSLATIONS_ALLOWED";
static final ALTER_DEBUG:FeatureFlag = "ALTER_DEBUG";
static final MULTITHREADED_LOADING:FeatureFlag = "MULTITHREADED_LOADING";
static final SHOW_LOADING_SCREEN:FeatureFlag = "SHOW_LOADING_SCREEN";
static final GITHUB_BUILD:FeatureFlag = "GITHUB_BUILD";
/**
* `-DULTRA_OPTIMIZED` (WIP / EXPERIMENTAL max-speed build flag).
* Strips debug instrumentation and forces the fastest codegen the
* installed toolchain supports. Enabled with `-DULTRA_OPTIMIZED`.
* Defines this flag toggles (see the ULTRA_OPTIMIZED block in
* configureCompileDefines):
* ON: analyzer-optimize, HXCPP_OPTIMIZE_LINK (/GL + /LTCG or -flto)
* OFF: HXCPP_CHECK_POINTER, HXCPP_STACK_LINE, HXCPP_STACK_TRACE,
* HXCPP_GC_CHECK_LEAKS, HXCPP_FAST_LINK
* KEPT: HXCPP_CATCH_SEGV (CrashHandler needs it), fast GC defines,
* HXCPP_CPP17, HXCPP_NO_ARCH_LEVEL, -dce no
* WIP: may crash code that relied on null-pointer checks / stack traces.
*/
static final ULTRA_OPTIMIZED:FeatureFlag = "ULTRA_OPTIMIZED";
/**
* `-DFEATURE_DEBUG_FUNCTIONS`
* If this flag is enabled, the game will have all playtester-only debugging functionality enabled.
* This includes debug hotkeys like time travel in the Play State.
* By default, enabled on debug builds or playtester builds and disabled on release builds.
*/
static final FEATURE_DEBUG_FUNCTIONS:FeatureFlag = "FEATURE_DEBUG_FUNCTIONS";
/**
* `-DFEATURE_DEBUG_TRACY`
* If this flag is enabled, the game will have the necessary hooks for the Tracy profiler.
* Only enable this if you're using the correct fork of Haxe to support this.
* @see https://github.com/HaxeFoundation/hxcpp/pull/1153
*/
static final FEATURE_DEBUG_TRACY:FeatureFlag = "FEATURE_DEBUG_TRACY";
/**
* `-DFEATURE_FUNKVIS`
* If this flag is enabled, the game will enable the Funkin Visualizer library.
* This is used to provide audio visualization like Nene's speaker.
* Disabling this will make some waveforms inactive.
*/
static final FEATURE_FUNKVIS:FeatureFlag = "FEATURE_FUNKVIS";
//
// CONFIGURATION FUNCTIONS
//
public function new():Void {
super();
setenv('HAXEPATH', './'); // Fixes an issue for haxelibs that use native C++ files until Haxe 5 releases.
#if !target.unicode
if (isWindowsHost()) Sys.command("chcp 65001 > nul");
#end
flair();
configureApp();
displayTarget();
configureFeatureFlags();
configureCompileDefines();
configureOutputDir();
configureHaxelibs();
configureAssets();
configureIcons();
if (isCPP()) buildHxCppTools();
}
/**
* Do something before building, display some ASCII or something IDK
*/
function flair():Void {
info("AlterEngine - " + VERSION);
info("Initializing build...");
var branch:String = getGitBranch();
if (branch != null && branch != "") {
info("Git Branch: " + branch);
info("Git Commit: " + getGitCommit());
}
info("Display? " + isDisplay());
}
/**
* Apply basic project metadata, such as the game title and version number,
* as well as info like the package name and company (used by various app stores).
*/
function configureApp():Void {
this.meta.title = TITLE;
this.meta.version = VERSION;
this.meta.packageName = PACKAGE_NAME;
this.meta.company = COMPANY;
this.app.main = MAIN_CLASS;
this.app.file = EXECUTABLE_NAME;
this.app.preloader = PRELOADER;
// Tell Lime where to look for the game's source code.
// If for some reason we have multiple source directories, we can add more entries here.
this.sources.push(SOURCE_DIR);
// Configure the window.
// Automatically configure FPS.
this.window.fps = 60;
// Set the window size.
this.window.width = 1280;
this.window.height = 720;
// Black background on release builds, magenta on debug builds.
this.window.background = FEATURE_DEBUG_FUNCTIONS.isEnabled(this) ? 0xFFFF00FF : 0xFF000000;
this.window.hardware = true;
this.window.vsync = false;
this.window.depthBuffer = false;
this.window.allowHighDPI = !isMac();
if (isDesktop()) {
this.window.orientation = Orientation.LANDSCAPE;
this.window.fullscreen = false;
this.window.resizable = true;
}
}
/**
* Log information about the configured target platform.
*/
function displayTarget() {
// Display the target operating system.
switch (this.target) {
case Platform.WINDOWS: info('Target Platform: Windows');
case Platform.MAC: info('Target Platform: MacOS');
case Platform.LINUX: info('Target Platform: Linux');
default: error('Unsupported platform! (got $target)');
}
if (isCPP()) info('Target Language: C++');
else error('Unsupported target language!');
for (arch in this.architectures) {
// Display the list of target architectures.
switch (arch) {
case Architecture.X64: info('Architecture: x64');
case Architecture.ARM64: info('Architecture: ARMx64');
case null: error('Unsupported architecture (got null on non-web platform)');
default: error('Unsupported architecture (got $arch)');
}
}
}
/**
* Apply various feature flags based on the target platform and the user-provided build flags.
*/
function configureFeatureFlags():Void {
// You can explicitly override any of these.
// For example, `-DMODS_ALLOWED` or `-DVIDEOS_ALLOWED`
CRASH_HANDLER.apply(this, true);
AWARDS_ALLOWED.apply(this, true);
CHECK_FOR_UPDATES.apply(this, true);
if (isDesktop()) {
MODS_ALLOWED.apply(this, !isWeb());
VIDEOS_ALLOWED.apply(this, (!isWeb() || !is32Bit()));
HSCRIPT_ALLOWED.apply(this, true);
DISCORD_ALLOWED.apply(this, true);
}
TRANSLATIONS_ALLOWED.apply(this, true);
NDLLS_ALLOWED.apply(this, isCPP());
ALTER_DEBUG.apply(this, true);
MULTITHREADED_LOADING.apply(this, true);
SHOW_LOADING_SCREEN.apply(this, true);
FEATURE_DEBUG_FUNCTIONS.apply(this, isDebug());
FEATURE_FUNKVIS.apply(this, true);
}
/**
* Set compilation flags which are not feature flags.
*/
function configureCompileDefines():Void {
// Enable OpenFL's error handler. Required for the crash logger.
setHaxedef("openfl-enable-handle-error");
// Enable stack trace tracking. Good for debugging but has a (minor) performance impact.
if (CRASH_HANDLER.isEnabled(this) && isCPP()) {
setHaxedef("HXCPP_CHECK_POINTER");
setHaxedef("HXCPP_STACK_LINE");
setHaxedef("HXCPP_STACK_TRACE");
setHaxedef("HXCPP_CATCH_SEGV");
}
if (HSCRIPT_ALLOWED.isEnabled(this)) {
addHaxeMacro("macros.KeepMacro.keep()");
setHaxedef("hscriptPos");
setHaxedef("CUSTOM_CLASSES");
setHaxedef("HSCRIPT_ABSTRACT_SUPPORT");
}
if (GITHUB_BUILD.isDisabled(this)) {
// HXCPP caching, which you can carry across projects
if (!isWindows()) setHaxedef("HXCPP_COMPILE_CACHE", "~/.hxcpp_cache");
else setHaxedef("HXCPP_COMPILE_CACHE", "C:/.hxcpp_cache");
setHaxedef("HXCPP_CACHE_MB", "4000");
}
if (FEATURE_DEBUG_FUNCTIONS.isDisabled(this)) {
setHaxedef("no-deprecation-warnings");
setHaxedef("FLX_NO_DEBUG"); // If we aren't using the Flixel debugger, strip it out.
setHaxedef("analyzer-optimize"); // Optimize haxe code when not in debug to improve performance.
}
// Allow memory to go above 1GB.
setHaxedef("HXCPP_GC_BIG_BLOCKS");
if (DISCORD_ALLOWED.isEnabled(this)) setHaxedef("DISCORD_DISABLE_IO_THREAD");
// Disable the built in pause screen when unfocusing the game.
setHaxedef("FLX_NO_FOCUS_LOST_SCREEN");
if (isDesktop()) setHaxedef("FLX_NO_TOUCH");
// Cleaner looking compiler errors.
setHaxedef("message.reporting", "pretty");
addHaxeMacro("macros.FlxMacro.initMacros()");
if (VIDEOS_ALLOWED.isEnabled(this)) {
if (isDebug()) setHaxedef("HXC_LIBVLC_LOGGING");
setHaxedef("HXVLC_NO_SHARE_DIRECTORY");
}
if (FEATURE_DEBUG_TRACY.isEnabled(this)) {
setHaxedef("HXCPP_TELEMETRY"); // Enable telemetry
setHaxedef("HXCPP_TRACY"); // Enable Tracy telemetry
setHaxedef("HXCPP_TRACY_MEMORY"); // Track memory allocations
setHaxedef("HXCPP_TRACY_ON_DEMAND"); // Only collect telemetry when Tracy is open and reachable
setHaxedef("absolute-paths"); // Fix source locations so Tracy can see them
}
if (ULTRA_OPTIMIZED.isEnabled(this)) {
// WIP: experimental max-speed build. See the flag doc above.
// hxcpp 4.3.2 has NO HXCPP_OPTIMIZE_SPEED/SIZE/O3 defines (grep
// toolchain + tools = zero hits): MSVC already emits /O2 in
// release (msvc-toolchain.xml:67,79) which is max-speed for cl,
// and gcc/clang already use -O2 (linux-toolchain.xml:35). So the
// real speed levers are: enable whole-program link-time codegen
// (HXCPP_OPTIMIZE_LINK → /GL + /LTCG, or -flto) and drop the
// fast-link default that forces INCREMENTAL LTCG.
// UNSET: debug instrumentation overhead (hxcpp toolchain applies
// each by emitting -D flags to haxe in common-defines.xml).
unsetHaxedef("HXCPP_CHECK_POINTER"); // null-pointer checks
unsetHaxedef("HXCPP_STACK_LINE"); // line info in stack traces
unsetHaxedef("HXCPP_STACK_TRACE"); // stack frame tracking
unsetHaxedef("HXCPP_GC_CHECK_LEAKS"); // GC leak scanning
// KEEP: HXCPP_CATCH_SEGV — CrashHandler.init() registers
// __hxcpp_set_critical_error_handler and CallStack.exceptionStack()
// in onUncaughtError; HXCPP_CATCH_SEGV enables SEH translation
// in hxcpp (Debug.cpp:273-279, StackContext.h:448) that keeps
// segfaults catchable as haxe exceptions instead of hard crashes.
// Removing it would crash the game instead of showing the crash
// screen; the overhead is negligible so we keep it.
// ENABLE: link-time codegen (whole-program LTCG/LTO).
// msvc-toolchain.xml:83 → -GL (compile), :123/:148 → -LTCG (link).
// linux-toolchain.xml:31, mac-toolchain.xml:51 → -flto.
// HXCPP_FAST_LINK must be unset first so hxcpp uses full LTCG
// rather than HXCPP_LTCG_INCREMENTAL (finish-setup.xml:34).
unsetHaxedef("HXCPP_FAST_LINK");
setHaxedef("HXCPP_OPTIMIZE_LINK");
// FORCE: analyzer-optimize is normally only set in !isDebug(); ensure it
// regardless of build type when ULTRA is on.
setHaxedef("analyzer-optimize");
// DCE: keep "-dce no" (already in haxeflags above). This game
// is moddable with heavy runtime reflection (128+ Reflect.* calls
// across 15+ source files, plus hscript-improved dynamic dispatch).
// -dce full would strip dead code that mods may reference via
// Reflect.field/callMethod at runtime → null-access crashes.
// Safer to keep -dce no and rely on analyzer-optimize instead.
info("ULTRA_OPTIMIZED: experimental max-speed build enabled (native target)");
}
if (command == "update" || isDisplay() || hasDefine("display") || hasDefine("display_details")) {
unsetHaxedef("HSCRIPT_ABSTRACT_SUPPORT");
unsetHaxedef("CUSTOM_CLASSES");
}
}
function configureOutputDir():Void {
// Set the output directory. Depends on the target platform and build type.
var buildDir:String = 'export/${isDebug() ? 'debug' : 'release'}';
// we use a dedicated 'tracy' folder, since it generally needs a recompile when in use
if (FEATURE_DEBUG_TRACY.isEnabled(this)) buildDir += "-tracy";
buildDir += "/";
info('Output directory: $buildDir');
app.path = buildDir;
}
function configureHaxelibs():Void {
addHaxelib('lime'); // Game engine backend
addHaxelib('openfl'); // Game engine backend
addHaxelib('flixel'); // Game engine
addHaxelib('flixel-addons'); // Additional utilities for Flixel
addHaxelib('flixel-animate'); // Texture atlas rendering
addHaxelib('json2object'); // JSON parsing
addHaxelib('parallaxlt'); // ParallaxSprites
addHaxelib('tjson');
addHaxelib('thx.core'); // General utility library, "the lodash of Haxe"
addHaxelib('thx.semver'); // Version string handling
if (HSCRIPT_ALLOWED.isEnabled(this)) addHaxelib('hscript-improved'); // Scripting
if (isDebug() && isCPP()) addHaxelib('hxcpp-debug-server'); // VSCode debug support
if (VIDEOS_ALLOWED.isEnabled(this)) addHaxelib('hxvlc'); // Video playback
if (isLinux() && !isDebug()) addHaxelib('hxgamemode'); // FeralInteractive gamemode
if (DISCORD_ALLOWED.isEnabled(this)) addHaxelib('hxdiscord_rpc'); // Discord API
if (FEATURE_FUNKVIS.isEnabled(this)) {
addHaxelib('funkin.vis'); // Audio visualization
addHaxelib('grig.audio'); // Audio data utilities
}
}
function configureAssets():Void {
if (!(isDisplay() || isCleanCommand())) clearAssets();
var exclude:Array<String> = EXCLUDE_ASSETS;
// Default asset library
var shouldEmbed:Bool = false;
addAssetLibrary("default", shouldEmbed, true);
addAssetPath("assets", null, "default", ["*"], exclude, shouldEmbed);
var shouldEmbedFonts:Bool = true;
addAssetPath("assets/fonts", null, "default", ["*"], exclude, shouldEmbedFonts);
addAssetPath("assets/embed", "assets", "default", ["*"], exclude, true);
addAssetPath("assets/songs", null, "default", ["*"], exclude, shouldEmbed);
addAssetPath("assets/shared", null, "default", ["*"], exclude, shouldEmbed);
addAssetPath("assets/week_assets", "assets", "default", ["*"], exclude, shouldEmbed);
removeAssetPath("assets/week_assets");
if (TRANSLATIONS_ALLOWED.isEnabled(this)) addAssetPath("assets/translations", null, "default", ["*"], exclude, shouldEmbed);
if (VIDEOS_ALLOWED.isEnabled(this)) addAssetPath("assets/videos", null, "default", ["*"], exclude, shouldEmbed);
if (MODS_ALLOWED.isEnabled(this)) {
addAssetPath("example_mods", "mods", "default", ["*"], exclude, shouldEmbed);
addAsset("list.txt", "modsList.txt");
}
}
function clearAssets():Void {
// Don't run on non-build commands.
if (!isBuild()) return;
var exportPath:Null<String> = app.path ?? "";
// mac target uses `macos` for the export folder instead of `mac`
var platform:String = !isMac() ? Std.string(this.target) : 'macos';
var basePath:String = Path.join([exportPath, platform, "bin"]);
var assetsPath:String = "";
if (platformType == PlatformType.DESKTOP) {
if (isMac()) assetsPath = Path.join([basePath, '${this.app.file}.app', "Contents", "Resources", "assets"]);
else assetsPath = Path.join([basePath, "assets"]);
}
if (FileSystem.exists(assetsPath)) {
info('Deleting assets folder from export folder ($assetsPath)');
deleteDirectoryRecursive(assetsPath);
} else info('Assets folder does not exist ($assetsPath)');
}
/**
* Recursively delete a directory and its contents.
* @param path The path to the directory to delete.
*/
function deleteDirectoryRecursive(path:String):Void {
if (!FileSystem.exists(path)) return;
for (file in FileSystem.readDirectory(path)) {
var fullPath:String = Path.join([path, file]);
if (FileSystem.isDirectory(fullPath)) deleteDirectoryRecursive(fullPath);
else FileSystem.deleteFile(fullPath);
}
FileSystem.deleteDirectory(path);
}
/**
* Configure the application's favicon and executable icon.
*/
function configureIcons():Void {
for (size in [16, 32, 64]) addIcon('icons/icon${size}.png', size);
addIcon("icons/iconOG.png");
}
//
// HELPER FUNCTIONS
// Easy functions to make the code more readable.
//
public function isWeb():Bool {
return this.platformType == PlatformType.WEB;
}
public function isDesktop():Bool {
return this.platformType == PlatformType.DESKTOP;
}
public function is32Bit():Bool {
return this.architectures.contains(Architecture.X86);
}
public function isWindowsHost():Bool {
return System.hostPlatform == WINDOWS;
}
public function isWindows():Bool {
return this.target == Platform.WINDOWS;
}
public function isMac():Bool {
return this.target == Platform.MAC;
}
public function isLinux():Bool {
return this.target == Platform.LINUX;
}
public function isCPP():Bool {
return this.defines.exists("cpp");
}
public function isDisplay():Bool {
return this.command == "display";
}
public function isCleanCommand():Bool {
return this.command == "clean";
}
public function isRun():Bool {
return this.command == "run";
}
public function isInstall():Bool {
return this.command == "install";
}
public function isBuild():Bool {
return this.command == "test" || this.command == "build";
}
public function isDebug():Bool {
return this.debug;
}
public function setHaxedef(name:String, ?value:String):Void {
if (value == null) value = "";
this.haxedefs.set(name, value);
}
public function unsetHaxedef(name:String):Void {
this.haxedefs.remove(name);
}
public function hasDefine(name:String):Bool {
return this.defines.exists(name);
}
/**
* Add a library to the list of dependencies for the project.
* @param name The name of the library to add.
* @param version The version of the library to add. Optional.
*/
public function addHaxelib(name:String, version:String = ""):Void {
this.haxelibs.push(new Haxelib(name, version));
}
/**
* Add a `haxeflag` to the project.
*/
public function addHaxeFlag(value:String):Void {
this.haxeflags.push(value);
}
/**
* Call a Haxe build macro.
*/
public function addHaxeMacro(value:String):Void {
addHaxeFlag('--macro $value');
}
/**
* Add an icon to the project.
* @param icon The path to the icon.
* @param size The size of the icon. Optional.
*/
public function addIcon(icon:String, ?size:Int):Void {
this.icons.push(new Icon(icon, size));
}
/**
* Add an asset to the game build.
* @param path The path the asset is located at.
* @param rename The path the asset should be placed.
* @param library The asset library to add the asset to. `null` = "default"
* @param embed Whether to embed the asset in the executable.
*/
public function addAsset(path:String, ?rename:String, ?library:String, embed:Bool = false):Void {
var asset:Asset = new Asset(path, rename, null, embed, true);
@:nullSafety(Off) {
asset.library = library ?? "default";
}
this.assets.push(asset);
}
/**
* Remove an asset from the game build.
* @param path The path the asset is located at.
*/
public function removeAsset(path:String, ?library:String):Void {
for (asset in this.assets) {
if (asset.sourcePath == path) {
if (library != null && asset.library != library) continue;
this.assets.remove(asset);
break;
}
}
}
/**
* Add an entire path of assets to the game build.
* @param path The path the assets are located at.
* @param rename The path the assets should be placed.
* @param library The asset library to add the assets to. `null` = "default"
* @param include An optional array to include specific asset names.
* @param exclude An optional array to exclude specific asset names.
* @param embed Whether to embed the assets in the executable.
*/
public function addAssetPath(path:String, ?rename:String, library:String, ?include:Array<String>, ?exclude:Array<String>, embed:Bool = false):Void {
// Argument parsing.
if (path == "") return;
if (include == null) include = [];
if (exclude == null) exclude = [];
var targetPath:String = rename ?? path;
if (targetPath != "") targetPath += "/";
// Validate path.
if (!FileSystem.exists(path)) error('Could not find asset path "$path".');
else if (!FileSystem.isDirectory(path)) error('Could not parse asset path "$path", expected a directory.');
for (file in FileSystem.readDirectory(path)) {
if (FileSystem.isDirectory('$path/$file')) {
if (this.filter(file, ["*"], exclude)) addAssetPath('$path/$file', '${targetPath}${file}', library, include, exclude, embed);
} else {
if (this.filter(file, include, exclude)) addAsset('$path/$file', '${targetPath}${file}', library, embed);
}
}
}
/**
* Remove all assets in a path recursively from the game build.
* @param path The path the assets are located at.
* @param library (optional) The asset library to remove the assets from. Defaults to all libraries.
*/
public function removeAssetPath(path:String, ?library:String):Void {
if (path == "") return; // Argument parsing.
// Validate path.
if (!FileSystem.exists(path)) error('Could not find asset path "$path".');
else if (!FileSystem.isDirectory(path)) error('Could not parse asset path "$path", expected a directory.');
for (exclude in EXCLUDE_ASSETS) {
if (path.contains(exclude.replace('*', ''))) return;
}
for (file in FileSystem.readDirectory(path)) {
if (FileSystem.isDirectory('$path/$file')) removeAssetPath('$path/$file', library); // Attempt to recursively remove all assets in the directory.
else removeAsset('$path/$file', library);
}
}
/**
* Add an asset library to the game build.
* @param name The name of the library.
* @param embed
* @param preload
*/
public function addAssetLibrary(name:String, embed:Bool = false, preload:Bool = false):Void {
this.libraries.push(new Library('', name, null, embed, preload, false, ""));
}
//
// PROCESS FUNCTIONS
//
public function getGitCommit():String {
var process:Process = new Process('git', ['rev-parse', 'HEAD']);
if (process.exitCode() != 0) error('Could not determine current git commit; is this a proper Git repository?');
var commitHash:String = process.stdout.readLine();
var commitHashSplice:String = commitHash.substr(0, 7);
process.close();
return commitHashSplice;
}
static function readLibraryCurrentVersion(libraryName:String):String {
var path:String = getLibraryCurrentFile(libraryName);
if (FileSystem.exists(path)) return File.getContent(path).trim();
return '';
}
static function getLibraryCurrentFile(libraryName:String):String {
return Path.join([Path.addTrailingSlash(Sys.getCwd()), '.haxelib', libraryName, '.current']);
}
static function getLibraryPath(libraryName:String):String {
var process:Process = new Process('haxelib', ['libpath', libraryName]);
var lib:String = process.stdout.readLine();
process.close();
return lib;
}
public function getGitBranch():String {
var branchProcess:Process = new Process('git', ['rev-parse', '--abbrev-ref', 'HEAD']);
if (branchProcess.exitCode() != 0) error('Could not determine current git branch; is this a proper Git repository?');
var branchName:String = branchProcess.stdout.readLine();
branchProcess.close();
return branchName;
}
public function buildHxCppTools():Void {
var path:String = getLibraryPath("hxcpp");
var dotNPath:String = Path.join([path, 'hxcpp.n']);
var toolsPath:String = Path.join([path, 'tools/hxcpp']);
// The output can be an error message if the library doesn't exist.
if (path.toLowerCase().startsWith("error") || !FileSystem.exists(path))
error('HXCPP could not be found while on C++ target.');
// Build Tools are compiled, no need for us to do it.
if (FileSystem.exists(dotNPath)) return;
info('Compiling the HXCPP Build Tool...');
var oldDir:String = Sys.getCwd();
info(toolsPath);
Sys.setCwd(toolsPath);
var compileProcess:Process = new Process('haxe', ['compile.hxml']);
if (compileProcess.exitCode() != 0) info('Could not compile the Build Tool. Is HXCPP installed correctly? Continuing...');
else info('Compiled the HXCPP Build Tool successfully!');
compileProcess.close();
Sys.setCwd(oldDir);
}
//
// LOGGING FUNCTIONS
//
/**
* Display an error message. This should stop the build process.
*/
public function error(message:String):Void {
Log.error(message);
}
/**
* Display an info message. This should not interfere with the build process.
*/
public function info(message:String):Void {
if (!(isDisplay() || isCleanCommand() || isRun() || isInstall())) Log.info('[INFO] $message');
}
}
/**
* An object representing a feature flag, which can be enabled or disabled.
* Includes features such as automatic generation of compile defines and inversion.
*/
abstract FeatureFlag(String) from String to String {
public static final INVERSE_PREFIX:String = "NO_";
public function new(input:String):Void {
this = input;
}
@:from
public static function fromString(input:String):FeatureFlag {
return new FeatureFlag(input);
}
/**
* Enable/disable a feature flag if it is unset, and handle the inverse flag.
* Doesn't override a feature flag that was set explicitly.
* @param enableByDefault Whether to enable this feature flag if it is unset.
*/
public function apply(project:Project, enableByDefault:Bool = false):Void {
if (isEnabled(project)) {
getInverse().disable(project, false); // If this flag was already enabled, disable the inverse.
} else if (getInverse().isEnabled(project)) {
disable(project, false);
} else {
if (enableByDefault) enable(project, true);
else disable(project, true);
}
}
/**
* Enable this feature flag by setting the appropriate compile define.
*
* @param project The project to modify.
* @param andInverse Also disable the feature flag's inverse.
*/
public function enable(project:Project, andInverse:Bool = true):Void {
project.setHaxedef(this, "");
if (andInverse) getInverse().disable(project, false);
}
/**
* Disable this feature flag by removing the appropriate compile define.
*
* @param project The project to modify.
* @param andInverse Also enable the feature flag's inverse.
*/
public function disable(project:Project, andInverse:Bool = true):Void {
project.unsetHaxedef(this);
if (andInverse) getInverse().enable(project, false);
}
/**
* Query if this feature flag is enabled.
* @param project The project to query.
*/
public function isEnabled(project:Project):Bool {
// Check both Haxedefs and Defines for this flag.
return project.haxedefs.exists(this) || project.defines.exists(this);
}
/**
* Query if this feature flag's inverse is enabled.
*/
public function isDisabled(project:Project):Bool {
return getInverse().isEnabled(project);
}
/**
* Return the inverse of this feature flag.
* @return A new feature flag that is the inverse of this one.
*/
public function getInverse():FeatureFlag {
if (this.startsWith(INVERSE_PREFIX)) return this.substring(INVERSE_PREFIX.length);
return INVERSE_PREFIX + this;
}
}