Skip to content

Commit

Permalink
Merge pull request #71 from roblabla/pbg3-fixes
Browse files Browse the repository at this point in the history
Pbg3 fixes
  • Loading branch information
roblabla authored Feb 17, 2024
2 parents 2e070b7 + 5b94624 commit 2aa44c7
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 146 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "3rdparty/munit"]
path = 3rdparty/munit
url = https://github.com/roblabla/munit
branch = msvc2002
1 change: 1 addition & 0 deletions 3rdparty/munit
Submodule munit added at 4ea671
2 changes: 2 additions & 0 deletions config/implemented.csv
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ IPbg3Parser::ReadMagic
IPbg3Parser::ReadString
Pbg3Parser::Pbg3Parser
Pbg3Parser::OpenArchive
Pbg3Parser::Close
Pbg3Parser::ReadBit
Pbg3Parser::ReadInt
Pbg3Parser::ReadByteAssumeAligned
Pbg3Parser::SeekToOffset
Pbg3Parser::SeekToNextByte
Pbg3Parser::ReadByteAlignedData
Pbg3Parser::GetLastWriteTime
Pbg3Parser::ReadByte
Pbg3Parser::~Pbg3Parser
Pbg3Archive::Pbg3Archive
Pbg3Archive::ParseHeader
Expand Down
32 changes: 23 additions & 9 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@
SCRIPTS_DIR = Path(__file__).parent


def build(build_type):
configure(build_type)

ninja_args = []

if build_type == BuildType.TESTS:
ninja_args += ["build/th06e-tests.exe"]
else:
ninja_args += ["build/th06e.exe"]

# Then, run the build. We use run_windows_program to automatically go through
# wine if running on linux/macos. scripts/th06run.bat will setup PATH and other
# environment variables for the MSVC toolchain to work before calling ninja.
run_windows_program(
[str(SCRIPTS_DIR / "th06run.bat"), "ninja"] + ninja_args,
cwd=str(SCRIPTS_DIR.parent),
)


def main():
parser = argparse.ArgumentParser("th06-build")
parser.add_argument(
"--build-type", choices=["normal", "diffbuild"], default="normal"
"--build-type", choices=["normal", "diffbuild", "tests"], default="normal"
)
args = parser.parse_args()

Expand All @@ -19,15 +38,10 @@ def main():
build_type = BuildType.NORMAL
elif args.build_type == "diffbuild":
build_type = BuildType.DIFFBUILD
configure(build_type)
elif args.build_type == "tests":
build_type = BuildType.TESTS

# Then, run the build. We use run_windows_program to automatically go through
# wine if running on linux/macos. scripts/th06run.bat will setup PATH and other
# environment variables for the MSVC toolchain to work before calling ninja.
run_windows_program(
[str(SCRIPTS_DIR / "th06run.bat"), "ninja", "build/th06e.exe"],
cwd=str(SCRIPTS_DIR.parent),
)
build(build_type)


if __name__ == "__main__":
Expand Down
57 changes: 52 additions & 5 deletions scripts/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class BuildType(Enum):
NORMAL = 1
DIFFBUILD = 2
TESTS = 3


def configure(build_type):
Expand All @@ -21,7 +22,7 @@ def configure(build_type):
writer.variable("cl", "cl.exe")
writer.variable(
"cl_common_flags",
"/nologo /MT /EHsc /G5 /Gs /GS /DNDEBUG /Zi /I $builddir/autogenerated /I src /I src/pbg3",
"/nologo /MT /EHsc /G5 /Gs /GS /DNDEBUG /Zi /I $builddir/autogenerated /I src /I src/pbg3 /I 3rdparty/munit",
)
writer.variable("cl_flags", "$cl_common_flags /Od /Oi /Ob1 /Op")
writer.variable("cl_flags_pbg3", "$cl_common_flags /O2")
Expand Down Expand Up @@ -53,7 +54,7 @@ def configure(build_type):
writer.rule("rc", "$rc /fo $out $in")
writer.rule(
"link",
"""cmd /c "for %F in ("$out") do $link /out:%F $link_flags /debug /pdb:%~pdnF.pdb $link_libs $in" """,
"""cmd /c "for %F in ("$out") do $link $link_flags /out:%F /debug /pdb:%~pdnF.pdb $link_libs $in" """,
)
writer.rule(
"copyicon",
Expand All @@ -64,9 +65,9 @@ def configure(build_type):
"""python -c "import sys; open(sys.argv[2], 'wb').write(open(sys.argv[1], 'rb').read().decode('utf8').encode('shift_jis'))" $in $out""",
)

main_sources = ["main"]
cxx_sources = [
"AsciiManager",
"main",
"Chain",
"FileSystem",
"Supervisor",
Expand All @@ -83,12 +84,20 @@ def configure(build_type):
]

pbg3_sources = [
"IPbg3Parser",
"Pbg3Parser",
"Pbg3Archive",
"FileAbstraction",
]

for rule in cxx_sources:
munit_sources = ["munit"]

test_sources = [
"tests",
"test_Pbg3Archive",
]

for rule in main_sources + cxx_sources:
writer.build(
"$builddir/" + rule + ".obj",
"cc",
Expand All @@ -104,6 +113,20 @@ def configure(build_type):
implicit=["$builddir/autogenerated/i18n.hpp"],
)

