Commit Graph
998 Commits
Author SHA1 Message Date
Nunuhara Cabbage 7f5e155f3e Implement system.OpenWeb 2020-03-05 19:05:10 -08:00
Nunuhara Cabbage 4826686953 Update ex lexer source file 2020-02-29 22:54:51 -08:00
Nunuhara Cabbage cd2280f28f exbuild: use read_file_utf8 for included files 2020-02-29 17:30:20 -08:00
Nunuhara Cabbage 4242882671 tools: Update README for v0.2.1 2020-02-29 17:16:10 -08:00
Nunuhara Cabbage 09dcdb3d14 tools: fix iconv issues on Windows
Need to use libiconv on Windows. libiconv doesn't accept all of the same
encoding names as the glibc iconv. In particular, CP932 needs to be used
instead of SJIS-WIN.
2020-02-29 16:49:26 -08:00
Nunuhara Cabbage 2be530b65f ainedit: add --transcode option
Add option to change text encoding of an ain file. E.g.

    ainedit --transcode GBK//TRANSLIT -o out.ain in.ain
2020-02-29 15:07:01 -08:00
Nunuhara Cabbage b7c12b1ce0 tools: allow changing input/output text encoding
Use iconv instead of utfsjis.h so that text encodings other than UTF-8
and SJIS can be used. For three reasons:

* to allow using other encodings for dumped files (e.g. UTF-16, SJIS)
* to support existing Chinese translations, which use GBK-encoded text
* to support theoretical translations to other languages that can't be
  represented in SJIS
2020-02-28 20:18:28 -08:00
Nunuhara Cabbage 40b67ed7f6 Initial afa archive support
Very incomplete implementation. Capable of reading the file table and
not much else.

Also add the "alice-ar" utility for testing. Eventually this tool should
support modifying ald and afa archives. For now only supports dumping
the afa file table.
2020-02-25 20:34:51 -08:00
Nunuhara Cabbage be1b5ecc8f Use abstract archive interface for ALD files
In preparation for AFA support in libsystem4.
2020-02-23 18:49:51 -08:00
Nunuhara Cabbage 6e29fee7ed exdump: fix garbled filenames on Windows
Convert filenames to UTF-16 and use _wfopen().
2020-02-23 14:18:52 -08:00
Nunuhara Cabbage 35a88b8669 Add alice-tools README 2020-02-23 11:22:14 -08:00
Nunuhara Cabbage 46243cc795 aindump: prefix files with block index
Workaround for garbled filenames on Windows.
2020-02-23 10:21:31 -08:00
Nunuhara Cabbage 159774a629 exbuild: add --old option
For building pre-Evenicle .ex files, where the table row/column
quantities are reversed.
2020-02-23 09:47:56 -08:00
Nunuhara Cabbage 00f25d698a exdump/exbuild: multiple files support
The -s,--split option to exdump can be used to split the dump into
multiple files (one per top-level data structure). The output file when
using the split option contains #include directives to stitch the full
dump back together when given to exbuild.
2020-02-23 08:51:49 -08:00
Nunuhara Cabbage 9576a97e05 exbuild: write (binary) ex file to disk
It is now possible to modify .ex files using exdump and exbuild. E.g.

    exdump -o out.x Rance10EX.ex
    $EDITOR out.x
    exbuild -o Rance10EX.ex out.x
2020-02-22 23:06:32 -08:00
Nunuhara Cabbage c58af1c17f exdump: add -d,--decrypt option
For debugging/reversing purposes.
2020-02-22 22:04:47 -08:00
Nunuhara Cabbage 4a9b928500 Add ainbuild utility
Tool for reading and rebuilding dumped .ex files. Currently this only
reads a file on stdin then redumps it to stdout.

Also, move tools code into src/tools/
2020-02-20 21:14:13 -08:00
Nunuhara Cabbage 38048ea9e6 Add exdump utility
Add tool for dumping the contents of .ex files, which contain various
structured data used by the game (tables, lists, trees).

The dump format uses a C-like syntax which should be relatively easy to
parse back in.
2020-02-17 19:51:25 -08:00
Nunuhara Cabbage c9dcf6e104 ainedit: convert .MSG/.STR text to shift-jis 2020-02-15 11:55:22 -08:00
Nunuhara Cabbage b8c9072acf ainedit: convert text back to shift-jis 2020-02-15 09:42:16 -08:00
Nunuhara Cabbage 4429bd0990 Fix NEW instruction args in ain v8 and below
NEW was changed in ain v11 to accept 2 arguments.
2020-02-14 22:16:20 -08:00
Nunuhara Cabbage 608aedb500 Fix various issues with MSYS2/mingw build 2020-02-14 16:29:44 -08:00
Nunuhara Cabbage 623f8c8ebf aindump/ainedit: clean up command line arguments
Use enums for long option values, and run dumps in the order they're
specified on the command line. Also, --no-strings mode is made the
default and a --inline-strings option is added to get the old (broken)
behavior.
2020-02-14 12:11:01 -08:00
Nunuhara Cabbage a898c67983 ainedit: add string/message editing mode
Add -t,--text option to read files produced by 'aindump -t ...'.

