コード例 #1
0
        /// <summary>
        /// Overwrites the Adapter.CS files with the selected library specific version.
        /// </summary>
        /// <param name="newLib"></param>
        /// <returns>Returns true if successful.</returns>
        private static bool CopyUncompiledAdapters(NetworkLibrary newLib)        // string libSuffix)
        {
            bool success = false;

            string libSuffix = (newLib == NetworkLibrary.UNET) ? "UNET" : (newLib == NetworkLibrary.PUN2) ? "PUN2" : "PUN";

            var _MA = GetPathFromAssetName("MasterNetAdapter" + libSuffix);
            var _NA = GetPathFromAssetName("NSTNetAdapter" + libSuffix);
            var MA  = GetPathFromAssetName("MasterNetAdapter");
            var NA  = GetPathFromAssetName("NSTNetAdapter");

            // fail if any of these files were not found.
            if (_MA == "" || _NA == "" || MA == "" || NA == "")
            {
                return(false);
            }

            XDebug.Log("Switching to " + libSuffix + " adapters... recompiling should happen automatically.", true, true);

            if (MasterNetAdapter.NetworkLibrary != newLib)
            {
                success |= AssetDatabase.CopyAsset(_MA, MA);
            }

            if (NSTNetAdapter.NetLibrary != newLib)
            {
                success |= AssetDatabase.CopyAsset(_NA, NA);
            }

            return(success);
        }
コード例 #2
0
        public INetworkConnection CreateConnection(NetworkLibrary.Core.NetworkType connectionType)
        {
            switch (connectionType)
            {
                case NetworkLibrary.Core.NetworkType.Client:
                    return new SharpClient();

                case NetworkLibrary.Core.NetworkType.Host:
                    return new SharpHost();
            }
            return null;
        }
コード例 #3
0
        /// <summary>
        /// Initiate the Library change process.
        /// </summary>
        /// <param name="newLib"></param>
        public static bool ChangeLibraries(NetworkLibrary newLib)
        {
            // Don't do anything if the adapters already are correct
            if (newLib == MasterNetAdapter.NetworkLibrary && newLib == NSTNetAdapter.NetLibrary)
            {
                return(true);
            }

            if (newLib == NetworkLibrary.PUN && !PUN_Exists)
            {
                Debug.LogError("Photon PUN does not appear to be installed (Cannot find the PhotonNetwork assembly). Be sure it is installed from the asset store for this project.");
                return(false);
            }

            if (!EditorUtility.DisplayDialog("Change Network Library To " + System.Enum.GetName(typeof(NetworkLibrary), newLib) + "?",
                                             "Changing libraries is a very messy brute force operation (you may see compile errors and may need to restart Unity). " +
                                             "Did you really want to do this, or are you just poking at things to see what they do?", "Change Library", "Cancel"))
            {
                return(false);
            }

            Debug.Log("Removing current adapters from game objects for Network Library change ...");
            PurgeLibraryReferences();

            // Close and reopen the current scene to remove the bugginess of orphaned scripts.
            var curscene     = EditorSceneManager.GetActiveScene();
            var curscenepath = curscene.path;

            if (EditorUtility.DisplayDialog("Save Scene Before Reload?",
                                            "Scene must be reloaded to complete the purging of old library adapters. Would you like to save this scene?", "Save Scene", "Don't Save"))
            {
                EditorSceneManager.SaveScene(curscene);
            }

            // force a scene close to eliminate weirdness
            EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);

            OverwriteAdapters(newLib);

            EditorUtility.DisplayDialog("Touch Nothing!",
                                        "Wait for the compiling animation in the bottom right of Unity to stop before doing anything. Touching NST related assets will result in broken scripts and errors.", "I Won't Touch, Promise.");


            // Flag the need for a deep global find of NSTs and NSTMasters in need of adapters
            DebugX.LogWarning("Add dependencies pending. Clicking on any NST related object in a scene " +
                              "will trigger the final steps of the transition to " + newLib + ". You may need to select the " +
                              "Player Prefab in the scene or asset folder in order to make it the default player object.", true, true);
            DependenciesHaveBeenAddedEverywhere = false;

            return(true);
        }
