-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
399 lines (324 loc) · 12.8 KB
/
ui.py
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
# MIT License
#
# Copyright (c) 2024 Ravener
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
from pathlib import Path
import gi
from card_reader import (
BLOCK_SIZE,
FIRST,
get_title,
parse_header,
read_block,
verify_file,
)
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Adw, Gdk, GdkPixbuf, Gio, GLib, GObject, Gtk
MENU_XML = """
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="app-menu">
<section>
<item>
<attribute name="action">app.about</attribute>
<attribute name="label" translatable="yes">_About</attribute>
</item>
<item>
<attribute name="action">app.quit</attribute>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="accel"><Primary>Q</attribute>
</item>
</section>
</menu>
</interface>
"""
def set_pixel(image_data, x, y, color):
pixel_offset = (y * 16 + x) * 3 # Each pixel has 3 bytes (RGB)
r, g, b = color
# Update the bytes data with the new color
image_data[pixel_offset] = r
image_data[pixel_offset + 1] = g
image_data[pixel_offset + 2] = b
def get_icon(data, i):
block = read_block(data, i + 1)
icon_type = block[2]
frames = []
# First frame.
frames.append(block[128:256])
# Add second frame if available.
if icon_type >= 0x12:
frames.append(block[256:384])
# Add third frame if available.
if icon_type >= 0x13:
frames.append(block[384:512])
# The raw 16-bit CLUT palette
palette = block[96:128]
# The RGB Palette
new_palette = [0] * 16
red_channel = 0
green_channel = 0
blue_channel = 0
color_counter = 0
# Convert the palette to 8-bit RGB
# Thanks https://github.com/ShendoXT/memcardrex/blob/master/MemcardRex/GUI/iconWindow.cs#L93
for i in range(0, 32, 2):
red_channel = (palette[i] & 0x1F) << 3
green_channel = ((palette[i + 1] & 0x3) << 6) | ((palette[i] & 0xE0) >> 2)
blue_channel = (palette[i + 1] & 0x7C) << 1
new_palette[color_counter] = (red_channel, green_channel, blue_channel)
color_counter += 1
# Create the bitmap image representation
image_frames = [[0, 0, 0] * 16 * 16] # First frame.
# Second frame
if icon_type >= 0x12:
image_frames.append([0, 0, 0] * 16 * 16)
# Third frame
if icon_type >= 0x13:
image_frames.append([0, 0, 0] * 16 * 16)
byte_count = 0
for i, frame in enumerate(frames):
byte_count = 0
bitmap = image_frames[i]
for y in range(16):
for x in range(0, 16, 2):
set_pixel(bitmap, x, y, new_palette[frame[byte_count] & 0xF])
set_pixel(bitmap, x + 1, y, new_palette[frame[byte_count] >> 4])
byte_count += 1
pixbufs = []
for bitmap in image_frames:
pixbuf = GdkPixbuf.Pixbuf.new_from_data(
bitmap, GdkPixbuf.Colorspace.RGB, False, 8, 16, 16, 48
)
pixbufs.append(Gdk.Texture.new_for_pixbuf(pixbuf))
return pixbufs
class CardEntry(GObject.GObject):
def __init__(self, pixbufs, file_name, size, blocks, title):
super().__init__()
self.pixbufs = pixbufs
self.icon = Gtk.Image.new_from_paintable(self.pixbufs[0])
self.file_name = file_name
self.size = size
self.blocks = blocks
self.title = title
self.timer_id = None
if len(self.pixbufs) > 1:
self.current_index = 0
# Start the animation loop
pal_frame_rate = 25
pal_frames = 11 if len(self.pixbufs) == 3 else 16
update_interval = int((1 / pal_frame_rate) * pal_frames * 1000)
self.timer_id = GLib.timeout_add(update_interval, self.update_image)
def update_image(self):
# Update the image source with the next Pixbuf in the list
self.icon.set_from_paintable(self.pixbufs[self.current_index])
# Increment index and wrap around if necessary
self.current_index = (self.current_index + 1) % len(self.pixbufs)
# Continue the animation
return True
def do_destroy(self, _):
# Clean up
if self.timer_id:
GLib.source_remove(self.timer_id)
def bind_icon(factory, item):
icon = item.get_item().icon
item.set_child(icon)
def bind_name(factory, item):
file_name = item.get_item().file_name
region = ""
if file_name.startswith("BI"):
region = "🇯🇵 "
elif file_name.startswith("BE"):
region = "🇪🇺 "
elif file_name.startswith("BA"):
region = "🇺🇸 "
label = Gtk.Label.new(region + file_name)
label.set_halign(Gtk.Align.START)
item.set_child(label)
def bind_size(factory, item):
label = Gtk.Label.new(str(int(item.get_item().size)) + " KB")
label.set_halign(Gtk.Align.START)
item.set_child(label)
def bind_title(factory, item):
label = Gtk.Label.new(item.get_item().title)
label.set_halign(Gtk.Align.START)
item.set_child(label)
def bind_blocks(factory, item):
label = Gtk.Label.new(str(item.get_item().blocks))
label.set_halign(Gtk.Align.START)
item.set_child(label)
class PSXWindow(Adw.ApplicationWindow):
def __init__(self, application):
super().__init__(application=application)
self.set_icon_name("media-memory-sd-symbolic")
self.set_default_size(800, 500)
self.set_title("PSX Card Reader")
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
self.set_content(self.vbox)
self.headerbar = Adw.HeaderBar()
self.titlebar = Adw.WindowTitle(
title="PSX Card Reader", subtitle="No File Open"
)
self.headerbar.set_title_widget(self.titlebar)
self.vbox.append(self.headerbar)
# Menu Button
self.menu_button = Gtk.MenuButton()
self.menu_button.set_icon_name("open-menu-symbolic")
menu_model = Gtk.Builder.new_from_string(MENU_XML, -1).get_object("app-menu")
self.menu_button.set_menu_model(menu_model)
self.headerbar.pack_end(self.menu_button)
# Open Button
button_content = Adw.ButtonContent(
icon_name="document-open-symbolic", label="Open"
)
self.open_button = Gtk.Button(child=button_content)
self.open_button.add_css_class("raised")
self.open_button.connect("clicked", self.on_open)
self.headerbar.pack_start(self.open_button)
# Placeholder page
suggest_open_button = Gtk.Button(label="Open")
suggest_open_button.add_css_class("suggested-action")
suggest_open_button.add_css_class("pill")
suggest_open_button.connect("clicked", self.on_open)
clamp = Adw.Clamp(child=suggest_open_button, maximum_size=30)
self.status_page = Adw.StatusPage(
title="Open a File",
description="No files opened, open a memory card file to get started.",
icon_name="media-memory-sd-symbolic",
child=clamp,
)
self.status_page.set_vexpand(True)
# Main content container, this will hold the status at startup and allow us to easily
# swap it with real content later.
self.bin = Gtk.ScrolledWindow()
self.bin.set_child(self.status_page)
self.vbox.append(self.bin)
def on_open(self, widget):
self.file_chooser = Gtk.FileChooserNative.new(
"Open File",
self,
Gtk.FileChooserAction.OPEN,
)
file_filter = Gtk.FileFilter.new()
file_filter.set_name("Raw Memory Card Files (*.mcd)")
file_filter.add_suffix("mcd")
self.file_chooser.add_filter(file_filter)
self.file_chooser.connect("response", self.on_file)
self.file_chooser.show()
def display_card(self, data):
directories = parse_header(read_block(data, 0))
selection = Gtk.SingleSelection()
store = Gio.ListStore.new(CardEntry)
selection.set_model(store)
total_size = 8 * 1024
column_view = Gtk.ColumnView()
column_view.set_vexpand(True)
for i, directory in enumerate(directories):
if directory.state == FIRST:
name = directory.file_name
size = directory.file_size / 1024
blocks = directory.file_size // BLOCK_SIZE
title = get_title(data, i)
icon = get_icon(data, i)
total_size += directory.file_size
entry = CardEntry(icon, name, size, blocks, title)
column_view.connect("destroy", entry.do_destroy)
store.append(entry)
def create_column(name, callback):
factory = Gtk.SignalListItemFactory()
factory.connect("bind", callback)
column = Gtk.ColumnViewColumn.new(name, factory)
column.set_expand(True)
column_view.append_column(column)
create_column("Icon", bind_icon)
create_column("File Name", bind_name)
create_column("Size", bind_size)
create_column("Blocks", bind_blocks)
create_column("Title", bind_title)
column_view.set_model(selection)
self.bin.set_child(column_view)
def on_file(self, widget, response):
if response == Gtk.ResponseType.ACCEPT:
file = self.file_chooser.get_file()
# TODO: Find out how to do this via GFile
data = Path(file.get_path()).read_bytes()
# Verify if the card is good.
if not verify_file(data):
dialog = Adw.MessageDialog.new(
self,
"Invalid Memory Card",
"File is not a correct memory card or was corrupted",
)
dialog.add_response("ok", "Okay")
dialog.show()
else:
# Update titlebar to reflect the current open file.
self.titlebar.set_subtitle(file.get_basename())
self.display_card(data)
class PSXCardReader(Adw.Application):
def __init__(self):
super().__init__(
application_id="io.github.ravener.psx-card-reader",
flags=Gio.ApplicationFlags.FLAGS_NONE,
)
# Application Actions
self.create_action("about", self.on_about)
self.create_action("quit", self.on_quit, ["<primary>q"])
def do_activate(self) -> None:
active_window = self.props.active_window
if active_window:
active_window.present()
else:
self.win = PSXWindow(application=self)
self.win.present()
def create_action(self, name, callback, shortcuts=None):
"""Add an application action.
Args:
name: the name of the action
callback: the function to be called when the action is
activated
shortcuts: an optional list of accelerators
"""
action = Gio.SimpleAction.new(name, None)
action.connect("activate", callback)
self.add_action(action)
if shortcuts:
self.set_accels_for_action(f"app.{name}", shortcuts)
def on_about(self, widget, _):
about = Adw.AboutWindow(
transient_for=self.props.active_window,
application_name="PSX Card Reader",
application_icon="media-memory-sd-symbolic",
developer_name="Ravener",
version="1.0.0",
developers=["Ravener"],
copyright="© 2023 Ravener",
issue_url="https://github.com/ravener/psx-card-reader/issues",
website="https://github.com/ravener/psx-card-reader",
)
about.present()
def on_quit(self, widget, _):
self.quit()
if __name__ == "__main__":
app = PSXCardReader()
app.run(sys.argv)