示例#1
0
        public void eval(string text)
        {
                        #if EVAL_ENABLED
            NitroCode script = new NitroCode(text, UI.BaseCodeType, UI.SecurityDomain);

            UICode codeInstance = script.Instance();
            codeInstance.BaseScript = script;

            if (codeInstance != null)
            {
                codeInstance.document = document;
                codeInstance.window   = window;
                codeInstance.OnWindowLoaded();
                codeInstance.Start();
                // Standard method that must be called.
                // Any code outside of functions gets dropped in here:
                codeInstance.OnScriptReady();
            }
                        #else
            Console.Warn("Eval currently disconnected in order to make sure people are aware of iOS and some platform restrictions (and also for security). Some platforms don't allow eval. Tick the enable eval box in Nitro settings.");
                        #endif
        }
示例#2
0
    /// <summary>Used internally - don't call this one. Startup the UI for use in the Editor with AOT Nitro.</summary>
    /// <param name="nitroAot">True if no gameobject should be generated.</param>
    public static void Start(bool nitroAot)
    {
        if (!Started)
        {
            Started = true;

            // Setup atlas stacks:
            AtlasStacks.Start();

            // Hookup the wrench logging method:
            Wrench.Log.OnLog += OnLogMessage;

            // Hookup the InfiniText logging method:
            InfiniText.Fonts.OnLog += OnLogMessage;

            // Startup the tag handlers:
            Wrench.TagHandlers.Setup();

            // Startup the file protocols (internally also starts up the CSS engine):
            FileProtocols.Setup();

            // Setup the text/language service:
            if (Variables == null)
            {
                Variables = new FullVariableSet();

                if (!nitroAot)
                {
                    // Sign up to the variable on change event - whenever a custom var is changed, we need to refresh the screen.
                    Variables.OnChange += OnVariableChange;
                    // Ensure that variables is set to whatever the default/current language is.
                    OnLanguageChange(Wrench.Text.Language);
                    // Sign on to the event that occurs when the language changes.
                    Wrench.Text.OnLanguageChanged += OnLanguageChange;
                    // Sign on to the event that occurs when the gender changes.
                    Wrench.Text.OnGenderChanged += ResolveAllVariables;
                }
            }

            // Setup the callback queue:
            Callbacks.Start();

            // Setup the character providers (for e.g. Emoji):
            CharacterProviders.Setup();

            Layer = LayerMask.NameToLayer("PowerUI");

            if (Layer < 0)
            {
                // Invalid layer.
                                #if UNITY_EDITOR
                // Create the new layer now (this will actually be a permanent change):
                Layer = PowerUI.LayerManager.Add();
                                #else
                throw new Exception("Error: PowerUI layer not found. Go to Edit->Project Settings->Tags and add a layer called PowerUI to fix this." +
                                    " Don't forget to make sure it doesn't render with your main camera too!"
                                    );
                                #endif
            }

            // Default FPS:
            SetRate(DefaultRate);

                        #if !NoNitroRuntime
            // Setup the compiler:
            NitroCode.Setup();
                        #endif
        }

        if (nitroAot)
        {
            return;
        }

        GUINode = GameObject.Find("#PowerUI");

        if (GUINode == null)
        {
            // Not started yet.

            // Create the UI game object:
            GUINode      = new GameObject();
            GUINode.name = "#PowerUI";

            // Create the camera:
            CameraNode      = new GameObject();
            CameraNode.name = "Camera";

            // Create the updater:
            GlobalUpdater = GUINode.AddComponent <StandardUpdater>();

            // Setup the camera:
            GUICamera = CameraNode.AddComponent <Camera>();
        }
        else
        {
            // Already started, but we might have updated.

            if (CameraNode == null)
            {
                // This can happen if the PowerUI assembly is actively reloaded (e.g. runtime updates).
                CameraNode      = GameObject.Find("#PowerUI/Camera");
                CameraTransform = CameraNode.transform;
                GUICamera       = CameraNode.GetComponent <Camera>();
            }
            else
            {
                // Already started!
                return;
            }
        }

        // Hide the PowerUI layer from all cameras other than GUICamera:
        Camera[] cameras = Camera.allCameras;

        int layerMask = ~(1 << UI.Layer);

        for (int i = 0; i < cameras.Length; i++)
        {
            // Grab the camera:
            Camera camera = cameras[i];

            // Is it the GUICamera?
            if (camera == GUICamera)
            {
                continue;
            }

            // Hide the UI layer from it:
            camera.cullingMask &= layerMask;
        }

        // Setup the transform:
        CameraTransform        = CameraNode.transform;
        CameraTransform.parent = GUINode.transform;

        GUICamera.nearClipPlane = 0.2f;
        GUICamera.depth         = CameraDepth;
        GUICamera.clearFlags    = CameraClearFlags.Depth;
        GUICamera.cullingMask   = (1 << UI.Layer);
        GUICamera.renderingPath = RenderingPath.Forward;

        SetCameraDistance(60f);
        SetFieldOfView(60f);

        Renderer = new Renderman();

        // Render Mesh.OutputGameObject with the GUI camera:
        Renderer.RenderWithCamera(UI.Layer);
        document            = Renderer.RootDocument;
        document.window.top = document.window;

        // Some overriding default UI settings:
        document.html.Style.Computed.ChangeTagProperty("color", new PowerUI.Css.Value("#ffffff", PowerUI.Css.ValueType.Color));
        document.html.Style.Computed.ChangeTagProperty("font-size", new PowerUI.Css.Value("14px", PowerUI.Css.ValueType.Pixels));
        document.body.Style.Computed.ChangeTagProperty("overflow", new PowerUI.Css.Value("hidden hidden", PowerUI.Css.ValueType.Point));

        UpdateTextDirection();

        // Fire the camera event:
        CameraGotCreated(GUICamera);
    }
