From b9e7d350a2d04a9dbe91a27e584ffc7d1c437d12 Mon Sep 17 00:00:00 2001 From: Maruno17 Date: Sun, 25 Apr 2021 23:12:05 +0100 Subject: [PATCH] More code changes for Plugin Manager --- .../001_Technical/001_Debugging/003_Errors.rb | 7 +- .../001_Technical/002_Files/001_FileTests.rb | 9 +- .../001_Technical/005_PluginManager.rb | 87 ++++++++++++++----- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb b/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb index 606bcb43e..0df26af22 100644 --- a/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb +++ b/Data/Scripts/001_Technical/001_Debugging/003_Errors.rb @@ -33,7 +33,7 @@ def pbPrintException(e) end btrace.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil message = "[Pokémon Essentials version #{Essentials::VERSION}]\r\n" - message += "#{Essentials::ERROR_TEXT}" # For third party scripts to add to + message += "#{Essentials::ERROR_TEXT}\r\n" # For third party scripts to add to message += "Exception: #{e.class}\r\n" message += "Message: #{emessage}\r\n" message += "\r\nBacktrace:\r\n#{btrace}" @@ -42,11 +42,10 @@ def pbPrintException(e) errorlog = RTP.getSaveFileName("errorlog.txt") end File.open(errorlog,"ab") { |f| f.write(premessage); f.write(message) } - errorlogline = errorlog - errorlogline.sub!(Dir.pwd + "/", "") + errorlogline = errorlog.gsub!("/", "\\") + errorlogline.sub!(Dir.pwd + "\\", "") errorlogline.sub!(pbGetUserName, "USERNAME") errorlogline = "\r\n" + errorlogline if errorlogline.length > 20 - errorlogline.gsub!("/", "\\") if System.platform[/Windows/] print("#{message}\r\nThis exception was logged in #{errorlogline}.\r\nHold Ctrl after closing this message to copy it to the clipboard.") # Give a ~500ms coyote time to start holding Control t = System.delta diff --git a/Data/Scripts/001_Technical/002_Files/001_FileTests.rb b/Data/Scripts/001_Technical/002_Files/001_FileTests.rb index b90527413..938e3ef3c 100644 --- a/Data/Scripts/001_Technical/002_Files/001_FileTests.rb +++ b/Data/Scripts/001_Technical/002_Files/001_FileTests.rb @@ -21,12 +21,17 @@ class Dir def self.all(dir, filters = "*", full = true) # sets variables for starting files = [] + subfolders = [] for file in self.get(dir, filters, full) # engages in recursion to read the entire file tree - files += self.safe?(file) ? self.get(file, filters, full) : [file] + if self.safe?(file) # Is a directory + subfolders += self.all(file, filters, full) + else # Is a file + files += [file] + end end # returns all found files - return files + return files + subfolders end #----------------------------------------------------------------------------- # Checks for existing directory, gets around accents diff --git a/Data/Scripts/001_Technical/005_PluginManager.rb b/Data/Scripts/001_Technical/005_PluginManager.rb index 9bff0b7b5..a95125ea2 100644 --- a/Data/Scripts/001_Technical/005_PluginManager.rb +++ b/Data/Scripts/001_Technical/005_PluginManager.rb @@ -428,6 +428,45 @@ module PluginManager return 0 end #----------------------------------------------------------------------------- + # formats the error message + #----------------------------------------------------------------------------- + def self.pluginErrorMsg(name, script) + # begin message formatting + msg = "[Pokémon Essentials version #{Essentials::VERSION}]\r\n" + msg += "#{Essentials::ERROR_TEXT}\r\n" # For third party scripts to add to + msg += "Error in Plugin [#{name}]:\r\n" + msg += "#{$!.class} occurred.\r\n" + # go through message content + for line in $!.message.split("\r\n") + next if !line || line == "" + n = line[/\d+/] + err = line.split(":")[-1].strip + lms = line.split(":")[0].strip + err.gsub!(n, "") if n + err = err.capitalize if err.is_a?(String) && !err.empty? + linum = n ? "Line #{n}: " : "" + msg += "#{linum}#{err}: #{lms}\r\n" + end + # show trace + msg += "\r\nFull trace can be found below:\r\n" + for bck in $!.backtrace + msg += "#{bck}\r\n" + end + # output to log + errorlog = "errorlog.txt" + if (Object.const_defined?(:RTP) rescue false) + errorlog = RTP.getSaveFileName("errorlog.txt") + end + premessage = "\r\n=================\r\n\r\n[#{Time.now}]\r\n" + File.open(errorlog, "ab") { |f| f.write(premessage); f.write(msg) } + errorlogline = errorlog.gsub!("/", "\\") + errorlogline.sub!(Dir.pwd + "\\", "") + errorlogline.sub!(pbGetUserName, "USERNAME") + errorlogline = "\r\n" + errorlogline if errorlogline.length > 20 + # output message + return "#{msg}\r\nThis exception was logged in #{errorlogline}.\r\nHold Ctrl after closing this message to copy it to the clipboard." + end + #----------------------------------------------------------------------------- # Used to read the metadata file #----------------------------------------------------------------------------- def self.readMeta(dir, file) @@ -481,7 +520,7 @@ module PluginManager meta[:scripts] = [] if !meta[:scripts] # get all script files from plugin Dir for fl in Dir.all(dir) - next if !File.extname(fl).include?(".rb") + next if !fl.include?(".rb") meta[:scripts].push(fl.gsub("#{dir}/", "")) end # ensure no duplicate script files are queued @@ -606,7 +645,9 @@ module PluginManager dat = [o, meta, []] # iterate through each file to deflate for file in plugins[o][:scripts] - File.open("#{plugins[o][:dir]}/#{file}", 'rb') { |f| dat[2].push(Zlib::Deflate.deflate(f.read)) } + File.open("#{plugins[o][:dir]}/#{file}", 'rb') do |f| + dat[2].push([file, Zlib::Deflate.deflate(f.read)]) + end end # push to the main scripts array scripts.push(dat) @@ -624,7 +665,6 @@ module PluginManager order, plugins = self.getPluginOrder # compile if necessary self.compilePlugins(order, plugins) if self.needCompiling?(order, plugins) - # run the plugins from compiled archive # load plugins scripts = load_data("Data/PluginScripts.rxdata") for plugin in scripts @@ -635,38 +675,37 @@ module PluginManager # go through each script and interpret for scr in script # turn code into plaintext - code = Zlib::Inflate.inflate(scr) + code = Zlib::Inflate.inflate(scr[1]) # get rid of tabs code.gsub!("\t", " ") # construct filename - fname = "[Plugin] " + name + sname = scr[0].gsub("\\","/").split("/")[-1] + fname = "[#{name}] #{sname}" # try to run the code begin eval(code, TOPLEVEL_BINDING, fname) rescue Exception # format error message to display - msg = "[Pokémon Essentials v#{Essentials::VERSION}] #{Essentials::ERROR_TEXT}\r\n\r\n" - msg += "#{$raise_msg}\r\n-------------------------------\r\n" if $raise_msg - msg += "Error in Plugin [#{name}]:\r\n" - msg += "#{$!.class} occurred.\r\n" - for line in $!.message.split("\r\n") - next if !line || line == "" - n = line[/\d+/] - err = line.split(":")[-1].strip - lms = line.split(":")[0].strip - err.gsub!(n, "") if n - err = err.capitalize if err.is_a?(String) && !err.empty? - linum = n ? "Line #{n}: " : "" - msg += "#{linum}#{err}: #{lms}\r\n" + Graphics.update + t = Thread.new do + print(self.pluginErrorMsg(name, sname)) + # Give a ~500ms coyote time to start holding Control + tm = System.delta + until (System.delta - tm) >= 500000 + Input.update + if Input.press?(Input::CTRL) + Input.clipboard = message + break + end + end + Thread.exit end - msg += "\r\nFull trace can be found below:\r\n" - for bck in $!.backtrace - msg += "#{bck}\r\n" + while t.status + Graphics.update end - msg += "\r\nEnd of Error." - $raise_msg = nil - raise msg + Kernel.exit! true end end end end + #----------------------------------------------------------------------------- end