Skip to content

Commit

Permalink
Libms setup (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
robojumper authored Oct 4, 2024
1 parent dc221b4 commit 1870130
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 23 deletions.
23 changes: 23 additions & 0 deletions config/SOUE01/splits.txt
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,29 @@ m/m_pad.cpp:
.sdata2 start:0x8057CDF8 end:0x8057CE00
.bss start:0x805CB0B8 end:0x805CB1F8

libms/commonlib.c:
extab start:0x80006720 end:0x80006738
extabindex start:0x80006920 end:0x80006944
.text start:0x80323A70 end:0x80323C94

libms/flowfile.c:
extab start:0x80006738 end:0x80006758
extabindex start:0x80006944 end:0x80006974
.text start:0x80323CA0 end:0x80323F74
.data start:0x80547268 end:0x80547278
.sdata start:0x80574010 end:0x80574028

libms/libms.c:
.text start:0x80323F80 end:0x803240D8
.sbss start:0x80575C90 end:0x80575C98

libms/msgfile.c:
extab start:0x80006758 end:0x80006780
extabindex start:0x80006974 end:0x800069B0
.text start:0x803240E0 end:0x80324434
.data start:0x80547278 end:0x80547288
.sdata start:0x80574028 end:0x80574048

toBeSorted/cc/d_cc_d.cpp:
.text start:0x80328120 end:0x8032BC78
.ctors start:0x804DB8F8 end:0x804DB8FC
Expand Down
46 changes: 23 additions & 23 deletions config/SOUE01/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18628,29 +18628,29 @@ fn_80323830 = .text:0x80323830; // type:function size:0x24
JSUList__append = .text:0x80323860; // type:function size:0xAC
JSUPtrList__prepend = .text:0x80323910; // type:function size:0xAC
JSUList__remove = .text:0x803239C0; // type:function size:0xAC
fn_80323A70 = .text:0x80323A70; // type:function size:0x68
libms__LMS_ReadSections = .text:0x80323AE0; // type:function size:0xAC
libms__LMS_InitReadSections = .text:0x80323B90; // type:function size:0x34
libms__LMS_FindSection = .text:0x80323BD0; // type:function size:0x88
libms__LMS_EntrypointHash = .text:0x80323C60; // type:function size:0x34
fn_80323CA0 = .text:0x80323CA0; // type:function size:0x78
libms__LMS_CloseFlow = .text:0x80323D20; // type:function size:0x44
libms__LMS_GetFlow = .text:0x80323D70; // type:function size:0x28
libms__LMS_GetEntrypoint = .text:0x80323DA0; // type:function size:0xFC
libms__LMS_GetFlowElement = .text:0x80323EA0; // type:function size:0x30
libms__LMS_GetBranchPoints = .text:0x80323ED0; // type:function size:0xA4
libms__LMS_SetMemFuncs = .text:0x80323F80; // type:function size:0xC
libms__LMSi_Malloc = .text:0x80323F90; // type:function size:0xC
libms__LMSi_Free = .text:0x80323FA0; // type:function size:0xC
libms__LMSi_MemCmp = .text:0x80323FB0; // type:function size:0x44
libms__LMSi_MemCopy = .text:0x80324000; // type:function size:0xD8
libms__LMS_InitMessage = .text:0x803240E0; // type:function size:0x88
libms__LMS_CloseMessage = .text:0x80324170; // type:function size:0x44
libms__LMS_GetTextIndexByLabel = .text:0x803241C0; // type:function size:0xFC
libms__LMS_GetText = .text:0x803242C0; // type:function size:0x48
libms__LMS_GetTextByLabel = .text:0x80324310; // type:function size:0x48
libms__LMS_GetLabelByTextIndex = .text:0x80324360; // type:function size:0xA4
libms__LMS_GetAttribute = .text:0x80324410; // type:function size:0x24
LMSi_AnalyzeMessageHeader = .text:0x80323A70; // type:function size:0x68
LMSi_AnalyzeMessageBlocks = .text:0x80323AE0; // type:function size:0xAC
LMSi_AnalyzeMessageBinary = .text:0x80323B90; // type:function size:0x34
LMSi_SearchBlockByName = .text:0x80323BD0; // type:function size:0x88
LMSi_GetHashTableIndexFromLabel = .text:0x80323C60; // type:function size:0x34
LMS_InitFlow = .text:0x80323CA0; // type:function size:0x78
LMS_CloseFlow = .text:0x80323D20; // type:function size:0x44
LMS_GetFlow = .text:0x80323D70; // type:function size:0x28
LMS_GetEntrypoint = .text:0x80323DA0; // type:function size:0xFC
LMS_GetFlowElement = .text:0x80323EA0; // type:function size:0x30
LMS_GetBranchPoints = .text:0x80323ED0; // type:function size:0xA4
LMS_SetMemFuncs = .text:0x80323F80; // type:function size:0xC
LMSi_Malloc = .text:0x80323F90; // type:function size:0xC
LMSi_Free = .text:0x80323FA0; // type:function size:0xC
LMSi_MemCmp = .text:0x80323FB0; // type:function size:0x44
LMSi_MemCopy = .text:0x80324000; // type:function size:0xD8
LMS_InitMessage = .text:0x803240E0; // type:function size:0x88
LMS_CloseMessage = .text:0x80324170; // type:function size:0x44
LMS_GetTextIndexByLabel = .text:0x803241C0; // type:function size:0xFC
LMS_GetText = .text:0x803242C0; // type:function size:0x48
LMS_GetTextByLabel = .text:0x80324310; // type:function size:0x48
LMS_GetLabelByTextIndex = .text:0x80324360; // type:function size:0xA4
LMS_GetAttribute = .text:0x80324410; // type:function size:0x24
fn_80324440 = .text:0x80324440; // type:function size:0xD0
fn_80324510 = .text:0x80324510; // type:function size:0x1C
fn_80324530 = .text:0x80324530; // type:function size:0x3C
Expand Down
22 changes: 22 additions & 0 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@
"-ipa file",
]

