More code changes for Plugin Manager

This commit is contained in:
Maruno17
2021-04-25 23:12:05 +01:00
parent 3e7ffb71a7
commit b9e7d350a2
3 changed files with 73 additions and 30 deletions

View File

@@ -33,7 +33,7 @@ def pbPrintException(e)
end end
btrace.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil btrace.gsub!(/Section(\d+)/) { $RGSS_SCRIPTS[$1.to_i][1] } rescue nil
message = "[Pokémon Essentials version #{Essentials::VERSION}]\r\n" 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 += "Exception: #{e.class}\r\n"
message += "Message: #{emessage}\r\n" message += "Message: #{emessage}\r\n"
message += "\r\nBacktrace:\r\n#{btrace}" message += "\r\nBacktrace:\r\n#{btrace}"
@@ -42,11 +42,10 @@ def pbPrintException(e)
errorlog = RTP.getSaveFileName("errorlog.txt") errorlog = RTP.getSaveFileName("errorlog.txt")
end end
File.open(errorlog,"ab") { |f| f.write(premessage); f.write(message) } File.open(errorlog,"ab") { |f| f.write(premessage); f.write(message) }
errorlogline = errorlog errorlogline = errorlog.gsub!("/", "\\")
errorlogline.sub!(Dir.pwd + "/", "") errorlogline.sub!(Dir.pwd + "\\", "")
errorlogline.sub!(pbGetUserName, "USERNAME") errorlogline.sub!(pbGetUserName, "USERNAME")
errorlogline = "\r\n" + errorlogline if errorlogline.length > 20 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.") 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 # Give a ~500ms coyote time to start holding Control
t = System.delta t = System.delta

View File

@@ -21,12 +21,17 @@ class Dir
def self.all(dir, filters = "*", full = true) def self.all(dir, filters = "*", full = true)
# sets variables for starting # sets variables for starting
files = [] files = []
subfolders = []
for file in self.get(dir, filters, full) for file in self.get(dir, filters, full)
# engages in recursion to read the entire file tree # 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 end
# returns all found files # returns all found files
return files return files + subfolders
end end
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# Checks for existing directory, gets around accents # Checks for existing directory, gets around accents

View File

@@ -428,6 +428,45 @@ module PluginManager
return 0 return 0
end 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 # Used to read the metadata file
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
def self.readMeta(dir, file) def self.readMeta(dir, file)
@@ -481,7 +520,7 @@ module PluginManager
meta[:scripts] = [] if !meta[:scripts] meta[:scripts] = [] if !meta[:scripts]
# get all script files from plugin Dir # get all script files from plugin Dir
for fl in Dir.all(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}/", "")) meta[:scripts].push(fl.gsub("#{dir}/", ""))
end end
# ensure no duplicate script files are queued # ensure no duplicate script files are queued
@@ -606,7 +645,9 @@ module PluginManager
dat = [o, meta, []] dat = [o, meta, []]
# iterate through each file to deflate # iterate through each file to deflate
for file in plugins[o][:scripts] 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 end
# push to the main scripts array # push to the main scripts array
scripts.push(dat) scripts.push(dat)
@@ -624,7 +665,6 @@ module PluginManager
order, plugins = self.getPluginOrder order, plugins = self.getPluginOrder
# compile if necessary # compile if necessary
self.compilePlugins(order, plugins) if self.needCompiling?(order, plugins) self.compilePlugins(order, plugins) if self.needCompiling?(order, plugins)
# run the plugins from compiled archive
# load plugins # load plugins
scripts = load_data("Data/PluginScripts.rxdata") scripts = load_data("Data/PluginScripts.rxdata")
for plugin in scripts for plugin in scripts
@@ -635,38 +675,37 @@ module PluginManager
# go through each script and interpret # go through each script and interpret
for scr in script for scr in script
# turn code into plaintext # turn code into plaintext
code = Zlib::Inflate.inflate(scr) code = Zlib::Inflate.inflate(scr[1])
# get rid of tabs # get rid of tabs
code.gsub!("\t", " ") code.gsub!("\t", " ")
# construct filename # construct filename
fname = "[Plugin] " + name sname = scr[0].gsub("\\","/").split("/")[-1]
fname = "[#{name}] #{sname}"
# try to run the code # try to run the code
begin begin
eval(code, TOPLEVEL_BINDING, fname) eval(code, TOPLEVEL_BINDING, fname)
rescue Exception # format error message to display rescue Exception # format error message to display
msg = "[Pokémon Essentials v#{Essentials::VERSION}] #{Essentials::ERROR_TEXT}\r\n\r\n" Graphics.update
msg += "#{$raise_msg}\r\n-------------------------------\r\n" if $raise_msg t = Thread.new do
msg += "Error in Plugin [#{name}]:\r\n" print(self.pluginErrorMsg(name, sname))
msg += "#{$!.class} occurred.\r\n" # Give a ~500ms coyote time to start holding Control
for line in $!.message.split("\r\n") tm = System.delta
next if !line || line == "" until (System.delta - tm) >= 500000
n = line[/\d+/] Input.update
err = line.split(":")[-1].strip if Input.press?(Input::CTRL)
lms = line.split(":")[0].strip Input.clipboard = message
err.gsub!(n, "") if n break
err = err.capitalize if err.is_a?(String) && !err.empty? end
linum = n ? "Line #{n}: " : "" end
msg += "#{linum}#{err}: #{lms}\r\n" Thread.exit
end end
msg += "\r\nFull trace can be found below:\r\n" while t.status
for bck in $!.backtrace Graphics.update
msg += "#{bck}\r\n"
end end
msg += "\r\nEnd of Error." Kernel.exit! true
$raise_msg = nil
raise msg
end end
end end
end end
end end
#-----------------------------------------------------------------------------
end end