v0.14.6.0
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public static class CrossThread
|
||||
{
|
||||
public delegate void TaskDelegate();
|
||||
|
||||
private class Task
|
||||
{
|
||||
public TaskDelegate Deleg;
|
||||
public ManualResetEvent Mre;
|
||||
public bool Done;
|
||||
|
||||
public Task(TaskDelegate d)
|
||||
{
|
||||
Deleg = d;
|
||||
Mre = new ManualResetEvent(false);
|
||||
Done = false;
|
||||
}
|
||||
|
||||
public void PerformWait()
|
||||
{
|
||||
if (!Done) { Mre.WaitOne(); }
|
||||
}
|
||||
}
|
||||
private static List<Task> enqueuedTasks;
|
||||
|
||||
static CrossThread() { enqueuedTasks = new List<Task>(); }
|
||||
|
||||
public static void ProcessTasks()
|
||||
{
|
||||
lock (enqueuedTasks)
|
||||
{
|
||||
foreach (var task in enqueuedTasks)
|
||||
{
|
||||
task.Deleg();
|
||||
task.Mre.Set();
|
||||
task.Done = true;
|
||||
}
|
||||
enqueuedTasks.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static void RequestExecutionOnMainThread(TaskDelegate deleg)
|
||||
{
|
||||
if (GameMain.MainThread == null || Thread.CurrentThread == GameMain.MainThread)
|
||||
{
|
||||
deleg();
|
||||
}
|
||||
else
|
||||
{
|
||||
Task newTask = new Task(deleg);
|
||||
lock (enqueuedTasks)
|
||||
{
|
||||
enqueuedTasks.Add(newTask);
|
||||
}
|
||||
newTask.PerformWait();
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOnMainThread<T>(this List<T> list, T element)
|
||||
{
|
||||
RequestExecutionOnMainThread(() => { list.Add(element); });
|
||||
}
|
||||
|
||||
public static void AddRangeOnMainThread<T>(this List<T> list, IEnumerable<T> elements)
|
||||
{
|
||||
RequestExecutionOnMainThread(() => { list.AddRange(elements); });
|
||||
}
|
||||
|
||||
public static void RemoveOnMainThread<T>(this List<T> list, T element)
|
||||
{
|
||||
RequestExecutionOnMainThread(() => { list.Remove(element); });
|
||||
}
|
||||
|
||||
public static void ClearOnMainThread<T>(this List<T> list)
|
||||
{
|
||||
RequestExecutionOnMainThread(() => { list.Clear(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public abstract class Either<T, U>
|
||||
{
|
||||
public static implicit operator Either<T, U>(T t) => new EitherT<T, U>(t);
|
||||
public static implicit operator Either<T, U>(U u) => new EitherU<T, U>(u);
|
||||
|
||||
public static explicit operator T(Either<T, U> e) => e.TryGet(out T t) ? t : throw new InvalidCastException($"Contained object is not of type {typeof(T).Name}");
|
||||
public static explicit operator U(Either<T, U> e) => e.TryGet(out U u) ? u : throw new InvalidCastException($"Contained object is not of type {typeof(U).Name}");
|
||||
|
||||
public abstract bool TryGet(out T t);
|
||||
public abstract bool TryGet(out U u);
|
||||
|
||||
public abstract bool TryCast<V>(out V v);
|
||||
|
||||
public abstract override string ToString();
|
||||
}
|
||||
|
||||
public sealed class EitherT<T, U> : Either<T, U>
|
||||
{
|
||||
public readonly T Value;
|
||||
|
||||
public EitherT(T value) { Value = value; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString();
|
||||
}
|
||||
|
||||
public override bool TryGet(out T t) { t = Value; return true; }
|
||||
public override bool TryGet(out U u) { u = default(U); return false; }
|
||||
|
||||
public override bool TryCast<V>(out V v)
|
||||
{
|
||||
if (Value is V result) { v = result; return true; }
|
||||
else { v = default(V); return false; }
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class EitherU<T, U> : Either<T, U>
|
||||
{
|
||||
public readonly U Value;
|
||||
|
||||
public EitherU(U value) { Value = value; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString();
|
||||
}
|
||||
|
||||
public override bool TryGet(out T t) { t = default(T); return false; }
|
||||
public override bool TryGet(out U u) { u = Value; return true; }
|
||||
|
||||
public override bool TryCast<V>(out V v)
|
||||
{
|
||||
if (Value is V result) { v = result; return true; }
|
||||
else { v = default(V); return false; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ namespace Barotrauma
|
||||
sanitizedText = text;
|
||||
if (!string.IsNullOrEmpty(text) && text.Contains(definitionIndicator))
|
||||
{
|
||||
text = text.Replace("\r", "");
|
||||
string[] segments = text.Split(definitionIndicator);
|
||||
|
||||
sanitizedText = string.Empty;
|
||||
|
||||
@@ -391,7 +391,7 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
private static Dictionary<string, List<string>> cachedLines = new Dictionary<string, List<string>>();
|
||||
public static string GetRandomLine(string filePath)
|
||||
public static string GetRandomLine(string filePath, Rand.RandSync randSync = Rand.RandSync.Server)
|
||||
{
|
||||
List<string> lines;
|
||||
if (cachedLines.ContainsKey(filePath))
|
||||
@@ -418,7 +418,7 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
if (lines.Count == 0) return "";
|
||||
return lines[Rand.Range(0, lines.Count, Rand.RandSync.Server)];
|
||||
return lines[Rand.Range(0, lines.Count, randSync)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user