Skip to content

Commit

Permalink
Implement GameContext::Parse
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Oct 9, 2023
1 parent 64a7bef commit cfbdd45
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ CFLAGS=/nologo /MT /EHsc /G5 /GS /Od /Oi /Ob1 /DNDEBUG /Zi /I $(OBJ_DIR)\autogen

TH06E_OBJS= $(OBJ_DIR)\main.obj \
$(OBJ_DIR)\Chain.obj \
$(OBJ_DIR)\FileSystem.obj \
$(OBJ_DIR)\GameContext.obj \
$(OBJ_DIR)\GameErrorContext.obj \
$(OBJ_DIR)\GameWindow.obj \
$(OBJ_DIR)\MidiOutput.obj \
$(OBJ_DIR)\Pbg3Archive.obj \
$(OBJ_DIR)\SoundPlayer.obj \
$(OBJ_DIR)\VeryBigStruct.obj \
$(OBJ_DIR)\utils.obj \
Expand Down
112 changes: 112 additions & 0 deletions src/FileSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <stdio.h>
#include <string.h>

#include "FileSystem.hpp"
#include "Pbg3Archive.hpp"
#include "utils.hpp"

u32 g_LastFileSize;

u8 *OpenPath(char *filepath, int isExternalResource)
{
// char *slashPos;
u8 *buf;
FILE *fileOb;
size_t fsize;
// this is pbg3Idx
i32 pleaseGiveMeC;
char *entryname;
i32 i;

pleaseGiveMeC = -1;
if (isExternalResource == 0)
{
entryname = strrchr(filepath, '¥¥');
if (entryname == (char *)0x0)
{
entryname = filepath;
}
else
{
entryname = entryname + 1;
}
entryname = strrchr(entryname, '/');
if (entryname == (char *)0x0)
{
entryname = filepath;
}
else
{
entryname = entryname + 1;
}
if (g_Pbg3Archives != NULL)
{
for (i = 0; i < 0x10; i += 1)
{
if (g_Pbg3Archives[i] != NULL)
{
pleaseGiveMeC = g_Pbg3Archives[i]->FindEntry(entryname);
if (pleaseGiveMeC >= 0)
{
break;
}
}
}
}
if (pleaseGiveMeC < 0)
{
return NULL;
}
}
if (pleaseGiveMeC >= 0)
{
DebugPrint2("%s Decode ... ¥n", entryname);
buf = g_Pbg3Archives[i]->ReadEntry(pleaseGiveMeC, entryname);
g_LastFileSize = g_Pbg3Archives[i]->GetEntrySize(pleaseGiveMeC);
}
else
{
DebugPrint2("%s Load ... ¥n", filepath);
fileOb = fopen(filepath, "rb");
if (fileOb == NULL)
{
DebugPrint2("error : %s is not found.¥n", filepath);
return NULL;
}
else
{
fseek(fileOb, 0, SEEK_END);
fsize = ftell(fileOb);
g_LastFileSize = fsize;
fseek(fileOb, 0, SEEK_SET);
buf = (u8 *)malloc(fsize);
fread(buf, 1, fsize, fileOb);
fclose(fileOb);
}
}
return buf;
}

int WriteDataToFile(char *path, void *data, size_t size)
{
FILE *f;

f = fopen(path, "wb");
if (f == (FILE *)0x0)
{
return -1;
}
else
{
if (fwrite(data, 1, size, f) != size)
{
fclose(f);
return -2;
}
else
{
fclose(f);
return 0;
}
}
}
8 changes: 8 additions & 0 deletions src/FileSystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "inttypes.hpp"

u8 *OpenPath(char *filepath, int isExternalResource);
int WriteDataToFile(char *path, void *data, size_t size);

extern u32 g_LastFileSize;
134 changes: 132 additions & 2 deletions src/GameContext.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "GameContext.hpp"
#include "FileSystem.hpp"
#include "GameErrorContext.hpp"
#include "i18n.hpp"
#include "inttypes.hpp"
#include "utils.hpp"

#include <stdio.h>
#include <string.h>

