예제 #1
0
        void Update()
        {
#if UNITY_EDITOR && UNITY_IOS
            // Pause applicatin simulation for debugging.

            if (Input.GetKeyDown("p"))
            {
                OnApplicationPause(true);
            }

            if (Input.GetKeyDown("o"))
            {
                OnApplicationPause(false);
            }

            if (state == STATE.PAUSED)
            {
                return;
            }
#endif


#if !SOLO
            if (displayNetworkGUIState())
            {
                SetNetworkIndicators();
            }
#endif

            int t = 0;

            while (t < taskList.Count)
            {
                StoryTask task = taskList[t];

                if (task.Instruction == "end")
                {
                    Log("Encountered end task, removing pointer " + task.Pointer.currentPoint.StoryLine);

                    GENERAL.ALLPOINTERS.Remove(task.Pointer);
                }

                if (!GENERAL.ALLTASKS.Exists(at => at == task))
                {
                    // Task was removed, so stop executing it.
                    Log("Removing task:" + task.Instruction);
                    taskList.RemoveAt(t);
                }
                else
                {
                    // Task needs to be executed.
                    // first check for storyengine tasks.
                    bool done = false;
                    switch (task.Instruction)
                    {
#if !SOLO
                    // ---------------------------- VISUAL DEBUGGING ----------------------------
                    case "debugon":

                        // Show network info.

                        displayNetworkGUI(true);
                        done = true;
                        break;

                    case "debugoff":

                        // Hide network info.

                        displayNetworkGUI(false);
                        done = true;
                        break;

                    case "toggledebug":

                        // Toggle network info.

                        displayNetworkGUI(!displayNetworkGUIState());
                        done = true;
                        break;

                    // ---------------------------- SCOPE ----------------------------

                    case "isglobal":

                        if (GENERAL.AUTHORITY == AUTHORITY.GLOBAL)
                        {
                            task.Pointer.scope = SCOPE.GLOBAL;
                        }

                        done = true;
                        break;

                    case "islocal":

                        task.Pointer.SetLocal();

                        //Verbose("Setting pointer scope to local: " + task.Pointer.currentPoint.StoryLine);

                        done = true;
                        break;



                    // ---------------------------- SERVER SIDE ----------------------------

                    case "startserver":

                        // Start broadcast and network server if not already going.

                        if (!sending)
                        {
                            Log("Starting broadcast server, key " + ConnectionKey + " message " + ConnectionMessage);
                            startBroadcastServer(ConnectionKey, ConnectionMessage);
                        }

                        if (!serving)
                        {
                            Log("Starting network server.");
                            startNetworkServer();
                        }

                        done = true;
                        break;

                    case "stopserver":

                        // Stop broadcast and network server

                        Log("Stopping broadcast server.");
                        stopBroadcast();

                        Log("Stopping network server.");
                        stopNetworkServer();

                        done = true;
                        break;

                    case "monitorconnections":

                        if (NetworkObject == null || state == STATE.PAUSED)
                        {
                            break;
                        }

                        // Keep servers up on IOS (because of application pause)

#if UNITY_IOS
                        if (!sending)
                        {
                            Log("Starting broadcast server, key " + ConnectionKey + " message " + ConnectionMessage);
                            startBroadcastServer(ConnectionKey, ConnectionMessage);
                        }

                        if (!serving)
                        {
                            Log("Starting network server.");
                            startNetworkServer();
                        }
#endif

                        // Watch for new clients. Stays live indefinitely.

                        if (serving && NewTrackedAddresses())
                        {
                            // new addresses connected since last call

                            foreach (string add in NewConnectedAddresses())
                            {
                                Log("New client at " + add);
                            }


                            task.setCallBack("addclient");
                        }

                        task.SetStringValue("debug", "clients: " + TrackedConnectedAddresses().Count);
                        WasConnected = TrackedConnectedAddresses().Count > 0;


                        break;

                    case "pushglobaltasks":

                        // go over all pointers  and mark everything as modified
                        // that way it'll get sent.
                        // with single client apps this is ok, but for multiple clients we need something more advanced

                        Log("Pushing global tasks.");

                        foreach (StoryTask theTask in GENERAL.ALLTASKS)
                        {
                            if (theTask.scope == SCOPE.GLOBAL)
                            {
                                theTask.MarkAllAsModified();
                            }
                        }

                        done = true;
                        break;

                    // ---------------------------- CLIENT SIDE ----------------------------


                    case "serversearch":

                        if (NetworkObject == null || state == STATE.PAUSED)
                        {
                            break;
                        }

                        if (listening)
                        {
                            if (foundServer())
                            {
                                Log("Found broadcast server " + networkBroadcast.serverAddress + " key " + networkBroadcast.broadcastKey);

                                stopBroadcast();

                                task.setCallBack("serverfound");
                                listening = false;

                                done = true;
                            }
                        }
                        else
                        {
                            startBroadcastClient(ConnectionKey);
                            Log("Restarting broadcast listening for key " + ConnectionKey);
                            listening = true;
                        }

                        break;



                    case "startclient":

                        string ServerAddress = networkBroadcast.serverAddress;

                        if (ServerAddress != "")
                        {
                            Log("Starting network client for remote server " + ServerAddress);
                            startNetworkClient(ServerAddress);
                        }
                        else
                        {
                            Warning("Trying to start client without server address.");
                        }

                        done = true;
                        break;

                    case "stopclient":

                        Log("Stopping network client.");

                        StopNetworkClient();
                        WasConnected = false;

                        done = true;

                        break;

                    case "monitorserver":

                        // Watches the connection, stays active until connection lost.

                        if (clientIsConnected())
                        {
                            WasConnected = true;
                        }
                        else
                        {
                            if (WasConnected)
                            {
                                // Lost connection. Restart server search.

                                Log("Lost server connection.");

                                StopNetworkClient();
                                WasConnected = false;
                                task.setCallBack("client");

                                done = true;
                            }
                            else
                            {
                                //
                            }
                        }

                        break;
#endif
                    default:

                        // Not a controller task, pass it on.

                        if (dataTaskHandler != null)
                        {
                            done = dataTaskHandler(task);
                        }
                        else
                        {
                            // If no handler available we just sign off, but issue a warning once.
                            done = true;

                            if (!handlerWarning)
                            {
                                Warning("No handler registered.");
                                handlerWarning = true;
                            }
                        }

                        break;
                    }

                    if (done)
                    {
                        task.signOff(ID);
                        taskList.RemoveAt(t);
                    }
                    else
                    {
                        t++;
                    }
                }
            }
        }
