|
| 1 | +#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN) |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Sockets; |
| 7 | +using System.Threading; |
| 8 | +using UnityEngine; |
| 9 | + |
| 10 | +namespace Immutable.Browser.Core |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Local HTTP server for index.html to provide a proper origin instead of null from file:// URLs. |
| 14 | + /// </summary> |
| 15 | + public class GameBridgeServer : IDisposable |
| 16 | + { |
| 17 | + private const string TAG = "[Game Bridge Server]"; |
| 18 | + |
| 19 | + // Fixed port to maintain consistent origin for localStorage/IndexedDB persistence |
| 20 | + private const int PORT = 51990; |
| 21 | + private static readonly string URL = "http://localhost:" + PORT + "/"; |
| 22 | + |
| 23 | + private HttpListener? _listener; |
| 24 | + private Thread? _listenerThread; |
| 25 | + private byte[]? _indexHtmlContent; |
| 26 | + private readonly CancellationTokenSource _cts = new CancellationTokenSource(); |
| 27 | + private bool _disposed; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Creates a new GameBridgeServer instance. |
| 31 | + /// </summary> |
| 32 | + /// <param name="indexHtmlPath">The file system path to the index.html file.</param> |
| 33 | + public GameBridgeServer(string indexHtmlPath) |
| 34 | + { |
| 35 | + if (string.IsNullOrEmpty(indexHtmlPath)) |
| 36 | + throw new ArgumentNullException(nameof(indexHtmlPath)); |
| 37 | + |
| 38 | + if (!File.Exists(indexHtmlPath)) |
| 39 | + throw new FileNotFoundException($"{TAG} index.html not found: {indexHtmlPath}"); |
| 40 | + |
| 41 | + _indexHtmlContent = File.ReadAllBytes(indexHtmlPath); |
| 42 | + Debug.Log($"{TAG} Loaded index.html ({_indexHtmlContent.Length} bytes)"); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Starts the game bridge server. |
| 47 | + /// </summary> |
| 48 | + /// <returns>The URL to the index.html file.</returns> |
| 49 | + public string Start() |
| 50 | + { |
| 51 | + if (_disposed) |
| 52 | + throw new ObjectDisposedException(nameof(GameBridgeServer)); |
| 53 | + |
| 54 | + if (_listener?.IsListening == true) |
| 55 | + return URL; |
| 56 | + |
| 57 | + EnsurePortAvailable(); |
| 58 | + |
| 59 | + _listener = new HttpListener(); |
| 60 | + _listener.Prefixes.Add(URL); |
| 61 | + _listener.Start(); |
| 62 | + |
| 63 | + Debug.Log($"{TAG} Started on {URL}"); |
| 64 | + |
| 65 | + _listenerThread = new Thread(ListenerLoop) |
| 66 | + { |
| 67 | + Name = "GameBridgeServer", |
| 68 | + IsBackground = true |
| 69 | + }; |
| 70 | + _listenerThread.Start(); |
| 71 | + |
| 72 | + return URL; |
| 73 | + } |
| 74 | + |
| 75 | + private void ListenerLoop() |
| 76 | + { |
| 77 | + while (!_cts.Token.IsCancellationRequested && _listener?.IsListening == true) |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + var context = _listener.GetContext(); |
| 82 | + HandleRequest(context); |
| 83 | + } |
| 84 | + catch (HttpListenerException) when (_cts.Token.IsCancellationRequested) |
| 85 | + { |
| 86 | + break; |
| 87 | + } |
| 88 | + catch (ObjectDisposedException) |
| 89 | + { |
| 90 | + break; |
| 91 | + } |
| 92 | + catch (Exception ex) |
| 93 | + { |
| 94 | + if (!_cts.Token.IsCancellationRequested) |
| 95 | + Debug.LogError($"{TAG} Error: {ex.Message}"); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void HandleRequest(HttpListenerContext context) |
| 101 | + { |
| 102 | + var response = context.Response; |
| 103 | + try |
| 104 | + { |
| 105 | + response.StatusCode = 200; |
| 106 | + response.ContentType = "text/html; charset=utf-8"; |
| 107 | + response.ContentLength64 = _indexHtmlContent!.Length; |
| 108 | + response.OutputStream.Write(_indexHtmlContent, 0, _indexHtmlContent.Length); |
| 109 | + } |
| 110 | + catch (Exception ex) |
| 111 | + { |
| 112 | + Debug.LogError($"{TAG} Error handling request: {ex.Message}"); |
| 113 | + } |
| 114 | + finally |
| 115 | + { |
| 116 | + try { response.Close(); } catch { } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void EnsurePortAvailable() |
| 121 | + { |
| 122 | + if (!IsPortAvailable(PORT)) |
| 123 | + { |
| 124 | + throw new InvalidOperationException( |
| 125 | + $"{TAG} Port {PORT} is already in use. " + |
| 126 | + "Please close any application using this port to ensure localStorage/IndexedDB data persists correctly."); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private bool IsPortAvailable(int port) |
| 131 | + { |
| 132 | + try |
| 133 | + { |
| 134 | + var listener = new TcpListener(IPAddress.Loopback, port); |
| 135 | + listener.Start(); |
| 136 | + listener.Stop(); |
| 137 | + return true; |
| 138 | + } |
| 139 | + catch |
| 140 | + { |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public void Dispose() |
| 146 | + { |
| 147 | + if (_disposed) return; |
| 148 | + _disposed = true; |
| 149 | + |
| 150 | + _cts.Cancel(); |
| 151 | + try |
| 152 | + { |
| 153 | + _listener?.Stop(); |
| 154 | + _listener?.Close(); |
| 155 | + } |
| 156 | + catch { } |
| 157 | + |
| 158 | + _listenerThread?.Join(TimeSpan.FromSeconds(1)); |
| 159 | + _cts.Dispose(); |
| 160 | + _indexHtmlContent = null; |
| 161 | + |
| 162 | + Debug.Log($"{TAG} Stopped"); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +#endif |
0 commit comments