Commit Graph
300 Commits
Author SHA1 Message Date
Nunuhara Cabbage 6e3a3e784a jaf: strings and reference types 2020-06-20 21:05:14 -07:00
Nunuhara Cabbage 2908e6fcdc jaf: initial compiler implementation
Supports reading from local variables (no assignment) and arithmetic.
2020-06-16 20:38:29 -07:00
Nunuhara Cabbage d8e21d0323 jaf: support parsing identifiers & function calls 2020-06-14 15:54:59 -07:00
Nunuhara Cabbage e618017e78 ainedit: support parsing .jaf functions 2020-06-13 13:36:39 -07:00
Nunuhara Cabbage 897cbde39c ainedit: initial .jaf support
Initial support for compiling .jaf (System 4 language) files. So far
this is limited to adding struct definitions and declaring new globals.
The actual bytecode compilation is not yet implemented.

This Bison grammar is based on a C grammar and should cover 95% of the
System 4 language syntax already, though many AST nodes are not yet
implemented.
2020-06-10 21:23:00 -07:00
Nunuhara Cabbage 767e2afd57 alice-tools 0.4.0
Fix some Windows-specific issues for alice-ar and update READMEs.
2020-05-23 17:46:00 -07:00
Nunuhara Cabbage 8f38b6141e alice-ar: various improvements
* Can now extract files from .afa version 3 archives
* Can now extract files from .flat files
  * Includes recursively extracting .flat files contained in other
    archive formats
* Added option to extract images only
* Now converts images to PNG format by default
  * WebP format also supported

Because the table of contents is encrypted in .afa version 3 archives,
the support is a bit limited. Only known file types can be extracted and
they do not retain their original filenames (they are given numbered
filenames instead).
2020-05-23 12:33:40 -07:00
Nunuhara Cabbage 9ab13af34e DCF CG support
For alice-ar. This is a wrapper format for QNT CGs. I have no idea what
the additional metadata is for.
2020-05-21 16:30:12 -07:00
Nunuhara Cabbage 05d998886f exdump/exbuild: fix issues with Evenicle2EX.ex
Evenicle2EX.ex uses the previously unknown field data in a different way
that what was assumed. Thanks to that, the unknown data is now more or
less understood:
  * uk0 indicates whether there is a value associated with the field
  * uk1 indicates whether the field is an index
  * uk2 is the value associated with the field, which can be an integer,
    float or string (so far, at least...)

Previously it was assumed that uk2 was a single 32-bit integer, but it
can actually store strings as well. Evenicle 2 is the first game to use
a field with a string value other than the empty string, which breaks
this assumption (the empty string is a 32-bit zero, which is why the old
approach worked).

There is now a new syntax for specifying this data. E.g., a field may
now look like this:
    indexed string s = ""
where it would previously have looked like this:
    string s[1,1,0]

The old syntax is still supported in ainbuild for backwards
compatibility, but is no longer emitted by aindump.

This commit also adds the tool "excmp" which compares two ex files to
see if they are equivalent.
2020-05-18 10:34:43 -07:00
Nunuhara Cabbage b617449178 aindump/ainedit: initial ain v14 support
Support for Evenicle 2 and Haha Ranman.

There are a few changes in v14:
* a number of new instructions (semantics yet unknown)
* HLL function arguments and return values can now be complex types
  (e.g. arrays, enums)
* support for nested arrays (this isn't necessarily new, but it doesn't
  occur in Rance X at all)
2020-05-17 13:21:37 -07:00
Nunuhara Cabbage bf9ce85079 exdump: fix for broken table in Rance 03
Rance03.ex has a broken table in which a field type does not match the
type of the data in the corresponding column. So rather than aborting
with an error, exdump will now print a warning when dumping this file.
2020-05-01 17:55:55 -07:00
Nunuhara Cabbage 094ce848b9 aindump/ainedit: STRUCTREF, etc. macros
Same idea as LOCALREF/GLOBALREF, etc. except for accessing struct
members inside of a member function. This is a bit trickier since it
requires knowing which functions belong to which structs, which isn't
directly encoded in the AIN format.
2020-05-01 16:31:12 -07:00
Nunuhara Cabbage f164480f35 Initial fnl (bitmap) font rendering implementation
Enough to get text on the screen, but it isn't pretty. No antialiasing,
kerning, vertical placement isn't correct, probably the size isn't even
quite correct, etc.
2020-04-25 08:09:13 -07:00
Nunuhara Cabbage 99b375acce alice-ar: support extracting ALD and AFA files
Support whole archive, by-index and by-name extraction of files from ALD
and AFA archives.
2020-04-24 22:49:42 -07:00
Nunuhara Cabbage 15ef8caa91 Add fnldump utility
Add tool for extracting bitmap fonts from .fnl files (font archives).
This is mainly for testing the underlying .fnl reading code, which is
needed for proper font rendering in the MangaGamer version of Sengoku
Rance. In the future this tool could be updated to support rebuilding
font archives, which would allow adding special glyphs, using condensed
fonts, etc.