The workflow here is:

* dump the text with 'aindump -t -o <outfile> ...'
* $EDITOR <outfile>
* locate the text you want to edit, remove the leading semicolon (i.e.
  uncomment it) and edit the string as desired.
* reinsert the modified text with 'ainedit -t <outfile> ...'
2020-02-13 20:42:51 -08:00
Nunuhara Cabbage 259b06edf1 aindump: add sorted string/message dump mode
Dump strings/messages sorted by function when given the -t,--text
option. This is intended to be read back in again by ainedit.
2020-02-13 17:50:10 -08:00
Nunuhara Cabbage ab670bd3a9 aindump: guess filenames if no FNAM section
If the filenames section isn't present, try to guess the filenames from
the functions defined in the file. This works pretty well for most of
the core engine files but gives pretty crappy results for scenario
files.

This feature is primarily intended to be used when dumping the code
section to multiple files (not yet implemented).
2020-02-12 18:09:37 -08:00
Nunuhara Cabbage 0bb6a7199f ainedit: allow adding switches/strings/messages
Allocate space in switch/string/message tables if index is greater than
the current size. Its up to the user to allocate their own indices.
2020-02-10 21:43:10 -08:00
Nunuhara Cabbage 63b3a9b65c aindump/ainedit: add .DEFAULT pseudo-op
For the default address of switch statements. With this addition,
adding/removing instructions is confirmed working.
2020-02-09 20:33:34 -08:00
Nunuhara Cabbage 0d5e411c3c ainedit: add .STR/.MSG pseudo-ops
These ops allow modifying the string/message tables when using the
--no-strings option.
2020-02-09 16:38:43 -08:00
Nunuhara Cabbage a76b7149d9 aindump/ainedit: add .CASE pseudo instruction
Add a pseudo instruction to represent switch case labels, along with the
infrastructure to support pseudo instructions in general. .CASE takes
two arguments: a switch:case index (e.g. "12:8") and the value to match
against.

Also, add a --no-strings option to both programs to allow modifying the
code without changing the string/message tables. This is needed for now
since rebuilding the string/message tables (as currently implemented)
causes serious bugs in Rance X (and probably other games).

Running the following command should produce an ain file that is almost
bit-for-bit identical to the input file (the only difference being minor
drift in floating point constants):

    aindump -c --no-strings in.ain | ainedit -c - --no-strings -o
        out.ain in.ain
2020-02-09 15:34:10 -08:00
Nunuhara Cabbage dab8deca42 Add ainedit utility
The ainedit utility is a tool capable of modifying AIN files up to
version 12 (Rance X). As far as I know, this is the first publicly
released tool that can handle AIN files above version 7.

The basic mode of operation is as follows:

* dump the CODE section with aindump
* edit the disassembled bytecode
* assemble & insert the edited bytebode back into the original AIN file
  using ainedit

E.g.

    aindump -c -o out.jam /path/to/Rance10.ain
    $EDITOR out.jam
    ainedit -c out.jam -o out.ain /path/to/Rance10.ain
    mv /path/to/Rance10.ain /path/to/Rance10.ain.backup
    mv out.ain /path/to/Rance10.ain

ainedit is also capable of editing declarations (structures, functions,
etc.) using the JSON (-j) output from aindump.

There is currently one major deficiency in this tool: it does not
rebuild the AIN file's switch table (SWI0 section). This means that you
can't insert or delete instructions, as it would render the addresses
stored in the switch table invalid. I hope to fix this soon.

A number of changes have also been made to the aindump utility, since
these tools need to work together.
2020-02-07 19:49:39 -08:00
Nunuhara Cabbage d8304b0e6f Fix bug (?) in utf2sjis
Don't generate SJIS with initial byte 0x80. I'm guessing that sjis2utf
handles initial byte 0x80 the way it does for compatibility reasons, but
there is no reason for utf2sjis to generate this coding.
2020-02-06 21:54:35 -08:00
Nunuhara Cabbage 4d9de80626 aindump: add JSON output format
Dump everything except the code to a JSON file.

With both the disassembled code and this JSON file in hand, it should be
possible to reconstruct the original AIN file.
2020-01-26 17:09:15 -08:00
Nunuhara Cabbage f5446fe1f7 aindump: don't access label table in raw mode 2020-01-24 17:50:07 -08:00
Nunuhara Cabbage 5aa2929b5f aindump: handle nested functions properly
Keep a stack of functions which gets pushed/popped on FUNC and ENDFUNC
instructions, respectively. Since methods do not get an ENDFUNC (for
whatever reason), this stack has to be a fixed size and discard the
oldest value when pushed to (otherwise it would grow indefinitely).

