Database Support

Refactored almost everything, but most importantly we now integrate with RethinkDB for game data.
This commit is contained in:
hyperion51 2020-03-22 19:05:26 +00:00
commit 0b46d5ee39
839 changed files with 77349 additions and 112660 deletions

View file

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine;
public static class AwaitExtensions
{
public static TaskAwaiter<int> GetAwaiter(this Process process)
{
var tcs = new TaskCompletionSource<int>();
process.EnableRaisingEvents = true;
process.Exited += (s, e) => tcs.TrySetResult(process.ExitCode);
if (process.HasExited)
{
tcs.TrySetResult(process.ExitCode);
}
return tcs.Task.GetAwaiter();
}
// Any time you call an async method from sync code, you can either use this wrapper
// method or you can define your own `async void` method that performs the await
// on the given Task
public static async void WrapErrors(this Task task)
{
await task;
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 386afd32a400664419ea609f8f1636fb
timeCreated: 1505905492
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,45 @@
using System;
using UnityEngine;
// TODO: Remove the allocs here, use a static memory pool?
public static class Awaiters
{
readonly static WaitForUpdate _waitForUpdate = new WaitForUpdate();
readonly static WaitForFixedUpdate _waitForFixedUpdate = new WaitForFixedUpdate();
readonly static WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame();
public static WaitForUpdate NextFrame
{
get { return _waitForUpdate; }
}
public static WaitForFixedUpdate FixedUpdate
{
get { return _waitForFixedUpdate; }
}
public static WaitForEndOfFrame EndOfFrame
{
get { return _waitForEndOfFrame; }
}
public static WaitForSeconds Seconds(float seconds)
{
return new WaitForSeconds(seconds);
}
public static WaitForSecondsRealtime SecondsRealtime(float seconds)
{
return new WaitForSecondsRealtime(seconds);
}
public static WaitUntil Until(Func<bool> predicate)
{
return new WaitUntil(predicate);
}
public static WaitWhile While(Func<bool> predicate)
{
return new WaitWhile(predicate);
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8d760c2bd4c003740956ef7de48cea40
timeCreated: 1506576813
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,393 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityAsyncAwaitUtil;
// We could just add a generic GetAwaiter to YieldInstruction and CustomYieldInstruction
// but instead we add specific methods to each derived class to allow for return values
// that make the most sense for the specific instruction type
public static class IEnumeratorAwaitExtensions
{
public static SimpleCoroutineAwaiter GetAwaiter(this WaitForSeconds instruction)
{
return GetAwaiterReturnVoid(instruction);
}
public static SimpleCoroutineAwaiter GetAwaiter(this WaitForUpdate instruction)
{
return GetAwaiterReturnVoid(instruction);
}
public static SimpleCoroutineAwaiter GetAwaiter(this WaitForEndOfFrame instruction)
{
return GetAwaiterReturnVoid(instruction);
}
public static SimpleCoroutineAwaiter GetAwaiter(this WaitForFixedUpdate instruction)
{
return GetAwaiterReturnVoid(instruction);
}
public static SimpleCoroutineAwaiter GetAwaiter(this WaitForSecondsRealtime instruction)
{
return GetAwaiterReturnVoid(instruction);
}
public static SimpleCoroutineAwaiter GetAwaiter(this WaitUntil instruction)
{
return GetAwaiterReturnVoid(instruction);
}
public static SimpleCoroutineAwaiter GetAwaiter(this WaitWhile instruction)
{
return GetAwaiterReturnVoid(instruction);
}
public static SimpleCoroutineAwaiter<AsyncOperation> GetAwaiter(this AsyncOperation instruction)
{
return GetAwaiterReturnSelf(instruction);
}
public static SimpleCoroutineAwaiter<UnityEngine.Object> GetAwaiter(this ResourceRequest instruction)
{
var awaiter = new SimpleCoroutineAwaiter<UnityEngine.Object>();
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
InstructionWrappers.ResourceRequest(awaiter, instruction)));
return awaiter;
}
public static SimpleCoroutineAwaiter<AssetBundle> GetAwaiter(this AssetBundleCreateRequest instruction)
{
var awaiter = new SimpleCoroutineAwaiter<AssetBundle>();
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
InstructionWrappers.AssetBundleCreateRequest(awaiter, instruction)));
return awaiter;
}
public static SimpleCoroutineAwaiter<UnityEngine.Object> GetAwaiter(this AssetBundleRequest instruction)
{
var awaiter = new SimpleCoroutineAwaiter<UnityEngine.Object>();
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
InstructionWrappers.AssetBundleRequest(awaiter, instruction)));
return awaiter;
}
public static SimpleCoroutineAwaiter<T> GetAwaiter<T>(this IEnumerator<T> coroutine)
{
var awaiter = new SimpleCoroutineAwaiter<T>();
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
new CoroutineWrapper<T>(coroutine, awaiter).Run()));
return awaiter;
}
public static SimpleCoroutineAwaiter<object> GetAwaiter(this IEnumerator coroutine)
{
var awaiter = new SimpleCoroutineAwaiter<object>();
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
new CoroutineWrapper<object>(coroutine, awaiter).Run()));
return awaiter;
}
static SimpleCoroutineAwaiter GetAwaiterReturnVoid(object instruction)
{
var awaiter = new SimpleCoroutineAwaiter();
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
InstructionWrappers.ReturnVoid(awaiter, instruction)));
return awaiter;
}
static SimpleCoroutineAwaiter<T> GetAwaiterReturnSelf<T>(T instruction)
{
var awaiter = new SimpleCoroutineAwaiter<T>();
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
InstructionWrappers.ReturnSelf(awaiter, instruction)));
return awaiter;
}
static void RunOnUnityScheduler(Action action)
{
if (SynchronizationContext.Current == SyncContextUtil.UnitySynchronizationContext)
{
action();
}
else
{
SyncContextUtil.UnitySynchronizationContext.Post(_ => action(), null);
}
}
static void Assert(bool condition)
{
if (!condition)
{
throw new Exception("Assert hit in UnityAsyncUtil package!");
}
}
public class SimpleCoroutineAwaiter<T> : INotifyCompletion
{
bool _isDone;
Exception _exception;
Action _continuation;
T _result;
public bool IsCompleted
{
get { return _isDone; }
}
public T GetResult()
{
Assert(_isDone);
if (_exception != null)
{
ExceptionDispatchInfo.Capture(_exception).Throw();
}
return _result;
}
public void Complete(T result, Exception e)
{
Assert(!_isDone);
_isDone = true;
_exception = e;
_result = result;
// Always trigger the continuation on the unity thread when awaiting on unity yield
// instructions
if (_continuation != null)
{
RunOnUnityScheduler(_continuation);
}
}
void INotifyCompletion.OnCompleted(Action continuation)
{
Assert(_continuation == null);
Assert(!_isDone);
_continuation = continuation;
}
}
public class SimpleCoroutineAwaiter : INotifyCompletion
{
bool _isDone;
Exception _exception;
Action _continuation;
public bool IsCompleted
{
get { return _isDone; }
}
public void GetResult()
{
Assert(_isDone);
if (_exception != null)
{
ExceptionDispatchInfo.Capture(_exception).Throw();
}
}
public void Complete(Exception e)
{
Assert(!_isDone);
_isDone = true;
_exception = e;
// Always trigger the continuation on the unity thread when awaiting on unity yield
// instructions
if (_continuation != null)
{
RunOnUnityScheduler(_continuation);
}
}
void INotifyCompletion.OnCompleted(Action continuation)
{
Assert(_continuation == null);
Assert(!_isDone);
_continuation = continuation;
}
}
class CoroutineWrapper<T>
{
readonly SimpleCoroutineAwaiter<T> _awaiter;
readonly Stack<IEnumerator> _processStack;
public CoroutineWrapper(
IEnumerator coroutine, SimpleCoroutineAwaiter<T> awaiter)
{
_processStack = new Stack<IEnumerator>();
_processStack.Push(coroutine);
_awaiter = awaiter;
}
public IEnumerator Run()
{
while (true)
{
var topWorker = _processStack.Peek();
bool isDone;
try
{
isDone = !topWorker.MoveNext();
}
catch (Exception e)
{
// The IEnumerators we have in the process stack do not tell us the
// actual names of the coroutine methods but it does tell us the objects
// that the IEnumerators are associated with, so we can at least try
// adding that to the exception output
var objectTrace = GenerateObjectTrace(_processStack);
if (objectTrace.Any())
{
_awaiter.Complete(
default(T), new Exception(
GenerateObjectTraceMessage(objectTrace), e));
}
else
{
_awaiter.Complete(default(T), e);
}
yield break;
}
if (isDone)
{
_processStack.Pop();
if (_processStack.Count == 0)
{
_awaiter.Complete((T)topWorker.Current, null);
yield break;
}
}
// We could just yield return nested IEnumerator's here but we choose to do
// our own handling here so that we can catch exceptions in nested coroutines
// instead of just top level coroutine
if (topWorker.Current is IEnumerator)
{
_processStack.Push((IEnumerator)topWorker.Current);
}
else
{
// Return the current value to the unity engine so it can handle things like
// WaitForSeconds, WaitToEndOfFrame, etc.
yield return topWorker.Current;
}
}
}
string GenerateObjectTraceMessage(List<Type> objTrace)
{
var result = new StringBuilder();
foreach (var objType in objTrace)
{
if (result.Length != 0)
{
result.Append(" -> ");
}
result.Append(objType.ToString());
}
result.AppendLine();
return "Unity Coroutine Object Trace: " + result.ToString();
}
static List<Type> GenerateObjectTrace(IEnumerable<IEnumerator> enumerators)
{
var objTrace = new List<Type>();
foreach (var enumerator in enumerators)
{
// NOTE: This only works with scripting engine 4.6
// And could easily stop working with unity updates
var field = enumerator.GetType().GetField("$this", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (field == null)
{
continue;
}
var obj = field.GetValue(enumerator);
if (obj == null)
{
continue;
}
var objType = obj.GetType();
if (!objTrace.Any() || objType != objTrace.Last())
{
objTrace.Add(objType);
}
}
objTrace.Reverse();
return objTrace;
}
}
static class InstructionWrappers
{
public static IEnumerator ReturnVoid(
SimpleCoroutineAwaiter awaiter, object instruction)
{
// For simple instructions we assume that they don't throw exceptions
yield return instruction;
awaiter.Complete(null);
}
public static IEnumerator AssetBundleCreateRequest(
SimpleCoroutineAwaiter<AssetBundle> awaiter, AssetBundleCreateRequest instruction)
{
yield return instruction;
awaiter.Complete(instruction.assetBundle, null);
}
public static IEnumerator ReturnSelf<T>(
SimpleCoroutineAwaiter<T> awaiter, T instruction)
{
yield return instruction;
awaiter.Complete(instruction, null);
}
public static IEnumerator AssetBundleRequest(
SimpleCoroutineAwaiter<UnityEngine.Object> awaiter, AssetBundleRequest instruction)
{
yield return instruction;
awaiter.Complete(instruction.asset, null);
}
public static IEnumerator ResourceRequest(
SimpleCoroutineAwaiter<UnityEngine.Object> awaiter, ResourceRequest instruction)
{
yield return instruction;
awaiter.Complete(instruction.asset, null);
}
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e25e4bf7a3b2f5f4e82e2de590d2e12a
timeCreated: 1506071089
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 0e49c304a8a375e4fb785571f1b582c2
folderAsset: yes
timeCreated: 1506153419
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,35 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnityAsyncAwaitUtil
{
public class AsyncCoroutineRunner : MonoBehaviour
{
static AsyncCoroutineRunner _instance;
public static AsyncCoroutineRunner Instance
{
get
{
if (_instance == null)
{
_instance = new GameObject("AsyncCoroutineRunner")
.AddComponent<AsyncCoroutineRunner>();
}
return _instance;
}
}
void Awake()
{
// Don't show in scene hierarchy
gameObject.hideFlags = HideFlags.HideAndDontSave;
DontDestroyOnLoad(gameObject);
}
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f11ec4617f62c224b99e34972dc917a8
timeCreated: 1506153419
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace UnityAsyncAwaitUtil
{
public static class SyncContextUtil
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Install()
{
UnitySynchronizationContext = SynchronizationContext.Current;
UnityThreadId = Thread.CurrentThread.ManagedThreadId;
}
public static int UnityThreadId
{
get; private set;
}
public static SynchronizationContext UnitySynchronizationContext
{
get; private set;
}
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: a184433eba65a614191b36436c5620cd
timeCreated: 1506158938
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,40 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using UnityEngine;
public static class TaskExtensions
{
public static IEnumerator AsIEnumerator(this Task task)
{
while (!task.IsCompleted)
{
yield return null;
}
if (task.IsFaulted)
{
ExceptionDispatchInfo.Capture(task.Exception).Throw();
}
}
public static IEnumerator<T> AsIEnumerator<T>(this Task<T> task)
where T : class
{
while (!task.IsCompleted)
{
yield return null;
}
if (task.IsFaulted)
{
ExceptionDispatchInfo.Capture(task.Exception).Throw();
}
yield return task.Result;
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 445df9c15261a3a4e97d1528208902e3
timeCreated: 1506110050
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine;
public class WaitForBackgroundThread
{
public ConfiguredTaskAwaitable.ConfiguredTaskAwaiter GetAwaiter()
{
return Task.Run(() => {}).ConfigureAwait(false).GetAwaiter();
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b49e8af9f6a40874191794f7bae31812
timeCreated: 1506162217
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine;
// This can be used as a way to return to the main unity thread when using multiple threads
// with async methods
public class WaitForUpdate : CustomYieldInstruction
{
public override bool keepWaiting
{
get { return false; }
}
}

View file

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: df6106e9f5d243e449fcef043d2d247a
timeCreated: 1506153732
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: