Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
theCapypara committed Aug 15, 2024
1 parent e99e9e6 commit 564fb85
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 119 deletions.
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mypy_path = .
explicit_package_bases = True
namespace_packages = True
check_untyped_defs = True
files = **/*.py
files = skytemple_ssb_debugger/**/*.py

[mypy-chara_wan.*]
ignore_missing_imports = True
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ Discord = "https://discord.gg/skytemple"

[project.scripts]
skytemple-ssb-debugger = "skytemple_ssb_debugger.main:main"

[tool.ruff]
line-length = 120

[tool.ruff.lint]
select = [ "W", "E", "F"]
ignore = [
"E402", # Module level import not at top of file -> gi imports may come after gi.require_version.
"E501", # Line too long. Maybe enable later.
"F841", # Unused variable: f-string magic
]
12 changes: 4 additions & 8 deletions skytemple_ssb_debugger/context/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ class AbstractDebuggerControlContext(ABC):
the SkyTemple main application.
"""

@abstractmethod
def gtk_template(self) -> type[Gtk.Template]:
"""Returns a type that can construct Gtk.Templates."""

@abstractmethod
def allows_interactive_file_management(self) -> bool:
"""Returns whether or not this context allows the user to load ROMs via the UI"""
Expand Down Expand Up @@ -124,14 +128,6 @@ def on_selected_string_changed(self, string: str):
*=cursor placed inside of string
"""

@abstractmethod
def show_ssb_script_editor(self) -> bool:
"""
Whether or not the tab for SSBScript editing should be shown in the editor.
:deprecated: Not used anymore.
"""

