Files
HackerOoT/tools/spec.c
Yanis aa0341f97b Merge decomp/main (#141)
* Match retail BSS ordering (#1927)

* Match retail BSS ordering

* Revert moving some global variables to headers

* Adjust block numbers after header changes

* Fix debug build

* Overlay bss ordering

* Fix BSS ordering after header changes

* gc-eu-mq OK

* Implement preprocessor for #pragma increment_block_number

* Transfer usage comment from reencode.sh

* Use temporary directory instead of temporary file

* Move ColChkMassType back

* Player: Document "WaitForPutAway"  (#1936)

* document put away delay

* functions.txt

* add a note on delaying indefinitely

* format

* typo

* delay -> wait for put away

* revert unintended formatting change

* add comment to struct member

* format

* fix functions.txt

* Set up gc-eu and match all code (#1938)

* Set up gc-eu and match all code

* Format

* Mark gc-eu-mq as WIP until it builds OK

* Move original/MQ map mark data to separate files

* Add #includes to .inc.c files to help out VS Code

* Use #if in spec instead of .inc.c files

* Delete disassembly data for gc-eu-mq (#1942)

* Player Docs: "sUpperBodyIsBusy" (#1944)

* document upperbodybusy

* change wording for comment and rename upperanimblendweight

* format

* review

* Fix miscategorized scenes (#1946)

* Fix miscategorized scenes

* Sort includes

* Player Docs: Action Interrupt (#1947)

* document action interrupt

* format

* new function comment

* format

* add a note about items

* format

* Add gc-eu-mq to CI (#1943)

* Add gc-eu-mq to CI

* Give up on scripting

* Revert quotes changes

* Player Docs: Name some high level update calls (#1593)

* name some low hanging fruit

* revert burn and shock, doing in seperate pr

* add some function comments

* yaw func

* adjust comment

* some review

* unname UpdateZTarget

* Player_DetectRumbleSecrets

* fix dive do action name

* Player Docs: Control stick buffers (#1945)

* name vars and add enum

* name some spin attack stuff

* fix right and left

* forward/backward

* format

* fix retail bss

* sControlStickWorldYaw

* Force string.o to be in boot for gcc builds (#1948)

In retail builds, memcpy is linked in code, not boot, but GCC likes to call memcpy when copying structs so currently GCC builds immediately crash in __osInitialize_common.

* Rename yDistToWater -> depthInWater (#1950)

* Rename yDistToWater -> yDistUnderWater

* yDistUnderWater -> depthInWater

* Check baserom hash before decompression (#1952)

* Remove Cygwin support (#1951)

* Document pause page switching (#1550)

* Document pause page switching

* document initial scroll left setup, when opening the pause menu

* `PAUSE_MAIN_STATE_1` -> `PAUSE_MAIN_STATE_SWITCHING_PAGE`

* try a diagram of the pages layout in world space as a comment

* expand `nextPageMode` comment

* touch up pause camera header comments

* expand comment on irrelevant init `mainState = PAUSE_MAIN_STATE_SWITCHING_PAGE`

* expand doc on `sKaleidoSetup*` data

* expand docs on `gPageSwitchNextButtonStatus`

* add some doc on `sPageSwitch*` arrays

* SwitchPage -> PageSwitch

* add `PAGE_SWITCH_NSTEPS`

* `SWITCH_PAGE_*_PT` -> `PAGE_SWITCH_PT_*`

* peepoArtist

---------

Co-authored-by: fig02 <fig02srl@gmail.com>

* Fix LensMode Enum Names (#1954)

* Change linker script so gGameOverTimer can be in z_game_over.c (#1939)

* Change linker script so gGameOverTimer can be in z_game_over.c

* gGameOverTimer -> sGameOverTimer

* include_data_only_with_rodata -> include_data_only_within_rodata

* fix build issues

* Check buffers segment in check_ordering.py (#1960)

* Delete unused yaz0tool (#1959)

* Revamp "AnimationContext" Docs, now called "AnimTaskQueue" (#1941)

* start using task terminology

* more docs

* format

* cleanups

* MoveActor -> ActorMove

* missed a couple

* hopefully the last changes

* comment explaining the group change

* some review

* dragorn review

* remove accidental file

* fix matching issue, now use while loop

* Experiment: remove global.h dependency from sys_math, sys_math3d, z_lib (#1956)

* split sys_math, sys_math3d, z_lib from global.h

* suggestions

* forgot this

* more math stuff

* nit fix

* re-add ichain.h

* resolve tharo's comments

* Fix check_ordering.py checking for shifted/nonmatching-besides-relocs (#1961)

* Run CC_CHECK with the correct CPP defines (#1963)

* Run CC_CHECK with the correct CPP defines

* Add "CPP_DEFINES ?="

---------

Co-authored-by: cadmic <cadmic24@gmail.com>
Co-authored-by: fig02 <fig02srl@gmail.com>
Co-authored-by: Dragorn421 <Dragorn421@users.noreply.github.com>
Co-authored-by: inspectredc <78732756+inspectredc@users.noreply.github.com>
Co-authored-by: mzxrules <mzxrules@gmail.com>
2024-06-22 22:36:26 +02:00

379 lines
11 KiB
C

#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "spec.h"
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
static struct Segment *add_segment(struct Segment **segments, int *segments_count)
{
struct Segment *seg;
(*segments_count)++;
*segments = realloc(*segments, *segments_count * sizeof(**segments));
seg = &(*segments)[*segments_count - 1];
memset(seg, 0, sizeof(*seg));
seg->align = 16;
return seg;
}
static char *skip_whitespace(char *str)
{
while (isspace(*str))
str++;
return str;
}
// null terminates the current token and returns a pointer to the next token
static char *token_split(char *str)
{
while (!isspace(*str))
{
if (*str == 0)
return str; // end of string
str++;
}
*str = 0; // terminate token
str++;
return skip_whitespace(str);
}
// null terminates the current line and returns a pointer to the next line
static char *line_split(char *str)
{
while (*str != '\n')
{
if (*str == 0)
return str; // end of string
str++;
}
*str = 0; // terminate line
return str + 1;
}
static bool parse_number(const char *str, unsigned int *num)
{
char *endptr;
long int n = strtol(str, &endptr, 0);
*num = n;
return endptr > str;
}
static bool parse_flags(char *str, unsigned int *flags)
{
unsigned int f = 0;
while (str[0] != 0)
{
char *next = token_split(str);
if (strcmp(str, "BOOT") == 0)
f |= FLAG_BOOT;
else if (strcmp(str, "OBJECT") == 0)
f |= FLAG_OBJECT;
else if (strcmp(str, "RAW") == 0)
f |= FLAG_RAW;
else if (strcmp(str, "NOLOAD") == 0)
f |= FLAG_NOLOAD;
else if (strcmp(str, "SYMS") == 0)
f |= FLAG_SYMS;
else
return false;
str = next;
}
*flags = f;
return true;
}
static bool parse_quoted_string(char *str, char **out)
{
if (*str != '"')
return false;
str++;
*out = str;
while (*str != '"')
{
if (*str == 0)
return false; // unterminated quote
str++;
}
*str = 0;
str++;
str = skip_whitespace(str);
if (*str != 0)
return false; // garbage after filename
return true;
}
static bool is_pow_of_2(unsigned int n)
{
return (n & (n - 1)) == 0;
}
static const char *const stmtNames[] =
{
[STMT_address] = "address",
[STMT_after] = "after",
[STMT_align] = "align",
[STMT_beginseg] = "beginseg",
[STMT_compress] = "compress",
[STMT_endseg] = "endseg",
[STMT_entry] = "entry",
[STMT_flags] = "flags",
[STMT_include] = "include",
[STMT_include_data_only_within_rodata] = "include_data_only_within_rodata",
[STMT_include_no_data] = "include_no_data",
[STMT_name] = "name",
[STMT_number] = "number",
[STMT_romalign] = "romalign",
[STMT_stack] = "stack",
[STMT_increment] = "increment",
[STMT_pad_text] = "pad_text",
};
STMTId get_stmt_id_by_stmt_name(const char *stmtName, int lineNum) {
STMTId stmt;
for (stmt = 0; stmt < ARRAY_COUNT(stmtNames); stmt++) {
if (strcmp(stmtName, stmtNames[stmt]) == 0)
return stmt;
}
util_fatal_error("line %i: unknown statement '%s'", lineNum, stmtName);
return -1;
}
bool parse_segment_statement(struct Segment *currSeg, STMTId stmt, char* args, int lineNum) {
// ensure no duplicates (except for 'include' or 'pad_text')
if (stmt != STMT_include && stmt != STMT_include_data_only_within_rodata &&
stmt != STMT_include_no_data && stmt != STMT_pad_text &&
(currSeg->fields & (1 << stmt)))
util_fatal_error("line %i: duplicate '%s' statement", lineNum, stmtNames[stmt]);
currSeg->fields |= 1 << stmt;
// statements valid within a segment definition
switch (stmt)
{
case STMT_beginseg:
util_fatal_error("line %i: '%s' inside of a segment definition", lineNum, stmtNames[stmt]);
break;
case STMT_endseg:
// verify segment data
if (currSeg->name == NULL)
util_fatal_error("line %i: no name specified for segment", lineNum);
if (currSeg->includesCount == 0)
util_fatal_error("line %i: no includes specified for segment", lineNum);
return true;
break;
case STMT_name:
if (!parse_quoted_string(args, &currSeg->name))
util_fatal_error("line %i: invalid name", lineNum);
break;
case STMT_after:
if (!parse_quoted_string(args, &currSeg->after))
util_fatal_error("line %i: invalid name for 'after'", lineNum);
break;
case STMT_address:
if (!parse_number(args, &currSeg->address))
util_fatal_error("line %i: expected number after 'address'", lineNum);
break;
case STMT_number:
if (!parse_number(args, &currSeg->number))
util_fatal_error("line %i: expected number after 'number'", lineNum);
break;
case STMT_flags:
if (!parse_flags(args, &currSeg->flags))
util_fatal_error("line %i: invalid flags", lineNum);
break;
case STMT_align:
if (!parse_number(args, &currSeg->align))
util_fatal_error("line %i: expected number after 'align'", lineNum);
if (!is_pow_of_2(currSeg->align))
util_fatal_error("line %i: alignment is not a power of two", lineNum);
break;
case STMT_romalign:
if (!parse_number(args, &currSeg->romalign))
util_fatal_error("line %i: expected number after 'romalign'", lineNum);
if (!is_pow_of_2(currSeg->romalign))
util_fatal_error("line %i: alignment is not a power of two", lineNum);
break;
case STMT_include:
case STMT_include_data_only_within_rodata:
case STMT_include_no_data:
currSeg->includesCount++;
currSeg->includes = realloc(currSeg->includes, currSeg->includesCount * sizeof(*currSeg->includes));
if (!parse_quoted_string(args, &currSeg->includes[currSeg->includesCount - 1].fpath))
util_fatal_error("line %i: invalid filename", lineNum);
currSeg->includes[currSeg->includesCount - 1].linkerPadding = 0;
currSeg->includes[currSeg->includesCount - 1].dataOnlyWithinRodata = (stmt == STMT_include_data_only_within_rodata);
currSeg->includes[currSeg->includesCount - 1].noData = (stmt == STMT_include_no_data);
break;
case STMT_increment:
if (!parse_number(args, &currSeg->increment))
util_fatal_error("line %i: expected number after 'increment'", lineNum);
break;
case STMT_compress:
currSeg->compress = true;
break;
case STMT_pad_text:
currSeg->includes[currSeg->includesCount - 1].linkerPadding += 0x10;
break;
default:
fprintf(stderr, "warning: '%s' is not implemented\n", stmtNames[stmt]);
break;
}
return false;
}
/**
* `segments` should be freed with `free_rom_spec` after use.
* Will write to the contents of `spec` to introduce string terminating '\0's.
* `segments` also contains pointers to inside `spec`, so `spec` should not be freed before `segments`
*/
void parse_rom_spec(char *spec, struct Segment **segments, int *segment_count)
{
int lineNum = 1;
char *line = spec;
struct Segment *currSeg = NULL;
// iterate over lines
while (line[0] != 0)
{
char *nextLine = line_split(line);
char* stmtName;
if (line[0] != 0)
{
stmtName = skip_whitespace(line);
}
if (line[0] != 0 && stmtName[0] != 0)
{
char *args = token_split(stmtName);
STMTId stmt = get_stmt_id_by_stmt_name(stmtName, lineNum);
if (currSeg != NULL)
{
bool segmentEnded = parse_segment_statement(currSeg, stmt, args, lineNum);
if (segmentEnded) {
currSeg = NULL;
}
}
else
{
// commands valid outside a segment definition
switch (stmt)
{
case STMT_beginseg:
currSeg = add_segment(segments, segment_count);
currSeg->includes = NULL;
break;
case STMT_endseg:
util_fatal_error("line %i: '%s' outside of a segment definition", lineNum, stmtName);
break;
default:
fprintf(stderr, "warning: '%s' is not implemented\n", stmtName);
break;
}
}
}
line = nextLine;
lineNum++;
}
}
/**
* @brief Parses the spec, looking only for the segment with the name `segmentName`.
* Returns true if the segment was found, false otherwise
*
* @param[out] dstSegment The Segment to be filled. Will only contain the data of the searched segment, or garbage if the segment was not found. dstSegment must be previously allocated, a stack variable is recommended
* @param[in,out] spec A null-terminated string containing the whole spec file. This string will be modified by this function
* @param[in] segmentName The name of the segment being searched
*/
bool get_single_segment_by_name(struct Segment* dstSegment, char *spec, const char *segmentName) {
bool insideSegment = false;
int lineNum = 1;
char *line = spec;
memset(dstSegment, 0, sizeof(struct Segment));
// iterate over lines
while (line[0] != '\0') {
char *nextLine = line_split(line);
char* stmtName = skip_whitespace(line);
if (stmtName[0] != '\0') {
char *args = token_split(stmtName);
STMTId stmt = get_stmt_id_by_stmt_name(stmtName, lineNum);
if (insideSegment) {
bool segmentEnded = parse_segment_statement(dstSegment, stmt, args, lineNum);
if (stmt == STMT_name) {
if (strcmp(segmentName, dstSegment->name) != 0) {
// Not the segment we are looking for
insideSegment = false;
}
} else if (segmentEnded) {
return true;
}
} else {
if (stmt == STMT_beginseg) {
insideSegment = true;
if (dstSegment->includes != NULL) {
free(dstSegment->includes);
}
memset(dstSegment, 0, sizeof(struct Segment));
}
}
}
line = nextLine;
lineNum++;
}
return false;
}
/**
* @brief Frees the elements of the passed Segment. Will not free the pointer itself
*
* @param segment
*/
void free_single_segment_elements(struct Segment *segment) {
if (segment->includes != NULL) {
free(segment->includes);
}
}
void free_rom_spec(struct Segment *segments, int segment_count)
{
int i;
for (i = 0; i < segment_count; i++)
{
if (segments[i].includes != NULL)
free(segments[i].includes);
}
free(segments);
}