for rule in munit_sources:
writer.build(
"$builddir/" + rule + ".obj",
"cc",
"3rdparty/munit/" + rule + ".c",
)

for rule in test_sources:
writer.build(
"$builddir/" + rule + ".obj",
"cc",
"tests/" + rule + ".cpp",
)

writer.build("$builddir/globals.obj", "as", inputs="src/globals.asm")
writer.build("$builddir/autogenerated/i18n.hpp", "geni18n", "src/i18n.tpl")
writer.build("$builddir/icon.ico", "copyicon", "resources/placeholder.ico")
Expand All @@ -114,13 +137,37 @@ def configure(build_type):
implicit="$builddir/icon.ico",
)
objfiles = (
["$builddir/" + src + ".obj" for src in cxx_sources]
["$builddir/" + src + ".obj" for src in main_sources]
+ ["$builddir/" + src + ".obj" for src in cxx_sources]
+ ["$builddir/" + src + ".obj" for src in pbg3_sources]
+ ["$builddir/th06.res"]
)
if build_type == BuildType.DIFFBUILD:
objfiles += ["$builddir/globals.obj"]
writer.build("$builddir/th06e.exe", "link", inputs=objfiles)

test_objfiles = (
["$builddir/" + src + ".obj" for src in cxx_sources]
+ ["$builddir/" + src + ".obj" for src in pbg3_sources]
+ ["$builddir/" + src + ".obj" for src in test_sources]
)
writer.build(
"$builddir/th06e-tests.exe",
"link",
inputs=test_objfiles + ["$builddir/munit.lib"],
variables={
"link_libs": "$link_libs $builddir/munit.lib",
"link_flags": "/debug /pdb:$builddir/th06e.pdb",
},
)

writer.build(
"$builddir/munit.lib",
"link",
inputs=["$builddir/" + s + ".obj" for s in munit_sources],
variables={"link_flags": "/lib", "link_libs": ""},
)

writer.close()


Expand Down
21 changes: 21 additions & 0 deletions scripts/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path

from build import BuildType, build
from winhelpers import run_windows_program

SCRIPTS_DIR = Path(__file__).parent


def main():
# Run the build for tests
build(BuildType.TESTS)

# Then, run the tests
run_windows_program(
[SCRIPTS_DIR.parent / "build" / "th06e-tests.exe"],
cwd=str(SCRIPTS_DIR.parent),
)


if __name__ == "__main__":
main()
85 changes: 85 additions & 0 deletions src/pbg3/IPbg3Parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "IPbg3Parser.hpp"

void IPbg3Parser::Reset()
{
this->bitIdxInCurByte = 128;
this->offsetInFile = 0;
this->fileSize = 0;
this->curByte = 0;
this->crc = 0;
}

u32 IPbg3Parser::ReadVarInt()
{
u32 res = 0;
i32 varintHdr = 0;

if (this->ReadBit())
{
varintHdr = 2;
}
if (this->ReadBit())
{
varintHdr |= 1;
}

u32 intLen;
switch (varintHdr)
{
case 0:
intLen = 0x80;
break;
case 1:
intLen = 0x8000;
break;
case 2:
intLen = 0x800000;
break;
case 3:
intLen = 0x80000000;
break;
default:
// TODO: There's probably a way to match without goto, but
// I can't figure it out... a simple `return 0;` won't share
// the function epilogue with the other return res.
goto end;
}

do
{
if (this->ReadBit())
{
res |= intLen;
}
intLen >>= 1;
} while (intLen != 0);
end:
return res;
}

u32 IPbg3Parser::ReadMagic()
{
u32 b0 = this->ReadInt(8);
u32 b1 = b0 + (this->ReadInt(8) << 8);
u32 b2 = b1 + (this->ReadInt(8) << 16);
u32 b3 = b2 + (this->ReadInt(8) << 24);

return b3;
}

u32 IPbg3Parser::ReadString(char *out, u32 maxSize)
{
if (out == NULL)
return FALSE;

for (u32 idx = 0; idx < maxSize; idx++)
{
out[idx] = this->ReadInt(8);
if (out[idx] == '\0')
{
return TRUE;
}
}

return FALSE;
}
34 changes: 34 additions & 0 deletions src/pbg3/IPbg3Parser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "inttypes.hpp"
#include <Windows.h>

class IPbg3Parser
{
public:
IPbg3Parser()
{
this->Reset();
}
void Reset();
u32 ReadVarInt();
u32 ReadMagic();
u32 ReadString(char *out, u32 maxSize);
virtual i32 ReadBit() = 0;
virtual u32 ReadInt(u32 numBitsAsPowersOf2) = 0;
virtual u8 ReadByteAssumeAligned() = 0;
virtual i32 SeekToOffset(u32 fileOffset) = 0;
virtual i32 SeekToNextByte() = 0;
virtual i32 ReadByteAlignedData(u8 *data, u32 bytesToRead) = 0;
virtual i32 GetLastWriteTime(LPFILETIME lastWriteTime) = 0;
virtual ~IPbg3Parser()
{
}

protected:
u32 offsetInFile;
u32 fileSize;
u32 curByte;
u8 bitIdxInCurByte;
u32 crc;
};
Loading

0 comments on commit 2aa44c7

Please sign in to comment.