Skip to content

Commit

Permalink
Add Lua Search Window
Browse files Browse the repository at this point in the history
Searches for text in all the level's script files and shows script name, line and line content
  • Loading branch information
RenolY2 committed Dec 17, 2024
1 parent 2aa1000 commit 97d225e
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
89 changes: 89 additions & 0 deletions widgets/lua_search_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os
import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui
import PyQt6.QtCore as QtCore
from widgets.editor_widgets import SearchBar
from lib.lua.luaworkshop import LuaWorkbench
from widgets.editor_widgets import open_message_dialog


class LuaSearchBar(SearchBar):
def __init__(self, parent):
super().__init__(parent)
self.case_sensitive = QtWidgets.QCheckBox(self)
self.case_sensitive_text = QtWidgets.QLabel("Case Sensitive", self)
self.l.addWidget(self.case_sensitive)
self.l.addWidget(self.case_sensitive_text)

def is_case_sensitive(self):
return self.case_sensitive.isChecked()


class LuaSearchResultItem(QtWidgets.QTreeWidgetItem):
def __init__(self, parent, script, line, text):
super().__init__(parent)
self.script = script
self.line = line
self.textcontent = text
self.setText(0, self.script)
self.setText(1, self.line)
self.setText(2, self.textcontent)


class LuaFindWindow(QtWidgets.QMdiSubWindow):
def __init__(self, parent, luaworkbench: LuaWorkbench):
super().__init__(parent)
self.resize(900, 500)
self.setWindowTitle("Lua Script Search")
self.centralwidget = QtWidgets.QWidget(self)
self.vbox = QtWidgets.QVBoxLayout(self)
self.searchbar = LuaSearchBar(self)
self.results = QtWidgets.QTreeWidget(self)
self.results.setColumnCount(3)
self.results.setHeaderLabels(["File", "Line", "Content"])

self.vbox.addWidget(self.searchbar)
self.vbox.addWidget(self.results)
self.centralwidget.setLayout(self.vbox)
self.luaworkbench = luaworkbench
self.setWidget(self.centralwidget)
self.searchbar.find.connect(self.search_line)

self.results.itemDoubleClicked.connect(self.open_script)

def open_script(self, item):
lua_script_name = item.script.removesuffix(".lua")
self.luaworkbench.open_script(lua_script_name)

def search_line(self, text):
root = self.results.invisibleRootItem()
root.takeChildren()

if text:
if not self.searchbar.is_case_sensitive():
text = text.lower()
case_sensitive = False
else:
case_sensitive = True

results = []
for script_path in self.luaworkbench.get_lua_script_paths():
with open(script_path, "r") as f:
for i, line in enumerate(f):
if case_sensitive:
if text in line:
results.append((os.path.basename(script_path), i+1, line))
else:
if text in line.lower():
results.append((os.path.basename(script_path), i + 1, line))

for script, line, text in results:
item = LuaSearchResultItem(self.results,
script.strip(), str(line), text.strip())
self.results.addTopLevelItem(item)

if len(results) == 0:
open_message_dialog("No results found.")
else:
self.results.resizeColumnToContents(0)
self.results.resizeColumnToContents(1)
52 changes: 52 additions & 0 deletions widgets/menu/menubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from widgets.editor_widgets import open_error_dialog
from typing import TYPE_CHECKING
from lib.game_visualizer import DebugInfoWIndow
from widgets.editor_widgets import open_message_dialog, open_yesno_box
from configuration import save_cfg
from widgets.lua_search_widgets import LuaFindWindow

if TYPE_CHECKING:
import bw_editor

Expand All @@ -22,6 +26,7 @@ def __init__(self, editor):

self.search_window = None
self.debug_window = None
self.lua_find_menu = None

self.visibility_menu = FilterViewMenu(self)
self.visibility_menu.filter_update.connect(self.editor.update_render)
Expand Down Expand Up @@ -61,6 +66,8 @@ def __init__(self, editor):
#self.choose_bco_area = QtWidgets.QAction("Highlight Collision Area (BCO)")

self.dolphin_menu = Menu(self, "Dolphin (Experimental)")
self.start_game_action = self.dolphin_menu.add_action("Start Game in Dolphin",
self.run_game)
self.show_debug_info_action = self.dolphin_menu.add_action("Show Freelist Info",
self.open_debug_window)
self.hook_game_action = self.dolphin_menu.add_action("Enable Live Edit",
Expand All @@ -77,6 +84,8 @@ def __init__(self, editor):
self.apply_live_positions)

self.lua_menu = Menu(self, "Lua")
self.lua_open_find_action = self.lua_menu.add_action("Open Lua Script Search",
self.lua_open_find)
self.lua_open_entity_init_action = self.lua_menu.add_action("Open EntityInitialise",
self.lua_open_entity_initialise)
self.lua_open_workdir_action = self.lua_menu.add_action("Open Lua Script Folder",
Expand All @@ -99,6 +108,49 @@ def __init__(self, editor):

self.last_obj_select_pos = 0

def lua_open_find(self):
self.lua_find_menu = LuaFindWindow(None, self.editor.lua_workbench)
self.lua_find_menu.show()

def run_game(self):
dolphin_path = self.editor.configuration["editor"].get("dolphin", fallback=None)
if dolphin_path is None:
open_message_dialog("Dolphin not found, please choose location of Dolphin's executable.")
filepath, chosentype = QtWidgets.QFileDialog.getOpenFileName(
self, "Choose Dolphin",
"",
"Dolphin executable (Dolphin.exe);;All files (*)",
None)

if filepath:
self.editor.configuration["editor"]["dolphin"] = filepath
save_cfg(self.editor.configuration)
dolphin_path = filepath

if dolphin_path is not None:
data_folder = os.path.dirname(os.path.dirname(self.editor.file_menu.current_path))
game_base_folder = os.path.dirname(os.path.dirname(data_folder))
game_executable = os.path.join(game_base_folder, "sys", "main.dol")
print(data_folder)
print(game_base_folder)
print(dolphin_path)
print(game_executable)
try:
subprocess.Popen([dolphin_path, game_executable])
except FileNotFoundError:
if open_yesno_box("Dolphin not found.\nDo you want to choose a new location for Dolphin?"):
filepath, chosentype = QtWidgets.QFileDialog.getOpenFileName(
self, "Choose Dolphin",
"",
"Dolphin executable (Dolphin.exe);;All files (*)",
None)
if filepath:
self.editor.configuration["editor"]["dolphin"] = filepath
save_cfg(self.editor.configuration)
open_message_dialog("New Dolphin location chosen. Use 'Start Game in Dolphin' again.")
else:
open_error_dialog("No Dolphin path chosen, can't start Dolphin.")

def lua_reload_scripts(self):
msgbox = QtWidgets.QMessageBox()
msgbox.setText("Reloading the scripts from the level's resource file will overwrite your current scripts!")
Expand Down

0 comments on commit 97d225e

Please sign in to comment.