GameContext g_GameContext;
ControllerMapping g_ControllerMapping;
JOYCAPSA g_JoystickCaps;

i32 InitD3dInterface(void)
Expand All @@ -21,10 +24,137 @@ i32 InitD3dInterface(void)
return 0;
}

// TODO: Implement this.
// TODO: Not a perfect match.
i32 GameContext::Parse(char *path)
{
return -1;
u8 *data;
FILE *wavFile;

memset(&g_GameContext.cfg, 0, sizeof(GameConfiguration));
g_GameContext.cfg.opts = g_GameContext.cfg.opts | USE_D3D_HW_TEXTURE_BLENDING;
data = OpenPath(path, 1);
if (data == NULL)
{
g_GameContext.cfg.lifeCount = 2;
g_GameContext.cfg.bombCount = 3;
g_GameContext.cfg.colorMode16bit = 0xff;
g_GameContext.cfg.version = 0x102;
g_GameContext.cfg.padXAxis = 600;
g_GameContext.cfg.padYAxis = 600;
wavFile = fopen("bgm/th06_01.wav", "rb");
if (wavFile == NULL)
{
g_GameContext.cfg.musicMode = MIDI;
DebugPrint(TH_ERR_NO_WAVE_FILE);
}
else
{
g_GameContext.cfg.musicMode = WAV;
fclose(wavFile);
}
g_GameContext.cfg.playSounds = 1;
g_GameContext.cfg.defaultDifficulty = 1;
g_GameContext.cfg.windowed = false;
g_GameContext.cfg.frameskipConfig = 0;
g_GameContext.cfg.controllerMapping = g_ControllerMapping;
GameErrorContextLog(&g_GameErrorContext, TH_ERR_CONFIG_NOT_FOUND);
}
else
{
memcpy(&g_GameContext.cfg, data, sizeof(GameConfiguration));
if ((4 < g_GameContext.cfg.lifeCount) || (3 < g_GameContext.cfg.bombCount) ||
(1 < g_GameContext.cfg.colorMode16bit) || (MIDI < g_GameContext.cfg.musicMode) ||
(4 < g_GameContext.cfg.defaultDifficulty) || (1 < g_GameContext.cfg.playSounds) ||
(1 < g_GameContext.cfg.windowed) || (2 < g_GameContext.cfg.frameskipConfig) ||
(g_GameContext.cfg.version != 0x102) || (g_LastFileSize != 0x38))
{
g_GameContext.cfg.lifeCount = 2;
g_GameContext.cfg.bombCount = 3;
g_GameContext.cfg.colorMode16bit = 0xff;
g_GameContext.cfg.version = 0x102;
g_GameContext.cfg.padXAxis = 600;
g_GameContext.cfg.padYAxis = 600;
wavFile = fopen("bgm/th06_01.wav", "rb");
if (wavFile == NULL)
{
g_GameContext.cfg.musicMode = MIDI;
DebugPrint(TH_ERR_NO_WAVE_FILE);
}
else
{
g_GameContext.cfg.musicMode = WAV;
fclose(wavFile);
}
g_GameContext.cfg.playSounds = 1;
g_GameContext.cfg.defaultDifficulty = 1;
g_GameContext.cfg.windowed = false;
g_GameContext.cfg.frameskipConfig = 0;
g_GameContext.cfg.controllerMapping = g_ControllerMapping;
g_GameContext.cfg.opts = g_GameContext.cfg.opts | USE_D3D_HW_TEXTURE_BLENDING;
GameErrorContextLog(&g_GameErrorContext, TH_ERR_CONFIG_CORRUPTED);
}
g_ControllerMapping = g_GameContext.cfg.controllerMapping;
free(data);
}
if (((this->cfg.opts >> 1) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_NO_VERTEX_BUFFER);
}
if (((this->cfg.opts >> 10) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_NO_FOG);
}
if (((this->cfg.opts >> 2) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_USE_16BIT_TEXTURES);
}
if (((this->cfg.opts >> 3) & 1 != 0 || (this->cfg.opts >> 4) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_FORCE_BACKBUFFER_CLEAR);
}
if (((this->cfg.opts >> 4) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_DONT_RENDER_ITEMS);
}
if (((this->cfg.opts >> 5) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_NO_GOURAUD_SHADING);
}
if (((this->cfg.opts >> 6) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_NO_DEPTH_TESTING);
}
if (((this->cfg.opts >> 7) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_FORCE_60FPS_MODE);
this->vsyncEnabled = 0;
}
if (((this->cfg.opts >> 8) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_NO_TEXTURE_COLOR_COMPOSITING);
}
if (((this->cfg.opts >> 8) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_LAUNCH_WINDOWED);
}
if (((this->cfg.opts >> 9) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_FORCE_REFERENCE_RASTERIZER);
}
if (((this->cfg.opts >> 0xb) & 1) != 0)
{
GameErrorContextLog(&g_GameErrorContext, TH_ERR_DO_NOT_USE_DIRECTINPUT);
}
if (WriteDataToFile(path, &g_GameContext.cfg, sizeof(GameConfiguration)) == 0)
{
return 0;
}
else
{
GameErrorContextFatal(&g_GameErrorContext, TH_ERR_FILE_CANNOT_BE_EXPORTED, path);
GameErrorContextFatal(&g_GameErrorContext, TH_ERR_FOLDER_HAS_WRITE_PROTECT_OR_DISK_FULL);
return -1;
}
}

