This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiceExpression.cs
204 lines (173 loc) · 6.46 KB
/
DiceExpression.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace csharp
{
class DiceExpression : IDisposable
{
/// <summary>Our Random object. Make it a first-class citizen so that it produces truly *random* results</summary>
private RNGCryptoServiceProvider r;
//private FileStream devRand;
/*private IntPtr randPtr;
[DllImport("./lib/randGen.so")]
private static extern IntPtr initRand();
[DllImport("./lib/randGen.so")]
private static extern uint getRand(IntPtr randPtr);
[DllImport("./lib/randGen.so")]
private static extern void freeRand(IntPtr randPtr);*/
private readonly Func<uint> randPtr = null;
public DiceExpression()
{
r = new RNGCryptoServiceProvider();
randPtr = InitRand();
}
public uint GetRandNum()
{
if (disposedValue == true)
{
//throw new Exception("resource closed");
throw new ObjectDisposedException("DiceExpression");
}
return randPtr.Invoke();
}
/// <summary>Roll</summary>
/// <param name="s">string to be evaluated</param>
/// <returns>result of evaluated string</returns>
public long R(string s, ref List<long> rolledSoFar)
{
if (disposedValue == true)
{
throw new Exception("resource closed");
}
long toReturn = 0;
// Addition is lowest order of precedence
var a = s.Split('+');
// Add results of each group
if (a.Count() > 1)
{
foreach (var b in a)
{
toReturn += R(b, ref rolledSoFar);
}
}
else
{
// Die definition is our highest order of precedence
var d = a[0].Split('d');
if (d.Length == 1)
{
if (long.TryParse(d[0], out long AmtToAdd))
{
rolledSoFar.Add(AmtToAdd);
return (uint)(toReturn + AmtToAdd);
}
}
// This operand will be our die count, static digits, or else something we don't understand
if (!long.TryParse(d[0].Trim(), out long AmtToRoll))
AmtToRoll = 0;
// Multiple definitions ("2d6d8") iterate through left-to-right: (2d6)d8
for (long i = 1; i < d.Count(); i++)
{
// If we don't have a right side (face count), assume 6
if (!long.TryParse(d[i].Trim(), out long f))
f = 6;
long u = 0;
// If we don't have a die count, use 1
for (long j = 0; j < (AmtToRoll == 0 ? 1 : AmtToRoll); j++)
{
/*byte[] arrToFill = new byte[sizeof(int)];
if(f < 256)
{
byte[] singleByte = new byte[1];
r.GetBytes(singleByte);
arrToFill[0] = singleByte[0];
}
else
{
r.GetBytes(arrToFill);
}
//u += r.Next(1, f);
int numToAdd = (BitConverter.ToInt32(arrToFill, 0) % f);
if (numToAdd < 0) numToAdd *= -1;
rolledSoFar.Add(numToAdd + 1);*/
var numToAdd = (randPtr.Invoke() % f + 1);
rolledSoFar.Add(numToAdd);
u += numToAdd;
}
toReturn += u;
}
}
return toReturn;
}
private uint GetRandLinux()
{
Span<byte> bytes = stackalloc byte[4];
var devRand = File.OpenRead("/dev/random/");
for (int i = 0; i < 4; i++)
{
bytes[i] = (byte)devRand.ReadByte();
}
devRand.Dispose();
return BitConverter.ToUInt32(bytes);
}
private uint GetRandWindows()
{
Span<byte> bytes = stackalloc byte[4];
r.GetBytes(bytes);
return BitConverter.ToUInt32(bytes);
}
private Func<uint> InitRand()
{
r = new RNGCryptoServiceProvider();
return GetRandWindows;
/*if (File.Exists("/dev/random"))
{
Console.WriteLine("using /dev/random");
//devRand = File.OpenRead("/dev/random");
return GetRandLinux;
}
else
{
Console.WriteLine("No /dev/random using alternative");
r = new RNGCryptoServiceProvider();
return GetRandWindows;
}*/
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
r?.Dispose();
//devRand?.Dispose();
// TODO: dispose managed state (managed objects).
}
//freeRand(randPtr);
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
~DiceExpression()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);
}
#endregion
}
}