(3a5d98b) v0.9.6.0
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public static class TaskPool
|
||||
{
|
||||
private struct TaskAction
|
||||
{
|
||||
public Task Task;
|
||||
public Action<Task, object> OnCompletion;
|
||||
public object UserData;
|
||||
}
|
||||
|
||||
private static List<TaskAction> taskActions = new List<TaskAction>();
|
||||
|
||||
private static void AddInternal(Task task, Action<Task, object> onCompletion, object userdata)
|
||||
{
|
||||
lock (taskActions)
|
||||
{
|
||||
taskActions.Add(new TaskAction() { Task = task, OnCompletion = onCompletion, UserData = userdata });
|
||||
}
|
||||
}
|
||||
|
||||
public static void Add(Task task, Action<Task> onCompletion)
|
||||
{
|
||||
AddInternal(task, (Task t, object obj) => { onCompletion(t); }, null);
|
||||
}
|
||||
|
||||
public static void Add<U>(Task task, U userdata, Action<Task, U> onCompletion) where U : class
|
||||
{
|
||||
AddInternal(task, (Task t, object obj) => { onCompletion(t, (U)obj); }, userdata);
|
||||
}
|
||||
|
||||
public static void Add<T>(Task<T> task, Action<Task<T>> onCompletion)
|
||||
{
|
||||
AddInternal(task, (Task t, object obj) => { onCompletion((Task<T>)t); }, null);
|
||||
}
|
||||
|
||||
public static void Add<T,U>(Task<T> task, U userdata, Action<Task<T>, U> onCompletion) where U : class
|
||||
{
|
||||
AddInternal(task, (Task t, object obj) => { onCompletion((Task<T>)t, (U)obj); }, userdata);
|
||||
}
|
||||
|
||||
public static void Update()
|
||||
{
|
||||
lock (taskActions)
|
||||
{
|
||||
for (int i = 0; i < taskActions.Count; i++)
|
||||
{
|
||||
if (taskActions[i].Task.IsCompleted)
|
||||
{
|
||||
taskActions[i].OnCompletion?.Invoke(taskActions[i].Task, taskActions[i].UserData);
|
||||
taskActions.RemoveAt(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void PrintTaskExceptions(Task task, string msg)
|
||||
{
|
||||
DebugConsole.ThrowError(msg);
|
||||
foreach (Exception e in task.Exception.InnerExceptions)
|
||||
{
|
||||
DebugConsole.ThrowError(e.Message + "\n" + e.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -437,5 +437,90 @@ namespace Barotrauma
|
||||
IEnumerable<string> filtered = splitted.SkipWhile(part => part != currentFolder).Skip(1);
|
||||
return string.Join("/", filtered);
|
||||
}
|
||||
|
||||
public static string EscapeCharacters(string str)
|
||||
{
|
||||
return str.Replace("\\", "\\\\").Replace("\"", "\\\"");
|
||||
}
|
||||
|
||||
public static string UnescapeCharacters(string str)
|
||||
{
|
||||
string retVal = "";
|
||||
for (int i=0;i<str.Length;i++)
|
||||
{
|
||||
if (str[i] != '\\')
|
||||
{
|
||||
retVal += str[i];
|
||||
}
|
||||
else if (i+1<str.Length)
|
||||
{
|
||||
if (str[i+1] == '\\')
|
||||
{
|
||||
retVal += "\\";
|
||||
}
|
||||
else if (str[i+1] == '\"')
|
||||
{
|
||||
retVal += "\"";
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public static string ParseQuotedArgument(string[] arguments, int startIndex, out int endIndex)
|
||||
{
|
||||
#if WINDOWS
|
||||
endIndex = startIndex + 1;
|
||||
return arguments[startIndex];
|
||||
#else
|
||||
string retVal = "";
|
||||
int currIndex = startIndex;
|
||||
bool escaped = false;
|
||||
if (arguments[startIndex][0] != '\"')
|
||||
{
|
||||
endIndex = startIndex+1;
|
||||
return UnescapeCharacters(arguments[startIndex]);
|
||||
}
|
||||
while (currIndex < arguments.Length)
|
||||
{
|
||||
for (int i=currIndex == startIndex ? 1 : 0;i<arguments[currIndex].Length;i++)
|
||||
{
|
||||
if (!escaped)
|
||||
{
|
||||
if (arguments[currIndex][i] == '\\')
|
||||
{
|
||||
escaped = true;
|
||||
}
|
||||
else if (arguments[currIndex][i] == '\"')
|
||||
{
|
||||
endIndex = currIndex+1;
|
||||
return UnescapeCharacters(retVal);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
escaped = false;
|
||||
}
|
||||
retVal += arguments[currIndex][i];
|
||||
}
|
||||
retVal += " ";
|
||||
currIndex++;
|
||||
}
|
||||
|
||||
endIndex = arguments.Length;
|
||||
return retVal;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string[] MergeArguments(string[] arguments)
|
||||
{
|
||||
List<string> mergedArgs = new List<string>();
|
||||
for (int i=0;i<arguments.Length;)
|
||||
{
|
||||
mergedArgs.Add(ParseQuotedArgument(arguments, i, out i));
|
||||
}
|
||||
return mergedArgs.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user