Merge branch 'main' of https://github.com/Derek-R-S/Light-Reflective-Mirror into main
This commit is contained in:
commit
f31bdad4c8
2 changed files with 46 additions and 8 deletions
|
|
@ -156,4 +156,43 @@ namespace LightReflectiveMirror
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class JsonUtilityHelper
|
||||
{
|
||||
public static bool IsJsonArray(string json)
|
||||
{
|
||||
return json.StartsWith("[") && json.EndsWith("]");
|
||||
}
|
||||
|
||||
public static T[] FromJson<T>(string json)
|
||||
{
|
||||
if (!IsJsonArray(json))
|
||||
{
|
||||
throw new System.FormatException("The input json string is not a Json Array");
|
||||
}
|
||||
json = "{\"Items\":" + json + "}";
|
||||
JsonWrapper<T> wrapper = JsonUtility.FromJson<JsonWrapper<T>>(json);
|
||||
return wrapper.Items;
|
||||
}
|
||||
|
||||
public static string ToJson<T>(T[] array)
|
||||
{
|
||||
JsonWrapper<T> wrapper = new JsonWrapper<T>();
|
||||
wrapper.Items = array;
|
||||
return JsonUtility.ToJson(wrapper);
|
||||
}
|
||||
|
||||
public static string ToJson<T>(T[] array, bool prettyPrint)
|
||||
{
|
||||
JsonWrapper<T> wrapper = new JsonWrapper<T>();
|
||||
wrapper.Items = array;
|
||||
return JsonUtility.ToJson(wrapper, prettyPrint);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
private class JsonWrapper<T>
|
||||
{
|
||||
public T[] Items;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
using Mirror;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
|
|
@ -41,9 +41,8 @@ namespace LightReflectiveMirror
|
|||
case UnityWebRequest.Result.ProtocolError:
|
||||
Debug.LogWarning("LRM | Network Error while getting a relay to join from Load Balancer.");
|
||||
break;
|
||||
|
||||
case UnityWebRequest.Result.Success:
|
||||
var parsedAddress = JsonConvert.DeserializeObject<RelayAddress>(result);
|
||||
var parsedAddress = JsonUtility.FromJson<RelayAddress>(result.Decompress());
|
||||
Connect(parsedAddress.Address, parsedAddress.Port);
|
||||
endpointServerPort = parsedAddress.EndpointPort;
|
||||
break;
|
||||
|
|
@ -56,7 +55,7 @@ namespace LightReflectiveMirror
|
|||
else
|
||||
{
|
||||
// join here
|
||||
var parsedAddress = JsonConvert.DeserializeObject<RelayAddress>(result);
|
||||
var parsedAddress = JsonUtility.FromJson<RelayAddress>(result.Decompress());
|
||||
Connect(parsedAddress.Address, parsedAddress.Port);
|
||||
endpointServerPort = parsedAddress.EndpointPort;
|
||||
}
|
||||
|
|
@ -148,7 +147,7 @@ namespace LightReflectiveMirror
|
|||
|
||||
case UnityWebRequest.Result.Success:
|
||||
relayServerList?.Clear();
|
||||
relayServerList = JsonConvert.DeserializeObject<List<Room>>(result.Decompress());
|
||||
relayServerList = JsonUtilityHelper.FromJson<Room>(result.Decompress()).ToList();
|
||||
serverListUpdated?.Invoke();
|
||||
break;
|
||||
}
|
||||
|
|
@ -160,7 +159,7 @@ namespace LightReflectiveMirror
|
|||
else
|
||||
{
|
||||
relayServerList?.Clear();
|
||||
relayServerList = JsonConvert.DeserializeObject<List<Room>>(result.Decompress());
|
||||
relayServerList = JsonUtilityHelper.FromJson<Room>(result.Decompress()).ToList();
|
||||
serverListUpdated?.Invoke();
|
||||
}
|
||||
#endif
|
||||
|
|
@ -201,7 +200,7 @@ namespace LightReflectiveMirror
|
|||
|
||||
case UnityWebRequest.Result.Success:
|
||||
relayServerList?.Clear();
|
||||
relayServerList = JsonConvert.DeserializeObject<List<Room>>(result);
|
||||
relayServerList = JsonUtilityHelper.FromJson<Room>(result.Decompress()).ToList();
|
||||
serverListUpdated?.Invoke();
|
||||
_serverListUpdated = true;
|
||||
break;
|
||||
|
|
@ -214,7 +213,7 @@ namespace LightReflectiveMirror
|
|||
else
|
||||
{
|
||||
relayServerList?.Clear();
|
||||
relayServerList = JsonConvert.DeserializeObject<List<Room>>(result);
|
||||
relayServerList = JsonUtilityHelper.FromJson<Room>(result.Decompress()).ToList();
|
||||
serverListUpdated?.Invoke();
|
||||
_serverListUpdated = true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue