-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.gd
382 lines (324 loc) · 14.5 KB
/
menu.gd
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
extends Node
const STEAM_APP_ID = 2744620
const GODOT_VERSIONS = {
"4.2": "4.2.1",
"4.1": "4.1.3",
"4.0": "4.0.4",
"3.5": "3.5.3",
"3.4": "3.4.5",
"3.3": "3.3.4",
"3.2": "3.2.3",
"3.1": "3.1.2",
"3.0": "3.0.6",
"2.1": "2.1.6",
"2.0": "2.0.4.1",
"1.1": "1.1"
}
var initialize_steam = {}
var steam_id = 0
var app_install_dir = ""
var cache_path = ""
var games = []
var page_number = 1
var get_games = 0
var getting_games = 0
var show_games = 0
var delete_unused_images = 0
var workshop_item_id = 0
var update_handle = 0
var update_progress = {}
@onready var workshop_button = $Buttons/Workshop
@onready var upload_window = $Buttons/Upload/Window
@onready var select_folder_file_dialog = $Buttons/Upload/Window/SelectFolder/FileDialog
@onready var folder_path_textedit = $Buttons/Upload/Window/FolderPath
@onready var select_image_file_dialog = $Buttons/Upload/Window/SelectImage/FileDialog
@onready var image_path_textedit = $Buttons/Upload/Window/ImagePath
@onready var item_type_button = $Buttons/Upload/Window/ItemType
@onready var update_or_new_button = $Buttons/Upload/Window/UpdateOrNew
@onready var item_id_textedit = $Buttons/Upload/Window/ItemID
@onready var upload_button = $Buttons/Upload/Window/Upload
@onready var update_progress_label = $Buttons/Upload/Window/UpdateProgress
@onready var upload_timer = $Buttons/Upload/Window/Timer
@onready var help_button = $Buttons/Help
@onready var about_window = $Buttons/About/Window
@onready var licenses_tab = $Buttons/About/Window/Tabs/Licenses
@onready var godotengine_tab = $Buttons/About/Window/Tabs/Licenses/GodotEngine
func _ready():
if Steam.restartAppIfNecessary(STEAM_APP_ID):
get_tree().quit()
else:
initialize_steam = Steam.steamInitEx()
if initialize_steam["status"] != 0:
OS.alert(initialize_steam["verbal"], "Error - Gamebox")
get_tree().quit()
else:
steam_id = Steam.getSteamID()
app_install_dir = Steam.getAppInstallDir(STEAM_APP_ID)["directory"]
cache_path = Steam.getAppInstallDir(STEAM_APP_ID)["directory"].path_join("cache/" + str(steam_id))
DirAccess.make_dir_recursive_absolute(cache_path)
Steam.connect("ugc_query_completed", _on_ugc_query_completed)
Steam.connect("item_installed", _on_item_installed)
Steam.connect("item_created", _on_item_created)
Steam.connect("item_updated", _on_item_updated)
$Versions.set_text($Versions.get_text() + str(Engine.get_version_info()["major"]) + "." + str(Engine.get_version_info()["minor"]) + "." + str(Engine.get_version_info()["patch"]))
folder_path_textedit.hide()
image_path_textedit.hide()
licenses_tab.set_tab_title(1, "Godot Engine")
godotengine_tab.set_tab_title(2, "Third-party licenses")
delete_unused_images = 1
get_games = 1
getting_games = 1
func _process(_delta):
Steam.run_callbacks()
if get_games == 1 and getting_games > 0:
get_games = 0
getting_games -= 1
games = []
_get_games()
if show_games == 1:
_show_games()
page_number = 1
show_games = 0
get_games = 1
if upload_window.is_visible():
if update_or_new_button.get_selected() == 0:
if not item_id_textedit.get_text():
upload_button.set_disabled(true)
if item_id_textedit.get_text() and (folder_path_textedit.get_text() or image_path_textedit.get_text()):
upload_button.set_disabled(false)
if update_or_new_button.get_selected() == 1:
if not folder_path_textedit.get_text() or not image_path_textedit.get_text():
upload_button.set_disabled(true)
if folder_path_textedit.get_text() and image_path_textedit.get_text():
upload_button.set_disabled(false)
if item_type_button.get_selected() == 1:
upload_button.set_disabled(true)
func _get_games():
var query_handle = Steam.createQueryUserUGCRequest(Steam.getSteamID(), Steam.USER_UGC_LIST_SUBSCRIBED, Steam.UGC_MATCHINGUGCTYPE_ITEMS, Steam.USERUGCLISTSORTORDER_TITLEASC, STEAM_APP_ID, STEAM_APP_ID, page_number)
Steam.addRequiredTag(query_handle, "Games")
Steam.sendQueryUGCRequest(query_handle)
func _on_ugc_query_completed(query_handle, result, results_returned, _total_matching, _cached):
if result == 1:
var cache_folder = DirAccess.open(cache_path)
for i in range(results_returned):
if Steam.getItemState(Steam.getQueryUGCResult(query_handle, i)["file_id"]) in [8, 9]:
Steam.downloadItem(Steam.getQueryUGCResult(query_handle, i)["file_id"], true)
games.append(Steam.getQueryUGCResult(query_handle, i))
if cache_folder:
cache_folder.list_dir_begin()
var file_name = cache_folder.get_next()
while not file_name.ends_with(str(Steam.getQueryUGCResult(query_handle, i)["handle_preview_file"]) + ".png"):
if file_name == "":
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", _on_request_completed)
http_request.set_download_file(cache_path + "/" + str(Steam.getQueryUGCResult(query_handle, i)["file_id"]) + "_" + str(Steam.getQueryUGCResult(query_handle, i)["handle_preview_file"]) + ".png")
http_request.request(Steam.getQueryUGCPreviewURL(query_handle, i))
break
file_name = cache_folder.get_next()
cache_folder.list_dir_end()
else:
OS.alert("Couldn't access the cache folder.", "Error - Gamebox")
Steam.releaseQueryUGCRequest(query_handle)
if result == 1:
if results_returned == 50:
page_number += 1
_get_games()
elif results_returned < 50:
show_games = 1
func _on_request_completed(_result, _response_code, _headers, _body):
if getting_games < 2:
getting_games += 1
func _on_item_installed(app_id, file_id):
if app_id == STEAM_APP_ID and getting_games < 2 and FileAccess.file_exists(Steam.getItemInstallInfo(file_id)["folder"].path_join("gamebox.json")):
getting_games += 1
func _add_included_games():
var image = Image.load_from_file(app_install_dir.path_join("2d.png"))
var icon_texture = ImageTexture.create_from_image(image)
$Games.add_item("2D", icon_texture)
image = Image.load_from_file(app_install_dir.path_join("3d.png"))
icon_texture = ImageTexture.create_from_image(image)
$Games.add_item("3D", icon_texture)
func _show_games():
$Games.clear()
_add_included_games()
for game in games:
var image = Image.load_from_file(cache_path + "/" + str(game["file_id"]) + "_" + str(game["handle_preview_file"]) + ".png")
var icon_texture = ImageTexture.create_from_image(image)
$Games.add_item(game["title"], icon_texture)
$Games.set_item_metadata($Games.get_item_count() - 1, game["file_id"])
if delete_unused_images == 1:
var size = games.size()
var cache_folder = DirAccess.open(cache_path)
cache_folder.list_dir_begin()
var file_name = cache_folder.get_next()
while file_name != "":
var i = 0
var id_handle = file_name.get_basename().split("_")
for game in games:
i += 1
if file_name.get_extension() == "png" and id_handle.size() == 2 and id_handle[0] == str(game["file_id"]) and id_handle[1] == str(game["handle_preview_file"]):
break
elif i == size:
cache_folder.remove(file_name)
file_name = cache_folder.get_next()
delete_unused_images = 0
func _on_games_item_activated(index):
if index in [0, 1]:
var game = "2d"
if index == 1:
game = "3d"
var ext = ".x86_64"
if OS.get_name() == "Windows":
ext = ".exe"
if OS.create_process(app_install_dir.path_join("godot/4.1gs/godot-" + GODOT_VERSIONS["4.1"] + ext), ["--main-pack", app_install_dir.path_join(game + ".pck")]) == -1:
OS.alert("This game's process creation of Godot failed.", "Error - Gamebox")
else:
var game_path = Steam.getItemInstallInfo($Games.get_item_metadata(index))["folder"]
var json = FileAccess.get_file_as_string(game_path.path_join("gamebox.json"))
var gamebox_json = JSON.parse_string(json)
if gamebox_json is Dictionary and "godot-pack" in gamebox_json and gamebox_json["godot-pack"] is String and FileAccess.file_exists(game_path.path_join(gamebox_json["godot-pack"])):
var pck = FileAccess.open(game_path.path_join(gamebox_json["godot-pack"]), FileAccess.READ)
var magic = pck.get_32()
pck.get_32()
var major = str(pck.get_32())
var minor = str(pck.get_32())
var godot_version = major + "." + minor
var file_extension = ".x86_64"
#var lib_path = ""
if magic == 0x43504447 and godot_version in GODOT_VERSIONS:
match OS.get_name():
"Linux":
if major in ["3", "2", "1"]:
file_extension = ".64"
#lib_path = OS.get_environment("LD_LIBRARY_PATH")
#OS.set_environment("LD_LIBRARY_PATH", lib_path + ":" + game_path.path_join(gamebox_json["godot-pack"]).get_base_dir())
"Windows":
file_extension = ".exe"
#lib_path = OS.get_environment("PATH")
#OS.set_environment("PATH", lib_path + ";" + game_path.path_join(gamebox_json["godot-pack"]).get_base_dir())
if OS.create_process(app_install_dir.path_join("godot/") + godot_version + "/godot-" + GODOT_VERSIONS[godot_version] + file_extension, ["--path", game_path.path_join(gamebox_json["godot-pack"].get_base_dir()), "--main-pack", game_path.path_join(gamebox_json["godot-pack"])]) == -1:
OS.alert("This game's process creation of Godot failed.", "Error - Gamebox")
# match OS.get_name():
# "Linux":
# OS.set_environment("LD_LIBRARY_PATH", lib_path)
# "Windows":
# OS.set_environment("PATH", lib_path)
else:
OS.alert("This game's .pck file can't be loaded.", "Error - Gamebox")
elif gamebox_json is Dictionary and ("linux-x64" in gamebox_json or "windows-x64" in gamebox_json) and gamebox_json.values().all(func(value): return value is String) and gamebox_json.keys().filter(func(key): return gamebox_json[key] != "").all(func(key): return FileAccess.file_exists(game_path.path_join(gamebox_json[key]))):
var os_name = ""
match OS.get_name():
"Linux":
os_name = "linux-x64"
"Windows":
os_name = "windows-x64"
if os_name in gamebox_json and gamebox_json[os_name] and OS.create_process(game_path.path_join(gamebox_json[os_name]), []) == -1:
OS.alert("This game's process creation failed.", "Error - Gamebox")
elif not os_name in gamebox_json or not gamebox_json[os_name]:
OS.alert("This game does not include a " + OS.get_name() + " executable.", "Error - Gamebox")
else:
OS.alert("This game's gamebox.json file is not properly configured.", "Error - Gamebox")
func _on_workshop_pressed():
Steam.activateGameOverlayToWebPage("https://steamcommunity.com/app/2744620/workshop/")
func _on_upload_pressed():
upload_window.show()
func _on_select_folder_pressed():
select_folder_file_dialog.show()
func _on_select_folder_file_dialog_dir_selected(dir):
folder_path_textedit.set_text(dir)
folder_path_textedit.show()
func _on_select_image_pressed():
select_image_file_dialog.show()
func _on_select_image_file_dialog_file_selected(path):
image_path_textedit.set_text(path)
image_path_textedit.show()
func _on_update_or_new_item_selected(index):
var item_text = update_or_new_button.get_item_text(index)
if item_text == "Update item":
item_id_textedit.show()
elif item_text == "New item":
item_id_textedit.set_text("")
item_id_textedit.hide()
func _on_upload_to_workshop_pressed():
upload_button.hide()
var item_text = update_or_new_button.get_item_text(update_or_new_button.get_selected())
if item_text == "Update item":
update_progress_label.set_text("Attempting upload...")
_upload_to_workshop(int(item_id_textedit.get_text()))
elif item_text == "New item":
update_progress_label.set_text("Creating new item...")
Steam.createItem(STEAM_APP_ID, Steam.WORKSHOP_FILE_TYPE_COMMUNITY)
func _upload_to_workshop(file_id):
workshop_item_id = file_id
update_handle = Steam.startItemUpdate(STEAM_APP_ID, file_id)
var folder_name = folder_path_textedit.get_text().split("/")[-1]
if update_or_new_button.get_item_text(update_or_new_button.get_selected()) == "New item":
Steam.setItemTitle(update_handle, folder_name)
Steam.setItemVisibility(update_handle, Steam.REMOTE_STORAGE_PUBLISHED_VISIBILITY_PRIVATE)
Steam.setItemContent(update_handle, folder_path_textedit.get_text())
Steam.setItemPreview(update_handle, image_path_textedit.get_text())
if item_type_button.get_selected() != 1:
Steam.setItemTags(update_handle, [item_type_button.get_item_text(item_type_button.get_selected())])
Steam.submitItemUpdate(update_handle, "")
update_progress = Steam.getItemUpdateProgress(update_handle)
if update_progress["status"] > 0:
update_progress_label.set_text("Uploading...")
upload_timer.start()
func _on_item_updated(result, accept_tos):
upload_timer.stop()
if result == 1:
update_progress_label.set_text("Upload successful.")
Steam.activateGameOverlayToWebPage("https://steamcommunity.com/sharedfiles/filedetails/?id=" + str(workshop_item_id))
else:
update_progress_label.set_text("Upload failed. Error code: " + str(result))
if accept_tos:
_open_tos()
func _on_item_created(result, file_id, accept_tos):
if result == 1:
_upload_to_workshop(file_id)
else:
update_progress_label.set_text("New item creation failed. Error code: " + str(result))
if accept_tos:
_open_tos()
func _open_tos():
update_progress_label.set_text("Error: You must accept the Steam Subscriber Agreement. Opening it now.")
Steam.activateGameOverlayToWebPage("http://steamcommunity.com/sharedfiles/workshoplegalagreement")
func _on_upload_timer_timeout():
update_progress = Steam.getItemUpdateProgress(update_handle)
if update_progress["total"] != 0:
var percent = 100 * update_progress["processed"] / update_progress["total"]
if update_progress["status"] == 2:
update_progress_label.set_text("Uploading... (preparing) " + str(percent) + "%")
elif update_progress["status"] == 3:
update_progress_label.set_text("Uploading... " + str(percent) + "%")
if update_progress["status"] == 4:
update_progress_label.set_text("Uploading... (image)")
func _on_upload_window_close_requested():
if update_progress_label.get_text().begins_with("Uploading..."):
OS.alert("Uploading... Please wait.\n\nYou can cancel the upload by quitting Gamebox.", "Alert - Gamebox")
else:
folder_path_textedit.set_text("")
folder_path_textedit.hide()
image_path_textedit.set_text("")
image_path_textedit.hide()
item_type_button.select(1)
update_or_new_button.select(0)
workshop_item_id = 0
update_handle = 0
item_id_textedit.set_text("")
item_id_textedit.show()
upload_button.show()
upload_button.set_disabled(true)
update_progress_label.set_text("")
upload_window.hide()
func _on_help_pressed():
Steam.activateGameOverlayToWebPage("https://steamcommunity.com/sharedfiles/filedetails/?id=3161344855")
func _on_about_pressed():
about_window.show()
func _on_open_changelog_pressed():
Steam.activateGameOverlayToWebPage("https://steamcommunity.com/sharedfiles/filedetails/?id=3161345597")
func _on_about_window_close_requested():
about_window.hide()
func _on_quit_pressed():
get_tree().quit()