# libms flags
cflags_libms = [
*cflags_base,
"-inline noauto",
"-fp_contract off",
"-lang=C++",
"-Cpp_exceptions on",
]

# EGG flags
cflags_egg = [
*cflags_base,
Expand Down Expand Up @@ -420,6 +429,19 @@ def nw4rLib(lib_name, objects, extra_cflags=[]):
Object(Matching, "m/m_pad.cpp"),
],
},
{
"lib": "libms",
"mw_version": "Wii/1.5",
"cflags": cflags_libms,
"progress_category": "core",
"host": False,
"objects": [
Object(NonMatching, "libms/commonlib.c"),
Object(NonMatching, "libms/flowfile.c"),
Object(NonMatching, "libms/libms.c"),
Object(NonMatching, "libms/msgfile.c"),
],
},
{
"lib": "slib",
"mw_version": "Wii/1.5",
Expand Down
51 changes: 51 additions & 0 deletions include/libms/commonlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <common.h>

#ifdef __cplusplus
extern "C" {
#endif

struct MsbBlockHeader {
/* 0x00 */ char name[4];
/* 0x04 */ int sectionLength;
/* 0x08 */ unsigned short field_0x08;
/* 0x0A */ unsigned char field_0x0A[0x10 - 0x0A];
/* 0x10 */ int numEntries;
};

struct MsbBlock {
/* 0x00 */ int *ptr;
/* 0x04 */ char name[4];
/* 0x08 */ int sectionLength;
/* 0x0C */ u16 field_0x0C;
};

#pragma push
#pragma pack(2)

struct MsbHeader {
/* 0x00 */ unsigned char field_0x00[0x0C - 0x00];
/* 0x0C */ unsigned char version;
/* 0x0E */ unsigned short sectionCount;
/* 0x10 */ unsigned char field_0x10[0x12 - 0x10];
/* 0x12 */ int fileLength;
};

#pragma pop

struct MsbInfo {
/* 0x00 */ struct MsbHeader *header;
/* 0x04 */ int fileLength;
/* 0x08 */ unsigned char version;
/* 0x0A */ unsigned short sectionCount;
/* 0x0C */ struct MsbBlock *sectionInfos;
};

void LMSi_AnalyzeMessageHeader(struct MsbInfo *info);
void LMSi_AnalyzeMessageBlocks(struct MsbInfo *info);
void LMSi_AnalyzeMessageBinary(struct MsbInfo *info, const char *type);
int LMSi_SearchBlockByName(struct MsbInfo *info, const char *name);
int LMSi_GetHashTableIndexFromLabel(const char *label, int tableSize);

#ifdef __cplusplus
}
#endif
30 changes: 30 additions & 0 deletions include/libms/flowfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <common.h>

#ifdef __cplusplus
extern "C" {
#endif

struct MsbfInfo;

struct MsbFlowInfo {
/* 0x00 */ char type;
/* 0x01 */ char subType;
/* 0x04 */ short params1n2;
/* 0x08 */ short next;
/* 0x0A */ short param3;
/* 0x0C */ short param4;
/* 0x0E */ short param5;
};

// Func names are made up

struct MsbfInfo *LMS_InitFlow(void *data);
void LMS_CloseFlow(struct MsbfInfo *info);
int LMS_GetFlow(struct MsbfInfo *info, int index);
int LMS_GetEntrypoint(struct MsbfInfo *info, const char *label);
struct MsbFlowInfo *LMS_GetFlowElement(struct MsbfInfo *info, int index);
const unsigned short *LMS_GetBranchPoints(struct MsbfInfo *info, int index);

#ifdef __cplusplus
}
#endif
17 changes: 17 additions & 0 deletions include/libms/libms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <common.h>

#ifdef __cplusplus
extern "C" {
#endif

void LMS_SetMemFuncs(void *(*alloc)(size_t size), void (*free)(void *ptr));

// internal
void *LMSi_Malloc(size_t size);
void LMSi_Free(void *ptr);
int LMSi_MemCmp(const char *p1, const char *p2, int n);
void LMSi_MemCopy(char *p1, const char *p2, int n);

#ifdef __cplusplus
}
#endif
24 changes: 24 additions & 0 deletions include/libms/msgfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <common.h>

#ifdef __cplusplus
extern "C" {
#endif

struct MsbtInfo;

struct MsbtAttrInfo {
char c_0x00;
char c_0x01;
};

struct MsbtInfo *LMS_InitMessage(void *data);
void LMS_CloseMessage(struct MsbtInfo *info);
int LMS_GetTextIndexByLabel(struct MsbtInfo *info, const char *label);
const char *LMS_GetText(struct MsbtInfo *info, int index);
const char *LMS_GetTextByLabel(struct MsbtInfo *info, const char *label);
const char *LMS_GetLabelByTextIndex(struct MsbtInfo *info, int index);
struct MsbtAttrInfo *LMS_GetAttribute(struct MsbtInfo *info, int index);

#ifdef __cplusplus
}
#endif
65 changes: 65 additions & 0 deletions src/libms/commonlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <libms/commonlib.h>
#include <libms/libms.h>

void LMSi_AnalyzeMessageHeader(struct MsbInfo *info) {
info->version = info->header->version;
info->sectionCount = info->header->sectionCount;
if (info->sectionCount == 0) {
info->sectionInfos = NULL;
} else {
info->sectionInfos = (struct MsbBlock *)LMSi_Malloc(sizeof(struct MsbBlock) * info->sectionCount);
}
info->fileLength = info->header->fileLength;
}

inline static struct MsbBlockHeader* GetBlockHeader(struct MsbInfo *info, int offset) {
return (struct MsbBlockHeader*)(offset + (unsigned int)info->header);
}

void LMSi_AnalyzeMessageBlocks(struct MsbInfo *info) {
// TODO regswap
int i;
int offset = ROUND_UP(sizeof(struct MsbHeader), 0x20); // file header
for (i = 0; i < info->sectionCount; i++) {
struct MsbBlock *section = &info->sectionInfos[i];
section->ptr = &GetBlockHeader(info, offset)->numEntries;
section->name[0] = GetBlockHeader(info, offset)->name[0];
section->name[1] = GetBlockHeader(info, offset)->name[1];
section->name[2] = GetBlockHeader(info, offset)->name[2];
section->name[3] = GetBlockHeader(info, offset)->name[3];
section->sectionLength = GetBlockHeader(info, offset)->sectionLength;
section->field_0x0C = GetBlockHeader(info, offset)->field_0x08;
offset = offset + 0x10; // section header
offset = offset + section->sectionLength; // section
offset = ROUND_UP(offset, 0x10); // align
}
}

void LMSi_AnalyzeMessageBinary(struct MsbInfo *info, const char *type) {
LMSi_AnalyzeMessageHeader(info);
LMSi_AnalyzeMessageBlocks(info);
}

int LMSi_SearchBlockByName(struct MsbInfo *info, const char *name) {
for (unsigned short i = 0; i < info->sectionCount; i++) {
if (LMSi_MemCmp(info->sectionInfos[i].name, name, 4)) {
return i;
}
}

return -1;
}

int LMSi_GetHashTableIndexFromLabel(const char *label, int tableSize) {
unsigned int hash = 0;
int c;
while (1) {
c = *label;
if (c == '\0') {
break;
}
hash = hash * 0x492 + c;
label++;
}
return hash % tableSize;
}
44 changes: 44 additions & 0 deletions src/libms/flowfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <libms/commonlib.h>
#include <libms/flowfile.h>
#include <libms/libms.h>


struct MsbfInfo {
/* 0x00 */ struct MsbInfo base;
/* 0x10 */ int flw3Index;
/* 0x14 */ int fen1Index;
/* 0x18 */ int ref1Index;
};

struct MsbfInfo *LMS_InitFlow(void *data) {
struct MsbfInfo *info = (struct MsbfInfo *)LMSi_Malloc(sizeof(struct MsbfInfo));
info->base.header = (struct MsbHeader *)data;
LMSi_AnalyzeMessageBinary(&info->base, "MsgStdBn");
info->flw3Index = LMSi_SearchBlockByName(&info->base, "LBL1");
info->fen1Index = LMSi_SearchBlockByName(&info->base, "TXT2");
info->ref1Index = LMSi_SearchBlockByName(&info->base, "ATR1");
return info;
}

void LMS_CloseFlow(struct MsbfInfo *info) {
if (info->base.sectionInfos != nullptr) {
LMSi_Free(info->base.sectionInfos);
}
LMSi_Free(info);
}

int LMS_GetFlow(struct MsbfInfo *info, int index) {
// TODO
}

int LMS_GetEntrypoint(struct MsbfInfo *info, const char *label) {
// TODO
}

struct MsbFlowInfo *LMS_GetFlowElement(struct MsbfInfo *info, int index) {
// TODO
}

const unsigned short *LMS_GetBranchPoints(struct MsbfInfo *info, int index) {
// TODO
}
37 changes: 37 additions & 0 deletions src/libms/libms.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <libms/libms.h>

static void *(*MSB_ALLOC_FUNC)(size_t size);
static void (*MSB_FREE_FUNC)(void *arg);

void LMS_SetMemFuncs(void *(*alloc)(size_t size), void (*free)(void *ptr)) {
MSB_ALLOC_FUNC = alloc;
MSB_FREE_FUNC = free;
}

// internal
void *LMSi_Malloc(size_t size) {
return (MSB_ALLOC_FUNC)(size);
}

void LMSi_Free(void *ptr) {
return (MSB_FREE_FUNC)(ptr);
}

int LMSi_MemCmp(const char *p1, const char *p2, int n) {
for (int i = 0; i < n; i++) {
if (p1[i] != p2[i]) {
return 0;
}
}

return 1;
}

void LMSi_MemCopy(char *p1, const char *p2, int n) {
// TODO register usage too optimal
for (int i = 0; i < n; i++) {
*p1 = *p2;
p1++;
p2++;
}
}
Loading

0 comments on commit 1870130

Please sign in to comment.