ips-patcher 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7da78146e2a5464fe4ed96f349275ee65216f41bdf8e027c8e4ee7d3047aa52f
4
+ data.tar.gz: 785fd7b0986a2723258883d25d3fb4e04a5d62590ec9d9aac6a545dc172b8755
5
+ SHA512:
6
+ metadata.gz: 4b42b64edb05084a18e3ee10a31fae908e7d5d22e11c4fef7d6f87d941e8e9b5aff9f2d611a424b041b741fa8965e4266485b38392f4d4a89ce37307e34136f9
7
+ data.tar.gz: dbef5867bbc2404732f76eec75d665f771d06996c3e1e90d406f3c8d09ebd2b968a68a0da5c7402d6a4efbcbcb52e675d65a8e75dcf4007dcc2262ab00eee050
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$" />
8
+ <orderEntry type="inheritedJdk" />
9
+ <orderEntry type="sourceFolder" forTests="false" />
10
+ </component>
11
+ </module>
data/.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-3.2.2" project-jdk-type="RUBY_SDK" />
4
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/ips-patcher.iml" filepath="$PROJECT_DIR$/.idea/ips-patcher.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.yardopts ADDED
@@ -0,0 +1,11 @@
1
+ --markup markdown
2
+ --no-private
3
+ --protected
4
+ --readme README.md
5
+ --output-dir doc
6
+ --exclude spec/
7
+ --exclude sig/
8
+ lib/**/*.rb
9
+ -
10
+ LICENSE
11
+ CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-01-20
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 meruen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Ips::Patcher
2
+
3
+ A Ruby library for applying IPS (International Patching System) patches to video game ROM files. IPS is a binary patch format commonly used to distribute patches for ROM images, such as translation patches, bug fixes, or game modifications.
4
+
5
+ ## Features
6
+
7
+ - ✅ Apply IPS patch files to ROM images
8
+ - ✅ Support for standard IPS records (byte replacement)
9
+ - ✅ Support for RLE (Run-Length Encoding) records (efficient byte filling)
10
+ - ✅ Non-destructive patching (creates new file, original remains unchanged)
11
+ - ✅ Comprehensive error handling
12
+ - ✅ Pure Ruby implementation with no external dependencies
13
+
14
+ ## Installation
15
+
16
+ Add this gem to your application's Gemfile:
17
+
18
+ ```bash
19
+ bundle add ips-patcher
20
+ ```
21
+
22
+ Or install it directly:
23
+
24
+ ```bash
25
+ gem install ips-patcher
26
+ ```
27
+
28
+ ## Requirements
29
+
30
+ - Ruby >= 3.1.0
31
+
32
+ ## Usage
33
+
34
+ ### Basic Usage
35
+
36
+ The simplest way to apply an IPS patch to a ROM file:
37
+
38
+ ```ruby
39
+ require 'ips/patcher'
40
+
41
+ # Apply a patch to a ROM file
42
+ Ips::Patcher.apply("game.rom", "patch.ips")
43
+ # Creates: game.patched.rom
44
+ ```
45
+
46
+ The patcher reads the ROM file and the IPS patch file, applies all patch records, and creates a new patched file. The output filename is automatically generated by inserting `.patched` before the file extension.
47
+
48
+ ### Example
49
+
50
+ ```ruby
51
+ require 'ips/patcher'
52
+
53
+ # Apply a translation patch to a Super Nintendo ROM
54
+ Ips::Patcher.apply("super_metroid.sfc", "translation_jp_to_en.ips")
55
+ # Output: super_metroid.patched.sfc
56
+ ```
57
+
58
+ ### How It Works
59
+
60
+ 1. Reads the ROM file and IPS patch file as binary data
61
+ 2. Validates the patch file has a valid "PATCH" header
62
+ 3. Processes each patch record:
63
+ - **Standard records**: Replace bytes at a specific offset
64
+ - **RLE records**: Fill bytes with a repeated value (when size is 0)
65
+ 4. Creates a new patched ROM file with `.patched` inserted before the extension
66
+ 5. The original ROM file remains unchanged
67
+
68
+ ## IPS Format
69
+
70
+ The IPS format is a simple binary patch format that stores:
71
+ - A "PATCH" header (5 bytes)
72
+ - One or more patch records containing:
73
+ - Offset (3 bytes, 24-bit big-endian)
74
+ - Size (2 bytes, 16-bit big-endian)
75
+ - Data bytes (if size > 0) or RLE size and value (if size == 0)
76
+ - An "EOF" marker (3 bytes)
77
+
78
+ For more information about the IPS format specification, see: https://zerosoft.zophar.net/ips.php
79
+
80
+ ## Error Handling
81
+
82
+ The library raises `Ips::Patcher::Error` for patcher-specific errors:
83
+
84
+ ```ruby
85
+ begin
86
+ Ips::Patcher.apply("game.rom", "patch.ips")
87
+ rescue Ips::Patcher::Error => e
88
+ puts "Error applying patch: #{e.message}"
89
+ end
90
+ ```
91
+
92
+ Common errors include:
93
+ - Invalid patch file (missing or incorrect "PATCH" header)
94
+ - File not found
95
+ - Permission errors
96
+ - IO errors
97
+
98
+ ## Development
99
+
100
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
101
+
102
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
103
+
104
+ ## Documentation
105
+
106
+ API documentation is available and can be generated using YARD.
107
+
108
+ To generate the documentation:
109
+
110
+ ```bash
111
+ bundle exec rake doc
112
+ ```
113
+
114
+ Or using YARD directly:
115
+
116
+ ```bash
117
+ bundle exec yard doc
118
+ ```
119
+
120
+ To view the documentation locally:
121
+
122
+ ```bash
123
+ bundle exec yard server
124
+ ```
125
+
126
+ Then open your browser to `http://localhost:8808`.
127
+
128
+ The generated documentation will be available in the `doc/` directory.
129
+
130
+ ## Contributing
131
+
132
+ Bug reports and pull requests are welcome on GitHub at https://github.com/meruen/ips-patcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/meruen/ips-patcher/blob/main/CODE_OF_CONDUCT.md).
133
+
134
+ ## License
135
+
136
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
137
+
138
+ ## Code of Conduct
139
+
140
+ Everyone interacting in the Ips::Patcher project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/meruen/ips-patcher/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ begin
9
+ require "yard"
10
+ YARD::Rake::YardocTask.new(:doc) do |t|
11
+ t.files = ["lib/**/*.rb"]
12
+ t.options = ["--readme", "README.md"]
13
+ end
14
+ rescue LoadError
15
+ # YARD is not available
16
+ end
17
+
18
+ task default: :spec
@@ -0,0 +1,72 @@
1
+ # Namespace for IPS (International Patching System) patcher functionality.
2
+ #
3
+ # This module provides classes and methods for reading, parsing, and applying
4
+ # IPS patch files to binary ROM images. The IPS format is commonly used to
5
+ # distribute patches for video game ROM files.
6
+ #
7
+ # @see Ips::Patcher The main module for applying patches
8
+ # @see Ips::BinaryReader Reader for parsing binary data
9
+ # @see Ips::BinaryWriter Writer for modifying binary data
10
+ module Ips
11
+ # Binary data reader for parsing IPS patch files.
12
+ #
13
+ # This class provides methods to read various data types from binary data,
14
+ # including strings, integers of different sizes, and raw bytes.
15
+ #
16
+ # @example Reading from binary data
17
+ # data = File.binread("patch.ips")
18
+ # reader = Ips::BinaryReader.new(data)
19
+ # header = reader.read_string(5) # => "PATCH"
20
+ class BinaryReader
21
+ # @return [StringIO] the internal StringIO object containing the binary data
22
+ attr_reader :data
23
+
24
+ # Creates a new BinaryReader instance.
25
+ #
26
+ # @param data [String] the binary data to read from
27
+ # @return [BinaryReader] a new instance of BinaryReader
28
+ def initialize(data)
29
+ @data = StringIO.new(data)
30
+ end
31
+
32
+ # Reads a string of specified size from the current position.
33
+ #
34
+ # @param size [Integer] the number of bytes to read
35
+ # @return [String] the string read from the data
36
+ def read_string(size)
37
+ @data.read(size)
38
+ end
39
+
40
+ # Reads a 24-bit big-endian integer (3 bytes) from the current position.
41
+ #
42
+ # @return [Integer] the 24-bit integer value
43
+ def read_int24
44
+ bytes = @data.read(3).bytes
45
+ (bytes[0] << 16) | (bytes[1] << 8) | bytes[2]
46
+ end
47
+
48
+ # Reads a 16-bit big-endian integer (2 bytes) from the current position.
49
+ #
50
+ # @return [Integer] the 16-bit integer value
51
+ def read_int16
52
+ bytes = @data.read(2).bytes
53
+ (bytes[0] << 8) | bytes[1]
54
+ end
55
+
56
+ # Reads a specified number of raw bytes from the current position.
57
+ #
58
+ # @param n_bytes [Integer] the number of bytes to read
59
+ # @return [String] the raw bytes as a binary string
60
+ def read_bytes(n_bytes)
61
+ @data.read(n_bytes)
62
+ end
63
+
64
+ # Moves the read position backwards by a specified number of bytes.
65
+ #
66
+ # @param n_bytes [Integer] the number of bytes to move back
67
+ # @return [Integer] the new position in the stream
68
+ def go_back(n_bytes)
69
+ @data.seek(-n_bytes, IO::SEEK_CUR)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,78 @@
1
+ module Ips
2
+ # Binary data writer for modifying ROM files with patch data.
3
+ #
4
+ # This class provides methods to write bytes at specific offsets
5
+ # and save the modified data to a file. It includes comprehensive
6
+ # error handling and validation.
7
+ #
8
+ # @example Writing bytes and saving
9
+ # rom_data = File.binread("game.rom")
10
+ # writer = Ips::BinaryWriter.new(rom_data)
11
+ # writer.set_bytes(0x1000, "\x42\x43\x44")
12
+ # writer.save_to_file("game_patched.rom")
13
+ class BinaryWriter
14
+ # Error class for BinaryWriter-specific errors
15
+ class Error < StandardError; end
16
+
17
+ # @return [StringIO] the internal StringIO object containing the binary data
18
+ attr_reader :data
19
+
20
+ # Creates a new BinaryWriter instance.
21
+ #
22
+ # @param data [String] the binary data to write to
23
+ # @raise [Error] if data is nil or not a String
24
+ # @return [BinaryWriter] a new instance of BinaryWriter
25
+ def initialize(data)
26
+ raise Error, "Data cannot be nil" if data.nil?
27
+ raise Error, "Data must be a String" unless data.is_a?(String)
28
+
29
+ @data = StringIO.new(data.b)
30
+ rescue => e
31
+ raise Error, "Failed to initialize BinaryWriter: #{e.message}"
32
+ end
33
+
34
+ # Writes bytes at a specific offset in the data.
35
+ #
36
+ # @param offset [Integer] the position to write the bytes at
37
+ # @param bytes [String] the bytes to write
38
+ # @raise [Error] if offset is nil, not numeric, or negative
39
+ # @raise [Error] if bytes is nil or not a String
40
+ # @raise [Error] if an IO or system error occurs during writing
41
+ # @return [void]
42
+ def set_bytes(offset, bytes)
43
+ raise Error, "Offset cannot be nil" if offset.nil?
44
+ raise Error, "Offset must be a numeric value" unless offset.is_a?(Numeric)
45
+ raise Error, "Offset cannot be negative" if offset < 0
46
+ raise Error, "Bytes cannot be nil" if bytes.nil?
47
+ raise Error, "Bytes must be a String" unless bytes.is_a?(String)
48
+
49
+ @data.seek(offset)
50
+ @data.write(bytes)
51
+ rescue IOError, SystemCallError => e
52
+ raise Error, "Failed to write bytes at offset #{offset}: #{e.message}"
53
+ end
54
+
55
+ # Saves the current data to a file.
56
+ #
57
+ # @param path [String] the file path to save to
58
+ # @raise [Error] if path is nil or empty
59
+ # @raise [Error] if the directory does not exist
60
+ # @raise [Error] if permission is denied
61
+ # @raise [Error] if no space is left on the device
62
+ # @raise [Error] if an IO or system error occurs during saving
63
+ # @return [void]
64
+ def save_to_file(path)
65
+ raise Error, "Path cannot be nil or empty" if path.nil? || path.to_s.empty?
66
+
67
+ File.binwrite(path, @data.string)
68
+ rescue Errno::ENOENT => e
69
+ raise Error, "Directory does not exist: #{e.message}"
70
+ rescue Errno::EACCES => e
71
+ raise Error, "Permission denied: #{e.message}"
72
+ rescue Errno::ENOSPC => e
73
+ raise Error, "No space left on device: #{e.message}"
74
+ rescue IOError, SystemCallError => e
75
+ raise Error, "Failed to save file to #{path}: #{e.message}"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ips
4
+ # IPS (International Patching System) patcher module for applying binary patches to ROM files.
5
+ module Patcher
6
+ # Current version of the ips-patcher gem
7
+ VERSION = "0.1.0"
8
+ end
9
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "patcher/version"
4
+ require_relative "binary_reader"
5
+ require_relative "binary_writer"
6
+
7
+ module Ips
8
+ # IPS (International Patching System) patcher for applying binary patches to ROM files.
9
+ #
10
+ # The IPS format is a binary patch format commonly used to distribute patches
11
+ # for ROM images of video games. This module provides functionality to read
12
+ # IPS patch files and apply them to ROM files.
13
+ #
14
+ # @example Basic usage
15
+ # Ips::Patcher.apply("game.rom", "patch.ips")
16
+ # # Creates: game.patched.rom
17
+ #
18
+ # @see https://zerosoft.zophar.net/ips.php IPS File Format Specification
19
+ module Patcher
20
+ # Error class for Patcher-specific errors
21
+ class Error < StandardError; end
22
+
23
+ # Applies an IPS patch file to a ROM file.
24
+ #
25
+ # This method reads both the ROM and patch files, validates the patch format,
26
+ # and applies all patch records to create a new patched ROM file. The output
27
+ # file is named with ".patched" inserted before the file extension.
28
+ #
29
+ # The IPS format supports two types of records:
30
+ # - Standard records: Replace bytes at a specific offset
31
+ # - RLE records: Fill bytes with a repeated value (when size is 0)
32
+ #
33
+ # @param rom_path [String] path to the ROM file to be patched
34
+ # @param patch_path [String] path to the IPS patch file
35
+ # @raise [Error] if the patch file does not have a valid "PATCH" header
36
+ # @return [void]
37
+ #
38
+ # @example Apply a patch
39
+ # Ips::Patcher.apply("game.nes", "translation.ips")
40
+ # # Output: game.patched.nes
41
+ #
42
+ # @note The original ROM file is not modified; a new patched file is created
43
+ def self.apply(rom_path, patch_path)
44
+ rom = File.binread(rom_path)
45
+ patch = File.binread(patch_path)
46
+
47
+ reader = Ips::BinaryReader.new(patch)
48
+ writer = Ips::BinaryWriter.new(rom)
49
+
50
+ header = reader.read_string(5)
51
+ if header != 'PATCH'
52
+ raise Error, "Invalid patch file"
53
+ end
54
+
55
+ loop do
56
+ eof = reader.read_string(3)
57
+ break if eof == 'EOF'
58
+
59
+ reader.go_back(3)
60
+
61
+ offset = reader.read_int24
62
+ size = reader.read_int16
63
+
64
+ if size == 0
65
+ rle_size = reader.read_int16
66
+ rle_value = reader.read_bytes(1)
67
+ writer.set_bytes(offset, rle_value * rle_size)
68
+ next
69
+ end
70
+
71
+ bytes = reader.read_bytes(size)
72
+ writer.set_bytes(offset, bytes)
73
+ end
74
+
75
+ rom_dir = File.dirname(rom_path)
76
+ filename = File.basename(rom_path, File.extname(rom_path))
77
+ extension = File.extname(rom_path)
78
+ output_path = File.join(rom_dir, "#{filename}.patched#{extension}")
79
+
80
+ writer.save_to_file(output_path)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,6 @@
1
+ module Ips
2
+ module Patcher
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ips-patcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - eml
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2026-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ description: Ruby library to parse and apply IPS (.ips) patch files to game ROM images.
28
+ email:
29
+ - emanuelbotelho@ymail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".idea/.gitignore"
35
+ - ".idea/ips-patcher.iml"
36
+ - ".idea/misc.xml"
37
+ - ".idea/modules.xml"
38
+ - ".idea/vcs.xml"
39
+ - ".rspec"
40
+ - ".yardopts"
41
+ - CHANGELOG.md
42
+ - CODE_OF_CONDUCT.md
43
+ - LICENSE
44
+ - README.md
45
+ - Rakefile
46
+ - lib/ips/binary_reader.rb
47
+ - lib/ips/binary_writer.rb
48
+ - lib/ips/patcher.rb
49
+ - lib/ips/patcher/version.rb
50
+ - sig/ips/patcher.rbs
51
+ homepage: https://github.com/meruen/ips-patcher
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ allowed_push_host: https://rubygems.org
56
+ homepage_uri: https://github.com/meruen/ips-patcher
57
+ source_code_uri: https://github.com/meruen/ips-patcher
58
+ changelog_uri: https://github.com/meruen/ips-patcher/blob/main/CHANGELOG.md
59
+ bug_tracker_uri: https://github.com/meruen/ips-patcher/issues
60
+ rubygems_mfa_required: 'false'
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.1.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.5.11
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Apply IPS patches to video game ROM files.
80
+ test_files: []