-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDeveloperConsole.as
More file actions
1695 lines (1456 loc) · 44.7 KB
/
DeveloperConsole.as
File metadata and controls
1695 lines (1456 loc) · 44.7 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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// What : AS3 Developer Console - https://github.com/Torrunt/AS3-Developer-Console
// Author : Corey Zeke Womack (Torrunt)
// Contact : me@torrunt.net
// Website : torrunt.net
// Notes:
// - Type 'help' in console for help.
// - If you're testing your project in the Adobe Flash IDE make sure 'Disable Keyboard Shortcuts' is ticked.
// - When testing your project in an Adobe Flash IDE you have to click the swf at least once to be able to see the text cursor.
package
{
import flash.display.Sprite;
import flash.text.*;
import flash.events.KeyboardEvent;
import flash.events.TextEvent;
import flash.events.Event;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getTimer;
public class DeveloperConsole extends Sprite
{
// Customisation (to customise look go to the constructor)
/** Height (in pixels) of the developer console. */
public var consoleHeight:Number = 145;
/** The max amount of suggestions that can be shown in the suggestion drop-down. */
public var maxSuggestions:int = 14;
/** If true, The data/return types of variables and functions are shown in the suggestion drop-down. */
public var showTypes:Boolean = true; // show data/return types of vars and functions
/** If true, when assigning variables that don't exist, they will be created as a temporary variable (can't be a member var). */
public var createVarsThatDontExist:Boolean = true;
/** If true, when the console is opened/closed a sliding animation is played. */
public var slideAnimation:Boolean = true;
/** The speed of the sliding open/close animation, if slideAnimation is true. */
public var slideAnimation_speed:Number = 15;
/** If true, the tracer table is shown while tracing. */
public var tracerView:Boolean = true;
/** If true, as3's trace() function is called when using the tracer (IDE Output). */
public var tracerActualTrace:Boolean = false;
/** If true, as3's trace() function is called when the tracer is tracing the fps */
public var tracerActualTraceFPS:Boolean = false;
/**
* The layout of a trace when it is in the IDE Output.
* Default: "name : value"
*/
public var tracerActualTraceLayout:String = "name : value";
/** If true, everything traced by the tracer into the IDE Output is placed on a single line. */
public var tracerOneCiclePerLine:Boolean = true;
/**
* If tracerOneCiclePerLine is true, this string is placed inbetween each traced item.
* Default: " "
*/
public var tracerOneCiclePerLine_seperator:String = " ";
private const colour_type:String = "06C1FF";
private const colour_param_type:String = "B5B5B5";
private const VERSION_NAME:String = "Torrunt's AS3 Developer Console"
private const HELP:String = " - Type 'clear' to clear the console\n" +
" - Type 'author' to get info on the author of this console\n" +
" - Use Quotations when you want enter string literal with spaces (\"\")\n" +
" - Use Square Brackets when you want to use an arral literal (e.g:[0][1])\n" +
" - You can do multiple commands at once by seperating them with ';'s\n" +
" - You can also put x# after a ';' to do that command # many times\n" +
" - Calculations are allowed when assigning or in parameters (+,-,*,/,%). BIMDAS is not supported\n" +
" - Type 'trace:something' to start tracing something or 'stoptrace:something' to stop tracing it\n" +
" - You can also use 'trace:fps' to check your fps\n" +
" - Toggle Fullscreen console with the F7 key\n" +
" - Use the Up/Down arrow keys to go through your previous used commands or suggestions\n" +
" - Use PAGE UP/DOWN and HOME/END on your keyboard to scroll up and down";
private const AUTHOR:String = VERSION_NAME + " was programmed by Corey Zeke Womack (Torrunt)\nme@torrunt.net\nhttp://torrunt.net";
// Creating / Defaults
private var main:*;
private var opened:Boolean = false;
private var container:Sprite = new Sprite();
private var suggestText:TextField;
private var consoleTextFormat:TextFormat;
private var historyText:TextField;
private var inputText:TextField;
private var cmdSuggest:Array = new Array();
private var cmdhistory:Array = new Array();
private var cicle:Array;
private var hpos:int = -1;
private const consoleHeight_default:Number = consoleHeight;
// temp variables
private var tempVarNames:Array = new Array();
private var tempVars:Array = new Array();
// slide animation
private var slideAnimation_animating:Boolean = false;
private var slideAnimation_target:Number = 0;
// tracer
private var tracer:TextField;
private var tracerNames:TextField;
private var traceVars:Array = new Array();
private var tracerAlignX:Number;
private var tracerAlignY:Number;
// fps counter
public var fps:String;
private var last:uint = getTimer();
private var ticks:uint = 0;
// Constructor
public function DeveloperConsole(_main:*)
{
main = _main; // Start off point for seeing/using variables and functions (main class or frame/stage)
addChild(container);
// Customise Look
// Text Format
consoleTextFormat = new TextFormat();
consoleTextFormat.size = 14;
consoleTextFormat.font = "Courier New";
consoleTextFormat.color = 0xFFFFFF;
// History Textbox
historyText = new TextField();
container.addChild(historyText);
historyText.width = main.stage.stageWidth;
historyText.height = consoleHeight - 20;
historyText.alpha = 0.85;
historyText.selectable = false;
historyText.multiline = true;
historyText.wordWrap = true;
historyText.defaultTextFormat = consoleTextFormat;
historyText.background = true;
historyText.backgroundColor = 0x000000;
// Input Textbox
inputText = new TextField();
inputText.type = TextFieldType.INPUT;
container.addChild(inputText);
inputText.width = main.stage.stageWidth;
inputText.height = 21;
inputText.y = historyText.height;
inputText.x = 0;
inputText.alpha = 0.85;
inputText.defaultTextFormat = consoleTextFormat;
inputText.background = true;
inputText.backgroundColor = 0x4B4B4B;
// Suggest/auto-complete Textbox
suggestText = new TextField();
container.addChild(suggestText);
suggestText.width = 150;
suggestText.height = 20;
suggestText.y = inputText.y + inputText.height;
suggestText.alpha = 0.85;
suggestText.selectable = false;
suggestText.defaultTextFormat = consoleTextFormat;
suggestText.background = true;
suggestText.backgroundColor = 0x000000;
suggestText.autoSize = TextFieldAutoSize.LEFT;
suggestText.visible = false;
suggestText.multiline = true;
// Tracer
tracerAlignX = main.stage.stageWidth - 5;
tracerAlignY = 5;
consoleTextFormat.bold = true;
tracer = new TextField();
addChild(tracer);
tracer.alpha = 0.75;
tracer.selectable = false;
consoleTextFormat.align = "right";
tracer.defaultTextFormat = consoleTextFormat;
tracer.background = true;
tracer.backgroundColor = 0x666666;
tracer.autoSize = TextFieldAutoSize.LEFT;
tracer.visible = false;
tracerNames = new TextField();
addChild(tracerNames);
tracerNames.alpha = 0.75;
tracerNames.selectable = false;
consoleTextFormat.align = "left";
tracerNames.defaultTextFormat = consoleTextFormat;
tracerNames.background = true;
tracerNames.backgroundColor = 0x666666;
tracerNames.autoSize = TextFieldAutoSize.LEFT;
tracerNames.visible = false;
// Startup Message
echo(VERSION_NAME, "-", "torrunt.net");
// Animation start position
if (slideAnimation)
container.y = -historyText.height - inputText.height;
// Hide self
container.visible = false;
}
//////////////////////////
// Open/Close //
/////////////////////////
public function open():void
{
if (opened)
return;
container.visible = true;
opened = true;
main.stage.focus = inputText;
main.stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
main.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
inputText.addEventListener(TextEvent.TEXT_INPUT, onTextInput);
// animation
if (slideAnimation)
startSlideAnimation(true);
}
public function close():void
{
if (!opened)
return;
container.visible = false;
opened = false;
inputText.text = "";
hpos = -1;
main.stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
main.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
inputText.removeEventListener(TextEvent.TEXT_INPUT, onTextInput);
// animation
if (slideAnimation)
startSlideAnimation(false);
}
/** Open/Close the developer console. */
public function toggle():void
{
if (opened)
close();
else
open();
}
public function isOpen():Boolean { return opened; }
//////////////////////////
// Controls //
/////////////////////////
private var pressedUp:Boolean = false;
private function keyDown(e:KeyboardEvent):void
{
// Enter
if (e.keyCode == 13 && inputText.text != "")
{
echo(inputText.text, "#999999");
if (cmdhistory[cmdhistory.length - 1] != inputText.text)
{
cmdhistory.push(inputText.text);
hpos = -1;
}
eval(inputText.text);
inputText.text = "";
hideSuggestions();
}
// Backspace
if (e.keyCode == 8)
{
if (inputText.length-1 <= 0)
hideSuggestions();
else
showSuggestions(inputText.text.substr(0,inputText.length-1));
}
// Up and Down
// Pick array to go through
if (suggestText.visible)
cicle = cmdSuggest;
else
cicle = cmdhistory;
// Up
if (e.keyCode == 38 && cicle[cicle.length-1-(hpos+1)] != null)
{
hpos++;
changeInputbox();
pressedUp = true;
}
// Down
if (e.keyCode == 40)
{
if (cicle[cicle.length-1-(hpos-1)] != null)
{
hpos--;
changeInputbox();
}
else if (cicle == cmdhistory)
{
inputText.text = "";
hpos = -1;
}
}
// Scrolling Up and Down
// Page Up
if (e.keyCode == 33)
historyText.scrollV--;
// Page Down
if (e.keyCode == 34)
historyText.scrollV++;
// Home
if (e.keyCode == 36)
historyText.scrollV = 0;
// End
if (e.keyCode == 35)
historyText.scrollV = historyText.maxScrollV;
// Toggle Fullscreen (F7)
if (e.keyCode == 118)
toggleFullscreen();
}
// Fix cursor position if you press up when moving through history or suggestions
private function keyUp(e:KeyboardEvent):void
{
if (pressedUp)
{
inputText.setSelection(inputText.length,inputText.length);
pressedUp = false;
}
}
// Change inputbox contents to the next/previous history or suggestion item
private function changeInputbox():void
{
// If going through suggestions: just replace the thing you're currently writing
if (cicle == cmdSuggest)
{
// if last suggestion added a '();' at the end - remove it
if (inputText.text.lastIndexOf("();")+3 == inputText.length)
inputText.text = inputText.text.substr(0,inputText.length-3);
// Remove text after last symbol or space
// get indexs of symbols
var symbols:Array = fillArrayWithIndexsOf(symbols,inputText.text,["."," ","(",",","-","+","/","*","%",";",":","["]);
// get highest index (last used symbol)
var ls:int = symbols[0]; // (last symbol)
for (var i:int = 1; i < symbols.length; i++)
{
if (symbols[i] > ls)
ls = symbols[i];
}
// if the last symbol is a bracket and is the last thing in the inputbox: make it the second last symbol
if (inputText.text.charAt(ls) == "(" && (ls == inputText.length-1))
{
inputText.text = inputText.text.substr(0,ls);
changeInputbox();
return;
}
// Remove what you're currently writing
inputText.text = inputText.text.substr(0,ls+1);
// Appened suggestion
inputText.appendText(cicle[cicle.length-1-hpos]);
}
else
{
// Otherwise replace the whole inputText
inputText.text = cicle[cicle.length-1-hpos]; // Replace text
}
inputText.setSelection(inputText.length,inputText.length);
}
//////////////////////////
// Suggesting //
/////////////////////////
// Defaulting
private var areSuggestions:Boolean = false;
private var hitMax:Boolean = false;
// Update while typing
private function onTextInput(e:TextEvent):void
{
if (e.text == "`" || inputText.text == "`")
inputText.text = inputText.text.slice(0, -1);
showSuggestions(inputText.text + e.text);
}
private function showSuggestions(str:String):void
{
// reset to defaults
hideSuggestions(); // hide previous
areSuggestions = false;
hitMax = false;
if (str.length == 0)
return;
// check for ';'s (end of commands)
if (str.indexOf(";") > 1)
str = str.slice(str.lastIndexOf(";")+1,str.length);
// change str to latest var user is writing
str = stringReplaceAll(str," ");
var symbols:Array = fillArrayWithIndexsOf(symbols,str,["(","=",",","-","+","/","*","%",":","["]);
// Don't look for '[' or '(' if last one was closed
if (characterCount(str,"]") == characterCount(str,"["))
symbols.pop();
if (characterCount(str, ")") == characterCount(str, "("))
symbols.shift();
var startFrom:int = symbols[0];
for (var s:int = 1; s < symbols.length; s++)
if (symbols[s] > startFrom) startFrom = symbols[s];
str = str.substring(startFrom+1,str.length);
// Get all public Vars and Functions
if (str != "")
{
try
{
// FIND ME
var ob:* = stringToVarWithCalculation(str, main, true);
var stre:String = str.substring(str.lastIndexOf(".")+1,str.length);
var description:XML = describeType(ob);
var type:String = "";
if (description.*.length() > 3) // if 3 (or less) that means ob doesn't exist
{
// Vars
for each (var v:XML in description.variable)
{
if (suggestText.numLines < maxSuggestions)
{
if(v.@name.indexOf(stre) == 0)
{
if (showTypes)
type = ":" + "<font color=\"#" + colour_type + "\">" + v.@type + "</font>";
cmdSuggest.push(v.@name);
suggestText.htmlText += v.@name + type + "<br>";
areSuggestions = true;
}
}
else
{
hitMax = true;
break;
}
}
// Accessors
for each (var a:XML in description.accessor)
{
if (suggestText.numLines < maxSuggestions)
{
if(a.@name.indexOf(stre) == 0)
{
if (showTypes)
type = ":" + "<font color=\"#" + colour_type + "\">" + a.@type + "</font>";
cmdSuggest.push(a.@name);
suggestText.htmlText += a.@name + type + " <font color=\"#818181\">(accessor)</font><br>";
areSuggestions = true;
}
}
else
{
hitMax = true;
break;
}
}
// Methods / Functions
for each (var m:XML in description.method)
{
if (suggestText.numLines < maxSuggestions)
{
if(m.@name.indexOf(stre) == 0)
{
var text:String;
if (showTypes)
type = ":" + "<font color=\"#" + colour_type + "\">" + m.@returnType + "</font>";
text = m.@name + "(";
areSuggestions = true;
// Parameters
if (m.parameter != undefined)
{
var first:Boolean = true;
for each (var p:XML in m.parameter)
{
if (!first)
text += ", ";
else
first = false;
text += "<font color=\"#" + colour_param_type + "\">" + p.@type + "</font>";
}
cmdSuggest.push(m.@name+"(");
}
else
{
cmdSuggest.push(m.@name+"();");
}
suggestText.htmlText += text + ")" + type + "<br>";
}
}
else
{
hitMax = true;
break;
}
}
}
// If there are suggestions
if (areSuggestions)
{
// if there were more then what can be displayed; add a last item called "..."
if (hitMax)
suggestText.htmlText += "...";
// Position (show above inputTest if console is fullscreen)
if (consoleHeight == main.stage.stageHeight)
suggestText.y = inputText.y - suggestText.height;
else
suggestText.y = inputText.y + inputText.height;
suggestText.visible = true;
hpos = cmdSuggest.length;
}
}
catch (er:Error)
{
// do nothing
}
}
}
private function hideSuggestions():void
{
suggestText.visible = false;
suggestText.htmlText = "";
suggestText.height = 20;
cmdSuggest = new Array();
hpos = -1;
}
//////////////////////////
// Interpreting //
/////////////////////////
/** Seperate commands (by ;) and evaluates them. */
public function eval(str:String):void
{
if (str.indexOf(";") > 1)
{
var c:Array = str.split(";");
for (var i:int = 0; i < c.length; i++)
{
// if there's an x then look for x# and repeat for that #
if (c[i].indexOf("x") == 0)
{
c[i] = Number(c[i].slice(1,c[i].length));
for (var r:int = 1; r < c[i]; r++)
interpretString(c[i-1]);
}
else if (c[i] != "")
{
interpretString(c[i]);
}
}
}
else
{
interpretString(str);
}
}
// Interpret String/Command
private function interpretString(str:String):void
{
// Remove ';'s and spaces outside of quotes
str = stringReplaceAll(str, ";");
str = stringReplaceButExclude(str, " ", ["\""], "", [false]);
if (str.indexOf("=") > 0)
{
// Assigning
var ar:Array = str.split("=");
ar = checkShorthandCalculations(ar);
ar[1] = stringToVarWithCalculation(ar[1]);
changeVar(ar[0], ar[1]);
}
else
{
// Console-only Commands and getVar
if (str.indexOf("trace:") == 0)
setTrace(str);
else if (str.indexOf("stoptrace:") == 0)
stopTrace(str);
else
{
switch (str)
{
case "clear" : historyText.text = ""; break;
case "help" : echo(HELP, "#0099CC"); break;
case "author" : echo(AUTHOR, "#0099CC"); break;
default : getVar(str); break;
}
}
}
}
// Echo var value / calculation
private function getVar(varname:String):void
{
var rstring:String = varname + " returned ";
try
{
var value:* = stringToVarWithCalculation(varname);
if (value == undefined)
return;
rstring += value;
echo(rstring);
}
catch (er:Error)
{
error(er.message);
}
}
// Assign a value to a variable
private function changeVar(varname:String, vset:*):void
{
try
{
// check if vset is an array
try
{
if (vset is String)
vset = stringToArray(vset);
}
catch (er:Error)
{
// do nothing
}
// check if vset is a boolean
if (vset == "true")
vset = true;
else if (vset == "false")
vset = false;
// check if vset is Number
if (!isNaN(vset))
vset = Number(vset);
// Assign
// change dots but leave dots inside array literals
var str:String = stringReplaceButExclude(varname,".",["[","]","(",")"],"`",[false,false,false,false]);
var v:Array = str.split("`");
if (!assignVar(varname, vset, v, main))
throw new Error();
}
catch (er:Error)
{
// Variable Does not exist / Invalid Type
var index:int = tempVarNames.indexOf(v[0]);
if (index == -1 && stringToVar(varname) != varname)
{
// Invalid Type
error("Invalid type");
}
else if (createVarsThatDontExist && v.length == 1)
{
// Create Temporary Variable
tempVarNames.push(v[0]);
tempVars.push(vset);
warn("Temporary variable called \"" + v[0] + "\" created with the value " + vset);
}
else
error(er.message);
}
}
private function assignVar(varname:String, vset:*, v:Array, ob:*, skipFirstIndex:Boolean = false):Boolean
{
try
{
var tempAry:Array; // for array items if needed
for (var i:int = skipFirstIndex ? 1 : 0; i < v.length-1; i++)
{
if (v[i].indexOf("[") > -1)
{
// if an Array Item
tempAry = stringToArrayItem(v[i]);
switch (tempAry.length)
{
case 4: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]][tempAry[3]]; break;
case 3: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]]; break;
default: ob = ob[tempAry[0]][tempAry[1]]; break;
}
}
else
ob = ob[v[i]];
}
if (v[v.length-1].indexOf("[") == -1)
{
ob[v[v.length-1]] = vset;
echo(varname + " is now " + ob[v[v.length-1]]);
}
else
{
// if an Array Item
tempAry = stringToArrayItem(v[v.length - 1]);
switch (tempAry.length)
{
case 4: ob[tempAry[0]][tempAry[1]][tempAry[2]][tempAry[3]] = vset; break;
case 3: ob[tempAry[0]][tempAry[1]][tempAry[2]] = vset; break;
default: ob[tempAry[0]][tempAry[1]] = vset; break;
}
}
return true;
}
catch (e:Error)
{
// failed? is it a class?
var cl:* = v[0];
for (i = 0; i < v.length; i++)
{
try
{
ob = getDefinitionByName(cl) as Class;
break; // break out if it get's this far (if it's a class)
}
catch (e:Error)
{
cl = cl + "." + v[i+1];
}
}
if (ob is Class)
{
// member of class?
for (i++; i < v.length; i++)
{
ob[v[v.length-1]] = vset;
echo(varname + " is now " + ob[v[v.length-1]]);
}
}
else
{
// temporary variable?
var index:int = tempVarNames.indexOf( v[0].indexOf("[") == -1 ? v[0] : v[0].substring(0, v[0].indexOf("[")) );
if (index != -1)
{
if (v.length > 1 || v[0].indexOf("[") != -1)
{
ob = tempVars[index];
for (i = (v[0].indexOf("[") != -1 ? 0 : 1); i < v.length; i++)
{
if (v[i].indexOf("[") != -1)
{
// if an Array Item
tempAry = stringToArrayItem(v[i]);
if (i == 0)
tempAry.shift();
switch (tempAry.length)
{
case 4: ob[tempAry[0]][tempAry[1]][tempAry[2]][tempAry[3]] = vset; break;
case 3: ob[tempAry[0]][tempAry[1]][tempAry[2]] = vset; break;
case 2: ob[tempAry[0]][tempAry[1]] = vset; break;
default: ob[tempAry[0]] = vset; break;
}
}
else
{
ob[v[i]] = vset;
echo(varname + " is now " + ob[v[i]]);
}
}
}
else
{
tempVars[index] = vset;
echo(varname + " is now " + tempVars[index]);
}
return true;
}
else
return false;
}
return v.length > 1;
}
return false;
}
// Covert a string to useable variable
private function stringToVar(str:String, base:* = null, leaveOutLast:Boolean = false):*
{
if (base == null)
base = main;
var ob:* = str;
var lo:int = 0;
if (leaveOutLast)
lo = 1;
if (str.indexOf("\"") == -1 && isNaN(Number(str)))
{
// change dots but leave dots inside array literals
var splitString:String = stringReplaceButExclude(str, ".", ["[","]","(",")"], "`", [false,false,false,false]);
var v:Array = splitString.split("`");
try
{
ob = base;
for (var i:int = 0; i < v.length-lo; i++)
{
if (v[i].indexOf("[") != -1)
{
// if an Array Item
var tempAry:Array = stringToArrayItem(v[i]);
switch (tempAry.length)
{
case 4: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]][tempAry[3]]; break;
case 3: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]]; break;
default: ob = ob[tempAry[0]][tempAry[1]]; break;
}
}
else
ob = ob[v[i]];
}
if (ob == undefined)
throw new Error();
}
catch (e:Error)
{
// failed? is it a class?
var cl:* = v[0];
for (i = 0; i < v.length; i++)
{
try
{
ob = getDefinitionByName(cl) as Class;
break; // break out if it get's this far (if it's a class)
}
catch (e:Error)
{
cl = cl + "." + v[i+1];
}
}
if (ob is Class)
{
// member of class?
for (i++; i < v.length-lo; i++)
{
if (v[i].indexOf("[") != -1)
{
// if an Array Item
tempAry = stringToArrayItem(v[i]);
switch (tempAry.length)
{
case 4: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]][tempAry[3]]; break;
case 3: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]]; break;
default: ob = ob[tempAry[0]][tempAry[1]]; break;
}
}
else
ob = ob[v[i]];
}
}
else
{
// temporary variable?
var index:int = tempVarNames.indexOf( v[0].indexOf("[") == -1 ? v[0] : v[0].substring(0, v[0].indexOf("[")) );
if (index != -1)
{
ob = tempVars[index];
for (i = (v[0].indexOf("[") != -1 ? 0 : 1); i < v.length; i++)
{
if (v[i].indexOf("[") != -1)
{
// if an Array Item
tempAry = stringToArrayItem(v[i]);
if (i == 0)
tempAry.shift();
switch (tempAry.length)
{
case 4: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]][tempAry[3]]; break;
case 3: ob = ob[tempAry[0]][tempAry[1]][tempAry[2]]; break;
case 2: ob = ob[tempAry[0]][tempAry[1]]; break;
default: ob = ob[tempAry[0]]; break;
}
}
else
ob = ob[v[i]];
}
}
else
ob = str;
}
}
}
return ob;
}
// Convert a string to a callable function
private function stringToFunc(str:String, base:* = null, leaveOutLast:Boolean = false):*
{
var member:String = str.substring(str.indexOf(")") + 2);
str = str.substring(0, str.indexOf(")"));
var fn:String = str.substring(0, str.indexOf("("));
var p:String = str.substring(str.indexOf("(") + 1);
// get parameters
var pars:Array;
if (p == "")
pars = new Array();
else
pars = stringToPars(p);
if (member == "")
return stringToVar(fn, base, leaveOutLast).apply(null,pars);
else
return stringToVarWithCalculation(member, stringToVar(fn).apply(null, pars), leaveOutLast);
}
// Convert a string to a new instance
private function stringToNewInstance(str:String, allowError:Boolean = true):*