<commit_before>
require_relative 'ui_gfx_editor.rb'

class GfxEditorDialog < Qt::Dialog
  slots "load_gfx_file_and_palette_list()"
  slots "gfx_page_changed(int)"
  slots "palette_changed(int)"
  slots "toggle_one_dimensional_mapping_mode(int)"
  slots "export_file()"
  slots "import_gfx()"
  slots "import_palette()"
  slots "palette_clicked(int, int, const Qt::MouseButton&)"
  slots "generate_palette_from_multiple_files()"
  
  def initialize(parent, fs, renderer, gfx_and_palette_data=nil)
    super(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
    
    @fs = fs
    @renderer = renderer
    
    @output_folder = "gfx"
    FileUtils.mkdir_p(@output_folder)
    
    @ui = Ui_GfxEditor.new
    @ui.setup_ui(self)
    
    if SYSTEM == :gba
      @ui.one_dimensional_mode.checked = true
      @ui.label_2.text = "GFX pointer(s)"
    end
    
    @gfx_file_graphics_scene = Qt::GraphicsScene.new
    @gfx_file_graphics_scene.setBackgroundBrush(ClickableGraphicsScene::BACKGROUND_BRUSH)
    @ui.gfx_file_graphics_view.setScene(@gfx_file_graphics_scene)
    
    @palette_graphics_scene = ClickableGraphicsScene.new
    @ui.palette_graphics_view.setScene(@palette_graphics_scene)
    connect(@palette_graphics_scene, SIGNAL("clicked(int, int, const Qt::MouseButton&)"), self, SLOT("palette_clicked(int, int, const Qt::MouseButton&)"))
    
    connect(@ui.view_button, SIGNAL("clicked()"), self, SLOT("load_gfx_file_and_palette_list()"))
    connect(@ui.gfx_index, SIGNAL("activated(int)"), self, SLOT("gfx_page_changed(int)"))
    connect(@ui.palette_index, SIGNAL("activated(int)"), self, SLOT("palette_changed(int)"))
    connect(@ui.one_dimensional_mode, SIGNAL("stateChanged(int)"), self, SLOT("toggle_one_dimensional_mapping_mode(int)"))
    connect(@ui.export_button, SIGNAL("clicked()"), self, SLOT("export_file()"))
    connect(@ui.import_gfx_button, SIGNAL("clicked()"), self, SLOT("import_gfx()"))
    connect(@ui.import_palette_button, SIGNAL("clicked()"), self, SLOT("import_palette()"))
    connect(@ui.generate_palette_from_files, SIGNAL("clicked()"), self, SLOT("generate_palette_from_multiple_files()"))
    
    if gfx_and_palette_data
      @ui.gfx_file_names.text = gfx_and_palette_data[:gfx_file_names]
      @ui.palette_pointer.text = "%08X" % gfx_and_palette_data[:palette_pointer]
      @ui.one_dimensional_mode.checked = !!gfx_and_palette_data[:one_dimensional_mode]
      load_gfx_file_and_palette_list()
      gfx_page_index = gfx_and_palette_data[:gfx_page_index]
      if (0..@gfx_pages.size-1).include?(gfx_page_index)
        gfx_page_changed(gfx_page_index)
      end
      if !gfx_and_palette_data[:palette_index].nil?
        palette_changed(gfx_and_palette_data[:palette_index])
      end
    end
    
    self.show()
  end
  
  def load_gfx_file_and_palette_list
    gfx_file_names = @ui.gfx_file_names.text.split(",")
    gfx_pages = []
    
    gfx_file_names.each do |gfx_file_name|
      gfx_file_name = gfx_file_name.strip
      
      if SYSTEM == :nds
        gfx_file = @fs.files_by_path[gfx_file_name]
        if gfx_file.nil?
          possible_asset_pointer = gfx_file_name.to_i(16)
          if @fs.is_pointer?(possible_asset_pointer)
            gfx_file = @fs.assets_by_pointer[possible_asset_pointer]
            if gfx_file.nil?
              possible_gfx_data_pointer = @fs.read(possible_asset_pointer+8, 4).unpack("V").first
              if @fs.is_pointer?(possible_gfx_data_pointer)
                gfx = GfxWrapper.new(possible_asset_pointer, @fs)
                gfx_pages << gfx
                next
              end
            end
          end
        end
        
        if gfx_file.nil?
          Qt::MessageBox.warning(self, "Not a file", "Couldn't find file with path: #{gfx_file_name}")
          return
        end
        
        gfx = GfxWrapper.new(gfx_file[:asset_pointer], @fs)
        gfx_pages << gfx
      else
        gfx_pointer = gfx_file_name.to_i(16)
        gfx = GfxWrapper.new(gfx_pointer, @fs)
        gfx_pages << gfx
      end
    end
    
    success = load_palettes()
    return unless success
    
    @palette_index = 0
    @gfx_index = 0
    
    @gfx_pages = gfx_pages
    
    @ui.gfx_index.clear()
    @gfx_pages.each_index do |i|
      @ui.gfx_index.addItem("%02X" % i)
    end
    
    @colors_per_palette = @gfx_pages.first.colors_per_palette
    
    load_gfx()
    
    if SYSTEM == :nds
      @ui.gfx_file_names.text = @gfx_pages.map do |gfx|
        if gfx.file
          gfx.file[:file_path]
        else
          "%08X" % gfx.gfx_pointer
        end
      end.join(", ")
    else
      @ui.gfx_file_names.text = @gfx_pages.map{|gfx| "%08X" % gfx.gfx_pointer}.join(", ")
    end
    @ui.palette_pointer.text = "%08X" % @palette_pointer
  rescue StandardError => e
    Qt::MessageBox.warning(self, "Error", "Error loading gfx:\n#{e.message}\n\n#{e.backtrace.join("\n")}")
  end<commit_msg>Fix edit crash when GFX editor runs into an error during initial load<commit_after>
require_relative 'ui_gfx_editor.rb'

class GfxEditorDialog < Qt::Dialog
  slots "load_gfx_file_and_palette_list()"
  slots "gfx_page_changed(int)"
  slots "palette_changed(int)"
  slots "toggle_one_dimensional_mapping_mode(int)"
  slots "export_file()"
  slots "import_gfx()"
  slots "import_palette()"
  slots "palette_clicked(int, int, const Qt::MouseButton&)"
  slots "generate_palette_from_multiple_files()"
  
  def initialize(parent, fs, renderer, gfx_and_palette_data=nil)
    super(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
    
    @fs = fs
    @renderer = renderer
    
    @output_folder = "gfx"
    FileUtils.mkdir_p(@output_folder)
    
    @ui = Ui_GfxEditor.new
    @ui.setup_ui(self)
    
    if SYSTEM == :gba
      @ui.one_dimensional_mode.checked = true
      @ui.label_2.text = "GFX pointer(s)"
    end
    
    @gfx_file_graphics_scene = Qt::GraphicsScene.new
    @gfx_file_graphics_scene.setBackgroundBrush(ClickableGraphicsScene::BACKGROUND_BRUSH)
    @ui.gfx_file_graphics_view.setScene(@gfx_file_graphics_scene)
    
    @palette_graphics_scene = ClickableGraphicsScene.new
    @ui.palette_graphics_view.setScene(@palette_graphics_scene)
    connect(@palette_graphics_scene, SIGNAL("clicked(int, int, const Qt::MouseButton&)"), self, SLOT("palette_clicked(int, int, const Qt::MouseButton&)"))
    
    connect(@ui.view_button, SIGNAL("clicked()"), self, SLOT("load_gfx_file_and_palette_list()"))
    connect(@ui.gfx_index, SIGNAL("activated(int)"), self, SLOT("gfx_page_changed(int)"))
    connect(@ui.palette_index, SIGNAL("activated(int)"), self, SLOT("palette_changed(int)"))
    connect(@ui.one_dimensional_mode, SIGNAL("stateChanged(int)"), self, SLOT("toggle_one_dimensional_mapping_mode(int)"))
    connect(@ui.export_button, SIGNAL("clicked()"), self, SLOT("export_file()"))
    connect(@ui.import_gfx_button, SIGNAL("clicked()"), self, SLOT("import_gfx()"))
    connect(@ui.import_palette_button, SIGNAL("clicked()"), self, SLOT("import_palette()"))
    connect(@ui.generate_palette_from_files, SIGNAL("clicked()"), self, SLOT("generate_palette_from_multiple_files()"))
    
    self.show()
    
    if gfx_and_palette_data
      @ui.gfx_file_names.text = gfx_and_palette_data[:gfx_file_names]
      @ui.palette_pointer.text = "%08X" % gfx_and_palette_data[:palette_pointer]
      @ui.one_dimensional_mode.checked = !!gfx_and_palette_data[:one_dimensional_mode]
      success = load_gfx_file_and_palette_list()
      if !success
        self.close()
        return
      end
      
      gfx_page_index = gfx_and_palette_data[:gfx_page_index]
      if (0..@gfx_pages.size-1).include?(gfx_page_index)
        gfx_page_changed(gfx_page_index)
      end
      if !gfx_and_palette_data[:palette_index].nil?
        palette_changed(gfx_and_palette_data[:palette_index])
      end
    end
  end
  
  def load_gfx_file_and_palette_list
    gfx_file_names = @ui.gfx_file_names.text.split(",")
    gfx_pages = []
    
    gfx_file_names.each do |gfx_file_name|
      gfx_file_name = gfx_file_name.strip
      
      if SYSTEM == :nds
        gfx_file = @fs.files_by_path[gfx_file_name]
        if gfx_file.nil?
          possible_asset_pointer = gfx_file_name.to_i(16)
          if @fs.is_pointer?(possible_asset_pointer)
            gfx_file = @fs.assets_by_pointer[possible_asset_pointer]
            if gfx_file.nil?
              possible_gfx_data_pointer = @fs.read(possible_asset_pointer+8, 4).unpack("V").first
              if @fs.is_pointer?(possible_gfx_data_pointer)
                gfx = GfxWrapper.new(possible_asset_pointer, @fs)
                gfx_pages << gfx
                next
              end
            end
          end
        end
        
        if gfx_file.nil?
          Qt::MessageBox.warning(self, "Not a file", "Couldn't find file with path: #{gfx_file_name}")
          return
        end
        
        gfx = GfxWrapper.new(gfx_file[:asset_pointer], @fs)
        gfx_pages << gfx
      else
        gfx_pointer = gfx_file_name.to_i(16)
        gfx = GfxWrapper.new(gfx_pointer, @fs)
        gfx_pages << gfx
      end
    end
    
    success = load_palettes()
    return unless success
    
    @palette_index = 0
    @gfx_index = 0
    
    @gfx_pages = gfx_pages
    
    @ui.gfx_index.clear()
    @gfx_pages.each_index do |i|
      @ui.gfx_index.addItem("%02X" % i)
    end
    
    @colors_per_palette = @gfx_pages.first.colors_per_palette
    
    load_gfx()
    
    if SYSTEM == :nds
      @ui.gfx_file_names.text = @gfx_pages.map do |gfx|
        if gfx.file
          gfx.file[:file_path]
        else
          "%08X" % gfx.gfx_pointer
        end
      end.join(", ")
    else
      @ui.gfx_file_names.text = @gfx_pages.map{|gfx| "%08X" % gfx.gfx_pointer}.join(", ")
    end
    @ui.palette_pointer.text = "%08X" % @palette_pointer
    
    return true
  rescue StandardError => e
    Qt::MessageBox.warning(self, "Error", "Error loading gfx:\n#{e.message}\n\n#{e.backtrace.join("\n")}")
    return false
  end
{'fraction_non_alphanumeric': 0.12000800720648584, 'fraction_numerical': 0.004203783405064558, 'mean_word_length': 3.257349808265871, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1334216', 'n_tokens_mistral': 3569, 'n_tokens_neox': 3634, 'n_words': 519}