示例#3
0
文件: UI.cs 项目: HippoAR/DemoPowerUI
    /// <summary>Used internally - don't call this one. Startup the UI for use in the Editor with AOT Nitro.</summary>
    /// <param name="nitroAot">True if no gameobject should be generated.</param>
    public static void Start(bool nitroAot)
    {
        if (_Started)
        {
            return;
        }

        _Started = true;

        // Setup atlas stacks:
        AtlasStacks.Start();

        // Hookup the wrench logging method:
        Dom.Log.OnLog += OnLogMessage;

        // Hookup the InfiniText logging method:
        InfiniText.Fonts.OnLog += OnLogMessage;

                #if !NoBIDI
        // Setup bidi character metadata:
        InfiniText.DirectionCategory.Setup();
                #endif

        // Setup the character entities such as &nbsp;
        CharacterEntities.Setup();

        // Start modules now! UI is always precompiled; Manager never is.
        // Starting them both will either start just one module or two
        // (depending on if we're precompiled or not).
        // Essentially that'll automatically catch all custom tags etc
        // regardless of where they are in the project.
        Modular.Start.Now(typeof(UI));
        Modular.Start.Now(typeof(PowerUI.Manager));

        // Setup language metadata:
        Languages.globalLoader.Setup();

        // Setup alert/confirm dialogues:
        BlockingDialogues.Setup();

        // Setup input:
        PowerUI.Input.Setup();

        // Setup the text/language service:
        if (Variables == null)
        {
            Variables = new FullVariableSet();

            if (!nitroAot)
            {
                // Sign up to the variable on change event - whenever a custom var is changed, we need to refresh the screen.
                Variables.OnChange += OnVariableChange;
                // Sign on to the event that occurs when the language changes.
                Dom.Text.OnLanguageChanged += OnLanguageChange;
                // Sign on to the event that occurs when the gender changes.
                Dom.Text.OnGenderChanged += ResolveAllVariables;
            }
        }

        // Setup the callback queue:
        Callbacks.Start();

        // Setup the character providers (for e.g. Emoji):
        CharacterProviders.Setup();

        Layer = LayerMask.NameToLayer("PowerUI");

                #if !NO_LAYER_CHECK
        if (Layer < 0)
        {
            // Invalid layer.
                        #if UNITY_EDITOR
            // Create the new layer now (this will actually be a permanent change):
            Layer = PowerUI.LayerManager.Add();
                        #else
            // On device - make one up:
            Layer = 21;
                        #endif
        }
                #endif

        // Default FPS:
        SetRate(DefaultRate);

                #if !NoNitroRuntime
        // Link up the text/nitro type by creating the engine:
        ScriptEngines.Add(new NitroScriptEngine());

        // Setup the compiler:
        NitroCode.Setup();

        // Setup extra aliases:
        PowerUI.TypeAliases.Setup();
                #endif

        if (nitroAot)
        {
            return;
        }

        GUINode = GameObject.Find("#PowerUI");

        if (GUINode == null)
        {
            // Not started yet.

            // Create the UI game object:
            GUINode      = new GameObject();
            GUINode.name = "#PowerUI";

            // Create the camera:
            CameraNode      = new GameObject();
            CameraNode.name = "Camera";

            // Create the updater:
            GlobalUpdater = GUINode.AddComponent <StandardUpdater>();

            // Setup the camera:
            GUICamera = CameraNode.AddComponent <Camera>();

            // Apply the new settings to the camera:
            GUICamera.orthographic = (CurrentCameraMode == CameraMode.Orthographic);
        }
        else
        {
            // Already started, but we might have updated.

            if (CameraNode == null)
            {
                // This can happen if the PowerUI assembly is actively reloaded (e.g. runtime updates).
                CameraNode      = GameObject.Find("#PowerUI/Camera");
                CameraTransform = CameraNode.transform;
                GUICamera       = CameraNode.GetComponent <Camera>();
            }
            else
            {
                // Already started!
                return;
            }
        }

        // Hide the PowerUI layer from all cameras other than GUICamera:
        Camera[] cameras = Camera.allCameras;

        int layerMask = ~(1 << UI.Layer);

        for (int i = 0; i < cameras.Length; i++)
        {
            // Grab the camera:
            Camera camera = cameras[i];

            // Is it the GUICamera?
            if (camera == GUICamera)
            {
                continue;
            }

            // Hide the UI layer from it:
            camera.cullingMask &= layerMask;
        }

        // Setup the transform:
        CameraTransform        = CameraNode.transform;
        CameraTransform.parent = GUINode.transform;

        GUICamera.nearClipPlane = 0.2f;
        GUICamera.depth         = CameraDepth;
        GUICamera.clearFlags    = CameraClearFlags.Depth;
        GUICamera.cullingMask   = (1 << UI.Layer);
        GUICamera.renderingPath = RenderingPath.Forward;

        SetCameraDistance(60f);
        SetFieldOfView(60f);

        Renderer = new Renderman();

        // Render Mesh.OutputGameObject with the GUI camera:
        Renderer.RenderWithCamera(UI.Layer);
        document            = Renderer.RootDocument as HtmlDocument;
        document.window.top = document.window;

        // Fire the camera event:
        CameraGotCreated(GUICamera);
    }
        /// <summary>Attempts to compile the code then run OnWindowLoaded. It's only successful if there are no nulls in the code buffer.</summary>
        /// <returns>Returns false if we're still waiting on code to download.</returns>
        public bool TryCompile()
        {
            if (CodeBuffer == null)
            {
                return(true);
            }

            if (CodeBuffer.Length == 0)
            {
                CodeBuffer = null;
                return(true);
            }

            for (int i = 0; i < CodeBuffer.Length; i++)
            {
                if (CodeBuffer[i] == null)
                {
                    return(false);
                }
            }

                        #if !NoNitroRuntime
            // Iframe security check - can code from this domain run at all?
            // We have the Nitro runtime so it could run unwanted code.
            if (window.parent != null && location != null && !location.fullAccess)
            {
                // It's an iframe to some unsafe location. We must have a security domain for this to be allowed at all.
                if (SecurityDomain == null || !SecurityDomain.AllowAccess(location.Protocol, location.host, location.ToString()))
                {
                    Wrench.Log.Add("Warning: blocked Nitro on a webpage - You must use a security domain to allow this. See http://help.kulestar.com/nitro-security/ for more.");
                    return(true);
                }
            }
                        #endif

            // Good to go!
            FinishedParsing = false;
            string codeToCompile = "";

            for (int i = 0; i < CodeBuffer.Length; i++)
            {
                codeToCompile += CodeBuffer[i] + "\n";
            }

            CodeBuffer = null;

            if (!AotDocument)
            {
                CodeInstance = NitroCache.TryGetScript(codeToCompile);
            }

            try{
                                #if !NoNitroRuntime
                string aotFile         = null;
                string aotAssemblyName = null;

                if (AotDocument)
                {
                    aotFile = "";
                    string[] pieces = ScriptLocation.Split('.');
                    for (int i = 0; i < pieces.Length - 1; i++)
                    {
                        if (i != 0)
                        {
                            aotFile += ".";
                        }
                        aotFile += pieces[i];
                    }
                    aotFile        += "-nitro-aot.dll";
                    aotAssemblyName = NitroCache.GetCodeSeed(codeToCompile) + ".ntro";
                }

                if (CodeInstance == null)
                {
                    NitroCode script = new NitroCode(codeToCompile, UI.BaseCodeType, SecurityDomain, aotFile, aotAssemblyName);
                    if (AotDocument)
                    {
                        // Internally the constructor will write it to the named file.
                        return(true);
                    }
                    CodeInstance            = (UICode)script.Instance();
                    CodeInstance.BaseScript = script;
                }
                                #endif

                if (CodeInstance != null)
                {
                    CodeInstance.document = this;
                    CodeInstance.window   = window;
                    CodeInstance.OnWindowLoaded();
                    CodeInstance.Start();
                    // Standard method that must be called.
                    // Any code outside of functions gets dropped in here:
                    CodeInstance.OnScriptReady();
                }
            }catch (Exception e) {
                string scriptLocation = ScriptLocation;

                if (string.IsNullOrEmpty(scriptLocation))
                {
                    // Use document.location instead:
                    scriptLocation = location.ToString();
                }

                if (!string.IsNullOrEmpty(scriptLocation))
                {
                    scriptLocation = " (At " + scriptLocation + ")";
                }

                Wrench.Log.Add("Script error" + scriptLocation + ": " + e);
            }
            return(true);
        }
