134 lines
4.4 KiB
GDScript3
134 lines
4.4 KiB
GDScript3
|
extends PanelContainer
|
||
|
|
||
|
const folder_icon := preload('res://theme/icon_folder.tres')
|
||
|
const allowed_exts := PoolStringArray(['bin', 'iso', 'sfc', 'srm', 'gba'])
|
||
|
var ext_icons := {
|
||
|
'bin': preload('res://theme/icon_disc.tres'),
|
||
|
'iso': preload('res://theme/icon_disc.tres'),
|
||
|
'sfc': preload('res://theme/icon_cart.tres'),
|
||
|
'gba': preload('res://theme/icon_cart.tres'),
|
||
|
}
|
||
|
var dir := Directory.new()
|
||
|
var home_path := ''
|
||
|
onready var itemlist: ItemList = $VBoxContainer/HBoxContainer/PanelContainer/ItemList
|
||
|
onready var vscroller: VScrollBar = itemlist.get_child(0)
|
||
|
onready var folder_buttons_scroller: ScrollContainer = $VBoxContainer/ScrollContainer
|
||
|
onready var folder_buttons: HBoxContainer = $VBoxContainer/ScrollContainer/folder_buttons
|
||
|
onready var btn_ok: Button = $VBoxContainer/HBoxContainer/VBoxContainer/btn_ok
|
||
|
onready var file_info: Label = $VBoxContainer/HBoxContainer/VBoxContainer/file_info
|
||
|
|
||
|
static func get_human_size(n: int) -> String:
|
||
|
if n > 0x100000000:
|
||
|
return '%.2f GiB' % (n/float(0x40000000))
|
||
|
if n > 0x400000:
|
||
|
return '%.1f MiB' % (n/float(0x100000))
|
||
|
if n > 0x1000:
|
||
|
return '%.1f KiB' % (n/float(0x400))
|
||
|
return '%d B' % n
|
||
|
|
||
|
func update_view():
|
||
|
var error = dir.list_dir_begin(true, true)
|
||
|
if error != OK:
|
||
|
print_debug(error)
|
||
|
return
|
||
|
|
||
|
for child in folder_buttons.get_children():
|
||
|
child.queue_free()
|
||
|
var path = ProjectSettings.globalize_path(dir.get_current_dir())
|
||
|
var cur_path: Array = path.split('/')
|
||
|
var l := len(cur_path)
|
||
|
for i in l:
|
||
|
var subpath = '/'.join(cur_path.slice(0, i))
|
||
|
var btn := Button.new()
|
||
|
btn.connect('pressed', self, 'activate_entry', [subpath])
|
||
|
btn.text = cur_path[i]
|
||
|
folder_buttons.add_child(btn)
|
||
|
if i == 0:
|
||
|
btn.text += '/'
|
||
|
if i == l-1:
|
||
|
btn.icon = folder_icon
|
||
|
folder_buttons_scroller.set_h_scroll(9999)
|
||
|
folder_buttons_scroller.ensure_control_visible(folder_buttons_scroller.get_h_scrollbar())
|
||
|
folder_buttons_scroller.get_h_scrollbar().update()
|
||
|
|
||
|
itemlist.clear()
|
||
|
var directories := PoolStringArray()
|
||
|
var files := []
|
||
|
while true:
|
||
|
var entry := dir.get_next()
|
||
|
if len(entry) == 0:
|
||
|
break
|
||
|
if dir.current_is_dir():
|
||
|
directories.append(entry)
|
||
|
elif '.' in entry:
|
||
|
var extension = entry.rsplit('.', true, 1)[1]
|
||
|
if extension in allowed_exts:
|
||
|
files.append([entry, ext_icons.get(extension)])
|
||
|
directories.sort()
|
||
|
files.sort()
|
||
|
for entry in directories:
|
||
|
itemlist.add_item(entry, folder_icon)
|
||
|
for entry in files:
|
||
|
itemlist.add_item(entry[0], entry[1])
|
||
|
# itemlist.sort_items_by_text()
|
||
|
|
||
|
func activate_entry(entry: String):
|
||
|
if dir.dir_exists(entry):
|
||
|
var error := dir.change_dir(entry)
|
||
|
if error == OK:
|
||
|
update_view()
|
||
|
else:
|
||
|
print_debug(error)
|
||
|
elif dir.file_exists(entry):
|
||
|
pass # Load the file
|
||
|
|
||
|
func view_entry(entry: String):
|
||
|
if dir.dir_exists(entry):
|
||
|
file_info.text = 'Filename:\n %s/\nType: Folder' % entry
|
||
|
elif dir.file_exists(entry):
|
||
|
var file := File.new()
|
||
|
var error := file.open(dir.get_current_dir() + '/' + entry, File.READ)
|
||
|
if error != OK:
|
||
|
print_debug(error)
|
||
|
return
|
||
|
var size = file.get_len()
|
||
|
var human_size = get_human_size(size)
|
||
|
var ext = entry.rsplit('.', true, 1)[1].to_lower()
|
||
|
var type = {'iso': 'CD-ROM Image', 'bin': 'Binary', 'sfc': 'SNES ROM', 'gba': 'GBA ROM', 'srm': 'SNES Savefile'}.get(ext, 'Unknown')
|
||
|
var prodcode := ''
|
||
|
if ext in ['iso', 'bin']:
|
||
|
var cd := RomLoader.loader_cd_image.new(file)
|
||
|
for key in cd.directory:
|
||
|
var s = key.trim_prefix('./')
|
||
|
var re_match := RomLoader.psx_productcode_regex.search(s)
|
||
|
if re_match:
|
||
|
prodcode = '%s\n %s\n %s' % [cd.pvd.system_identifier.strip_edges(), cd.pvd.volume_identifier.strip_edges(), re_match.get_string(1)]
|
||
|
type = 'PSX CD-ROM Image'
|
||
|
break
|
||
|
var info = ('Info:\n %s' % prodcode) if prodcode else ''
|
||
|
file_info.text = 'Filename:\n %s\nType:\n %s\nSize:\n %s\n%s' % [entry, type, human_size, info]
|
||
|
|
||
|
func _ready() -> void:
|
||
|
if OS.has_feature('windows'):
|
||
|
home_path = OS.get_environment('USERPROFILE')
|
||
|
else:
|
||
|
home_path = OS.get_environment('HOME')
|
||
|
# dir.open(OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS))
|
||
|
var error = dir.open(home_path)
|
||
|
if error == OK:
|
||
|
update_view()
|
||
|
print(ProjectSettings.globalize_path('user://'))
|
||
|
print(ProjectSettings.globalize_path(dir.get_current_dir()))
|
||
|
|
||
|
#func _process(_delta):
|
||
|
#vscroller.set_margin(MARGIN_TOP, 4)
|
||
|
#vscroller.set_margin(MARGIN_BOTTOM, -4)
|
||
|
|
||
|
|
||
|
func _on_ItemList_item_activated(index: int) -> void:
|
||
|
activate_entry(itemlist.get_item_text(index))
|
||
|
|
||
|
|
||
|
func _on_ItemList_item_selected(index: int) -> void:
|
||
|
view_entry(itemlist.get_item_text(index))
|