Repository navigation
Expand file tree
/
Copy pathUtilities.cs
More file actions
86 lines (78 loc) · 2.38 KB
/
Copy pathUtilities.cs
File metadata and controls
86 lines (78 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Sandbox;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace PluginManager.Utilities
{
public class Log
{
public static void WriteLineAndConsole(string msg) => MySandboxGame.Log.WriteLineAndConsole("PluginManager: " + msg);
}
class AsyncLock
{
private readonly Task<IDisposable> _releaserTask;
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private readonly IDisposable _releaser;
public AsyncLock()
{
_releaser = new Releaser(_semaphore);
_releaserTask = Task.FromResult(_releaser);
}
public IDisposable Lock()
{
_semaphore.Wait();
return _releaser;
}
public Task<IDisposable> LockAsync()
{
var waitTask = _semaphore.WaitAsync();
return waitTask.IsCompleted
? _releaserTask
: waitTask.ContinueWith(
(_, releaser) => (IDisposable)releaser,
_releaser,
CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}
private class Releaser : IDisposable
{
private readonly SemaphoreSlim _semaphore;
public Releaser(SemaphoreSlim semaphore)
{
_semaphore = semaphore;
}
public void Dispose()
{
_semaphore.Release();
}
}
}
public static class HashSetExtension
{
public static bool Exists<T>(this System.Collections.Generic.HashSet<T> self, Predicate<T> match)
{
foreach (var e in self) if (match(e)) return true;
return false;
}
}
public static class FileStreamExtension
{
public static byte[] ReadAll(this System.IO.FileStream self)
{
byte[] bytes = new byte[self.Length];
int bytesToRead = (int)self.Length;
int bytesRead = 0;
int num;
while (bytesToRead > 0)
{
num = self.Read(bytes, bytesRead, bytesToRead);
if (num == 0)
break;
bytesRead += num;
bytesToRead -= num;
}
return bytes;
}
}
}