From 98ef41dc890bb869c133338148625faed76bc098 Mon Sep 17 00:00:00 2001 From: Hugh Sanderson Date: Mon, 25 Mar 2024 12:35:41 +0800 Subject: [PATCH] Allow bootType to be specified to avoid macro in build system and error log --- templates/haxe/ApplicationBoot.hx | 5 +++ templates/haxe/ApplicationDocument.hx | 4 +++ templates/haxe/ApplicationMain.hx | 42 +++++++++++++++++++----- tools/nme/src/project/ApplicationData.hx | 4 +++ tools/nme/src/project/BootType.hx | 7 ++++ tools/nme/src/project/NMEProject.hx | 8 ++++- tools/nme/src/project/NMMLParser.hx | 10 ++++++ 7 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 tools/nme/src/project/BootType.hx diff --git a/templates/haxe/ApplicationBoot.hx b/templates/haxe/ApplicationBoot.hx index c6b47270a..fecf5d32b 100644 --- a/templates/haxe/ApplicationBoot.hx +++ b/templates/haxe/ApplicationBoot.hx @@ -1,3 +1,7 @@ +::if (APP_BOOT_TYPE!="BootTypeAuto"):: +#error "ApplicationBoot should not be called unless ApplicationBoot==BootTypeAuto" +::else:: + import haxe.macro.Expr; import haxe.macro.Context; @@ -50,3 +54,4 @@ class ApplicationBoot return Context.parse("{}", p); } } +::end:: diff --git a/templates/haxe/ApplicationDocument.hx b/templates/haxe/ApplicationDocument.hx index aafb00c2a..7f8961ee4 100644 --- a/templates/haxe/ApplicationDocument.hx +++ b/templates/haxe/ApplicationDocument.hx @@ -13,7 +13,11 @@ class ApplicationDocument extends ::APP_MAIN:: } #end + ::if (APP_BOOT_TYPE=="BootTypeAuto"):: ApplicationBoot.callSuper(); + ::else:: + super(); + ::end:: #if nme if (added!=null && added.stage!=null) diff --git a/templates/haxe/ApplicationMain.hx b/templates/haxe/ApplicationMain.hx index 3fe570282..f4df35a0a 100644 --- a/templates/haxe/ApplicationMain.hx +++ b/templates/haxe/ApplicationMain.hx @@ -51,6 +51,20 @@ class ApplicationMain public static var winBackground:Int = ::WIN_BACKGROUND::; public static var onLoaded:Void->Void; + + static function createInstance() + { + ::if (APP_BOOT_TYPE=="BootTypeMain"):: + ::APP_MAIN::.main(); + ::elseif (APP_BOOT_TYPE=="BootTypeNew"):: + new ::APP_MAIN::(); + ::elseif (APP_BOOT_TYPE=="BootTypeAddStage"):: + new ApplicationDocument(); + ::else:: + ApplicationBoot.createInstance(); + ::end:: + } + public static function main() { #if cpp @@ -111,7 +125,13 @@ class ApplicationMain flash.Lib.current.stage.align = flash.display.StageAlign.TOP_LEFT; flash.Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE; - var load = function() ApplicationBoot.createInstance(); + function load() { + ::if (APP_BOOT_TYPE=="BootTypeAuto"):: + ApplicationBoot.createInstance(); + ::else:: + new ApplicationDocument(); + ::end:: + } ::if (PRELOADER_NAME!=null):: onLoaded = load; @@ -129,9 +149,15 @@ class ApplicationMain nme.display.ManagedStage.initSdlAudio(); #end + ::if (APP_BOOT_TYPE=="BootTypeMain"):: + ::APP_MAIN::.main(); + ::else:: + + ::if (APP_BOOT_TYPE=="BootTypeAuto"):: if (ApplicationBoot.canCallMain()) ApplicationBoot.createInstance(); else + ::end:: { wx.App.boot(function() { @@ -163,11 +189,11 @@ class ApplicationMain // Show frame before creating instance so context is good. frame.shown = true; - ApplicationBoot.createInstance(); + createInstance(); wx.App.setTopWindow(frame); #else - ApplicationBoot.createInstance(); + createInstance(); if (autoShowFrame) { wx.App.setTopWindow(frame); @@ -177,9 +203,10 @@ class ApplicationMain }); } + ::end:: #elseif cppia // Cppia - ApplicationBoot.createInstance(); + createInstance(); #elseif nme var flags:Int = (::WIN_HARDWARE:: ? nme.app.Application.HARDWARE : 0) | @@ -222,7 +249,7 @@ class ApplicationMain nme.Lib.current.stage.align = nme.display.StageAlign.::STAGE_ALIGN::; nme.Lib.current.stage.scaleMode = nme.display.StageScaleMode.::STAGE_SCALE::; nme.Lib.current.loaderInfo = nme.display.LoaderInfo.create (null); - ApplicationBoot.createInstance(); + createInstance(); }, ::WIN_WIDTH::, ::WIN_HEIGHT::, ::WIN_FPS::, @@ -237,10 +264,7 @@ class ApplicationMain #end #else // Unknown framework - if (ApplicationBoot.canCallMain()) - ApplicationBoot.createInstance(); - else - ApplicationBoot.createInstance(); + createInstance(); #end } diff --git a/tools/nme/src/project/ApplicationData.hx b/tools/nme/src/project/ApplicationData.hx index f501d44b4..5fbf07f28 100644 --- a/tools/nme/src/project/ApplicationData.hx +++ b/tools/nme/src/project/ApplicationData.hx @@ -1,3 +1,5 @@ +import BootType; + class ApplicationData { @@ -7,6 +9,7 @@ class ApplicationData // Haxe main class public var main:String; public var preloader:String; + public var bootType = BootTypeAuto; // Build directory base public var binDir:String; @@ -29,6 +32,7 @@ class ApplicationData public var swfVersion:Float; + public function new() { file = "MyApplication"; diff --git a/tools/nme/src/project/BootType.hx b/tools/nme/src/project/BootType.hx new file mode 100644 index 000000000..5d2908b24 --- /dev/null +++ b/tools/nme/src/project/BootType.hx @@ -0,0 +1,7 @@ +enum BootType +{ + BootTypeAuto; + BootTypeMain; + BootTypeNew; + BootTypeAddStage; +} diff --git a/tools/nme/src/project/NMEProject.hx b/tools/nme/src/project/NMEProject.hx index 684e7d4f0..a790f0e20 100644 --- a/tools/nme/src/project/NMEProject.hx +++ b/tools/nme/src/project/NMEProject.hx @@ -877,7 +877,13 @@ class NMEProject for(field in Reflect.fields(app)) - Reflect.setField(context, "APP_" + StringHelper.formatUppercaseVariable(field), Reflect.field(app, field)); + { + var val:Dynamic = Reflect.field(app, field); + if (!Std.isOfType(val,Float)) + val = "" + val; + Log.verbose(" APP_" + StringHelper.formatUppercaseVariable(field) + "=" + val); + Reflect.setField(context, "APP_" + StringHelper.formatUppercaseVariable(field), val); + } context.APP_PACKAGE = app.packageName; diff --git a/tools/nme/src/project/NMMLParser.hx b/tools/nme/src/project/NMMLParser.hx index c6824ec38..29744f563 100644 --- a/tools/nme/src/project/NMMLParser.hx +++ b/tools/nme/src/project/NMMLParser.hx @@ -15,6 +15,7 @@ import NMEProject; import IconType; import platforms.Platform; import nme.AlphaMode; +import BootType; using StringTools; @@ -228,6 +229,15 @@ class NMMLParser case "preloader": project.app.preloader = substitute(element.att.preloader); + case "bootType": + var types = ["auto", "main", "new", "addStage" ]; + var type = substitute(element.att.bootType); + var typeId = types.indexOf(type); + if (typeId<0) + Log.error('Invalid bootType "$type". Should be one of $types.'); + project.app.bootType = Type.createEnumIndex( BootType, typeId ); + + case "package", "packageName", "package-name": project.app.packageName = substitute(element.att.resolve(attribute));