Chaos Project

Game Development => Sea of Code => Topic started by: diagostimo on February 02, 2013, 04:28:15 am

Title: script compiling?
Post by: diagostimo on February 02, 2013, 04:28:15 am
hey guys, I was wondering if anyone could offer me any insight how to compile multiple scripts into one file, like how in RPG maker, all the scripts are compiled within Scripts.rxdata, I was just wondering as I am creating my own engine sort of thing and it would be really handy to be able to have one master file that I load to get to all my scripts, same for editing them, I would probably go about making some sort of script editor that opens up the file and lets me write to it, if anyone knows how I would pull this sort of thing off that would be great, im writing in java to be specific, so maybe there is something out there that already achieves this. 
Title: Re: script compiling?
Post by: Blizzard on February 02, 2013, 04:53:48 am
They are not really compiled, they are just zipped. You can load the scripts from the file just like any other rxdata file using Marshal (you will get a Hash instance if I remember right) and then using Zlib::inflate to decompress the strings.
Title: Re: script compiling?
Post by: diagostimo on February 02, 2013, 05:57:37 am
ok cool, i did try marshal loading it but I just got a load of gobbledy goop, but knowing I need to use Zlib that helps, just tested that out in RPG maker xp and I see that everything is inside an array, so I am guessing the structure is like this:
[[script], [script], [script]]
where each script is is separated into lines(strings) right?
that is what I am guessing as the print screen console is pretty limited, I also noticed that the from the first 2 elements of the script, the first was an integer, and the second was the name, just wondering what the integer was for really? as its a high number.
that actually makes it really easy now as I can totally see how the editor separates and allows you to edit these, I guess my main question now is how does the engine turn these into scripts? like how would I convert the strings into a program?
Title: Re: script compiling?
Post by: Blizzard on February 02, 2013, 08:42:19 am
Here, just take this:

Spoiler: ShowHide
require 'zlib'

file = File.open('../main/Data/Scripts.rxdata', 'r')
scripts = Marshal.load(file)
file.close

decrypted = []
keys = []
scripts.each {|script|
puts "Processing #{script[1]}..."
s = Zlib::Inflate.inflate(script[2])
s.gsub!("\r") {''}
decrypted.push(s)
keys.push(script[1])
}

Dir.mkdir('../Scripts') rescue nil

keys.each_index {|i|
file = File.open("../Scripts/#{"%04d" % i}-#{keys[i].gsub('>', '').gsub('<', '').gsub('*', '+').gsub(':', '-').gsub('?', '!').gsub('/', '-')}.rb", 'wb')
file.write("\xEF\xBB\xBF") # UTF-8 identifier
file.write(decrypted[i])
file.close
}
file = File.open('../Scripts/Scripts.rb', 'w')
file.write("\xEF\xBB\xBF") # UTF-8 identifier
file.write(decrypted.join("\n"))
file.close

puts 'Done.'


This is the external Ruby script that I use to open a Scripts.rxdata and extract all scripts into files. Take out the code for extraction and have fun with it. You won't need "require 'zlib'" if you are using this from within RMXP.

The structure is like this:

[[INT, NAME, COMPRESSED_SCRIPT], [INT, NAME, COMPRESSED_SCRIPT], ...]

I can't remember what the first value is supposed to be.
Title: Re: script compiling?
Post by: diagostimo on February 02, 2013, 07:04:19 pm
thanks, that's awesome but what I meant is, how would I run the scripts from the file like when the game runs, say I need to use a class for something outside rmxp, instead of making a copy of the script, how could I load all the classes from there strings, like requiring all the scripts in the file, would I use eval or is eval just to execute code?
edit:
ok i tryed using eval, and it let me use the classes fine, but is this a correct way to do it? also thanks you have helped out loads i thought it would have being way more complicated  :D
Title: Re: script compiling?
Post by: Blizzard on February 02, 2013, 10:01:08 pm
Simply use one of these two:

load 'filename'

require 'filename'


eval() works, too, but these two are the proper practice to do it.
Title: Re: script compiling?
Post by: diagostimo on February 03, 2013, 12:45:04 am
what so i could just do:


require 'Scripts.rxdata'


and that would load all the classes out of the strings? i have tried that but that does not work,
here is an example of what I am doing at the moment via the ruby interpreter:


require 'zlib'
begin

File.open("Scripts.rxdata", "rb") {|f|
file = Marshal.load(f)
inflated = Zlib::Inflate.new.inflate(file[0][2])
eval(inflated)

puts inflated
}

$game_temp = Game_Temp.new
   
puts "=============================================="
puts $game_temp.choice_start
gets


end


instead could I do each require/load on the inflated string instead of using eval?
Title: Re: script compiling?
Post by: Blizzard on February 03, 2013, 06:48:16 am
No, require and load are for text files. If you want to load the scripts for Scripts.rxdata, then this script you made with eval works fine.