// Decompiled by AS3 Sorcerer 6.78
// www.buraks.com/as3sorcerer

//GameLoader

package 
{
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.display.StageAlign;
    import flash.events.KeyboardEvent;
    import flash.desktop.NativeApplication;
    import flash.events.InvokeEvent;
    import flash.display.StageDisplayState;
    import flash.ui.Keyboard;
    import flash.geom.Rectangle;
	import flash.display.LoaderInfo;
    import flash.display.Screen;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.display.StageQuality;
    import flash.system.LoaderContext;
    import flash.system.ApplicationDomain;
    import flash.events.IOErrorEvent;
    import flash.net.URLVariables;
    import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.errors.IOError;

    public class GameLoader extends Sprite 
    {

        private var loader:Loader;
        private var locale:String;
        public var log:TextField;
        private var fullScreen:Boolean;

        public function GameLoader()
        {
            if (stage)
            {
                this.init();
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, this.init);
            };
        }

        private function init(e:Event=null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, this.init);
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;
            stage.nativeWindow.maximize();
            stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKey);
            this.setCenterPosition();
            this.log = new TextField();
            this.log.x = 15;
            this.log.y = 15;
            this.log.width = 520;
            this.log.height = 370;
            this.log.background = true;
            this.logEvent("aaa");
            try
            {
                NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, this.onInvoke);
            }
            catch(e:Error)
            {
                logEvent(e.getStackTrace());
                throw (e);
            };
        }

        private function onKey(e:KeyboardEvent):void
        {
            switch (e.keyCode)
            {
                case Keyboard.F11:
                    this.fullScreen = (!(this.fullScreen));
                    if (this.fullScreen)
                    {
                        stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
                    }
                    else
                    {
                        stage.displayState = StageDisplayState.NORMAL;
                    };
                    break;
            };
        }

        private function setCenterPosition():void
        {
            var appBounds:Rectangle = stage.nativeWindow.bounds;
            var screen:Screen = Screen.getScreensForRectangle(appBounds)[0];
            stage.nativeWindow.x = ((screen.bounds.width - stage.nativeWindow.width) / 2);
            stage.nativeWindow.y = ((screen.bounds.height - stage.nativeWindow.height) / 2);
        }
		
        private function readGameProperties():String
        {
            var file:File = File.applicationStorageDirectory.resolvePath("game.properties");
            var fileStream:FileStream = new FileStream();
            var gameProperties:String = "";

            try
            {
                fileStream.open(file, FileMode.READ);
                gameProperties = fileStream.readUTFBytes(fileStream.bytesAvailable);
                fileStream.close();

                // Разбить game.properties на строки и найти строку с параметром “locale”
                var lines:Array = gameProperties.split("\r\n"); // В Windows используется символ возврата каретки (\r\n)
                for each(var line:String in lines)
                {
                    if(line.indexOf("locale=") == 0)
                    {
                        return line.substr(7); // Возвращает значение после “locale=”
                    }
                }
            }
            catch(e:IOError)
            {
                trace("Error read file:", e);
            }

            return ""; // Если параметр “locale” не найден или возникла ошибка при чтении файла
        }

        private function onInvoke(event:*):void
        {
            if (event.arguments.length > 0)
            {
                this.logEvent(("Arguments: " + event.arguments.toString()));
                this.locale = event.arguments[0];
            };
            var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
            this.loader = new Loader();
          // this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onComplete);
            //this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onConnectError);
            var flashvars:URLVariables = new URLVariables();
            flashvars["locale"] = this.locale;
            flashvars["rnd"] = Math.random();
            var urlReq:URLRequest = new URLRequest("https://redagereborn.ru/library.swf");
            urlReq.data = flashvars;
			
			///
            var urlLoader:URLLoader = new URLLoader();
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.addEventListener(Event.COMPLETE, this.byteLoadComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.onConnectError);
            urlLoader.load(urlReq);
			//
			
			
          //this.loader.load(urlReq, context);
        }
		
		public function byteLoadComplete(event:Event):void
		{
            var bytes:ByteArray = (URLLoader(event.target).data as ByteArray);
            this.loader = new Loader();
            var loaderContext:LoaderContext = new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain));
			var loaderInfo:LoaderInfo = this.loader.contentLoaderInfo;
            loaderInfo.addEventListener(Event.COMPLETE, this.onComplete, false, 0, true);
            loaderContext.allowLoadBytesCodeExecution = true;
            this.loader.loadBytes(bytes, loaderContext);
		}
		
        private function onConnectError(event:IOErrorEvent):void
        {
            Alert.showMessage("Can not connect to server!");
            stage.nativeWindow.visible = true;
        }
		
        public function logEvent(entry:String):void
        {
            this.log.appendText((entry + "\n"));
            trace(entry);
        }

        private function onComplete(e:Event):void
        {
            this.loader.removeEventListener(Event.COMPLETE, this.onComplete);
            var mainClass:Class = Class(this.loader.contentLoaderInfo.applicationDomain.getDefinition("Game"));
            var obj:* = new (mainClass)();
            obj.SUPER(stage);
            addChild(obj);
        }


    }
}//package 

flashvars не работают