Skip to content

Commit

Permalink
Steal from AC & match JSU streams
Browse files Browse the repository at this point in the history
One of the funcs has a regswap >:(
  • Loading branch information
Mrkol committed Dec 24, 2024
1 parent 1307d46 commit b35882e
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 12 deletions.
15 changes: 13 additions & 2 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,32 @@ def MatchingFor(*versions):
"cflags": cflags_jsystem,
"progress_category": "jsystem",
"objects": [
Object(Matching, "JSystem/J3DClusterLoader.cpp"),
Object(Matching, "JSystem/JASProbe.cpp"),

# JDrama
Object(Matching, "JSystem/JDRPlacement.cpp"),
Object(Matching, "JSystem/JDRResolution.cpp"),
Object(Matching, "JSystem/JSUList.cpp"),

# JUtility
Object(Matching, "JSystem/JUTRect.cpp"),

# J3D
Object(Matching, "JSystem/J3DClusterLoader.cpp"),
# Object(Matching, "JSystem/J3DModel.cpp"),
Object(Matching, "JSystem/J3DVertex.cpp"),
Object(Matching, "JSystem/J3DPacket.cpp"),
# Object(Matching, "JSystem/J3DShape.cpp"),

# JKernel
Object(Matching, "JSystem/JKRDisposer.cpp"),
Object(Matching, "JSystem/JKRHeap.cpp"),
Object(Matching, "JSystem/JKRFileLoader.cpp"),
Object(Matching, "JSystem/JKRThread.cpp"),
Object(Matching, "JSystem/JKRFileFinder.cpp"),

# JSupport
Object(Matching, "JSystem/JSUInputStream.cpp"),
Object(Matching, "JSystem/JSUList.cpp"),
],
},
{
Expand Down
126 changes: 116 additions & 10 deletions include/JSystem/JSupport/JSUInputStream.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,124 @@
#ifndef JSUINPUTSTREAM_HPP
#define JSUINPUTSTREAM_HPP
#ifndef JSUINPUTSTREAM_H
#define JSUINPUTSTREAM_H

#include <types.h>
#include <JSystem/JSupport/JSUIosBase.h>

class JSUInputStream {
class JSUInputStream : public JSUIosBase {
public:
virtual ~JSUInputStream();
virtual void getAvailable() = 0;
void read(void*, s32);
virtual void skip(u16);
u32 _000;
u32 _004;
};
virtual int getAvailable() const = 0;
virtual int skip(s32 amount);
virtual int readData(void* buf, s32 size) = 0;

int read(void* buf, s32 size);
char* read(char* buf);
char* readString();
char* readString(char* buf, u16 len);

int read(s8& p) { return this->read(&p, sizeof(s8)); } /* @fabricated */
int read(u8& p) { return this->read(&p, sizeof(u8)); }
int read(bool& p) { return this->read(&p, sizeof(bool)); }
int read(s16& p) { return this->read(&p, sizeof(s16)); } /* @fabricated */
int read(u16& p) { return this->read(&p, sizeof(u16)); } /* @fabricated */
int read(s32& p) { return this->read(&p, sizeof(s32)); } /* @fabricated */
int read(u32& p) { return this->read(&p, sizeof(u32)); }
int read(s64& p) { return this->read(&p, sizeof(s64)); } /* @fabricated */
int read(u64& p) { return this->read(&p, sizeof(u64)); } /* @fabricated */

u8 read8b()
{
u8 b;
this->read(&b, sizeof(u8));
return b;
}

u16 read16b()
{
u16 s;
this->read(&s, sizeof(u16));
return s;
}

u32 read32b()
{
u32 i;
this->read(&i, sizeof(u32));
return i;
}

/* @fabricated */
s8 readS8()
{
s8 b;
this->read(&b, sizeof(s8));
return b;
}

u8 readU8()
{
u8 b;
this->read(&b, sizeof(u8));
return b;
}

s16 readS16()
{
s16 s;
this->read(&s, sizeof(s16));
return s;
}

class JSURandomInputStream : public JSUInputStream { };
u16 readU16()
{
u16 s;
this->read(&s, sizeof(u16));
return s;
}

s32 readS32()
{
s32 i;
this->read(&i, sizeof(s32));
return i;
}

u32 readU32()
{
u32 i;
this->read(&i, sizeof(u32));
return i;
}

JSUInputStream& operator>>(s8& p)
{
this->read(&p, sizeof(s8));
return *this;
}

JSUInputStream& operator>>(u8& p)
{
this->read(&p, sizeof(u8));
return *this;
}

JSUInputStream& operator>>(s16& p)
{
this->read(&p, sizeof(s16));
return *this;
}

JSUInputStream& operator>>(u16& p)
{
this->read(&p, sizeof(u16));
return *this;
}

JSUInputStream& operator>>(u32& p)
{
this->read(&p, sizeof(u32));
return *this;
}
};

#endif
23 changes: 23 additions & 0 deletions include/JSystem/JSupport/JSUIosBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef JSUIOSBASE_H
#define JSUIOSBASE_H

#include <types.h>
#include <JSystem/JSupport/JSUStreamEnum.hpp>

class JSUIosBase {
public:
inline JSUIosBase()
: mState(GOOD)
{
}

virtual ~JSUIosBase() { }

bool isGood() { return !this->mState; }
void clrState(EIoState ioState) { this->mState &= ~ioState; }
void setState(EIoState ioState) { this->mState |= ioState; }

u8 mState;
};

#endif
28 changes: 28 additions & 0 deletions include/JSystem/JSupport/JSURandomInputStream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef JSU_RANDOM_INPUT_STREAM_H
#define JSU_RANDOM_INPUT_STREAM_H

#include <types.h>
#include <JSystem/JSupport/JSUInputStream.hpp>

#ifdef __cplusplus
class JSURandomInputStream : public JSUInputStream {
public:
virtual ~JSURandomInputStream() { }

virtual int getAvailable() const
{
return this->getLength() - this->getPosition();
}
virtual int skip(s32 amount);
virtual int readData(void* buf, s32 count) = 0;
virtual int getLength() const = 0;
virtual int getPosition() const = 0;
virtual int seekPos(s32 offset, JSUStreamSeekFrom from) = 0;

int align(s32 alignment);
int peek(void* buf, s32 len);
int seek(s32 offset, JSUStreamSeekFrom from);
};
#endif

#endif
8 changes: 8 additions & 0 deletions include/JSystem/JSupport/JSUStreamEnum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef JSU_STREAM_ENUM_H
#define JSU_STREAM_ENUM_H

enum JSUStreamSeekFrom { SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2 };

enum EIoState { GOOD = 0, EOF = 1 };

#endif
3 changes: 3 additions & 0 deletions include/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#define FLAG_ON(V, F) (((V) & (F)) == 0)
#define FLAG_OFF(V, F) (((V) & (F)) != 0)

#define ALIGN_PREV(u, align) (u & (~(align - 1)))
#define ALIGN_NEXT(u, align) ((u + (align - 1)) & (~(align - 1)))

#ifdef DEBUG
#define ASSERTLINE(line, cond) \
((cond) || (OSPanic(__FILE__, line, "Failed assertion " #cond), 0))
Expand Down
130 changes: 130 additions & 0 deletions src/JSystem/JSUInputStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <JSystem/JSupport/JSUInputStream.hpp>
#include <JSystem/JSupport/JSURandomInputStream.hpp>
#include <macros.h>

JSUInputStream::~JSUInputStream() { }

int JSUInputStream::read(void* buf, s32 size)
{
int len = readData(buf, size);
if (len != size) {
setState(EOF);
}
return len;
}

char* JSUInputStream::readString()
{
u16 strLen;
if (readData(&strLen, sizeof(strLen)) != sizeof(strLen)) {
setState(EOF);
return nullptr;
}

char* buf = new char[strLen + 1];

int r;
if (buf == nullptr) {
r = skip(strLen);
} else {
r = readData(buf, strLen);
buf[r] = '\0';
}

if (r != strLen) {
setState(EOF);
}
return buf;
}

char* JSUInputStream::readString(char* buf, u16 len)
{
u16 strLen;
if (readData(&strLen, sizeof(strLen)) != sizeof(strLen)) {
buf[0] = '\0';
setState(EOF);
return nullptr;
}

s32 r = 0;
if (strLen < len) {
r = readData(buf, strLen);
buf[r] = '\0';
} else {
r = readData(buf, len - 1);
buf[r] = '\0';
r += skip(strLen - (len - 1));
}

if (r != strLen) {
setState(EOF);
}

return buf;
}

int JSUInputStream::skip(s32 amount)
{
u8 _p;
int i;

for (i = 0; i < amount; i++) {
if (readData(&_p, sizeof(_p)) != sizeof(_p)) {
setState(EOF);
break;
}
}

return i;
}

/* JSURandomInputStream */

int JSURandomInputStream::skip(s32 amount)
{
int s = seekPos(amount, SEEK_CUR);
if (s != amount) {
setState(EOF);
}
return s;
}

int JSURandomInputStream::align(s32 alignment)
{
int pos = getPosition();
int aligned = ALIGN_NEXT(pos, alignment);
int change;
// NOTE: this is insane but it matches
if ((change = aligned - pos) != 0) {
int s = seekPos(aligned, SEEK_SET);
if (s != change) {
setState(EOF);
}
}

return change;
}

int JSURandomInputStream::peek(void* buf, s32 len)
{
int r = 0; // NOTE: does not match unless we declare this first
int pos = getPosition();
// NOTE: this is read, but there's no inlining padding here so the code was
// actually copy-pasted in the original?
r = readData(buf, len);
if (r != len) {
setState(EOF);
}
if (r != 0) {
seekPos(pos, SEEK_SET);
}

return r;
}

int JSURandomInputStream::seek(s32 offset, JSUStreamSeekFrom from)
{
int s = seekPos(offset, from);
clrState(EOF);
return s;
}

0 comments on commit b35882e

Please sign in to comment.