From e80bfafbe2401863024a54d75828154de1f739fe Mon Sep 17 00:00:00 2001 From: Joshua Reason Date: Tue, 8 Oct 2019 10:33:00 +1100 Subject: [PATCH] Got webGL build working, some bugs still --- .../Networking/Server/Realtime/Server.asset | 4 +- Assets/Scenes/Menus/Lobby.unity | 4 +- .../Sprites.meta => Scripts/Browser.meta} | 2 +- Assets/Scripts/Browser/BrowserServer.cs | 120 +++++++++ Assets/Scripts/Browser/BrowserServer.cs.meta | 11 + Assets/Scripts/Browser/SimpleHttpsServer.cs | 230 ++++++++++++++++++ .../Scripts/Browser/SimpleHttpsServer.cs.meta | 11 + Assets/Scripts/Browser/URLReader.cs | 23 ++ Assets/Scripts/Browser/URLReader.cs.meta | 11 + Assets/Scripts/Character.cs | 2 +- Assets/Scripts/LevelBlocks/Water.cs | 3 +- 11 files changed, 414 insertions(+), 7 deletions(-) rename Assets/{Plugins/IngameDebugConsole/Sprites.meta => Scripts/Browser.meta} (77%) create mode 100644 Assets/Scripts/Browser/BrowserServer.cs create mode 100644 Assets/Scripts/Browser/BrowserServer.cs.meta create mode 100644 Assets/Scripts/Browser/SimpleHttpsServer.cs create mode 100644 Assets/Scripts/Browser/SimpleHttpsServer.cs.meta create mode 100644 Assets/Scripts/Browser/URLReader.cs create mode 100644 Assets/Scripts/Browser/URLReader.cs.meta diff --git a/Assets/Data/Networking/Server/Realtime/Server.asset b/Assets/Data/Networking/Server/Realtime/Server.asset index ac53ae2..d8157f2 100644 --- a/Assets/Data/Networking/Server/Realtime/Server.asset +++ b/Assets/Data/Networking/Server/Realtime/Server.asset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b79400c63f907550e1cd21c157f2745ed5a483daf6f454465e332def68b794de -size 489 +oid sha256:6d7b72510c56d1d1adc4dc2473034b884e8b831978ca1efc7ad50b536157b696 +size 508 diff --git a/Assets/Scenes/Menus/Lobby.unity b/Assets/Scenes/Menus/Lobby.unity index 69c8415..45ec2c9 100644 --- a/Assets/Scenes/Menus/Lobby.unity +++ b/Assets/Scenes/Menus/Lobby.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2884d7c10378352a9a3863aa21382541b6fb45a0d4b69c9c59ca99352d6555f8 -size 45893 +oid sha256:1a5f27543fe7238c6f5003414611a9d25367d3b80ef34dd6f1e09c8f59375984 +size 46346 diff --git a/Assets/Plugins/IngameDebugConsole/Sprites.meta b/Assets/Scripts/Browser.meta similarity index 77% rename from Assets/Plugins/IngameDebugConsole/Sprites.meta rename to Assets/Scripts/Browser.meta index 504a23d..442d7dc 100644 --- a/Assets/Plugins/IngameDebugConsole/Sprites.meta +++ b/Assets/Scripts/Browser.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: cb5d7b23a9e684a41a6a5d4f300eb1e6 +guid: 0f8c61b1677edbf4c8a8b1e8813346ed folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/Scripts/Browser/BrowserServer.cs b/Assets/Scripts/Browser/BrowserServer.cs new file mode 100644 index 0000000..5608108 --- /dev/null +++ b/Assets/Scripts/Browser/BrowserServer.cs @@ -0,0 +1,120 @@ +using System.Collections; +using System.Collections.Generic; +using System.Threading.Tasks; +using UnityEngine; +using System.Net; +using System.Text; +using System.IO; + +public class BrowserServer : MonoBehaviour +{ + + [SerializeField] + private string HTMLPath; + + [SerializeField] + private int port; + + SimpleHTTPServer server; + + // Start is called before the first frame update + void Start() + { + server = new SimpleHTTPServer(HTMLPath, port); + } + + private void OnDisable() + { + server.Stop(); + } + + private void StartServer() + { + if (!HttpListener.IsSupported) + { + Debug.LogWarning("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); + return; + } + + string responseString = "

Error Connecting to server

"; + + + + // Create a listener. + HttpListener server = new HttpListener(); + // Add the prefixes. + + + server.Prefixes.Add("http://*:" + port.ToString() + "/"); + server.Start(); + Debug.Log(" HTML Listening..."); + + // while (enabled) + // { + HttpListenerContext context = server.GetContext(); + HttpListenerResponse response = context.Response; + + + string page = HTMLPath + context.Request.Url.LocalPath; + Debug.Log("Client asked for: " + context.Request.Url.LocalPath); + if (page.EndsWith("/")) + page += "index.html"; + + Debug.Log("Client asked for: " + page); + + if (page == string.Empty) + page = "index.html"; + + try + { + TextReader tr = new StreamReader(page); + string msg = tr.ReadToEnd(); + byte[] buffer = Encoding.UTF8.GetBytes(msg); + + response.ContentLength64 = buffer.Length; + Stream st = response.OutputStream; + st.Write(buffer, 0, buffer.Length); + } + catch (IOException e) + { + Debug.LogError(e); + } + + + + context.Response.Close(); + //} + + server.Stop(); + Task.Run(() => StartServer()); + } + + private void Respond(HttpListenerContext context) + { + + string responseString = "

Error Connecting to server

"; + try + { + responseString = System.IO.File.ReadAllText(HTMLPath); + Debug.Log(responseString); + } + catch (System.IO.IOException e) + { + Debug.LogError(e); + } + + HttpListenerRequest request = context.Request; + // Obtain a response object. + HttpListenerResponse response = context.Response; + // Construct a response. + + byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); + // Get a response stream and write the response to it. + response.ContentLength64 = buffer.Length; + System.IO.Stream output = response.OutputStream; + output.Write(buffer, 0, buffer.Length); + // You must close the output stream. + output.Close(); + } + +} diff --git a/Assets/Scripts/Browser/BrowserServer.cs.meta b/Assets/Scripts/Browser/BrowserServer.cs.meta new file mode 100644 index 0000000..17e09fc --- /dev/null +++ b/Assets/Scripts/Browser/BrowserServer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1a34f2d03e796714a93c549ae4c4513c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Browser/SimpleHttpsServer.cs b/Assets/Scripts/Browser/SimpleHttpsServer.cs new file mode 100644 index 0000000..778af99 --- /dev/null +++ b/Assets/Scripts/Browser/SimpleHttpsServer.cs @@ -0,0 +1,230 @@ +// MIT License - Copyright (c) 2016 Can Güney Aksakalli +// https://aksakalli.github.io/2014/02/24/simple-http-server-with-csparp.html + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net.Sockets; +using System.Net; +using System.IO; +using System.Threading; +using System.Diagnostics; + + +class SimpleHTTPServer +{ + private readonly string[] _indexFiles = { + "index.html", + "index.htm", + "default.html", + "default.htm" + }; + + private static IDictionary _mimeTypeMappings = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { + #region extension to MIME type list + {".asf", "video/x-ms-asf"}, + {".asx", "video/x-ms-asf"}, + {".avi", "video/x-msvideo"}, + {".bin", "application/octet-stream"}, + {".cco", "application/x-cocoa"}, + {".crt", "application/x-x509-ca-cert"}, + {".css", "text/css"}, + {".deb", "application/octet-stream"}, + {".der", "application/x-x509-ca-cert"}, + {".dll", "application/octet-stream"}, + {".dmg", "application/octet-stream"}, + {".ear", "application/java-archive"}, + {".eot", "application/octet-stream"}, + {".exe", "application/octet-stream"}, + {".flv", "video/x-flv"}, + {".gif", "image/gif"}, + {".hqx", "application/mac-binhex40"}, + {".htc", "text/x-component"}, + {".htm", "text/html"}, + {".html", "text/html"}, + {".ico", "image/x-icon"}, + {".img", "application/octet-stream"}, + {".iso", "application/octet-stream"}, + {".jar", "application/java-archive"}, + {".jardiff", "application/x-java-archive-diff"}, + {".jng", "image/x-jng"}, + {".jnlp", "application/x-java-jnlp-file"}, + {".jpeg", "image/jpeg"}, + {".jpg", "image/jpeg"}, + {".js", "application/x-javascript"}, + {".mml", "text/mathml"}, + {".mng", "video/x-mng"}, + {".mov", "video/quicktime"}, + {".mp3", "audio/mpeg"}, + {".mpeg", "video/mpeg"}, + {".mpg", "video/mpeg"}, + {".msi", "application/octet-stream"}, + {".msm", "application/octet-stream"}, + {".msp", "application/octet-stream"}, + {".pdb", "application/x-pilot"}, + {".pdf", "application/pdf"}, + {".pem", "application/x-x509-ca-cert"}, + {".pl", "application/x-perl"}, + {".pm", "application/x-perl"}, + {".png", "image/png"}, + {".prc", "application/x-pilot"}, + {".ra", "audio/x-realaudio"}, + {".rar", "application/x-rar-compressed"}, + {".rpm", "application/x-redhat-package-manager"}, + {".rss", "text/xml"}, + {".run", "application/x-makeself"}, + {".sea", "application/x-sea"}, + {".shtml", "text/html"}, + {".sit", "application/x-stuffit"}, + {".swf", "application/x-shockwave-flash"}, + {".tcl", "application/x-tcl"}, + {".tk", "application/x-tcl"}, + {".txt", "text/plain"}, + {".war", "application/java-archive"}, + {".wbmp", "image/vnd.wap.wbmp"}, + {".wmv", "video/x-ms-wmv"}, + {".xml", "text/xml"}, + {".xpi", "application/x-xpinstall"}, + {".zip", "application/zip"}, + #endregion + }; + private Thread _serverThread; + private string _rootDirectory; + private HttpListener _listener; + private int _port; + + public int Port { + get { return _port; } + private set { } + } + + /// + /// Construct server with given port. + /// + /// Directory path to serve. + /// Port of the server. + public SimpleHTTPServer(string path, int port) + { + this.Initialize(path, port); + } + + /// + /// Construct server with suitable port. + /// + /// Directory path to serve. + public SimpleHTTPServer(string path) + { + //get an empty port + TcpListener l = new TcpListener(IPAddress.Loopback, 0); + l.Start(); + int port = ((IPEndPoint)l.LocalEndpoint).Port; + l.Stop(); + this.Initialize(path, port); + } + + /// + /// Stop server and dispose all functions. + /// + public void Stop() + { + _serverThread.Abort(); + _listener.Stop(); + } + + private void Listen() + { + _listener = new HttpListener(); + _listener.Prefixes.Add("http://*:" + _port.ToString() + "/"); + _listener.Start(); + while (true) + { + try + { + HttpListenerContext context = _listener.GetContext(); + UnityEngine.Debug.Log("client Connected to webpage"); + Process(context); + } + catch (Exception ex) + { + + } + } + } + + private void Process(HttpListenerContext context) + { + string filename = context.Request.Url.AbsolutePath; + Console.WriteLine(filename); + filename = filename.Substring(1); + + if (string.IsNullOrEmpty(filename)) + { + foreach (string indexFile in _indexFiles) + { + if (File.Exists(Path.Combine(_rootDirectory, indexFile))) + { + filename = indexFile; + break; + } + } + } + + filename = Path.Combine(_rootDirectory, filename); + + UnityEngine.Debug.Log("Client wants:" + filename); + + if (File.Exists(filename)) + { + try + { + Stream input = new FileStream(filename, FileMode.Open); + + //Adding permanent http response headers + string mime; + context.Response.ContentType = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime) ? mime : "application/octet-stream"; + context.Response.AddHeader("Date", DateTime.Now.ToString("r")); + context.Response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filename).ToString("r")); + + if (Path.GetExtension(filename) == ".unityweb") + context.Response.AddHeader("Content-Encoding", "gzip"); + + var bytes = default(byte[]); + using (var memstream = new MemoryStream()) + { + input.CopyTo(memstream); + bytes = memstream.ToArray(); + } + + context.Response.StatusCode = (int)HttpStatusCode.OK; + + context.Response.ContentLength64 = bytes.Length; + Stream st = context.Response.OutputStream; + st.Write(bytes, 0, bytes.Length); + + UnityEngine.Debug.Log("Sent"); + } + catch (Exception ex) + { + context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; + } + + } + else + { + context.Response.StatusCode = (int)HttpStatusCode.NotFound; + } + + context.Response.OutputStream.Close(); + } + + private void Initialize(string path, int port) + { + this._rootDirectory = path; + this._port = port; + _serverThread = new Thread(this.Listen); + _serverThread.Start(); + } + + +} \ No newline at end of file diff --git a/Assets/Scripts/Browser/SimpleHttpsServer.cs.meta b/Assets/Scripts/Browser/SimpleHttpsServer.cs.meta new file mode 100644 index 0000000..49981e4 --- /dev/null +++ b/Assets/Scripts/Browser/SimpleHttpsServer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ae836f4db7ac22f4db15d0f174d03e73 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Browser/URLReader.cs b/Assets/Scripts/Browser/URLReader.cs new file mode 100644 index 0000000..f32682b --- /dev/null +++ b/Assets/Scripts/Browser/URLReader.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using UnityEngine; + +public class URLReader : MonoBehaviour +{ + [DllImport("__Internal")] + private static extern string GetURLFromPage(); + + [DllImport("__Internal")] + private static extern string GetQueryParam(string paramId); + + public string ReadQueryParam(string paramId) + { + return GetQueryParam(paramId); + } + + public string ReadURL() + { + return GetURLFromPage(); + } +} diff --git a/Assets/Scripts/Browser/URLReader.cs.meta b/Assets/Scripts/Browser/URLReader.cs.meta new file mode 100644 index 0000000..14bf58f --- /dev/null +++ b/Assets/Scripts/Browser/URLReader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1279557eeb86d0549a53072cf7e3eac1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs index b91bf88..ae8ebf2 100644 --- a/Assets/Scripts/Character.cs +++ b/Assets/Scripts/Character.cs @@ -51,7 +51,7 @@ public class Character : MonoBehaviour #region Read Only public Block CurrentBlock { get { return _currentBlock; } } - public float lastRotation { get; private set;} + public float lastRotation; #endregion Read Only #region Unity Functions diff --git a/Assets/Scripts/LevelBlocks/Water.cs b/Assets/Scripts/LevelBlocks/Water.cs index 9b8598d..32e94d4 100644 --- a/Assets/Scripts/LevelBlocks/Water.cs +++ b/Assets/Scripts/LevelBlocks/Water.cs @@ -57,9 +57,10 @@ public class Water : ActiveBlock { //if character is on an angle undo their last rotation - if (trappedCharacter.transform.eulerAngles.y % 90 != 0) + if (trappedCharacter.transform.eulerAngles.y % 90 > 10 && trappedCharacter.transform.eulerAngles.y % 90 < 80) { trappedCharacter.transform.Rotate(Vector3.up, -trappedCharacter.lastRotation); + trappedCharacter.lastRotation = 0; //if they are still on an angle fix to the closest rotation if (trappedCharacter.transform.eulerAngles.y % 90 != 0)