Browse Source

Got webGL build working, some bugs still

master
Joshua Reason 4 years ago
parent
commit
e80bfafbe2
11 changed files with 414 additions and 7 deletions
  1. +2
    -2
      Assets/Data/Networking/Server/Realtime/Server.asset
  2. +2
    -2
      Assets/Scenes/Menus/Lobby.unity
  3. +1
    -1
      Assets/Scripts/Browser.meta
  4. +120
    -0
      Assets/Scripts/Browser/BrowserServer.cs
  5. +11
    -0
      Assets/Scripts/Browser/BrowserServer.cs.meta
  6. +230
    -0
      Assets/Scripts/Browser/SimpleHttpsServer.cs
  7. +11
    -0
      Assets/Scripts/Browser/SimpleHttpsServer.cs.meta
  8. +23
    -0
      Assets/Scripts/Browser/URLReader.cs
  9. +11
    -0
      Assets/Scripts/Browser/URLReader.cs.meta
  10. +1
    -1
      Assets/Scripts/Character.cs
  11. +2
    -1
      Assets/Scripts/LevelBlocks/Water.cs

+ 2
- 2
Assets/Data/Networking/Server/Realtime/Server.asset View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b79400c63f907550e1cd21c157f2745ed5a483daf6f454465e332def68b794de
size 489
oid sha256:6d7b72510c56d1d1adc4dc2473034b884e8b831978ca1efc7ad50b536157b696
size 508

+ 2
- 2
Assets/Scenes/Menus/Lobby.unity View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2884d7c10378352a9a3863aa21382541b6fb45a0d4b69c9c59ca99352d6555f8
size 45893
oid sha256:1a5f27543fe7238c6f5003414611a9d25367d3b80ef34dd6f1e09c8f59375984
size 46346

Assets/Plugins/IngameDebugConsole/Sprites.meta → Assets/Scripts/Browser.meta View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: cb5d7b23a9e684a41a6a5d4f300eb1e6
guid: 0f8c61b1677edbf4c8a8b1e8813346ed
folderAsset: yes
DefaultImporter:
externalObjects: {}

+ 120
- 0
Assets/Scripts/Browser/BrowserServer.cs View File

@ -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 = "<html><body><h1>Error Connecting to server</h1></body></html>";
// 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 = "<html><body><h1>Error Connecting to server</h1></body></html>";
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();
}
}

+ 11
- 0
Assets/Scripts/Browser/BrowserServer.cs.meta View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1a34f2d03e796714a93c549ae4c4513c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

+ 230
- 0
Assets/Scripts/Browser/SimpleHttpsServer.cs View File

@ -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<string, string> _mimeTypeMappings = new Dictionary<string, string>(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 { }
}
/// <summary>
/// Construct server with given port.
/// </summary>
/// <param name="path">Directory path to serve.</param>
/// <param name="port">Port of the server.</param>
public SimpleHTTPServer(string path, int port)
{
this.Initialize(path, port);
}
/// <summary>
/// Construct server with suitable port.
/// </summary>
/// <param name="path">Directory path to serve.</param>
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);
}
/// <summary>
/// Stop server and dispose all functions.
/// </summary>
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();
}
}

+ 11
- 0
Assets/Scripts/Browser/SimpleHttpsServer.cs.meta View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ae836f4db7ac22f4db15d0f174d03e73
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

+ 23
- 0
Assets/Scripts/Browser/URLReader.cs View File

@ -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();
}
}

+ 11
- 0
Assets/Scripts/Browser/URLReader.cs.meta View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1279557eeb86d0549a53072cf7e3eac1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

+ 1
- 1
Assets/Scripts/Character.cs View File

@ -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

+ 2
- 1
Assets/Scripts/LevelBlocks/Water.cs View File

@ -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)

Loading…
Cancel
Save