-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquickbar.lua
More file actions
726 lines (680 loc) · 19.1 KB
/
quickbar.lua
File metadata and controls
726 lines (680 loc) · 19.1 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
-- Carl Frank Otto III
-- carlotto81@gmail.com
-- GitHub: https://github.com/M45-Science/SoftMod
-- License: MPL 2.0
local QUALITY_ALIAS = {
uncommon = "u",
rare = "r",
epic = "e",
legendary = "l",
}
local QUALITY_FROM_ALIAS = {}
for k, v in pairs(QUALITY_ALIAS) do
QUALITY_FROM_ALIAS[v] = k
end
local ITEM_ALIAS = {}
local ITEM_FROM_ALIAS = {}
local ITEM_LIST = {
"accumulator",
"active-provider-chest",
"advanced-circuit",
"agricultural-science-pack",
"agricultural-tower",
"arithmetic-combinator",
"artificial-jellynut-soil",
"artificial-yumako-soil",
"artillery-shell",
"artillery-targeting-remote",
"artillery-turret",
"artillery-wagon",
"artillery-wagon-cannon",
"assembling-machine-1",
"assembling-machine-2",
"assembling-machine-3",
"asteroid-collector",
"atomic-bomb",
"automation-science-pack",
"barrel",
"battery",
"battery-equipment",
"battery-mk2-equipment",
"battery-mk3-equipment",
"beacon",
"belt-immunity-equipment",
"big-electric-pole",
"big-mining-drill",
"biochamber",
"bioflux",
"biolab",
"biter-egg",
"blueprint",
"blueprint-book",
"boiler",
"buffer-chest",
"bulk-inserter",
"burner-generator",
"burner-inserter",
"burner-mining-drill",
"calcite",
"cannon-shell",
"captive-biter-spawner",
"capture-robot-rocket",
"car",
"carbon",
"carbon-fiber",
"carbonic-asteroid-chunk",
"cargo-bay",
"cargo-landing-pad",
"cargo-wagon",
"centrifuge",
"chemical-plant",
"chemical-science-pack",
"cliff-explosives",
"cluster-grenade",
"coal",
"coin",
"combat-shotgun",
"concrete",
"constant-combinator",
"construction-robot",
"copper-bacteria",
"copper-cable",
"copper-ore",
"copper-plate",
"copper-wire",
"copy-paste-tool",
"crude-oil-barrel",
"crusher",
"cryogenic-plant",
"cryogenic-science-pack",
"cut-paste-tool",
"decider-combinator",
"deconstruction-planner",
"defender-capsule",
"depleted-uranium-fuel-cell",
"destroyer-capsule",
"discharge-defense-equipment",
"discharge-defense-remote",
"display-panel",
"distractor-capsule",
"efficiency-module",
"efficiency-module-2",
"efficiency-module-3",
"electric-energy-interface",
"electric-engine-unit",
"electric-furnace",
"electric-mining-drill",
"electromagnetic-plant",
"electromagnetic-science-pack",
"electronic-circuit",
"empty-module-slot",
"energy-shield-equipment",
"energy-shield-mk2-equipment",
"engine-unit",
"exoskeleton-equipment",
"explosive-cannon-shell",
"explosive-rocket",
"explosive-uranium-cannon-shell",
"explosives",
"express-loader",
"express-splitter",
"express-transport-belt",
"express-underground-belt",
"fast-inserter",
"fast-loader",
"fast-splitter",
"fast-transport-belt",
"fast-underground-belt",
"firearm-magazine",
"fission-reactor-equipment",
"flamethrower",
"flamethrower-ammo",
"flamethrower-turret",
"fluid-wagon",
"fluoroketone-cold-barrel",
"fluoroketone-hot-barrel",
"flying-robot-frame",
"foundation",
"foundry",
"fusion-generator",
"fusion-power-cell",
"fusion-reactor",
"fusion-reactor-equipment",
"gate",
"green-wire",
"grenade",
"gun-turret",
"hazard-concrete",
"heat-exchanger",
"heat-interface",
"heat-pipe",
"heating-tower",
"heavy-armor",
"heavy-oil-barrel",
"holmium-ore",
"holmium-plate",
"ice",
"ice-platform",
"infinity-cargo-wagon",
"infinity-chest",
"infinity-pipe",
"inserter",
"iron-bacteria",
"iron-chest",
"iron-gear-wheel",
"iron-ore",
"iron-plate",
"iron-stick",
"item-unknown",
"jelly",
"jellynut",
"jellynut-seed",
"lab",
"land-mine",
"landfill",
"lane-splitter",
"laser-turret",
"light-armor",
"light-oil-barrel",
"lightning-collector",
"lightning-rod",
"linked-belt",
"linked-chest",
"lithium",
"lithium-plate",
"loader",
"locomotive",
"logistic-robot",
"logistic-science-pack",
"long-handed-inserter",
"low-density-structure",
"lubricant-barrel",
"mech-armor",
"medium-electric-pole",
"metallic-asteroid-chunk",
"metallurgic-science-pack",
"military-science-pack",
"modular-armor",
"night-vision-equipment",
"nuclear-fuel",
"nuclear-reactor",
"nutrients",
"offshore-pump",
"oil-refinery",
"one-way-valve",
"overflow-valve",
"overgrowth-jellynut-soil",
"overgrowth-yumako-soil",
"oxide-asteroid-chunk",
"parameter-0",
"parameter-1",
"parameter-2",
"parameter-3",
"parameter-4",
"parameter-5",
"parameter-6",
"parameter-7",
"parameter-8",
"parameter-9",
"passive-provider-chest",
"pentapod-egg",
"personal-laser-defense-equipment",
"personal-roboport-equipment",
"personal-roboport-mk2-equipment",
"petroleum-gas-barrel",
"piercing-rounds-magazine",
"piercing-shotgun-shell",
"pipe",
"pipe-to-ground",
"pistol",
"plastic-bar",
"poison-capsule",
"power-armor",
"power-armor-mk2",
"power-switch",
"processing-unit",
"production-science-pack",
"productivity-module",
"productivity-module-2",
"productivity-module-3",
"programmable-speaker",
"promethium-asteroid-chunk",
"promethium-science-pack",
"proxy-container",
"pump",
"pumpjack",
"quality-module",
"quality-module-2",
"quality-module-3",
"quantum-processor",
"radar",
"rail",
"rail-chain-signal",
"rail-ramp",
"rail-signal",
"rail-support",
"railgun",
"railgun-ammo",
"railgun-turret",
"raw-fish",
"recycler",
"red-wire",
"refined-concrete",
"refined-hazard-concrete",
"repair-pack",
"requester-chest",
"roboport",
"rocket",
"rocket-fuel",
"rocket-launcher",
"rocket-part",
"rocket-silo",
"rocket-turret",
"science",
"scrap",
"selection-tool",
"selector-combinator",
"shotgun",
"shotgun-shell",
"simple-entity-with-force",
"simple-entity-with-owner",
"slowdown-capsule",
"small-electric-pole",
"small-lamp",
"solar-panel",
"solar-panel-equipment",
"solid-fuel",
"space-platform-foundation",
"space-platform-hub",
"space-platform-starter-pack",
"space-science-pack",
"speed-module",
"speed-module-2",
"speed-module-3",
"spidertron",
"spidertron-remote",
"spidertron-rocket-launcher-1",
"spidertron-rocket-launcher-2",
"spidertron-rocket-launcher-3",
"spidertron-rocket-launcher-4",
"splitter",
"spoilage",
"stack-inserter",
"steam-engine",
"steam-turbine",
"steel-chest",
"steel-furnace",
"steel-plate",
"stone",
"stone-brick",
"stone-furnace",
"stone-wall",
"storage-chest",
"storage-tank",
"submachine-gun",
"substation",
"sulfur",
"sulfuric-acid-barrel",
"supercapacitor",
"superconductor",
"tank",
"tank-cannon",
"tank-flamethrower",
"tank-machine-gun",
"tesla-ammo",
"tesla-turret",
"teslagun",
"thruster",
"toolbelt-equipment",
"top-up-valve",
"train-stop",
"transport-belt",
"tree-seed",
"tungsten-carbide",
"tungsten-ore",
"tungsten-plate",
"turbo-loader",
"turbo-splitter",
"turbo-transport-belt",
"turbo-underground-belt",
"underground-belt",
"upgrade-planner",
"uranium-235",
"uranium-238",
"uranium-cannon-shell",
"uranium-fuel-cell",
"uranium-ore",
"uranium-rounds-magazine",
"utility-science-pack",
"vehicle-machine-gun",
"water-barrel",
"wood",
"wooden-chest",
"yumako",
"yumako-mash",
"yumako-seed",
}
local function QUICKBAR_GenerateItemAliases()
ITEM_ALIAS = {}
ITEM_FROM_ALIAS = {}
local function idx_to_alias(i)
i = i - 1
local a = math.floor(i / 26)
local b = i % 26
return string.char(65 + a) .. string.char(65 + b)
end
for i, name in ipairs(ITEM_LIST) do
local alias = idx_to_alias(i)
ITEM_ALIAS[name] = alias
ITEM_FROM_ALIAS[alias] = name
end
end
function ExportQuickbar(player, limit)
if not player or not player.valid then
return
end
local outbuf = ""
local maxExport = 100
if limit then
maxExport = 20
end
if not next(ITEM_ALIAS) then
QUICKBAR_GenerateItemAliases()
end
for i = 1, maxExport do
local slot = player.get_quick_bar_slot(i)
if slot ~= nil then
local quality = slot.quality
if quality ~= nil and type(quality) ~= "string" and quality.name then
quality = quality.name
end
quality = quality or "normal"
local name = slot.name
if name ~= nil and type(name) ~= "string" and name.name then
name = name.name
end
local ialias = ITEM_ALIAS[name]
if ialias then
outbuf = outbuf .. ialias
else
outbuf = outbuf .. name
end
if quality ~= "normal" then
local alias = QUALITY_ALIAS[quality]
if alias then
outbuf = outbuf .. ":" .. alias
else
outbuf = outbuf .. ":" .. quality
end
end
end
outbuf = outbuf .. ","
end
UTIL_SmartPrint(player,"Quickbar Exported!")
return helpers.encode_string("M45-QB2=" .. outbuf)
end
--Split with empty strings
function SplitStr(str, delimiter)
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
function ImportQuickbar(player, data)
if not player or not player.valid then
return false
end
if data == nil or data == "" then
return false
end
if not next(ITEM_ALIAS) then
QUICKBAR_GenerateItemAliases()
end
--Limit compressed size
if string.len(data) > 10240 then
UTIL_SmartPrint(player, "String too long.")
return false
end
local decoded = helpers.decode_string(data)
if decoded == nil or decoded == "" then
UTIL_SmartPrint(player, "Could not decode that string.")
return false
end
--Limit decompressed size
if string.len(decoded) > 10240 then
UTIL_SmartPrint(player, "String too long.")
return false
end
local version, payload = decoded:match("^([^=]+)=(.*)$")
if not version then
UTIL_SmartPrint(player, "That isn't a valid M45 quickbar exchange string! (No Header)")
return false
end
if version ~= "M45-QB1" and version ~= "M45-QB2" then
UTIL_SmartPrint(player, "That isn't a valid M45 quickbar exchange string! (Invalid Header)")
return false
end
--Clear all bars
for i = 1, 100 do
player.set_quick_bar_slot(i, nil)
end
--Restore from string
local items = SplitStr(payload or "", ",")
if not prototypes or not prototypes.item or not prototypes.quality then
UTIL_SmartPrint(player, "Quickbar Import: global 'prototypes' unavailable; aborting import.")
return false
end
local error_list = ""
for i, entry in ipairs(items) do
if i > 100 then
return
end
if entry ~= "" then
local item = entry
local quality = "normal"
if version == "M45-QB2" then
local parts = UTIL_SplitStr(entry, ":")
local ialias = parts[1] or ""
item = ITEM_FROM_ALIAS[ialias] or ialias
local qalias = parts[2]
if qalias ~= nil then
quality = QUALITY_FROM_ALIAS[qalias] or qalias
end
else
item = ITEM_FROM_ALIAS[item] or item
end
local valid_item = prototypes.item[item]
local valid_quality = prototypes.quality[quality]
if valid_item and valid_quality then
if quality ~= "normal" then
player.set_quick_bar_slot(i, { name = item, quality = quality })
else
player.set_quick_bar_slot(i, item)
end
else
if error_list ~= "" then
error_list = error_list .. ", "
end
if not valid_item then
error_list = error_list .. item
else
error_list = error_list .. item .. ":" .. quality
end
end
end
end
if error_list ~= "" then
UTIL_SmartPrint(player, "Quickbar Import: Invalid items/qualities skipped: " .. error_list)
else
UTIL_SmartPrint(player, "Quickbar imported!")
end
return true
end
function QUICKBAR_MakeExchangeButton(player)
QUICKBAR_ClearString(player)
if not (player and player.valid and player.gui and player.gui.top) then
return
end
if player.gui.top.qb_exchange_button then
player.gui.top.qb_exchange_button.destroy()
end
if not player.gui.top.qb_exchange_button then
local ex_button = player.gui.top.add {
type = "sprite-button",
name = "qb_exchange_button",
sprite = "file/img/buttons/exchange-64.png",
tooltip = "Import or Export a M45 quickbar exchange string."
}
ex_button.style.size = { 64, 64 }
end
end
function QUICKBAR_MakeExchangeWindow(player, exportMode)
QUICKBAR_ClearString(player)
if not (player and player.valid and player.gui and player.gui.screen) then
return
end
if player.gui.screen.quickbar_exchange then
player.gui.screen.quickbar_exchange.destroy()
end
local main_flow = player.gui.screen.add {
type = "frame",
name = "quickbar_exchange",
direction = "vertical"
}
main_flow.style.horizontal_align = "center"
main_flow.style.vertical_align = "center"
main_flow.force_auto_center()
-- Title Bar--
local info_titlebar = main_flow.add {
type = "flow",
direction = "horizontal"
}
info_titlebar.drag_target = main_flow
info_titlebar.style.horizontal_align = "center"
info_titlebar.style.horizontally_stretchable = true
info_titlebar.add {
type = "label",
name = "online_title",
style = "frame_title",
caption = "M45 Quickbar Exchange String"
}
local pusher = info_titlebar.add {
type = "empty-widget",
style = "draggable_space_header"
}
pusher.style.vertically_stretchable = true
pusher.style.horizontally_stretchable = true
pusher.drag_target = main_flow
info_titlebar.add {
type = "sprite-button",
name = "qb_exchange_close",
sprite = "utility/close",
style = "frame_action_button",
tooltip = "Close this window"
}
main_flow.style.padding = 4
local mframe = main_flow.add {
type = "flow",
direction = "vertical"
}
mframe.style.minimal_height = 75
mframe.style.horizontally_squashable = false
local qbes = ""
if exportMode then
qbes = ExportQuickbar(player, false)
end
mframe.add {
type = "text-box",
name = "quickbar_string",
text = qbes,
tooltip = "COPY: Click text then Control-C\nPASTE: Click text then Control-V",
}
mframe.quickbar_string.style.minimal_width = 500
mframe.quickbar_string.style.minimal_height = 50
local bframe = mframe.add {
type = "flow",
direction = "horizontal"
}
bframe.add {
type = "button",
caption = "Import",
style = "green_button",
name = "import_qb",
tooltip = "Import a new quickbar."
}
local pusher = bframe.add {
type = "empty-widget",
}
pusher.style.vertically_stretchable = true
pusher.style.horizontally_stretchable = true
bframe.add {
type = "button",
caption = "Export",
style = "red_button",
name = "export_qb",
tooltip = "Export current quickbars."
}
end
function QUICKBAR_Clicks(event)
if event and event.element and event.element.valid and event.player_index then
local player = game.players[event.player_index]
if player and player.valid and event.element.name then
if event.element.name == "quickbar_string" then
event.element.select_all()
end
if event.element.name == "qb_exchange_close" and player.gui and player.gui.screen and
player.gui.screen.quickbar_exchange then
QUICKBAR_ClearString(player)
player.gui.screen.quickbar_exchange.destroy()
elseif event.element.name == "qb_exchange_button" and player.gui and player.gui.screen then
if player.gui.screen.quickbar_exchange then
player.gui.screen.quickbar_exchange.destroy()
QUICKBAR_ClearString(player)
else
QUICKBAR_MakeExchangeWindow(player, false)
end
elseif event.element.name == "export_qb" and player.gui and player.gui.screen then
if player.gui.screen.quickbar_exchange then
QUICKBAR_ClearString(player)
player.gui.screen.quickbar_exchange.destroy()
end
QUICKBAR_MakeExchangeWindow(player, true)
elseif event.element.name == "import_qb" and player.gui and player.gui.screen then
if storage.PData and storage.PData[player.index] and
storage.PData[event.player_index].qb_import_string then
ImportQuickbar(player, storage.PData[event.player_index].qb_import_string)
QUICKBAR_ClearString(player)
if player.gui.screen.quickbar_exchange then
player.gui.screen.quickbar_exchange.destroy()
end
end
end
end
end
end
function QUICKBAR_ClearString(player)
if not player or not player.valid then
return
end
if storage.PData and storage.PData[player.index] and
storage.PData[player.index].qb_import_string then
storage.PData[player.index].qb_import_string = ""
end
end
-- Grab text from text box
function QUICKBAR_TextChanged(event)
if event and event.element and event.player_index and event.text and event.element.name then
local player = game.players[event.player_index]
if event.element.name == "quickbar_string" then
if storage.PData and storage.PData[event.player_index] then
--Limit import size
if string.len(event.element.text) > 10240 then
event.element.text = "String too long."
return
end
storage.PData[event.player_index].qb_import_string = event.element.text
end
end
end
end