示例#5
0
        protected override void Compile(string codeToCompile)
        {
            if (!AotDocument)
            {
                CodeInstance = NitroCache.TryGetScript(codeToCompile);
            }

            try{
                                #if !NoNitroRuntime
                string aotFile         = null;
                string aotAssemblyName = null;

                if (AotDocument)
                {
                    aotFile = "";
                    string[] pieces = htmlDocument.ScriptLocation.Split('.');
                    for (int i = 0; i < pieces.Length - 1; i++)
                    {
                        if (i != 0)
                        {
                            aotFile += ".";
                        }
                        aotFile += pieces[i];
                    }
                    aotFile        += "-nitro-aot.dll";
                    aotAssemblyName = NitroCache.GetCodeSeed(codeToCompile) + ".ntro";
                }

                if (CodeInstance == null)
                {
                    NitroCode script = new NitroCode(codeToCompile, UI.BaseCodeType, SecurityDomain, aotFile, aotAssemblyName);
                    if (AotDocument)
                    {
                        // Internally the constructor will write it to the named file.
                        return;
                    }
                    CodeInstance            = (UICode)script.Instance();
                    CodeInstance.BaseScript = script;
                }
                                #endif

                if (CodeInstance != null)
                {
                    CodeInstance.document = htmlDocument;
                    CodeInstance.window   = htmlDocument.window;

                    // Trigger an event to say Nitro is about to start:
                    Dom.Event e = new Dom.Event("scriptenginestart");
                    htmlDocument.dispatchEvent(e);


                    CodeInstance.OnWindowLoaded();
                    CodeInstance.Start();
                    // Standard method that must be called.
                    // Any code outside of functions gets dropped in here:
                    CodeInstance.OnScriptReady();
                }
            }catch (Exception e) {
                string scriptLocation = htmlDocument.ScriptLocation;

                if (string.IsNullOrEmpty(scriptLocation))
                {
                    // Use document.basepath instead:
                    scriptLocation = Document.basepath.ToString();
                }

                if (!string.IsNullOrEmpty(scriptLocation))
                {
                    scriptLocation = " (At " + scriptLocation + ")";
                }

                Dom.Log.Add("Script error" + scriptLocation + ": " + e);
            }
        }