u16 GetJoystickCaps(void)
Expand Down
15 changes: 12 additions & 3 deletions src/GameContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

enum GameConfigOpts
{
USE_D3D_HW_TEXTURE_BLENDING = 1 << 0x0,
DONT_USE_VERTEX_BUF = 1 << 0x1,
FORCE_16BIT_COLOR_MODE = 1 << 0x2,
CLEAR_BACKBUFFER_ON_REFRESH = 1 << 0x3,
Expand All @@ -34,6 +35,13 @@ struct ControllerMapping
i16 skipButton;
};

enum MusicMode
{
OFF = 0,
WAV = 1,
MIDI = 2
};

struct GameConfiguration
{
ControllerMapping controllerMapping;
Expand All @@ -42,10 +50,9 @@ struct GameConfiguration
i8 lifeCount;
i8 bombCount;
i8 colorMode16bit;
// 0 is off, 1 for wav, 2 for midi
i8 musicMode;
MusicMode musicMode;
i8 playSounds;
i8 unk7;
i8 defaultDifficulty;
u8 windowed;
// 0 = fullspeed, 1 = 1/2 speed, 2 = 1/4 speed.
i8 frameskipConfig;
Expand Down Expand Up @@ -75,6 +82,7 @@ struct GameContext
GameConfiguration cfg;

i32 unk198;
i32 vsyncEnabled;

MidiOutput *midiOutput;
};
Expand Down Expand Up @@ -112,4 +120,5 @@ unsigned int SetButtonFromDirectInputJoystate(u16 *outButtons, i16 controllerBut
u16 GetControllerInput(u16 buttons);
u16 GetInput(void);

extern ControllerMapping g_ControllerMapping;
extern GameContext g_GameContext;
20 changes: 20 additions & 0 deletions src/Pbg3Archive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stddef.h>

#include "Pbg3Archive.hpp"

Pbg3Archive **g_Pbg3Archives;

u32 Pbg3Archive::FindEntry(char *path)
{
return -1;
}

u8 *Pbg3Archive::ReadEntry(u32 entryIdx, char *filename)
{
return NULL;
}

u32 Pbg3Archive::GetEntrySize(u32 entryIdx)
{
return 0;
}
12 changes: 12 additions & 0 deletions src/Pbg3Archive.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "inttypes.hpp"

struct Pbg3Archive
{
u32 FindEntry(char *path);
u8 *ReadEntry(u32 entryIdx, char *filename);
u32 GetEntrySize(u32 entryIdx);
};

extern Pbg3Archive **g_Pbg3Archives;
Loading

0 comments on commit cfbdd45

Please sign in to comment.