コード例 #4
0
        //public static void EnsureNSTMasterConforms()
        //{
        //	GameObject nstMasterPrefab = Resources.Load("NST Master", typeof(GameObject)) as GameObject;
        //	nstMasterPrefab.EnsureRootComponentExists<NSTMaster>();
        //	nstMasterPrefab.EnsureRootComponentExists<MasterNetAdapter>();

        //	if (MasterNetAdapter.NetLib == NetworkLibrary.UNET)
        //		nstMasterPrefab.EnsureRootComponentExists<NetworkIdentity>();
        //	else
        //	{
        //		NetworkIdentity ni = nstMasterPrefab.GetComponent<NetworkIdentity>();
        //		if (ni)
        //			Object.DestroyImmediate(ni);
        //	}
        //}

        //public static T EnsureContainsComponent<T>(this GameObject go) where T : Component
        //{
        //	T comp = go.GetComponent<T>();
        //	if (!comp)
        //		comp = go.AddComponent<T>();
        //	return comp;
        //}

        /// <summary>
        /// Return true if that library successfully was activated, or was already activated. False indicates a fail, to notify the enum
        /// selector to not make the change and stay in the current mode.
        /// </summary>
        /// <param name="netlib"></param>
        /// <returns></returns>
        public static bool OverwriteAdapters(NetworkLibrary newLib)
        {
            // Test to see if this library is already what is active
            if (MasterNetAdapter.NetworkLibrary != newLib)
            {
                // Don't try to change to PUN if the PUN classes are missing (not installed)
                if (newLib == NetworkLibrary.PUN)
                {
                    if (!PUN_Exists)
                    {
                        DebugX.LogError("Photon PUN does not appear to exist in this project. You will need to download Photon PUN from the Unity Asset Store and import it into this project in order to have this option.", true, true);
                        return(false);
                    }
                }
            }

            return(CopyUncompiledAdapters(newLib));
        }
コード例 #5
0
        /// <summary>
        /// Create server
        /// </summary>
        /// <param name="networkPort">Server network port</param>
        /// <param name="timeoutTime">Timeout time in seconds</param>
        /// <returns>Server if successful, otherwise "null"</returns>
        public static IServerSynchronizer Create(ushort networkPort, uint timeoutTime)
        {
            if (networkPort < 1)
            {
                throw new ArgumentException("Network port can't be smaller than 1.");
            }
            IServerSynchronizer ret = null;

            if (NetworkLibrary.Initialize())
            {
                Host         server_host = null;
                IConnector[] connectors  = new IConnector[2];
                try
                {
                    server_host = new Host();
                    Address address = new Address
                    {
                        Port = networkPort
                    };
                    server_host.Create(address, (int)Library.maxPeers, 0);
                    ret = new ServerSynchronizer();
                    bool on_handle_peer_connection_attempt(IPeer peer) => !ret.Bans.IsPeerBanned(peer, out _);

                    connectors[0] = new LocalConnector(on_handle_peer_connection_attempt);
                    connectors[1] = new ENetConnector(server_host, networkPort, timeoutTime, on_handle_peer_connection_attempt);
                    foreach (IConnector connector in connectors)
                    {
                        ret.AddConnector(connector);
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);
                    server_host?.Dispose();
                    foreach (IConnector connector in connectors)
                    {
                        connector?.Dispose();
                    }
                    ret?.Dispose();
                    NetworkLibrary.Deinitialize();
                }
            }
            return(ret);
        }
コード例 #6
0
        /// <summary>
        /// Connects to a network instance
        /// </summary>
        /// <param name="ipAddress">IP address</param>
        /// <param name="port">Port</param>
        /// <param name="token">Token</param>
        /// <param name="timeoutTime">Timeout time in seconds</param>
        /// <returns>Client synchronizer if successful, otherwise "null"</returns>
        public static IClientSynchronizer ConnectToNetwork(string ipAddress, ushort port, string token, uint timeoutTime)
        {
            if (ipAddress == null)
            {
                throw new ArgumentNullException(nameof(ipAddress));
            }
            if (port <= 0)
            {
                throw new ArgumentException(nameof(port));
            }
            IClientSynchronizer ret = null;

            if (NetworkLibrary.Initialize())
            {
                Host client_host = null;
                try
                {
                    client_host = new Host();
                    Address address = new Address();
                    address.SetHost(ipAddress);
                    address.Port = port;
                    client_host.Create(1, 0);
                    ENetConnector connector = new ENetConnector(client_host, port, timeoutTime, (_) => true);
                    ret = new ClientSynchronizer(connector.ConnectToNetwork(address), token);
                    ret.AddConnector(connector);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);
                    if (client_host != null)
                    {
                        client_host.Dispose();
                    }
                    NetworkLibrary.Deinitialize();
                }
            }
            return(ret);
        }
コード例 #7
0
 public void ChangeNetworkingEngine(NetworkLibrary netLib)
 {
     Debug.Log("Change to " + netLib);
 }
コード例 #8
0
ファイル: NetGame.cs プロジェクト: TheThing/Shooter
 void Network_OnWarningOccured(object source, NetworkLibrary.Exceptions.Warning warning)
 {
     Console.SetCursorPosition(0, 27);
     Console.Write(warning.Message + "               ");
 }