Initial Client/Server Communication
#1 Followed the MagicOnion quickstart guide, borrowing code from the chat sample app to get something working
This commit is contained in:
parent
52b0d4d8ff
commit
a0c8dbee72
493 changed files with 57790 additions and 3 deletions
8
Assets/Scripts/MessagePack/Unity/Extension.meta
Normal file
8
Assets/Scripts/MessagePack/Unity/Extension.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: da809bc2e70ea85429f483b800355d89
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
114
Assets/Scripts/MessagePack/Unity/Extension/UnityBlitResolver.cs
Normal file
114
Assets/Scripts/MessagePack/Unity/Extension/UnityBlitResolver.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
// Copyright (c) All contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack.Formatters;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MessagePack.Unity.Extension
|
||||
{
|
||||
/// <summary>
|
||||
/// Special Resolver for Vector2[], Vector3[], Vector4[], Quaternion[], Color[], Bounds[], Rect[].
|
||||
/// </summary>
|
||||
public class UnityBlitResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly UnityBlitResolver Instance = new UnityBlitResolver();
|
||||
|
||||
private UnityBlitResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.Formatter;
|
||||
}
|
||||
|
||||
private static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> Formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
Formatter = (IMessagePackFormatter<T>)UnityBlitResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Special Resolver for Vector2[], Vector3[], Vector4[], Quaternion[], Color[], Bounds[], Rect[] + int[], float[], double[].
|
||||
/// </summary>
|
||||
public class UnityBlitWithPrimitiveArrayResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly UnityBlitWithPrimitiveArrayResolver Instance = new UnityBlitWithPrimitiveArrayResolver();
|
||||
|
||||
private UnityBlitWithPrimitiveArrayResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.Formatter;
|
||||
}
|
||||
|
||||
private static class FormatterCache<T>
|
||||
{
|
||||
internal static readonly IMessagePackFormatter<T> Formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
Formatter = (IMessagePackFormatter<T>)UnityBlitWithPrimitiveResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
if (Formatter == null)
|
||||
{
|
||||
Formatter = UnityBlitResolver.Instance.GetFormatter<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class UnityBlitResolverGetFormatterHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, Type> FormatterMap = new Dictionary<Type, Type>()
|
||||
{
|
||||
{ typeof(Vector2[]), typeof(Vector2ArrayBlitFormatter) },
|
||||
{ typeof(Vector3[]), typeof(Vector3ArrayBlitFormatter) },
|
||||
{ typeof(Vector4[]), typeof(Vector4ArrayBlitFormatter) },
|
||||
{ typeof(Quaternion[]), typeof(QuaternionArrayBlitFormatter) },
|
||||
{ typeof(Color[]), typeof(ColorArrayBlitFormatter) },
|
||||
{ typeof(Bounds[]), typeof(BoundsArrayBlitFormatter) },
|
||||
{ typeof(Rect[]), typeof(RectArrayBlitFormatter) },
|
||||
};
|
||||
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
Type formatterType;
|
||||
if (FormatterMap.TryGetValue(t, out formatterType))
|
||||
{
|
||||
return Activator.CreateInstance(formatterType);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class UnityBlitWithPrimitiveResolverGetFormatterHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, Type> FormatterMap = new Dictionary<Type, Type>()
|
||||
{
|
||||
{ typeof(int[]), typeof(IntArrayBlitFormatter) },
|
||||
{ typeof(float[]), typeof(FloatArrayBlitFormatter) },
|
||||
{ typeof(double[]), typeof(DoubleArrayBlitFormatter) },
|
||||
};
|
||||
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
Type formatterType;
|
||||
if (FormatterMap.TryGetValue(t, out formatterType))
|
||||
{
|
||||
return Activator.CreateInstance(formatterType);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ef834ed39aa3bf457860be80e97a5d9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,179 @@
|
|||
// Copyright (c) All contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using MessagePack.Formatters;
|
||||
using UnityEngine;
|
||||
|
||||
#pragma warning disable SA1402 // multiple types in a file
|
||||
#pragma warning disable SA1649 // file name matches type name
|
||||
|
||||
namespace MessagePack.Unity.Extension
|
||||
{
|
||||
// use ext instead of ArrayFormatter to extremely boost up performance.
|
||||
// Layout: [extHeader, byteSize(integer), isLittleEndian(bool), bytes()]
|
||||
// Used Ext:30~36
|
||||
public abstract class UnsafeBlitFormatterBase<T> : IMessagePackFormatter<T[]>
|
||||
where T : struct
|
||||
{
|
||||
protected abstract sbyte TypeCode { get; }
|
||||
|
||||
protected void CopyDeserializeUnsafe(ReadOnlySpan<byte> src, Span<T> dest) => src.CopyTo(MemoryMarshal.Cast<T, byte>(dest));
|
||||
|
||||
public void Serialize(ref MessagePackWriter writer, T[] value, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNil();
|
||||
return;
|
||||
}
|
||||
|
||||
var byteLen = value.Length * Marshal.SizeOf<T>();
|
||||
|
||||
writer.WriteExtensionFormatHeader(new ExtensionHeader(this.TypeCode, byteLen));
|
||||
writer.Write(byteLen); // write original header(not array header)
|
||||
writer.Write(BitConverter.IsLittleEndian);
|
||||
writer.WriteRaw(MemoryMarshal.Cast<T, byte>(value));
|
||||
}
|
||||
|
||||
public T[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ExtensionHeader header = reader.ReadExtensionFormatHeader();
|
||||
if (header.TypeCode != this.TypeCode)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid typeCode.");
|
||||
}
|
||||
|
||||
var byteLength = reader.ReadInt32();
|
||||
var isLittleEndian = reader.ReadBoolean();
|
||||
|
||||
// Allocate a T[] that we will return. We'll then cast the T[] as byte[] so we can copy the byte sequence directly into it.
|
||||
var result = new T[byteLength / Marshal.SizeOf<T>()];
|
||||
Span<byte> resultAsBytes = MemoryMarshal.Cast<T, byte>(result);
|
||||
reader.ReadRaw(byteLength).CopyTo(resultAsBytes);
|
||||
|
||||
// Reverse the byte order if necessary.
|
||||
if (isLittleEndian != BitConverter.IsLittleEndian)
|
||||
{
|
||||
for (int i = 0, j = resultAsBytes.Length - 1; i < j; i++, j--)
|
||||
{
|
||||
byte tmp = resultAsBytes[i];
|
||||
resultAsBytes[i] = resultAsBytes[j];
|
||||
resultAsBytes[j] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector2>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityVector2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector3ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector3>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityVector3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector4ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector4>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityVector4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class QuaternionArrayBlitFormatter : UnsafeBlitFormatterBase<Quaternion>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityQuaternion;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ColorArrayBlitFormatter : UnsafeBlitFormatterBase<Color>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BoundsArrayBlitFormatter : UnsafeBlitFormatterBase<Bounds>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityBounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class RectArrayBlitFormatter : UnsafeBlitFormatterBase<Rect>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get
|
||||
{
|
||||
return ThisLibraryExtensionTypeCodes.UnityRect;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class IntArrayBlitFormatter : UnsafeBlitFormatterBase<int>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get { return ThisLibraryExtensionTypeCodes.UnityInt; }
|
||||
}
|
||||
}
|
||||
|
||||
public class FloatArrayBlitFormatter : UnsafeBlitFormatterBase<float>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get { return ThisLibraryExtensionTypeCodes.UnityFloat; }
|
||||
}
|
||||
}
|
||||
|
||||
public class DoubleArrayBlitFormatter : UnsafeBlitFormatterBase<double>
|
||||
{
|
||||
protected override sbyte TypeCode
|
||||
{
|
||||
get { return ThisLibraryExtensionTypeCodes.UnityDouble; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2955ae5d4f31948548df2c805124f4c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1135
Assets/Scripts/MessagePack/Unity/Formatters.cs
Normal file
1135
Assets/Scripts/MessagePack/Unity/Formatters.cs
Normal file
File diff suppressed because it is too large
Load diff
11
Assets/Scripts/MessagePack/Unity/Formatters.cs.meta
Normal file
11
Assets/Scripts/MessagePack/Unity/Formatters.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bccce8bedf4b84bfd8a5b74edf3f232f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
314
Assets/Scripts/MessagePack/Unity/MessagePackWindow.cs
Normal file
314
Assets/Scripts/MessagePack/Unity/MessagePackWindow.cs
Normal file
|
|
@ -0,0 +1,314 @@
|
|||
#if UNITY_EDITOR
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MessagePack.Unity.Editor
|
||||
{
|
||||
internal class MessagePackWindow : EditorWindow
|
||||
{
|
||||
static MessagePackWindow window;
|
||||
|
||||
bool processInitialized;
|
||||
|
||||
bool isDotnetInstalled;
|
||||
string dotnetVersion;
|
||||
|
||||
bool isInstalledMpc;
|
||||
bool installingMpc;
|
||||
bool invokingMpc;
|
||||
|
||||
MpcArgument mpcArgument;
|
||||
|
||||
[MenuItem("Window/MessagePack/CodeGenerator")]
|
||||
public static void OpenWindow()
|
||||
{
|
||||
if (window != null)
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
|
||||
// will called OnEnable(singleton instance will be set).
|
||||
GetWindow<MessagePackWindow>("MessagePack CodeGen").Show();
|
||||
}
|
||||
|
||||
async void OnEnable()
|
||||
{
|
||||
window = this; // set singleton.
|
||||
try
|
||||
{
|
||||
var dotnet = await ProcessHelper.FindDotnetAsync();
|
||||
isDotnetInstalled = dotnet.found;
|
||||
dotnetVersion = dotnet.version;
|
||||
|
||||
if (isDotnetInstalled)
|
||||
{
|
||||
isInstalledMpc = await ProcessHelper.IsInstalledMpc();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
mpcArgument = MpcArgument.Restore();
|
||||
processInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
async void OnGUI()
|
||||
{
|
||||
if (!processInitialized)
|
||||
{
|
||||
GUILayout.Label("Check .NET Core SDK/CodeGen install status.");
|
||||
return;
|
||||
}
|
||||
if (mpcArgument == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isDotnetInstalled)
|
||||
{
|
||||
GUILayout.Label(".NET Core SDK not found.");
|
||||
GUILayout.Label("MessagePack CodeGen requires .NET Core Runtime.");
|
||||
if (GUILayout.Button("Open .NET Core install page."))
|
||||
{
|
||||
Application.OpenURL("https://dotnet.microsoft.com/download");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isInstalledMpc)
|
||||
{
|
||||
GUILayout.Label("MessagePack CodeGen does not instaled.");
|
||||
EditorGUI.BeginDisabledGroup(installingMpc);
|
||||
|
||||
if (GUILayout.Button("Install MessagePack CodeGen."))
|
||||
{
|
||||
installingMpc = true;
|
||||
try
|
||||
{
|
||||
var log = await ProcessHelper.InstallMpc();
|
||||
if (!string.IsNullOrWhiteSpace(log))
|
||||
{
|
||||
UnityEngine.Debug.Log(log);
|
||||
}
|
||||
if (log != null && log.Contains("error"))
|
||||
{
|
||||
isInstalledMpc = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
isInstalledMpc = true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
installingMpc = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
EditorGUI.EndDisabledGroup();
|
||||
return;
|
||||
}
|
||||
|
||||
EditorGUILayout.LabelField("-i input path(csproj or directory):");
|
||||
TextField(mpcArgument, x => x.Input, (x, y) => x.Input = y);
|
||||
|
||||
EditorGUILayout.LabelField("-o output filepath(.cs) or directory(multiple):");
|
||||
TextField(mpcArgument, x => x.Output, (x, y) => x.Output = y);
|
||||
|
||||
EditorGUILayout.LabelField("-m(optional) use map mode:");
|
||||
var newToggle = EditorGUILayout.Toggle(mpcArgument.UseMapMode);
|
||||
if (mpcArgument.UseMapMode != newToggle)
|
||||
{
|
||||
mpcArgument.UseMapMode = newToggle;
|
||||
mpcArgument.Save();
|
||||
}
|
||||
|
||||
EditorGUILayout.LabelField("-c(optional) conditional compiler symbols(split with ','):");
|
||||
TextField(mpcArgument, x => x.ConditionalSymbol, (x, y) => x.ConditionalSymbol = y);
|
||||
|
||||
EditorGUILayout.LabelField("-r(optional) generated resolver name:");
|
||||
TextField(mpcArgument, x => x.ResolverName, (x, y) => x.ResolverName = y);
|
||||
|
||||
EditorGUILayout.LabelField("-n(optional) namespace root name:");
|
||||
TextField(mpcArgument, x => x.Namespace, (x, y) => x.Namespace = y);
|
||||
|
||||
EditorGUILayout.LabelField("-ms(optional) Generate #if-- files by symbols, split with ','");
|
||||
TextField(mpcArgument, x => x.MultipleIfDirectiveOutputSymbols, (x, y) => x.MultipleIfDirectiveOutputSymbols = y);
|
||||
|
||||
EditorGUI.BeginDisabledGroup(invokingMpc);
|
||||
if (GUILayout.Button("Generate"))
|
||||
{
|
||||
var commnadLineArguments = mpcArgument.ToString();
|
||||
UnityEngine.Debug.Log("Generate MessagePack Files, command:" + commnadLineArguments);
|
||||
|
||||
invokingMpc = true;
|
||||
try
|
||||
{
|
||||
var log = await ProcessHelper.InvokeProcessStartAsync("dotnet", "mpc " + commnadLineArguments);
|
||||
UnityEngine.Debug.Log(log);
|
||||
}
|
||||
finally
|
||||
{
|
||||
invokingMpc = false;
|
||||
}
|
||||
}
|
||||
EditorGUI.EndDisabledGroup();
|
||||
}
|
||||
|
||||
void TextField(MpcArgument args, Func<MpcArgument, string> getter, Action<MpcArgument, string> setter)
|
||||
{
|
||||
var current = getter(args);
|
||||
var newValue = EditorGUILayout.TextField(current);
|
||||
if (newValue != current)
|
||||
{
|
||||
setter(args, newValue);
|
||||
args.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class MpcArgument
|
||||
{
|
||||
public string Input;
|
||||
public string Output;
|
||||
public string ConditionalSymbol;
|
||||
public string ResolverName;
|
||||
public string Namespace;
|
||||
public bool UseMapMode;
|
||||
public string MultipleIfDirectiveOutputSymbols;
|
||||
|
||||
static string Key => "MessagePackCodeGen." + Application.productName;
|
||||
|
||||
public static MpcArgument Restore()
|
||||
{
|
||||
if (EditorPrefs.HasKey(Key))
|
||||
{
|
||||
var json = EditorPrefs.GetString(Key);
|
||||
return JsonUtility.FromJson<MpcArgument>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new MpcArgument();
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var json = JsonUtility.ToJson(this);
|
||||
EditorPrefs.SetString(Key, json);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("-i "); sb.Append(Input);
|
||||
sb.Append(" -o "); sb.Append(Output);
|
||||
if (!string.IsNullOrWhiteSpace(ConditionalSymbol))
|
||||
{
|
||||
sb.Append(" -c "); sb.Append(ConditionalSymbol);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(ResolverName))
|
||||
{
|
||||
sb.Append(" -r "); sb.Append(ResolverName);
|
||||
}
|
||||
if (UseMapMode)
|
||||
{
|
||||
sb.Append(" -m");
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(Namespace))
|
||||
{
|
||||
sb.Append(" -n "); sb.Append(Namespace);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(MultipleIfDirectiveOutputSymbols))
|
||||
{
|
||||
sb.Append(" -ms "); sb.Append(MultipleIfDirectiveOutputSymbols);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
internal static class ProcessHelper
|
||||
{
|
||||
const string InstallName = "messagepack.generator";
|
||||
|
||||
public static async Task<bool> IsInstalledMpc()
|
||||
{
|
||||
var list = await InvokeProcessStartAsync("dotnet", "tool list -g");
|
||||
if (list.Contains(InstallName))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> InstallMpc()
|
||||
{
|
||||
return await InvokeProcessStartAsync("dotnet", "tool install --global " + InstallName);
|
||||
}
|
||||
|
||||
public static async Task<(bool found, string version)> FindDotnetAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var version = await InvokeProcessStartAsync("dotnet", "--version");
|
||||
return (true, version);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return (false, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static Task<string> InvokeProcessStartAsync(string fileName, string arguments)
|
||||
{
|
||||
var psi = new ProcessStartInfo()
|
||||
{
|
||||
CreateNoWindow = true,
|
||||
WindowStyle = ProcessWindowStyle.Hidden,
|
||||
StandardOutputEncoding = Encoding.UTF8,
|
||||
StandardErrorEncoding = Encoding.UTF8,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
FileName = fileName,
|
||||
Arguments = arguments,
|
||||
WorkingDirectory = Application.dataPath
|
||||
};
|
||||
|
||||
Process p;
|
||||
try
|
||||
{
|
||||
p = Process.Start(psi);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Task.FromException<string>(ex);
|
||||
}
|
||||
|
||||
var tcs = new TaskCompletionSource<string>();
|
||||
p.EnableRaisingEvents = true;
|
||||
p.Exited += (object sender, System.EventArgs e) =>
|
||||
{
|
||||
var data = p.StandardOutput.ReadToEnd();
|
||||
p.Dispose();
|
||||
p = null;
|
||||
|
||||
tcs.TrySetResult(data);
|
||||
};
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
11
Assets/Scripts/MessagePack/Unity/MessagePackWindow.cs.meta
Normal file
11
Assets/Scripts/MessagePack/Unity/MessagePackWindow.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4e1974f6daf2611d0b8a78fe6230f4a8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
200
Assets/Scripts/MessagePack/Unity/UnityResolver.cs
Normal file
200
Assets/Scripts/MessagePack/Unity/UnityResolver.cs
Normal file
|
|
@ -0,0 +1,200 @@
|
|||
// Copyright (c) All contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack.Formatters;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MessagePack.Unity
|
||||
{
|
||||
public class UnityResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly UnityResolver Instance = new UnityResolver();
|
||||
|
||||
private UnityResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.Formatter;
|
||||
}
|
||||
|
||||
private static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> Formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
Formatter = (IMessagePackFormatter<T>)UnityResolveryResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class UnityResolveryResolverGetFormatterHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, object> FormatterMap = new Dictionary<Type, object>()
|
||||
{
|
||||
// standard
|
||||
{ typeof(Vector2), new Vector2Formatter() },
|
||||
{ typeof(Vector3), new Vector3Formatter() },
|
||||
{ typeof(Vector4), new Vector4Formatter() },
|
||||
{ typeof(Quaternion), new QuaternionFormatter() },
|
||||
{ typeof(Color), new ColorFormatter() },
|
||||
{ typeof(Bounds), new BoundsFormatter() },
|
||||
{ typeof(Rect), new RectFormatter() },
|
||||
{ typeof(Vector2?), new StaticNullableFormatter<Vector2>(new Vector2Formatter()) },
|
||||
{ typeof(Vector3?), new StaticNullableFormatter<Vector3>(new Vector3Formatter()) },
|
||||
{ typeof(Vector4?), new StaticNullableFormatter<Vector4>(new Vector4Formatter()) },
|
||||
{ typeof(Quaternion?), new StaticNullableFormatter<Quaternion>(new QuaternionFormatter()) },
|
||||
{ typeof(Color?), new StaticNullableFormatter<Color>(new ColorFormatter()) },
|
||||
{ typeof(Bounds?), new StaticNullableFormatter<Bounds>(new BoundsFormatter()) },
|
||||
{ typeof(Rect?), new StaticNullableFormatter<Rect>(new RectFormatter()) },
|
||||
|
||||
// standard + array
|
||||
{ typeof(Vector2[]), new ArrayFormatter<Vector2>() },
|
||||
{ typeof(Vector3[]), new ArrayFormatter<Vector3>() },
|
||||
{ typeof(Vector4[]), new ArrayFormatter<Vector4>() },
|
||||
{ typeof(Quaternion[]), new ArrayFormatter<Quaternion>() },
|
||||
{ typeof(Color[]), new ArrayFormatter<Color>() },
|
||||
{ typeof(Bounds[]), new ArrayFormatter<Bounds>() },
|
||||
{ typeof(Rect[]), new ArrayFormatter<Rect>() },
|
||||
{ typeof(Vector2?[]), new ArrayFormatter<Vector2?>() },
|
||||
{ typeof(Vector3?[]), new ArrayFormatter<Vector3?>() },
|
||||
{ typeof(Vector4?[]), new ArrayFormatter<Vector4?>() },
|
||||
{ typeof(Quaternion?[]), new ArrayFormatter<Quaternion?>() },
|
||||
{ typeof(Color?[]), new ArrayFormatter<Color?>() },
|
||||
{ typeof(Bounds?[]), new ArrayFormatter<Bounds?>() },
|
||||
{ typeof(Rect?[]), new ArrayFormatter<Rect?>() },
|
||||
|
||||
// standard + list
|
||||
{ typeof(List<Vector2>), new ListFormatter<Vector2>() },
|
||||
{ typeof(List<Vector3>), new ListFormatter<Vector3>() },
|
||||
{ typeof(List<Vector4>), new ListFormatter<Vector4>() },
|
||||
{ typeof(List<Quaternion>), new ListFormatter<Quaternion>() },
|
||||
{ typeof(List<Color>), new ListFormatter<Color>() },
|
||||
{ typeof(List<Bounds>), new ListFormatter<Bounds>() },
|
||||
{ typeof(List<Rect>), new ListFormatter<Rect>() },
|
||||
{ typeof(List<Vector2?>), new ListFormatter<Vector2?>() },
|
||||
{ typeof(List<Vector3?>), new ListFormatter<Vector3?>() },
|
||||
{ typeof(List<Vector4?>), new ListFormatter<Vector4?>() },
|
||||
{ typeof(List<Quaternion?>), new ListFormatter<Quaternion?>() },
|
||||
{ typeof(List<Color?>), new ListFormatter<Color?>() },
|
||||
{ typeof(List<Bounds?>), new ListFormatter<Bounds?>() },
|
||||
{ typeof(List<Rect?>), new ListFormatter<Rect?>() },
|
||||
|
||||
// new
|
||||
{ typeof(AnimationCurve), new AnimationCurveFormatter() },
|
||||
{ typeof(RectOffset), new RectOffsetFormatter() },
|
||||
{ typeof(Gradient), new GradientFormatter() },
|
||||
{ typeof(WrapMode), new WrapModeFormatter() },
|
||||
{ typeof(GradientMode), new GradientModeFormatter() },
|
||||
{ typeof(Keyframe), new KeyframeFormatter() },
|
||||
{ typeof(Matrix4x4), new Matrix4x4Formatter() },
|
||||
{ typeof(GradientColorKey), new GradientColorKeyFormatter() },
|
||||
{ typeof(GradientAlphaKey), new GradientAlphaKeyFormatter() },
|
||||
{ typeof(Color32), new Color32Formatter() },
|
||||
{ typeof(LayerMask), new LayerMaskFormatter() },
|
||||
{ typeof(WrapMode?), new StaticNullableFormatter<WrapMode>(new WrapModeFormatter()) },
|
||||
{ typeof(GradientMode?), new StaticNullableFormatter<GradientMode>(new GradientModeFormatter()) },
|
||||
{ typeof(Keyframe?), new StaticNullableFormatter<Keyframe>(new KeyframeFormatter()) },
|
||||
{ typeof(Matrix4x4?), new StaticNullableFormatter<Matrix4x4>(new Matrix4x4Formatter()) },
|
||||
{ typeof(GradientColorKey?), new StaticNullableFormatter<GradientColorKey>(new GradientColorKeyFormatter()) },
|
||||
{ typeof(GradientAlphaKey?), new StaticNullableFormatter<GradientAlphaKey>(new GradientAlphaKeyFormatter()) },
|
||||
{ typeof(Color32?), new StaticNullableFormatter<Color32>(new Color32Formatter()) },
|
||||
{ typeof(LayerMask?), new StaticNullableFormatter<LayerMask>(new LayerMaskFormatter()) },
|
||||
|
||||
// new + array
|
||||
{ typeof(AnimationCurve[]), new ArrayFormatter<AnimationCurve>() },
|
||||
{ typeof(RectOffset[]), new ArrayFormatter<RectOffset>() },
|
||||
{ typeof(Gradient[]), new ArrayFormatter<Gradient>() },
|
||||
{ typeof(WrapMode[]), new ArrayFormatter<WrapMode>() },
|
||||
{ typeof(GradientMode[]), new ArrayFormatter<GradientMode>() },
|
||||
{ typeof(Keyframe[]), new ArrayFormatter<Keyframe>() },
|
||||
{ typeof(Matrix4x4[]), new ArrayFormatter<Matrix4x4>() },
|
||||
{ typeof(GradientColorKey[]), new ArrayFormatter<GradientColorKey>() },
|
||||
{ typeof(GradientAlphaKey[]), new ArrayFormatter<GradientAlphaKey>() },
|
||||
{ typeof(Color32[]), new ArrayFormatter<Color32>() },
|
||||
{ typeof(LayerMask[]), new ArrayFormatter<LayerMask>() },
|
||||
{ typeof(WrapMode?[]), new ArrayFormatter<WrapMode?>() },
|
||||
{ typeof(GradientMode?[]), new ArrayFormatter<GradientMode?>() },
|
||||
{ typeof(Keyframe?[]), new ArrayFormatter<Keyframe?>() },
|
||||
{ typeof(Matrix4x4?[]), new ArrayFormatter<Matrix4x4?>() },
|
||||
{ typeof(GradientColorKey?[]), new ArrayFormatter<GradientColorKey?>() },
|
||||
{ typeof(GradientAlphaKey?[]), new ArrayFormatter<GradientAlphaKey?>() },
|
||||
{ typeof(Color32?[]), new ArrayFormatter<Color32?>() },
|
||||
{ typeof(LayerMask?[]), new ArrayFormatter<LayerMask?>() },
|
||||
|
||||
// new + list
|
||||
{ typeof(List<AnimationCurve>), new ListFormatter<AnimationCurve>() },
|
||||
{ typeof(List<RectOffset>), new ListFormatter<RectOffset>() },
|
||||
{ typeof(List<Gradient>), new ListFormatter<Gradient>() },
|
||||
{ typeof(List<WrapMode>), new ListFormatter<WrapMode>() },
|
||||
{ typeof(List<GradientMode>), new ListFormatter<GradientMode>() },
|
||||
{ typeof(List<Keyframe>), new ListFormatter<Keyframe>() },
|
||||
{ typeof(List<Matrix4x4>), new ListFormatter<Matrix4x4>() },
|
||||
{ typeof(List<GradientColorKey>), new ListFormatter<GradientColorKey>() },
|
||||
{ typeof(List<GradientAlphaKey>), new ListFormatter<GradientAlphaKey>() },
|
||||
{ typeof(List<Color32>), new ListFormatter<Color32>() },
|
||||
{ typeof(List<LayerMask>), new ListFormatter<LayerMask>() },
|
||||
{ typeof(List<WrapMode?>), new ListFormatter<WrapMode?>() },
|
||||
{ typeof(List<GradientMode?>), new ListFormatter<GradientMode?>() },
|
||||
{ typeof(List<Keyframe?>), new ListFormatter<Keyframe?>() },
|
||||
{ typeof(List<Matrix4x4?>), new ListFormatter<Matrix4x4?>() },
|
||||
{ typeof(List<GradientColorKey?>), new ListFormatter<GradientColorKey?>() },
|
||||
{ typeof(List<GradientAlphaKey?>), new ListFormatter<GradientAlphaKey?>() },
|
||||
{ typeof(List<Color32?>), new ListFormatter<Color32?>() },
|
||||
{ typeof(List<LayerMask?>), new ListFormatter<LayerMask?>() },
|
||||
|
||||
// unity 2017.2
|
||||
#if UNITY_2017_2_OR_NEWER
|
||||
|
||||
{typeof(Vector2Int), new Vector2IntFormatter()},
|
||||
{typeof(Vector3Int), new Vector3IntFormatter()},
|
||||
{typeof(RangeInt), new RangeIntFormatter()},
|
||||
{typeof(RectInt), new RectIntFormatter()},
|
||||
{typeof(BoundsInt), new BoundsIntFormatter()},
|
||||
{typeof(Vector2Int?), new StaticNullableFormatter<Vector2Int>(new Vector2IntFormatter())},
|
||||
{typeof(Vector3Int?), new StaticNullableFormatter<Vector3Int>(new Vector3IntFormatter())},
|
||||
{typeof(RangeInt?), new StaticNullableFormatter<RangeInt>(new RangeIntFormatter())},
|
||||
{typeof(RectInt?), new StaticNullableFormatter<RectInt>(new RectIntFormatter())},
|
||||
{typeof(BoundsInt?), new StaticNullableFormatter<BoundsInt>(new BoundsIntFormatter())},
|
||||
// unity 2017.2 + array
|
||||
{typeof(Vector2Int[]), new ArrayFormatter<Vector2Int>()},
|
||||
{typeof(Vector3Int[]), new ArrayFormatter<Vector3Int>()},
|
||||
{typeof(RangeInt[]), new ArrayFormatter<RangeInt>()},
|
||||
{typeof(RectInt[]), new ArrayFormatter<RectInt>()},
|
||||
{typeof(BoundsInt[]), new ArrayFormatter<BoundsInt>()},
|
||||
{typeof(Vector2Int?[]), new ArrayFormatter<Vector2Int?>()},
|
||||
{typeof(Vector3Int?[]), new ArrayFormatter<Vector3Int?>()},
|
||||
{typeof(RangeInt?[]), new ArrayFormatter<RangeInt?>()},
|
||||
{typeof(RectInt?[]), new ArrayFormatter<RectInt?>()},
|
||||
{typeof(BoundsInt?[]), new ArrayFormatter<BoundsInt?>()},
|
||||
// unity 2017.2 + list
|
||||
{typeof(List<Vector2Int>), new ListFormatter<Vector2Int>()},
|
||||
{typeof(List<Vector3Int>), new ListFormatter<Vector3Int>()},
|
||||
{typeof(List<RangeInt>), new ListFormatter<RangeInt>()},
|
||||
{typeof(List<RectInt>), new ListFormatter<RectInt>()},
|
||||
{typeof(List<BoundsInt>), new ListFormatter<BoundsInt>()},
|
||||
{typeof(List<Vector2Int?>), new ListFormatter<Vector2Int?>()},
|
||||
{typeof(List<Vector3Int?>), new ListFormatter<Vector3Int?>()},
|
||||
{typeof(List<RangeInt?>), new ListFormatter<RangeInt?>()},
|
||||
{typeof(List<RectInt?>), new ListFormatter<RectInt?>()},
|
||||
{typeof(List<BoundsInt?>), new ListFormatter<BoundsInt?>()},
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
object formatter;
|
||||
if (FormatterMap.TryGetValue(t, out formatter))
|
||||
{
|
||||
return formatter;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/MessagePack/Unity/UnityResolver.cs.meta
Normal file
11
Assets/Scripts/MessagePack/Unity/UnityResolver.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a3b64d9daec5fd9b985abcb65adcf699
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue