@@ -3,6 +3,110 @@ package archipelago;
33import haxe .format .JsonParser ;
44import haxe .ds .StringMap ;
55import haxe .ds .Map ;
6+
7+ abstract APOption (String ) {
8+ public inline function new (v : String ) {
9+ this = v ;
10+ }
11+
12+ // Auto-conversion from various types
13+ @:from
14+ public static inline function fromString (value : String ): APOption {
15+ return new APOption (value );
16+ }
17+
18+ @:from
19+ public static inline function fromBool (value : Bool ): APOption {
20+ return new APOption (value ? " true" : " false" );
21+ }
22+
23+ @:from
24+ public static inline function fromFloat (value : Float ): APOption {
25+ return new APOption (Std .string (value ));
26+ }
27+
28+ @:from
29+ public static inline function fromInt (value : Int ): APOption {
30+ return new APOption (Std .string (value ));
31+ }
32+
33+ @:from
34+ public static inline function fromArray (value : Array <String >): APOption {
35+ return new APOption (" [" + value .join (" , " ) + " ]" );
36+ }
37+
38+ // Auto-conversion to various types
39+ @:to
40+ public inline function toString (): String {
41+ return this ;
42+ }
43+
44+ @:to
45+ public function toBool (): Bool {
46+ return this == " true" ;
47+ }
48+
49+ @:to
50+ public function toFloat (): Float {
51+ var parsed = Std .parseFloat (this );
52+ return Math .isNaN (parsed ) ? 0.0 : parsed ;
53+ }
54+
55+ @:to
56+ public function toInt (): Int {
57+ return Std .int (toFloat ());
58+ }
59+
60+ @:to
61+ public function toArray (): Array <String > {
62+ if (this .startsWith (" [" ) && this .endsWith (" ]" )) {
63+ var content = this .substr (1 , this .length - 2 );
64+ return content .split (" ," ).map (function (item ) return item .trim ());
65+ }
66+ return [this ];
67+ }
68+
69+ // Smart parsing function that attempts to determine and convert to the appropriate type
70+ public function parseValue (): Dynamic {
71+ // Check for array format
72+ if (this .startsWith (" [" ) && this .endsWith (" ]" )) {
73+ return toArray ();
74+ }
75+
76+ // Check for boolean
77+ if (this == " true" || this == " false" ) {
78+ return toBool ();
79+ }
80+
81+ // Check for numeric (float/int)
82+ var floatValue = Std .parseFloat (this );
83+ if (! Math .isNaN (floatValue )) {
84+ // Check if it's an integer
85+ if (floatValue == Std .int (floatValue )) {
86+ return toInt ();
87+ }
88+ return toFloat ();
89+ }
90+
91+ // Default to string
92+ return toString ();
93+ }
94+
95+ // Check if the value represents a specific type
96+ public function isArray (): Bool {
97+ return this .startsWith (" [" ) && this .endsWith (" ]" );
98+ }
99+
100+ public function isBool (): Bool {
101+ return this == " true" || this == " false" ;
102+ }
103+
104+ public function isNumeric (): Bool {
105+ return ! Math .isNaN (Std .parseFloat (this ));
106+ }
107+ }
108+
109+
6110class APYaml {
7111 public var game : String ;
8112 public var name : String ;
@@ -44,22 +148,13 @@ class APYaml {
44148 var value = keyValue [1 ].trim ();
45149
46150 if (key == " game" )
47- this .game = value ;
151+ this .game = new APOption ( value ) ;
48152 else if (key == " name" )
49- this .name = value ;
153+ this .name = new APOtion ( value ) ;
50154 else if (key == " description" )
51- this .description = value ;
52-
53- if (value .startsWith (" [" ) && value .endsWith (" ]" )) {
54- value = value .substr (1 , value .length - 2 );
55- sectionData .set (key , value .split (" ," ).map (function (item ) return item .trim ()));
56- } else if (value == " true" || value == " false" ) {
57- sectionData .set (key , value == " true" );
58- } else if (cast (Std .parseFloat (value ), Null <Float >) != null || Math .isNaN (Std .parseFloat (value ))) {
59- sectionData .set (key , Std .parseFloat (value ));
60- } else {
61- sectionData .set (key , value );
62- }
155+ this .description = new APOption (value );
156+
157+ sectionData .set (key , new APOption (value ));
63158 }
64159 }
65160 }
0 commit comments