mirror of
https://github.com/risingPhil/libpokemegb.git
synced 2026-03-21 17:44:24 -05:00
Add setMoney example executable
This commit is contained in:
parent
06f894d52c
commit
4ca381bb61
|
|
@ -16,6 +16,7 @@ all:
|
||||||
$(MAKE) -C gen2_setEventFlag $(MAKECMDGOALS) $(MAKEOVERRIDES)
|
$(MAKE) -C gen2_setEventFlag $(MAKECMDGOALS) $(MAKEOVERRIDES)
|
||||||
$(MAKE) -C decodePartyIcons $(MAKECMDGOALS) $(MAKEOVERRIDES)
|
$(MAKE) -C decodePartyIcons $(MAKECMDGOALS) $(MAKEOVERRIDES)
|
||||||
$(MAKE) -C findLocalizationOffsets $(MAKECMDGOALS) $(MAKEOVERRIDES)
|
$(MAKE) -C findLocalizationOffsets $(MAKECMDGOALS) $(MAKEOVERRIDES)
|
||||||
|
$(MAKE) -C setMoney $(MAKECMDGOALS) $(MAKEOVERRIDES)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(MAKE) -C do_stuff_gen1 clean
|
$(MAKE) -C do_stuff_gen1 clean
|
||||||
|
|
@ -32,4 +33,5 @@ clean:
|
||||||
$(MAKE) -C gen2_setEventFlag clean
|
$(MAKE) -C gen2_setEventFlag clean
|
||||||
$(MAKE) -C decodePartyIcons clean
|
$(MAKE) -C decodePartyIcons clean
|
||||||
$(MAKE) -C findLocalizationOffsets clean
|
$(MAKE) -C findLocalizationOffsets clean
|
||||||
|
$(MAKE) -C setMoney clean
|
||||||
|
|
||||||
|
|
|
||||||
48
examples/setMoney/Makefile
Normal file
48
examples/setMoney/Makefile
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
# # Compiler flags
|
||||||
|
CXXFLAGS := -std=c++11 -fno-rtti -fno-exceptions -fno-unwind-tables -Wall -Wextra -I $(CURDIR) -I $(CURDIR)/../../include -g -Os
|
||||||
|
|
||||||
|
# Source files directory
|
||||||
|
SRC_DIR := .
|
||||||
|
# Build directory
|
||||||
|
BUILD_DIR := build
|
||||||
|
|
||||||
|
# Source files (add more as needed)
|
||||||
|
SRCS := $(shell find $(SRC_DIR) -type f -name '*.cpp')
|
||||||
|
# Object files
|
||||||
|
OBJS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
|
||||||
|
|
||||||
|
LIBS := -lpokemegb
|
||||||
|
ifeq ($(PNG_SUPPORT),1)
|
||||||
|
LIBS += -lpng
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Ensure necessary directories exist
|
||||||
|
# This function ensures the directory for the target exists
|
||||||
|
define make_directory
|
||||||
|
@mkdir -p $(dir $@)
|
||||||
|
endef
|
||||||
|
|
||||||
|
# Target executable
|
||||||
|
TARGET := ../../setMoney
|
||||||
|
|
||||||
|
# Phony targets
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
# Default target
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
$(CXX) $(CXXFLAGS) $^ -o $@ $(LIBS) -L ../../
|
||||||
|
|
||||||
|
# Rule to compile source files
|
||||||
|
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp | $(BUILD_DIR)
|
||||||
|
$(make_directory)
|
||||||
|
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Create the build directory if it doesn't exist
|
||||||
|
$(BUILD_DIR):
|
||||||
|
mkdir -p $(BUILD_DIR)
|
||||||
|
|
||||||
|
# Clean rule
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR) $(TARGET)
|
||||||
79
examples/setMoney/main.cpp
Normal file
79
examples/setMoney/main.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
#include "gen1/Gen1GameReader.h"
|
||||||
|
#include "gen2/Gen2GameReader.h"
|
||||||
|
#include "RomReader.h"
|
||||||
|
#include "SaveManager.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
static void print_usage()
|
||||||
|
{
|
||||||
|
printf("Usage: setMoney <path/to/rom.gbc> <path/to/file.sav> <amount>\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if(argc != 4)
|
||||||
|
{
|
||||||
|
print_usage();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
uint8_t* romBuffer;
|
||||||
|
uint8_t* savBuffer;
|
||||||
|
uint32_t romFileSize;
|
||||||
|
uint32_t savFileSize;
|
||||||
|
uint32_t amount = std::atoi(argv[3]);
|
||||||
|
|
||||||
|
printf("rom: %s, save: %s, amount: %u\n", argv[1], argv[2], amount);
|
||||||
|
|
||||||
|
romBuffer = readFileIntoBuffer(argv[1], romFileSize);
|
||||||
|
if(!romBuffer)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Couldn't read file %s\n", argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
savBuffer = readFileIntoBuffer(argv[2], savFileSize);
|
||||||
|
if(!savBuffer)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Couldn't read file %s\n", argv[2]);
|
||||||
|
free(romBuffer);
|
||||||
|
romBuffer = nullptr;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameboyCartridgeHeader cartridgeHeader;
|
||||||
|
BufferBasedRomReader romReader(romBuffer, romFileSize);
|
||||||
|
BufferBasedSaveManager saveManager(savBuffer, savFileSize);
|
||||||
|
|
||||||
|
readGameboyCartridgeHeader(romReader, cartridgeHeader);
|
||||||
|
|
||||||
|
// check if we're dealing with gen 1
|
||||||
|
const Gen1GameType gen1Type = gen1_determineGameType(cartridgeHeader);
|
||||||
|
const Gen2GameType gen2Type = gen2_determineGameType(cartridgeHeader);
|
||||||
|
if (gen1Type != Gen1GameType::INVALID)
|
||||||
|
{
|
||||||
|
Gen1GameReader gameReader(romReader, saveManager, gen1Type);
|
||||||
|
gameReader.setTrainerMoney(amount);
|
||||||
|
gameReader.updateMainChecksum();
|
||||||
|
}
|
||||||
|
else if (gen2Type != Gen2GameType::INVALID)
|
||||||
|
{
|
||||||
|
Gen2GameReader gameReader(romReader, saveManager, gen2Type);
|
||||||
|
gameReader.setTrainerMoney(amount);
|
||||||
|
gameReader.finishSave();
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE* f = fopen(argv[2], "w");
|
||||||
|
fwrite(savBuffer, 1, savFileSize, f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
free(romBuffer);
|
||||||
|
romBuffer = nullptr;
|
||||||
|
|
||||||
|
free(savBuffer);
|
||||||
|
savBuffer = nullptr;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user