Advertisement
Guest User

Untitled

a guest
Mar 5th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.15 KB | None | 0 0
  1. package ftk;
  2.  
  3. import haxe.macro.Context;
  4. import haxe.macro.Expr;
  5.  
  6. /**
  7.  * ...
  8.  * @author filt3rek
  9.  */
  10.  
  11. class Texts {
  12.  
  13. #if !macro
  14.  
  15.     public static var list      = new DotAccess();
  16.     public static var sep       = "|";
  17.     public static var plural    = "s";
  18.    
  19.     public static function set( lang = "fr" ) : Void {
  20.         var s       = haxe.Resource.getString( MyList.__res );
  21.         var fast    = new haxe.xml.Access( Xml.parse( s ).firstElement() );
  22.         for ( n in fast.nodes.s ) {
  23.             var str : String = switch ( lang ) {
  24.                 case "fr"   : n.node.fr.innerData;
  25.                 case _      : null;
  26.             }
  27.            
  28.             var ind = str.indexOf( sep );
  29.             if ( ind != -1 ){
  30.                 list.set( n.att.id, str.substr( 0, ind ) );
  31.                 list.set( n.att.id + "_s", str.substr( ind + 1 ) );
  32.             }else{
  33.                 list.set( n.att.id, str );
  34.                 list.set( n.att.id + "_s", str + plural );
  35.             }
  36.         }
  37.     }
  38.  
  39. #end
  40.  
  41.     /*
  42.      * Checks if n > 1 add "_s" to the word's name ( Texts.list.)
  43.      */
  44.     public static macro function s( expr : Expr, i : haxe.macro.Expr.ExprOf<Float> ){
  45.         var texpr   = Context.typeExpr( expr );
  46.         switch( texpr.expr ){
  47.             case TCall( callee, args )  :
  48.                 var stcallee    = Context.storeTypedExpr( callee );
  49.                 var stargs  = [ for( arg in args ) macro cast ${ Context.storeTypedExpr( arg ) } ];
  50.                 var lastArg = stargs[ stargs.length - 1 ];
  51.                 stargs[ stargs.length - 1 ] = macro if ( $i > 1 ) $lastArg + "_s" else $lastArg;
  52.                 var ret = macro @:pos( expr.pos ) $stcallee( $a{ stargs } );
  53.                 return ret;
  54.             default :   Context.fatalError( "Unsupported expression", expr.pos );
  55.         }
  56.         return macro $expr;
  57.     }
  58. }
  59.  
  60. #if !macro
  61.  
  62. #if display
  63. @:forward
  64. #end
  65. private abstract DotAccess(MyList) from MyList to MyList {
  66.    public inline function new() {
  67.         this = new MyList();
  68.     }
  69.  
  70.     @:op(a.b) function get(field:String):String {
  71.         return this.get( field );
  72.     }
  73.  
  74.     public function set(field:String,val : String):String {
  75.         this.set( field, val );
  76.         return val;
  77.     }
  78. }
  79.  
  80.  
  81. @:build( ftk.Texts.BuildXmlProxy.build() )
  82. private class MyList extends haxe.ds.StringMap<String>{}
  83.  
  84. #else
  85.  
  86. class BuildXmlProxy{
  87.     public static function build(){
  88.         var fields  = Context.getBuildFields();
  89.         var spath   = Context.definedValue( "xmlProxy" );
  90.         if( spath == null ){
  91.             Context.fatalError( "Please define xmlProxy (-D xmlProxy=my/path.xml)", Context.currentPos() );
  92.         }
  93.         var path    = new haxe.io.Path( spath );
  94.         fields.push({
  95.             name    : "__res",
  96.             access  : [ Access.APublic, Access.AStatic ],
  97.             kind    : FieldType.FVar( macro:String, macro $v{ path.file } ),
  98.             pos     : Context.currentPos(),
  99.         });
  100.  
  101.         var s       = sys.io.File.getContent( spath );
  102.         Context.addResource( path.file, haxe.io.Bytes.ofString( s ) );
  103.        
  104.         var fast    = new haxe.xml.Access( Xml.parse( s ).firstElement() );
  105.         for ( n in fast.nodes.s ) {
  106.         fields.push({
  107.                 name    : n.att.id,
  108.                 access  : [ Access.APublic ],
  109.                 kind    : FieldType.FVar( macro:String ),
  110.                 pos     : Context.currentPos(),
  111.             });
  112.         }
  113.         return fields;
  114.     }
  115. }
  116.  
  117. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement