@Stanlyhalo/

World Loader

Lua

World Loader is my own module I am making for my roblox game, as you can read it is for loading world data.

fork
loading
Files
  • main.lua
  • json.lua
main.lua
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
--local json = require "./json"
local loading = ""
local objectCode = "[instance:'floor';brickcolorR:'255';brickcolorG:'255';brickcolorB:'255';material:'Metal';reflectance:'0';transparency:'0';shape:'Block';posX:'5';posY:'5';posZ:'5';anchored:'true';cancollide:'true';sizeX:'20';sizeY:'0.5';sizeZ:'20';]"
local structureCode = "[instance:'hl1poahlway1';posX:'10';posY:'5';posZ:'10';rotationX:'0';rotationY:'0';rotationZ:'0';anchored:'true';cancollide:'true';]"
local tokens = {}
local instances = {}
function lex(data)
	tokens = {}
	instances = {}
	local state = 0
	local tok = ""
	local varstarted = 0
	local propBRICKCOLORRstarted = 0
	local propBRICKCOLORGstarted = 0
	local propBRICKCOLORBstarted = 0
	local propMATERIALstarted = 0
	local propREFLECTANCEstarted = 0
	local propTRANSPARENCYstarted = 0
	local propPOSXstarted = 0
	local propPOSYstarted = 0
	local propPOSZstarted = 0
	local propANCHOREDstarted = 0
	local propCANCOLLIDEstarted = 0
	local propSHAPEstarted = 0
	local propSIZEXstarted = 0
	local propSIZEYstarted = 0
	local propSIZEZstarted = 0
	local var = {}
	local string = ""
	local allowCharAdd = true
	local allowContAdd = true
	local contents = {}
	for i = 1, #data do
		local c = data:sub(i,i)
		contents[i] = c
	end
	local char = 1
	while char ~= #contents do
		if allowContAdd == true then
			tok = tok..contents[char]
		else
			allowContAdd = true
		end
		if loading == "objectCode" then
			if tok == ";" and varstarted == 0 and varcontinue == 0 then
				propBRICKCOLORRstarted = 0
				propBRICKCOLORGstarted = 0
				propBRICKCOLORBstarted = 0
				propMATERIALstarted = 0
				propREFLECTANCEstarted = 0
				propTRANSPARENCYstarted = 0
				propPOSXstarted = 0
				propPOSYstarted = 0
				propPOSZstarted = 0
				propANCHOREDstarted = 0
				propCANCOLLIDEstarted = 0
				propSHAPEstarted = 0
				propSIZEXstarted = 0
				propSIZEYstarted = 0
				propSIZEZstarted = 0
				tok = ""
			else
				if tok == "[" then
					varstarted = 1
					tok = ""
				elseif tok == "]" then
					state = 0
					tok = ""
					varstarted = 0
					propBRICKCOLORRstarted = 0
					propBRICKCOLORGstarted = 0
					propBRICKCOLORBstarted = 0
					propMATERIALstarted = 0
					propREFLECTANCEstarted = 0
					propTRANSPARENCYstarted = 0
					propPOSXstarted = 0
					propPOSYstarted = 0
					propPOSZstarted = 0
					propANCHOREDstarted = 0
					propCANCOLLIDEstarted = 0
					propSHAPEstarted = 0
					propSIZEXstarted = 0
					propSIZEYstarted = 0
					propSIZEZstarted = 0
					var = {}
					string = ""
					allowCharAdd = true
					allowContAdd = true
				else
					if tok == "brickcolorR" and varstarted == 0 and varcontinue == 0 then
						propBRICKCOLORRstarted = 1
						tok = ""
					elseif tok == ":" and propBRICKCOLORRstarted == 1 then
						tok = ""
					elseif propBRICKCOLORRstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.brickcolorr = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "brickcolorG" and varstarted == 0 and varcontinue == 0 then
						propBRICKCOLORGstarted = 1
						tok = ""
					elseif tok == ":" and propBRICKCOLORGstarted == 1 then
						tok = ""
					elseif propBRICKCOLORGstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.brickcolorg = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "brickcolorB" and varstarted == 0 and varcontinue == 0 then
						propBRICKCOLORBstarted = 1
						tok = ""
					elseif tok == ":" and propBRICKCOLORBstarted == 1 then
						tok = ""
					elseif propBRICKCOLORBstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.brickcolorb = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "material" and varstarted == 0 and varcontinue == 0 then
						propMATERIALstarted = 1
						tok = ""
					elseif tok == ":" and propMATERIALstarted == 1 then
						tok = ""
					elseif propMATERIALstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.material = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "reflectance" and varstarted == 0 and varcontinue == 0 then
						propREFLECTANCEstarted = 1
						tok = ""
					elseif tok == ":" and propREFLECTANCEstarted == 1 then
						tok = ""
					elseif propREFLECTANCEstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.reflectance = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "transparency" and varstarted == 0 and varcontinue == 0 then
						propTRANSPARENCYstarted = 1
						tok = ""
					elseif tok == ":" and propTRANSPARENCYstarted == 1 then
						tok = ""
					elseif propTRANSPARENCYstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.transparency = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "posX" and varstarted == 0 and varcontinue == 0 then
						propPOSXstarted = 1
						tok = ""
					elseif tok == ":" and propPOSXstarted == 1 then
						tok = ""
					elseif propPOSXstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.posx = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "posY" and varstarted == 0 and varcontinue == 0 then
						propPOSYstarted = 1
						tok = ""
					elseif tok == ":" and propPOSYstarted == 1 then
						tok = ""
					elseif propPOSYstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.posy = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "posZ" and varstarted == 0 and varcontinue == 0 then
						propPOSZstarted = 1
						tok = ""
					elseif tok == ":" and propPOSZstarted == 1 then
						tok = ""
					elseif propPOSZstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.posz = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "anchored" and varstarted == 0 and varcontinue == 0 then
						propANCHOREDstarted = 1
						tok = ""
					elseif tok == ":" and propANCHOREDstarted == 1 then
						tok = ""
					elseif propANCHOREDstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.anchored = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "cancollide" and varstarted == 0 and varcontinue == 0 then
						propCANCOLLIDEstarted = 1
						tok = ""
					elseif tok == ":" and propCANCOLLIDEstarted == 1 then
						tok = ""
					elseif propCANCOLLIDEstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.cancollide = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "shape" and varstarted == 0 and varcontinue == 0 then
						propSHAPEstarted = 1
						tok = ""
					elseif tok == ":" and propSHAPEstarted == 1 then
						tok = ""
					elseif propSHAPEstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.shape = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "sizeX" and varstarted == 0 and varcontinue == 0 then
						propSIZEXstarted = 1
						tok = ""
					elseif tok == ":" and propSIZEXstarted == 1 then
						tok = ""
					elseif propSIZEXstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.sizex = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "sizeY" and varstarted == 0 and varcontinue == 0 then
						propSIZEYstarted = 1
						tok = ""
					elseif tok == ":" and propSIZEYstarted == 1 then
						tok = ""
					elseif propSIZEYstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.sizey = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "sizeZ" and varstarted == 0 and varcontinue == 0 then
						propSIZEZstarted = 1
						tok = ""
					elseif tok == ":" and propSIZEZstarted == 1 then
						tok = ""
					elseif propSIZEZstarted == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							local myVar = var.var
							local varName = var.var.name
							myVar.sizez = string
							tokens[varName] = myVar
							state = 0
							string = ""
							tok = ""
						end
					elseif tok == "instance" and varstarted == 1 then
						varcontinue = 1
						tok = ""
					elseif tok == ":" and varstarted == 1 and varcontinue == 1 then
						tok = ""
					elseif varstarted == 1 and varcontinue == 1 then
						if tok == "'" and state == 0 then
							state = 1
							tok = ""
						elseif contents[char] == "'" and state == 1 then
							string = tok.sub(tok, 0,tok.len(tok)-1)
							var.var = {}
							local myVar = var.var
							myVar.name = string
							tokens[string] = myVar
							instances[#instances+1] = string
							state = 0
							string = ""
							tok = ""
						elseif tok == ";" and state == 0 and varstarted == 1 and varcontinue == 1 then
							varstarted = 0
							varcontinue = 0
							allowCharAdd = false
							allowContAdd = false
						end
					end
				end
			end
		elseif loading == "structureCode" then
			--print(tok)
		end
		if allowCharAdd == true then
			char = char + 1
		else
			allowCharAdd = true
		end
	end
	return tokens
end
local createPart = function(_name,_brickcolorr,_brickcolorg,_brickcolorb,_material,_reflectance,_transparency,_posx,_posy,_posz,_anchored,_cancollide,_shape,_sizex,_sizey,_sizez)
	print("[           New Part           ]")
	--local part = Instance.new("Part", game.Workspace)
	print("==APPEARANCE====================")
	print("BRICKCOLOR R: ".._brickcolorr)
	print("BRICKCOLOR G: ".._brickcolorg)
	print("BRICKCOLOR B: ".._brickcolorb)
	--part.BrickColor = Color.FromRGB(_brickcolorr,_brickcolorg,_brickcolorb)
	print("MATERIAL: ".._material)
	--part.Material = _material
	print("REFLECTANCE: ".._reflectance)
	--part.Reflectance = _reflectance
	print("TRANSPARENCY: ".._transparency)
	--part.Transparency = _transparency
	print("==DATA==========================")
	print("NAME: ".._name)
	--part.Name = _name
	print("POSITION X: ".._posx)
	print("POSITION Y: ".._posy)
	print("POSITION Z: ".._posz)
	--part.Position = Vector3.new(_posx,_posy,_posz)
	print("==BEHAVIOR======================")
	print("ANCHORED: ".._anchored)
	if _anchored == "true" then
		--part.Anchored = true
	elseif _anchored == "false" then
		--part.Anchored = false
	end
	print("CAN COLLIDE: ".._cancollide)
	if _cancollide == "true" then
		--part.CanCollide = true
	elseif _cancollide == "false" then
		--part.CanCollide = false
	end
	print("==PART==========================")
	print("SHAPE: ".._shape)
	--part.Shape = _class
	print("SIZE X: ".._sizex)
	print("SIZE Y: ".._sizey)
	print("SIZE Z: ".._sizez)
	--part.Size = Vector3.new(_sizex,_sizey,_sizez)
	print("==SURFACE_INPUTS================")
	print("==SURFACE=======================")
	print()
	return
end
function parse(toks)
	--print(json.encode(toks))
	for i = 1, #instances do
		local inst = instances[i]
		local toks = toks[inst]
		if toks.shape == "Block" or toks.shape == "Sphere" then
			createPart(toks.name,toks.brickcolorr,toks.brickcolorg,toks.brickcolorb,toks.material,toks.reflectance,toks.transparency,toks.posx,toks.posy,toks.posz,toks.anchored,toks.cancollide,toks.shape,toks.sizex,toks.sizey,toks.sizez)
		end
	end
	return
end
function run(data)
	toks = lex(data)
	parse(toks)
	return
end
print()
loading = "objectCode"
run(objectCode)
loading = "structureCode"
run(structureCode)