@abstractmethod
def open_rom(self, filename: str):
"""
Expand Down
74 changes: 30 additions & 44 deletions skytemple_ssb_debugger/context/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@
# You should have received a copy of the GNU General Public License
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations

import logging
import traceback
from threading import Lock
from typing import Optional, TYPE_CHECKING, Dict, List
from collections.abc import Iterable

import gi
from threading import Lock
from typing import TYPE_CHECKING

from explorerscript.source_map import SourceMapPositionMark
from skytemple_files.script.ssb.constants import SsbConstant
from skytemple_ssb_emulator import emulator_shutdown

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import Gtk, Adw
from ndspy.rom import NintendoDSRom

from skytemple_files.common.ppmdu_config.data import Pmd2Data
from skytemple_files.common.project_file_manager import ProjectFileManager
from skytemple_files.common.script_util import (
Expand All @@ -44,6 +38,9 @@
get_ppmdu_config_for_rom,
Capturable,
)
from skytemple_files.script.ssb.constants import SsbConstant
from skytemple_ssb_emulator import emulator_shutdown

from skytemple_ssb_debugger.context.abstract import (
AbstractDebuggerControlContext,
EXPS_KEYWORDS,
Expand All @@ -59,13 +56,19 @@
class StandaloneDebuggerControlContext(AbstractDebuggerControlContext):
"""Context for running the debugger as a standalone application."""

def __init__(self, main_window: Gtk.Window):
def __init__(self):
self._rom: NintendoDSRom | None = None
self._rom_filename: str | None = None
self._project_fm: ProjectFileManager | None = None
self._static_data: Pmd2Data | None = None
self._open_files: dict[str, SsbLoadedFile] = {}
self._main_window = main_window
self._main_window: Adw.ApplicationWindow | None = None

def set_window(self, value: Adw.ApplicationWindow):
self._main_window = value

def gtk_template(self) -> type[Gtk.Template]:
return Gtk.Template

def allows_interactive_file_management(self) -> bool:
return True
Expand All @@ -74,7 +77,6 @@ def before_quit(self) -> bool:
return True

def on_quit(self):
Gtk.main_quit()
emulator_shutdown()

def on_focus(self):
Expand All @@ -86,9 +88,6 @@ def on_blur(self):
def on_selected_string_changed(self, string: str):
pass

def show_ssb_script_editor(self) -> bool:
return False

def open_rom(self, filename: str):
self._rom = NintendoDSRom.fromFile(filename)
self._rom_filename = filename
Expand Down Expand Up @@ -144,9 +143,7 @@ def get_ssb(self, filename, ssb_file_manager: SsbFileManager) -> SsbLoadedFile:
ssb_file_manager,
self._project_fm,
)
self._open_files[filename].exps.ssb_hash = ssb_file_manager.hash(
ssb_bin
)
self._open_files[filename].exps.ssb_hash = ssb_file_manager.hash(ssb_bin)
return self._open_files[filename]

def on_script_edit(self, filename):
Expand All @@ -156,9 +153,7 @@ def save_ssb(self, filename, ssb_model, ssb_file_manager: SsbFileManager):
assert self._rom is not None
with file_load_lock:
self._check_loaded()
self._rom.setFileByName(
filename, FileType.SSB.serialize(ssb_model, self._static_data)
)
self._rom.setFileByName(filename, FileType.SSB.serialize(ssb_model, self._static_data))
self.save_rom()

def _check_loaded(self):
Expand All @@ -181,20 +176,20 @@ def edit_position_mark(
) -> bool:
self.display_error(
None,
f"Visual Position Mark editing is not supported in the standalone version of "
f"SkyTemple Script Engine Debugger.\n"
f"Please open the debugger through the SkyTemple main application "
f"instead.",
"Visual Position Mark editing is not supported in the standalone version of "
"SkyTemple Script Engine Debugger.\n"
"Please open the debugger through the SkyTemple main application "
"instead.",
)
return False

def _scene_editing_not_supported(self):
self.display_error(
None,
f"Scene editing is not supported in the standalone version of "
f"SkyTemple Script Engine Debugger.\n"
f"Please open the debugger through the SkyTemple main application "
f"instead.",
"Scene editing is not supported in the standalone version of "
"SkyTemple Script Engine Debugger.\n"
"Please open the debugger through the SkyTemple main application "
"instead.",
"Action not supported",
)

Expand All @@ -209,11 +204,8 @@ def display_error(
logger.error(error_message, exc_info=exc_info)
exc_info_str = ""
if exc_info:
exc_info_str = "\n" + "".join(
traceback.format_exception(
exc_info[0], value=exc_info[1], tb=exc_info[2]
)
)
exc_info_str = "\n" + "".join(traceback.format_exception(exc_info[0], value=exc_info[1], tb=exc_info[2]))
assert self._main_window is not None
md = self.message_dialog(
self._main_window,
Gtk.DialogFlags.DESTROY_WITH_PARENT,
Expand All @@ -222,7 +214,7 @@ def display_error(
f"{error_message}{exc_info_str}",
title=error_title,
)
md.set_position(Gtk.WindowPosition.CENTER)
# md.set_position(Gtk.WindowPosition.CENTER)
md.run()
md.destroy()

Expand All @@ -236,10 +228,7 @@ def get_special_words(self) -> Iterable[str]:
"""
assert self._static_data is not None
yield from self._static_data.script_data.op_codes__by_name.keys()
yield from (
x.name.replace("$", "")
for x in SsbConstant.collect_all(self._static_data.script_data)
)
yield from (x.name.replace("$", "") for x in SsbConstant.collect_all(self._static_data.script_data))
yield from EXPS_KEYWORDS