Credit to CookieWukk for reverse engineering the .fnl file format.
2020-04-08 20:08:45 -07:00
Nunuhara Cabbage 40ce6e2e82 Add webp CG support
For the MangaGamer version of Sengoku Rance. This makes libwebp a
dependency.

These webp files have a non-standard metadata block appended to them
which specifies the base CG that should be used for unfilled pixels,
where "unfilled" means any pixels with a pure magenta color (255,0,255).
2020-04-07 18:41:01 -07:00
Nunuhara Cabbage 9e0ae18a05 Add stubs for SengokuRanceFont.dll
And enough implementation to run up to the intro without crashing.
2020-04-05 19:36:42 -07:00
Nunuhara Cabbage 576703d141 ini: combine 'var[n] = val' assignments into lists
Create INI_LIST entries from _INI_LIST_ENTRY entries. E.g.

    VolumeValancer[0] = "BGM"
    VolumeValancer[1] = "Sounds"

is equivalent to,

    VolumeValancer = {
        "BGM",
        "Sounds",
    }
2020-03-11 20:36:19 -07:00
Nunuhara Cabbage e4a2e965df Use custom parser for System40.ini
This is not really a standard ini file, so a custom parser is needed to
access all of the information stored in it (specifically, lists like
"LoadDLL").

This commit also makes flex/bison a hard dependency.
2020-03-10 21:36:28 -07:00
Nunuhara Cabbage 0d74a3b686 Partial MsgLogManager/MsgLogViewer implementation
Enough to get the message log in Sengoku Rance working.
2020-03-08 17:19:19 -07:00
Nunuhara Cabbage 3e1919e17c AJP alpha channel support
Add PMS decoder from xsystem35 for AJP alpha mask.
2020-03-08 10:53:23 -07:00
Nunuhara Cabbage 2d1b8dad3a Partial AJP CG support
Support for AJP without alpha channel.
2020-03-07 21:47:09 -08:00
Nunuhara Cabbage 529c7024c2 Move --audit from aindump to xsystem4
Move --audit option from aindump to xsystem4 proper, and implement
auditing of HLL functions.
2020-03-06 21:14:02 -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 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 608aedb500 Fix various issues with MSYS2/mingw build 2020-02-14 16:29:44 -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 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 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 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 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 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
Nunuhara Cabbage 61097cfe73 aindump: option to check xsystem4 compatibility
Add -A,--audit option to check an ain file's compatibility with
xsystem4. Currently only checks for unimplemented instructions and
syscalls.
2020-01-09 19:10:06 -08:00
Nunuhara Cabbage e00501346b Fix LI_* instructions
The 'lint' type in system 4 is NOT a 64-bit integer type. It is actually
a signed 32-bit integer type that is clamped between 0 and INT32_MAX.

Negative numbers can be assigned to a lint using SH_LOCALASSIGN (or at
least, the unofficial compiler emits such code) but LI_* instructions
always produce a positive result.

I'm guessing System40.exe converts lints to 64-bit integers for doing
arithmetic, and then clamps the result to the 32-bit integer range
before writing back to a variable.

This fixes a bug in Sengoku Rance where units would become invincible in
commander battles after their HP fell below zero.
2020-01-08 19:14:08 -08:00
Nunuhara Cabbage 6a4d11f3de Use libffi for library calls
Use libffi to portably call arbitrary functions at runtime, rather than
forcing library functions to use a standard prototype.
2020-01-07 22:48:29 -08:00
Nunuhara Cabbage f1cc9082b3 Don't store HLL function pointers in ain struct
Store them in vm.c. The ain structure should not contain data that
wasn't in the ain file on disk.
2020-01-03 18:48:30 -08:00
Nunuhara Cabbage 03e9d59825 aindump: improve disassembler output
Replace many instruction arguments with names/values (strings/messages,
local/global variables, structs, functions, etc.) and add labels to jump
targets.
2020-01-01 20:34:35 -08:00
Nunuhara Cabbage 5d6f148e1b Reorganize project file structure
- Create separate directories for source and header files.
- Build code shared between xsystem4 and aindump as a static library.
- Create header dirs gfx/ system4/ and vm/
- Make CG.c/h usable without a running VM
2019-12-31 17:50:40 -08:00