예제 #2
0
        bool HandleTask(StoryTask task)
        {
            //#if UNITY_EDITOR && UNITY_IOS

            //            // Pause applicatin simulation for debugging.

            //            if (Input.GetKeyDown("p"))
            //            {
            //                OnApplicationPause(true);
            //            }

            //            if (Input.GetKeyDown("o"))
            //            {
            //                OnApplicationPause(false);
            //            }

            //            if (state == STATE.PAUSED)
            //                return;

            //#endif



            //if (displayNetworkGUIState())
            //    SetNetworkIndicators();



            // Task needs to be executed.
            // first check for storyengine tasks.
            bool done = false;

            switch (task.Instruction)
            {
            // ---------------------------- SCOPE ----------------------------

            case "isglobal":

                if (GENERAL.AUTHORITY == AUTHORITY.GLOBAL)
                {
                    task.Pointer.scope = SCOPE.GLOBAL;
                    Log("Setting pointer scope to global: " + task.Pointer.currentPoint.StoryLine);
                }
                else
                {
                    Log("No global authority, not changing pointer scope. ");
                }

                done = true;
                break;

            case "islocal":

                task.Pointer.SetLocal();

                Log("Setting pointer scope to local: " + task.Pointer.currentPoint.StoryLine);

                done = true;
                break;

            case "setglobalauth":
            case "amhub":

                GENERAL.AUTHORITY = AUTHORITY.GLOBAL;
                Log("Setting authority to global");
                //     AlertHandler.Log("Launching environment " + SessionSettings.Environment);
                //      Log("Launching " + Management.UserSettingsHandler.GetActiveModuleName() + ", environment " + StorageSettings.Environment);

                done = true;
                break;

            case "setlocalauth":
            case "amremote":

                GENERAL.AUTHORITY = AUTHORITY.LOCAL;
                Log("Setting authority to local");
                //    AlertHandler.Log("Launching environment " + SessionSettings.Environment);
                //  Log("Launching " + Management.UserSettingsHandler.GetActiveModuleName() + ", environment " + StorageSettings.Environment);

                done = true;
                break;


            // ---------------------------- SERVER SIDE ----------------------------

            case "startserver":

                // Start broadcast and network server if not already going.

                if (!Broadcasting)
                {
                    if (BroadcastKey == 0 || BroadcastMessageString == null || BroadcastMessageString.Length == 0)
                    {
                        // no info on how to configure broadcasterver

                        Warning("No broadcast info, not starting broadcastserver.");
                    }
                    else
                    {
                        startBroadcastServer(BroadcastKey, BroadcastMessageString);
                        Log("Starting broadcast server, key: " + BroadcastKey + " message: " + BroadcastMessageString);
                    }

                    //ModuleInfo mi = SessionData.GetModuleInfo();

                    //if (mi != null)
                    //{
                    //    startBroadcastServer(mi.BroadcastKey, mi.ID);
                    //    Log("Starting broadcast server, key: " + mi.BroadcastKey + " message: " + mi.ID);
                    //}
                    //else
                    //{
                    //    Warning("No module info, starting generic broadcast server.");
                    //    Log("key: " + ConnectionKey + " message: " + ConnectionMessage);
                    //    startBroadcastServer(ConnectionKey, ConnectionMessage);
                    //}
                }

                if (!Serving)
                {
                    Log("Starting network server.");
                    startNetworkServer();
                }

                done = true;
                break;

            case "stopserver":

                // Stop broadcast and network server

                Log("Stopping broadcast server.");
                stopBroadcast();

                Log("Stopping network server.");
                stopNetworkServer();

                done = true;
                break;

            case "monitorconnections":

                if (NetworkObject == null || state == STATE.PAUSED)
                {
                    break;
                }

                // Keep servers up on IOS (because of application pause)

#if UNITY_IOS
                if (!Broadcasting)
                {
                    Log("Starting broadcast server, key " + BroadcastKey + " message " + BroadcastMessageString);
                    //startBroadcastServer(ConnectionKey, ConnectionMessage);
                    startBroadcastServer(BroadcastKey, BroadcastMessageString);
                }

                if (!Serving)
                {
                    Log("Starting network server.");
                    startNetworkServer();
                }
#endif



                // Watch for new clients. Stays live indefinitely.

                if (Serving && NewTrackedAddresses())
                {
                    // new addresses connected since last call

                    foreach (string add in NewConnectedAddresses())
                    {
                        Log("New client at " + add);
                        // note: pass addresses on so we can send targeted messages...
                    }

                    task.setCallBack("addclient");
                }

                if (networkManager != null)
                {
                    NetworkConnection[] connections = networkManager.GetConnectedClients();
                    task.SetStringValue("debug", "clients: " + connections.Length);
                    WasConnected = connections.Length > 0;
                }
                else
                {
                    Warning("Network manager not available, cannot monitor connections");
                }


                //task.SetStringValue("debug", "clients: " + TrackedConnectedAddresses().Count);
                //WasConnected = TrackedConnectedAddresses().Count > 0;

                break;


            //case "pushglobaldata":



            //    done = true;
            //    break;


            case "pushglobaltasks":

                // go over all pointers  and mark everything as modified
                // that way it'll get sent.
                // with single client apps this is ok, but for multiple clients we need something more advanced

                Log("Pushing global tasks.");

                foreach (StoryTask theTask in GENERAL.ALLTASKS)
                {
                    if (theTask.scope == SCOPE.GLOBAL)
                    {
                        theTask.MarkAllAsModified();
                    }
                }

                done = true;
                break;

            // ---------------------------- CLIENT SIDE ----------------------------

            case "serversearch":

                if (NetworkObject == null || state == STATE.PAUSED)
                {
                    break;
                }

                if (Listening)
                {
                    if (foundServer())
                    {
                        stopBroadcast();

                        string   message = networkBroadcast.serverMessage;
                        char[]   param   = { '\0' };
                        string[] split   = message.Split(param);

                        task.SetStringValue("persistantData", split[0]);

                        task.setCallBack("serverfound");

                        Listening = false;

                        Log("Found broadcast server: " + networkBroadcast.serverAddress + " key: " + networkBroadcast.broadcastKey + " message: " + split[0]);


                        done = true;
                    }
                }
                else
                {
                    /*
                     * ModuleInfo mi = SessionData.GetModuleInfo();
                     * if (mi != null)
                     * {
                     *  startBroadcastClient(mi.BroadcastKey);
                     *  Log("(Re)starting broadcast listening for key " + mi.BroadcastKey);
                     * }
                     * else
                     * {
                     *  startBroadcastClient(BroadcastKey);
                     *  Log("(Re)starting broadcast listening for key " + BroadcastKey);
                     * }
                     */

                    startBroadcastClient(BroadcastKey);
                    Log("(Re)starting broadcast listening for key " + BroadcastKey);

                    Listening = true;
                }

                break;



            case "startclient":

                string ServerAddress = networkBroadcast.serverAddress;

                if (ServerAddress != "")
                {
                    //        Log("Starting network client for remote server " + ServerAddress);
                    startNetworkClient(ServerAddress);
                }
                else
                {
                    Warning("Trying to start client without server address.");
                }

                done = true;
                break;

            case "stopclient":

                //          Verbose("Stopping network client.");

                StopNetworkClient();
                WasConnected = false;

                done = true;

                break;

            case "monitorserver":

                // Watches the connection, stays active until connection lost.

                if (clientIsConnected())
                {
                    WasConnected = true;
                }
                else
                {
                    if (WasConnected)
                    {
                        // Lost connection. Restart server search.

                        Log("Lost server connection.");

                        StopNetworkClient();
                        WasConnected = false;
                        task.setCallBack("client");

                        done = true;
                    }
                    else
                    {
                        //
                    }
                }

                break;



            default:

                done = true;

                break;
            }
            return(done);
        }