This fixes a bug when dumping the code of Ixseal, where a lambda's
ENDFUNC would clear dasm->func while the parent function was still being
disassembled. Interestingly, this doesn't become an issue in Rance X
because the code is more indirect in how it accesses local variables (no
SH_LOCAL* instructions).
2020-01-16 20:23:42 -08:00
Nunuhara Cabbage 41c39baaba aindump: show function prototype in dasm output
Show full function prototypes in disassembled output.
Also, fix a bug with enum types.
2020-01-16 19:51:50 -08:00
Nunuhara Cabbage 3fa214a22a Implement system.Sleep 2020-01-16 06:04:00 -08:00
Nunuhara Cabbage 167953f33c aindump: dump full enum definitions (incl. values)
This requires scanning through the code for a particular method for each
enum type. Not a great solution, but there's not much else that can be
done here.

Also, use khash.h (from klib) for hash tables.
2020-01-15 21:53:51 -08:00
Nunuhara Cabbage e1f6093565 aindump: fix CALLMETHOD argument in v11+
In ain v11+, the first and only argument to CALLMETHOD is the number of
arguments that are being passed to the method. The calling convention
is:

    PUSH <struct page>
    PUSH <function index>
    PUSH <arg 0>
    ...
    PUSH <arg n>
    CALLMETHOD <n>
2020-01-15 14:29:47 -08:00
Nunuhara Cabbage 74c5b91e0c aindump: enums, iterators, interface objects
Type 82
-------
I am assuming this is an iterator type because it always appears as a
local variable alongside "dummy" variables with "foreach" in their name,
and the naming of the variables suggests an iterator. Probably there is
a new syntax like:

    foreach (iter_var in array_var) { ... }

Type 89
-------
An instance of an interface. This is a 2-valued type (similar to ref
int, etc.) where the first value is the index of a struct page, and the
second value is an index into the struct's vtable.

Any struct that implements an interface gets an array<int> named
"<vtable>" as its first member. This array stores indices into the
global function table. CALLMETHOD now looks up methods in the vtable
rather than the global function table.

Types 92, 93
------------
These are enum types. Type 92 is the normal enum type, and type 93 is
the reference-to-enum type. The "struct type" for these types stores an
index into the enum table.

Types 86, 91
------------
Type 86 only seems to be used as the return value from EnumType::Parse
methods. In the ain file, it has an extra type descriptor, similar to an
array type. This extra type descriptor always has data type 91, and its
struct type is an index into the enum table. Type 91 is simlar to types
92 & 93: it stores an enum table index in its struct type.

Type 86 is a 2-valued type. The first value is the value of the enum (an
integer between 0 and n). The second value seems to be either 0 or -1,
depending on whether EnumType::Parse was able to produce a valid enum
value.

It is possible that type 86 is actually some kind of generic maybe-error
type that only happens to be used by the enum code. But that doesn't
explain why it is paired with type 91 and not type 92.
2020-01-15 12:59:41 -08:00
Nunuhara Cabbage 32bee0c242 aindump: print array types properly
Print the new v11+ array types properly. Also, print the array rank for
old array types.

The code for reading v11+ ain data is simplified a lot here as well.
2020-01-14 20:43:10 -08:00
Nunuhara Cabbage 6b34660e18 aindump: ain v8+ improvements
I've figured out what most of the extra fields are for. Mostly they are
for:

* a new array type, in which the contained value type is stored in the
  variable object rather than encoded in the type number
* argument initvals (I guess the VM is now responsible for these?)
* interfaces
2020-01-12 21:50:17 -08:00
Nunuhara Cabbage 4e6bf60e1b aindump: quote identifiers containing spaces
...so that they can be reliably parsed.
2020-01-11 21:24:53 -08:00
Nunuhara Cabbage 11a29dd4e0 aindump: fix arg types for opcodes 0x103, 0x105 2020-01-11 18:01:04 -08:00
Nunuhara Cabbage 9f67b40d25 aindump: various improvements
Add index numbers to various output types.
Add local variables to function declarations.
Allow dumping delegates.
2020-01-11 15:24:38 -08:00
Nunuhara Cabbage 70a8b9d136 aindump: correct argument types, all syscalls
Use the correct argument types for all instructions, with the exception
of SH_REF_LOCAL_ASSIGN_STRUCTREF2 which I could find an example of.

List all system calls.
2020-01-11 15:05:45 -08:00
Nunuhara Cabbage 8c226ab535 aindump: support code dump up to ain v12 (Rance X)
v11 ain files have 3 new instructions, and 3 modified instructions. It
looks like they decided to break compatibility with this release by
adding arguments to some instructions.

v12 adds one instruction.
2020-01-10 13:38:19 -08:00
Nunuhara Cabbage 302598ace2 aindump: add decrypt mode
Add -d,--decrypt option to dump decrypted ain file (for reverse
engineering).
2020-01-10 12:28:44 -08:00
Nunuhara Cabbage d764757894 aindump: add "raw" code dumping mode
Add mode which prints all arguments as numbers and does not generate
labels. This is for debugging.
2020-01-10 09:07:26 -08:00
Nunuhara Cabbage 55e44b881a aindump: support dumping code up to Heartful Maman
The argument types for many of the newer instructions are likely
still incorrect.
2020-01-10 08:49:25 -08:00