@staticmethod
Expand All @@ -253,10 +242,7 @@ def message_dialog(
):
kwargs.update(
{
"destroy_with_parent": (
dialog_flags & Gtk.DialogFlags.DESTROY_WITH_PARENT
)
> 0,
"destroy_with_parent": (dialog_flags & Gtk.DialogFlags.DESTROY_WITH_PARENT) > 0,
"modal": (dialog_flags & Gtk.DialogFlags.MODAL) > 0,
"use_header_bar": (dialog_flags & Gtk.DialogFlags.USE_HEADER_BAR) > 0,
"message_type": message_type,
Expand Down
1 change: 0 additions & 1 deletion skytemple_ssb_debugger/controller/global_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

from skytemple_ssb_debugger.ui_util import builder_get_assert

gi.require_version("Gtk", "3.0")

from gi.repository import Gtk

Expand Down
1 change: 0 additions & 1 deletion skytemple_ssb_debugger/controller/ground_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

GE_FILE_STORE_SCRIPT = _("Script")

gi.require_version("Gtk", "3.0")

from gi.repository import Gtk

Expand Down
2 changes: 0 additions & 2 deletions skytemple_ssb_debugger/controller/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
emulator_debug_init_breakpoint_manager,
)

gi.require_version("GtkSource", "4")
from gi.repository.GtkSource import StyleSchemeManager

from explorerscript import EXPLORERSCRIPT_EXT
Expand Down Expand Up @@ -106,7 +105,6 @@
from skytemple_files.common.i18n_util import f, _
from skytemple_ssb_debugger.ui_util import assert_not_none

gi.require_version("Gtk", "3.0")

from gi.repository import Gtk, Gdk, GLib

Expand Down
1 change: 0 additions & 1 deletion skytemple_ssb_debugger/controller/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

from skytemple_ssb_debugger.ui_util import builder_get_assert

gi.require_version("Gtk", "3.0")

from gi.repository import Gtk

Expand Down
80 changes: 19 additions & 61 deletions skytemple_ssb_debugger/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,35 @@
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations

import gi

gi.require_version("Gtk", "4.0")
gi.require_version("GtkSource", "5")
gi.require_version("Adw", "1")

import logging
import os
import sys

import gi

gi.require_version("Gtk", "3.0")
from skytemple_ssb_debugger.widget.application import SkyTempleSsbDebuggerApp

from skytemple_icons import icons
from skytemple_ssb_debugger.context.standalone import StandaloneDebuggerControlContext
from skytemple_ssb_debugger.controller.main import MainController
from skytemple_ssb_emulator import emulator_shutdown

from skytemple_ssb_debugger.ui_util import builder_get_assert

from gi.repository import Gtk, GLib
from gi.repository.Gtk import Window

from gi.repository import Gtk, Gdk

def main():
try:
if sys.platform.startswith("win"):
# Load theming under Windows
_windows_load_theme()

itheme: Gtk.IconTheme = Gtk.IconTheme.get_default()
itheme.append_search_path(os.path.abspath(icons()))
itheme.append_search_path(
os.path.abspath(os.path.join(get_debugger_data_dir(), "icons"))
)
itheme.rescan_if_needed()
def main(argv: list[str] | None = None):
if argv is None:
argv = sys.argv

# Load Builder and Window
builder = get_debugger_builder()
main_window = builder_get_assert(builder, Window, "main_window")
main_window.set_role("SkyTemple Script Engine Debugger")
GLib.set_application_name("SkyTemple Script Engine Debugger")
GLib.set_prgname("skytemple_ssb_debugger")
# TODO: Deprecated but the only way to set the app title on GNOME...?
main_window.set_wmclass(
"SkyTemple Script Engine Debugger", "SkyTemple Script Engine Debugger"
)
display = Gdk.Display.get_default()
assert display is not None
itheme = Gtk.IconTheme.get_for_display(display)
itheme.add_search_path(os.path.abspath(icons()))
itheme.add_search_path(os.path.abspath(os.path.join(get_debugger_data_dir(), "icons")))

# Load main window + controller
MainController(
builder, main_window, StandaloneDebuggerControlContext(main_window)
)

Gtk.main()
finally:
emulator_shutdown()


def get_debugger_builder() -> Gtk.Builder:
builder = Gtk.Builder()
builder.add_from_file(os.path.join(get_debugger_package_dir(), "debugger.glade"))
return builder
app = SkyTempleSsbDebuggerApp()
sys.exit(app.run(argv))


def get_debugger_package_dir():
Expand All @@ -83,21 +55,7 @@ def get_debugger_data_dir():
return os.path.join(get_debugger_package_dir(), "data")


def _windows_load_theme():
from skytemple_files.common.platform_utils.win import win_use_light_theme

settings = Gtk.Settings.get_default()
if settings is not None:
theme_name = "Windows-10-Dark-3.2-dark"
if win_use_light_theme():
theme_name = "Windows-10-3.2"
else:
settings.set_property("gtk-application-prefer-dark-theme", True)
settings.set_property("gtk-theme-name", theme_name)


if __name__ == "__main__":
# TODO: At the moment doesn't support any cli arguments.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger().setLevel(logging.INFO)
main()
Empty file.
Loading

0 comments on commit 564fb85

Please sign in to comment.