Class: Ips::BinaryWriter
- Inherits:
-
Object
- Object
- Ips::BinaryWriter
- Defined in:
- lib/ips/binary_writer.rb
Overview
Binary data writer for modifying ROM files with patch data.
This class provides methods to write bytes at specific offsets and save the modified data to a file. It includes comprehensive error handling and validation.
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#data ⇒ StringIO
readonly
The internal StringIO object containing the binary data.
Instance Method Summary collapse
-
#initialize(data) ⇒ BinaryWriter
constructor
Creates a new BinaryWriter instance.
-
#save_to_file(path) ⇒ void
Saves the current data to a file.
-
#set_bytes(offset, bytes) ⇒ void
Writes bytes at a specific offset in the data.
Constructor Details
#initialize(data) ⇒ BinaryWriter
Creates a new BinaryWriter instance.
25 26 27 28 29 30 31 32 |
# File 'lib/ips/binary_writer.rb', line 25 def initialize(data) raise Error, "Data cannot be nil" if data.nil? raise Error, "Data must be a String" unless data.is_a?(String) @data = StringIO.new(data.b) rescue => e raise Error, "Failed to initialize BinaryWriter: #{e.}" end |
Instance Attribute Details
#data ⇒ StringIO (readonly)
Returns the internal StringIO object containing the binary data.
18 19 20 |
# File 'lib/ips/binary_writer.rb', line 18 def data @data end |
Instance Method Details
#save_to_file(path) ⇒ void
This method returns an undefined value.
Saves the current data to a file.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ips/binary_writer.rb', line 64 def save_to_file(path) raise Error, "Path cannot be nil or empty" if path.nil? || path.to_s.empty? File.binwrite(path, @data.string) rescue Errno::ENOENT => e raise Error, "Directory does not exist: #{e.}" rescue Errno::EACCES => e raise Error, "Permission denied: #{e.}" rescue Errno::ENOSPC => e raise Error, "No space left on device: #{e.}" rescue IOError, SystemCallError => e raise Error, "Failed to save file to #{path}: #{e.}" end |
#set_bytes(offset, bytes) ⇒ void
This method returns an undefined value.
Writes bytes at a specific offset in the data.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ips/binary_writer.rb', line 42 def set_bytes(offset, bytes) raise Error, "Offset cannot be nil" if offset.nil? raise Error, "Offset must be a numeric value" unless offset.is_a?(Numeric) raise Error, "Offset cannot be negative" if offset < 0 raise Error, "Bytes cannot be nil" if bytes.nil? raise Error, "Bytes must be a String" unless bytes.is_a?(String) @data.seek(offset) @data.write(bytes) rescue IOError, SystemCallError => e raise Error, "Failed to write bytes at offset #{offset}: #{e.}" end |