Skip to content

Commit 30e827a

Browse files
committed
VFS v0.5 release
Check VFS v0.5 release notes for changes and fixes.
1 parent a602872 commit 30e827a

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

VFS/Examples/Ex01WorldController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ void Update()
2727

2828
public void SaveDataToFile()
2929
{
30+
// Reset variables to default values, just in case.
31+
boolVar = true;
32+
intVar = -24;
33+
charVar = 'a';
34+
3035
/* Data is packed as follows:
3136
dataBuffer[0] = boolVar
3237
dataBuffer[1-4] = intVar
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+

2+
using UdonSharp;
3+
using UnityEngine;
4+
using VRC.SDKBase;
5+
using VRC.Udon;
6+
using UnityEngine.UI;
7+
8+
public class Ex03WorldController : UdonSharpBehaviour
9+
{
10+
public UdonBehaviour FileManager;
11+
public UdonBehaviour FileSystem;
12+
const int blockSize = 4;
13+
const int blockCount = 4;
14+
byte[] dataBuffer; // The size should always be blockSize*blockCount, for encryption purposes.
15+
byte[] keyByteArr = new byte[]{0xa1,0x5d,0x1d,0xf2};
16+
byte[] magicNumberByteArr = new byte[]{0xd2,0x8c,0x4a,0xc6};
17+
18+
// Data Variables
19+
bool boolVar = false;
20+
int intVar = 0;
21+
char charVar = '-';
22+
Color32 colorVar;
23+
24+
// UI Variables
25+
public Toggle boolVarToggle;
26+
public Slider intVarSlider;
27+
public Text intVarText;
28+
public InputField charVarInputField;
29+
public Slider ColorVarRSlider;
30+
public Slider ColorVarGSlider;
31+
public Slider ColorVarBSlider;
32+
public Image ColorVarPrevImage;
33+
34+
void Start()
35+
{
36+
colorVar = new Color32((byte)0,(byte)0,(byte)0,(byte)255);
37+
OnBoolVarToggleValueChanged();
38+
OnIntVarSliderValueChanged();
39+
OnCharVarInputFieldValueChanged();
40+
OnColorVarRSliderValueChanged();
41+
OnColorVarGSliderValueChanged();
42+
OnColorVarBSliderValueChanged();
43+
}
44+
45+
void Update()
46+
{
47+
48+
}
49+
50+
public void OnBoolVarToggleValueChanged()
51+
{
52+
boolVar = boolVarToggle.isOn;
53+
}
54+
55+
public void OnIntVarSliderValueChanged()
56+
{
57+
intVar = (int)intVarSlider.value;
58+
intVarText.text = intVar.ToString();
59+
}
60+
61+
public void OnCharVarInputFieldValueChanged()
62+
{
63+
charVar = charVarInputField.text.Length > 0 ? charVarInputField.text[0] : '-';
64+
}
65+
66+
public void OnColorVarRSliderValueChanged()
67+
{
68+
colorVar.r = (byte)ColorVarRSlider.value;
69+
ColorVarPrevImage.color = colorVar;
70+
}
71+
72+
public void OnColorVarGSliderValueChanged()
73+
{
74+
colorVar.g = (byte)ColorVarGSlider.value;
75+
ColorVarPrevImage.color = colorVar;
76+
}
77+
78+
public void OnColorVarBSliderValueChanged()
79+
{
80+
colorVar.b = (byte)ColorVarBSlider.value;
81+
ColorVarPrevImage.color = colorVar;
82+
}
83+
84+
public void SaveDataToFile()
85+
{
86+
// Debug.Log($"bool:{boolVar},int:{intVar},char:{charVar},color:{colorVar}");
87+
dataBuffer = new byte[blockSize*blockCount];
88+
89+
// Bytes 0-3 are the magic number
90+
for(int i = 0; i < magicNumberByteArr.Length; i++)
91+
dataBuffer[i] = magicNumberByteArr[i];
92+
93+
dataBuffer[4] = System.Convert.ToByte(boolVar);
94+
95+
byte[] intByteArr = Int32ToByteArray(intVar);
96+
dataBuffer[5] = intByteArr[0];
97+
dataBuffer[6] = intByteArr[1];
98+
dataBuffer[7] = intByteArr[2];
99+
dataBuffer[8] = intByteArr[3];
100+
101+
dataBuffer[9] = System.Convert.ToByte(charVar);
102+
103+
dataBuffer[10] = colorVar.r;
104+
dataBuffer[11] = colorVar.g;
105+
dataBuffer[12] = colorVar.b;
106+
dataBuffer[13] = colorVar.a;
107+
108+
// Encrypt data
109+
dataBuffer = EncryptByteArray(dataBuffer);
110+
111+
FileManager.SetProgramVariable("fileDataBuffer",dataBuffer);
112+
113+
}
114+
115+
public void LoadDataFromFile()
116+
{
117+
dataBuffer = (byte[])FileManager.GetProgramVariable("fileDataBuffer");
118+
119+
// Decrypt data
120+
dataBuffer = DecryptByteArray(dataBuffer);
121+
122+
// Data integrity check. If magic number in dataBuufer is wrong, data has been
123+
// tampered with. Let user know, and don't proceed further.
124+
for(int i = 0; i < magicNumberByteArr.Length; i++)
125+
{
126+
if(dataBuffer[i] != magicNumberByteArr[i])
127+
{
128+
Debug.Log("File data has been tampered with. Data integrity test failed.");
129+
return;
130+
}
131+
}
132+
133+
boolVar = System.Convert.ToBoolean(dataBuffer[4]);
134+
135+
byte[] intByteArr = new byte[4] {dataBuffer[5],dataBuffer[6],dataBuffer[7],dataBuffer[8]};
136+
intVar = ByteArrayToInt32(intByteArr);
137+
138+
charVar = System.Convert.ToChar(dataBuffer[9]);
139+
140+
colorVar.r = dataBuffer[10];
141+
colorVar.g = dataBuffer[11];
142+
colorVar.b = dataBuffer[12];
143+
colorVar.a = dataBuffer[13];
144+
145+
Debug.Log($"bool:{boolVar},int:{intVar},char:{charVar},color:{colorVar}");
146+
UpdateUIComponents();
147+
}
148+
149+
void UpdateUIComponents()
150+
{
151+
boolVarToggle.isOn = boolVar;
152+
intVarSlider.value = intVar;
153+
charVarInputField.text = charVar.ToString();
154+
ColorVarRSlider.value = (int)colorVar.r;
155+
ColorVarGSlider.value = (int)colorVar.g;
156+
ColorVarBSlider.value = (int)colorVar.b;
157+
}
158+
159+
int ByteArrayToInt32(byte[] bArr)
160+
{
161+
if(bArr.Length != 4) return 0;
162+
int res = 0;
163+
for(int i = 0; i < bArr.Length; i++)
164+
{
165+
int maskedNum = 0;
166+
maskedNum = bArr[i] & 0xff;
167+
maskedNum <<= (8*((bArr.Length-1)-i));
168+
res |= maskedNum;
169+
}
170+
return res;
171+
}
172+
173+
byte[] Int32ToByteArray(int num)
174+
{
175+
byte[] res = new byte[4];
176+
for(int i = 0; i < res.Length; i++)
177+
{
178+
int maskedNum = 0;
179+
maskedNum = num >> (8*((res.Length-1)-i));
180+
res[i] = (byte)(maskedNum & 0xff);
181+
}
182+
return res;
183+
}
184+
185+
byte[] EncryptByteArray(byte[] dataArr)
186+
{
187+
for(int i = 0; i < blockCount; i++)
188+
{
189+
for(int j = 0; j < blockSize; j++)
190+
{
191+
if(i==0) dataArr[(i*blockSize)+j] ^= keyByteArr[j];
192+
else
193+
{
194+
dataArr[(i*blockSize)+j] ^= dataArr[((i*blockSize)-blockSize)+j];
195+
dataArr[(i*blockSize)+j] ^= keyByteArr[j];
196+
dataArr[j] ^= dataArr[(i*blockSize)+j];
197+
}
198+
}
199+
}
200+
201+
return dataArr;
202+
}
203+
204+
byte[] DecryptByteArray(byte[] dataArr)
205+
{
206+
for(int i = blockCount-1; i > -1; i--)
207+
{
208+
for(int j = 0; j < blockSize; j++)
209+
{
210+
if(i==0) dataArr[(i*blockSize)+j] ^= keyByteArr[j];
211+
else
212+
{
213+
dataArr[j] ^= dataArr[(i*blockSize)+j];
214+
dataArr[(i*blockSize)+j] ^= keyByteArr[j];
215+
dataArr[(i*blockSize)+j] ^= dataArr[((i*blockSize)-blockSize)+j];
216+
}
217+
}
218+
}
219+
return dataArr;
220+
}
221+
}

0 commit comments

Comments
 (0)