AppSettings.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // -----------------------------------------------------------------------
  2. // <copyright file="AppSettings.cs" company="Exit Games GmbH">
  3. // Loadbalancing Framework for Photon - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>Settings for Photon application(s) and the server to connect to.</summary>
  6. // <author>developer@photonengine.com</author>
  7. // ----------------------------------------------------------------------------
  8. #if UNITY_2017_4_OR_NEWER
  9. #define SUPPORTED_UNITY
  10. #endif
  11. namespace Photon.Realtime
  12. {
  13. using System;
  14. using ExitGames.Client.Photon;
  15. #if SUPPORTED_UNITY || NETFX_CORE
  16. using Hashtable = ExitGames.Client.Photon.Hashtable;
  17. using SupportClass = ExitGames.Client.Photon.SupportClass;
  18. #endif
  19. /// <summary>
  20. /// Settings for Photon application(s) and the server to connect to.
  21. /// </summary>
  22. /// <remarks>
  23. /// This is Serializable for Unity, so it can be included in ScriptableObject instances.
  24. /// </remarks>
  25. #if !NETFX_CORE || SUPPORTED_UNITY
  26. [Serializable]
  27. #endif
  28. public class AppSettings
  29. {
  30. /// <summary>AppId for Realtime or PUN.</summary>
  31. public string AppIdRealtime;
  32. /// <summary>AppId for Photon Fusion.</summary>
  33. public string AppIdFusion;
  34. /// <summary>AppId for Photon Chat.</summary>
  35. public string AppIdChat;
  36. /// <summary>AppId for Photon Voice.</summary>
  37. public string AppIdVoice;
  38. /// <summary>The AppVersion can be used to identify builds and will split the AppId distinct "Virtual AppIds" (important for matchmaking).</summary>
  39. public string AppVersion;
  40. /// <summary>If false, the app will attempt to connect to a Master Server (which is obsolete but sometimes still necessary).</summary>
  41. /// <remarks>if true, Server points to a NameServer (or is null, using the default), else it points to a MasterServer.</remarks>
  42. public bool UseNameServer = true;
  43. /// <summary>Can be set to any of the Photon Cloud's region names to directly connect to that region.</summary>
  44. /// <remarks>if this IsNullOrEmpty() AND UseNameServer == true, use BestRegion. else, use a server</remarks>
  45. public string FixedRegion;
  46. /// <summary>Set to a previous BestRegionSummary value before connecting.</summary>
  47. /// <remarks>
  48. /// This is a value used when the client connects to the "Best Region".</br>
  49. /// If this is null or empty, all regions gets pinged. Providing a previous summary on connect,
  50. /// speeds up best region selection and makes the previously selected region "sticky".</br>
  51. ///
  52. /// Unity clients should store the BestRegionSummary in the PlayerPrefs.
  53. /// You can store the new result by implementing <see cref="IConnectionCallbacks.OnConnectedToMaster"/>.
  54. /// If <see cref="LoadBalancingClient.SummaryToCache"/> is not null, store this string.
  55. /// To avoid storing the value multiple times, you could set SummaryToCache to null.
  56. /// </remarks>
  57. #if SUPPORTED_UNITY
  58. [NonSerialized]
  59. #endif
  60. public string BestRegionSummaryFromStorage;
  61. /// <summary>The address (hostname or IP) of the server to connect to.</summary>
  62. public string Server;
  63. /// <summary>If not null, this sets the port of the first Photon server to connect to (that will "forward" the client as needed).</summary>
  64. public int Port;
  65. /// <summary>The address (hostname or IP and port) of the proxy server.</summary>
  66. public string ProxyServer;
  67. /// <summary>The network level protocol to use.</summary>
  68. public ConnectionProtocol Protocol = ConnectionProtocol.Udp;
  69. /// <summary>Enables a fallback to another protocol in case a connect to the Name Server fails.</summary>
  70. /// <remarks>See: LoadBalancingClient.EnableProtocolFallback.</remarks>
  71. public bool EnableProtocolFallback = true;
  72. /// <summary>Defines how authentication is done. On each system, once or once via a WSS connection (safe).</summary>
  73. public AuthModeOption AuthMode = AuthModeOption.Auth;
  74. /// <summary>If true, the client will request the list of currently available lobbies.</summary>
  75. public bool EnableLobbyStatistics;
  76. /// <summary>Log level for the network lib.</summary>
  77. public DebugLevel NetworkLogging = DebugLevel.ERROR;
  78. /// <summary>If true, the Server field contains a Master Server address (if any address at all).</summary>
  79. public bool IsMasterServerAddress
  80. {
  81. get { return !this.UseNameServer; }
  82. }
  83. /// <summary>If true, the client should fetch the region list from the Name Server and find the one with best ping.</summary>
  84. /// <remarks>See "Best Region" in the online docs.</remarks>
  85. public bool IsBestRegion
  86. {
  87. get { return this.UseNameServer && string.IsNullOrEmpty(this.FixedRegion); }
  88. }
  89. /// <summary>If true, the default nameserver address for the Photon Cloud should be used.</summary>
  90. public bool IsDefaultNameServer
  91. {
  92. get { return this.UseNameServer && string.IsNullOrEmpty(this.Server); }
  93. }
  94. /// <summary>If true, the default ports for a protocol will be used.</summary>
  95. public bool IsDefaultPort
  96. {
  97. get { return this.Port <= 0; }
  98. }
  99. /// <summary>ToString but with more details.</summary>
  100. public string ToStringFull()
  101. {
  102. return string.Format(
  103. "appId {0}{1}{2}{3}" +
  104. "use ns: {4}, reg: {5}, {9}, " +
  105. "{6}{7}{8}" +
  106. "auth: {10}",
  107. String.IsNullOrEmpty(this.AppIdRealtime) ? string.Empty : "Realtime/PUN: " + this.HideAppId(this.AppIdRealtime) + ", ",
  108. String.IsNullOrEmpty(this.AppIdFusion) ? string.Empty : "Fusion: " + this.HideAppId(this.AppIdFusion) + ", ",
  109. String.IsNullOrEmpty(this.AppIdChat) ? string.Empty : "Chat: " + this.HideAppId(this.AppIdChat) + ", ",
  110. String.IsNullOrEmpty(this.AppIdVoice) ? string.Empty : "Voice: " + this.HideAppId(this.AppIdVoice) + ", ",
  111. String.IsNullOrEmpty(this.AppVersion) ? string.Empty : "AppVersion: " + this.AppVersion + ", ",
  112. "UseNameServer: " + this.UseNameServer + ", ",
  113. "Fixed Region: " + this.FixedRegion + ", ",
  114. //this.BestRegionSummaryFromStorage,
  115. String.IsNullOrEmpty(this.Server) ? string.Empty : "Server: " + this.Server + ", ",
  116. this.IsDefaultPort ? string.Empty : "Port: " + this.Port + ", ",
  117. String.IsNullOrEmpty(ProxyServer) ? string.Empty : "Proxy: " + this.ProxyServer + ", ",
  118. this.Protocol,
  119. this.AuthMode
  120. //this.EnableLobbyStatistics,
  121. //this.NetworkLogging,
  122. );
  123. }
  124. /// <summary>Checks if a string is a Guid by attempting to create one.</summary>
  125. /// <param name="val">The potential guid to check.</param>
  126. /// <returns>True if new Guid(val) did not fail.</returns>
  127. public static bool IsAppId(string val)
  128. {
  129. try
  130. {
  131. new Guid(val);
  132. }
  133. catch
  134. {
  135. return false;
  136. }
  137. return true;
  138. }
  139. private string HideAppId(string appId)
  140. {
  141. return string.IsNullOrEmpty(appId) || appId.Length < 8
  142. ? appId
  143. : string.Concat(appId.Substring(0, 8), "***");
  144. }
  145. public AppSettings CopyTo(AppSettings d)
  146. {
  147. d.AppIdRealtime = this.AppIdRealtime;
  148. d.AppIdFusion = this.AppIdFusion;
  149. d.AppIdChat = this.AppIdChat;
  150. d.AppIdVoice = this.AppIdVoice;
  151. d.AppVersion = this.AppVersion;
  152. d.UseNameServer = this.UseNameServer;
  153. d.FixedRegion = this.FixedRegion;
  154. d.BestRegionSummaryFromStorage = this.BestRegionSummaryFromStorage;
  155. d.Server = this.Server;
  156. d.Port = this.Port;
  157. d.ProxyServer = this.ProxyServer;
  158. d.Protocol = this.Protocol;
  159. d.AuthMode = this.AuthMode;
  160. d.EnableLobbyStatistics = this.EnableLobbyStatistics;
  161. d.NetworkLogging = this.NetworkLogging;
  162. d.EnableProtocolFallback = this.EnableProtocolFallback;
  163. return d;
  164. }
  165. }
  166. }