-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunity_rng.c
More file actions
172 lines (131 loc) · 3.71 KB
/
unity_rng.c
File metadata and controls
172 lines (131 loc) · 3.71 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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define debug_printf(...) \
do { if (debug) printf(__VA_ARGS__); } while(0)
static int debug = 0;
typedef struct {
uint32_t s0, s1, s2, s3;
} Rand;
Rand* gScriptingRand;
static int urandom_fd = -1;
// initial seed for libTAS algorithm, which would come from system time setting
static uint64_t prng_state = 1;
int in_libTAS(){
char* libtas_path = getenv("LIBTAS_LIBRARY_PATH");
if (libtas_path){
return 1;
}
return 0;
}
Rand* GetScriptingRand(){
return gScriptingRand;
}
// xorshift64* used by libTAS
uint64_t urandom_rand()
{
if (!prng_state)
prng_state = 1;
uint64_t r = prng_state;
r ^= r >> 12;
r ^= r << 25;
r ^= r >> 27;
prng_state = r;
r *= 0x2545F4914F6CDD1DULL;
return r;
}
void urandom_handler(){
// 10 is arbitrary small value
for (int i = 0; i < 10; i++){
uint64_t r = urandom_rand();
debug_printf( "Generated urandom value: 0x%016llx\n", (unsigned long long)r);
write(urandom_fd, &r, sizeof(uint64_t));
}
}
int urandom_get_fd(){
if (urandom_fd == -1){
char filename[] = "/tmp/urandom_XXXXXX";
urandom_fd = mkstemp(filename);
// fill with bytes
urandom_handler();
lseek(urandom_fd, 0, SEEK_SET);
}
return urandom_fd;
}
void GetSystemEntropy(unsigned char *target, size_t size){
int file;
if(in_libTAS()){
file = open64("/dev/urandom", O_RDONLY);
} else {
file = urandom_get_fd();
}
ssize_t n = read(file, target, size);
}
void RandomizeState(Rand *this){
GetSystemEntropy((unsigned char*)this, sizeof(*this));
}
void InitScriptingRand(){
Rand* rng = malloc(sizeof(Rand));
RandomizeState(rng); // discarded call, Unity uses it for UNETRand
RandomizeState(rng); // actual call
gScriptingRand = rng;
}
// xorshift128 used by Unity
uint32_t NextRandom(Rand* rng){
uint32_t t = rng->s0 ^ (rng->s0 << 11);
rng->s0 = rng->s1;
rng->s1 = rng->s2;
rng->s2 = rng->s3;
rng->s3 = (rng->s3 ^ (rng->s3 >> 19)) ^ (t ^ (t >> 8));
return rng->s3;
}
// https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Random.InitState.html
// Random_CUSTOM_InitState
void RandomInitState(int seed){
Rand* rng = GetScriptingRand();
rng->s0 = seed;
rng->s1 = rng->s0 * 0x6c078965 + 1;
rng->s2 = rng->s1 * 0x6c078965 + 1;
rng->s3 = rng->s2 * 0x6c078965 + 1;
}
// https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Random.Range.html
// Random_CUSTOM_Range
float RandomRange(float a, float b){
Rand* rng = GetScriptingRand();
float value = (float)(NextRandom(rng) & 0x7fffff) * 1.192093e-07;
return (1.0 - value) * b + value * a;
}
// https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Random.Range.html
// Random_CUSTOM_RandomRangeInt
int RandomRangeInt(int a, int b){
Rand* rng = GetScriptingRand();
if (a < b){
return a + NextRandom(rng) % (uint32_t)(b - a);
}
if (b < a){
return a - NextRandom(rng) % (uint32_t)(a - b);
}
return a;
}
// https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Random-value.html
// Random_Get_Custom_PropValue
float RandomValue(){
Rand* rng = GetScriptingRand();
return (float)(NextRandom(rng) & 0x7fffff) * 1.192093e-07;
}
int main(int argc, char* argv[]){
if (argc > 1){
prng_state = atoi(argv[1]);
}
InitScriptingRand();
debug_printf("Rand state: %8x %8x %8x %8x\n",
gScriptingRand->s0,
gScriptingRand->s1,
gScriptingRand->s2,
gScriptingRand->s3);
close(urandom_fd);
return 0;
}