Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rubberduckycooly
GitHub Repository: rubberduckycooly/Sonic-1-2-2013-Decompilation
Path: blob/main/RSDKv4/Reader.hpp
817 views
1
#ifndef READER_H
2
#define READER_H
3
4
#ifdef FORCE_CASE_INSENSITIVE
5
6
#include "fcaseopen.h"
7
#define FileIO FILE
8
#define fOpen(path, mode) fcaseopen(path, mode)
9
#define fRead(buffer, elementSize, elementCount, file) fread(buffer, elementSize, elementCount, file)
10
#define fSeek(file, offset, whence) fseek(file, offset, whence)
11
#define fTell(file) ftell(file)
12
#define fClose(file) fclose(file)
13
#define fWrite(buffer, elementSize, elementCount, file) fwrite(buffer, elementSize, elementCount, file)
14
15
#else
16
17
#if RETRO_USING_SDL1 || RETRO_USING_SDL2
18
#define FileIO SDL_RWops
19
#define fOpen(path, mode) SDL_RWFromFile(path, mode)
20
#define fRead(buffer, elementSize, elementCount, file) SDL_RWread(file, buffer, elementSize, elementCount)
21
#define fSeek(file, offset, whence) SDL_RWseek(file, offset, whence)
22
#define fTell(file) SDL_RWtell(file)
23
#define fClose(file) SDL_RWclose(file)
24
#define fWrite(buffer, elementSize, elementCount, file) SDL_RWwrite(file, buffer, elementSize, elementCount)
25
#else
26
#define FileIO FILE
27
#define fOpen(path, mode) fopen(path, mode)
28
#define fRead(buffer, elementSize, elementCount, file) fread(buffer, elementSize, elementCount, file)
29
#define fSeek(file, offset, whence) fseek(file, offset, whence)
30
#define fTell(file) ftell(file)
31
#define fClose(file) fclose(file)
32
#define fWrite(buffer, elementSize, elementCount, file) fwrite(buffer, elementSize, elementCount, file)
33
#endif
34
35
#endif
36
37
#define RETRO_PACKFILE_COUNT (0x1000)
38
#define RETRO_PACK_COUNT (0x4)
39
40
struct FileInfo {
41
char fileName[0x100];
42
int fileSize;
43
int vfileSize;
44
int readPos;
45
int bufferPosition;
46
int virtualFileOffset;
47
byte eStringPosA;
48
byte eStringPosB;
49
byte eStringNo;
50
byte eNybbleSwap;
51
bool useEncryption;
52
byte packID;
53
byte encryptionStringA[0x10];
54
byte encryptionStringB[0x10];
55
#if !RETRO_USE_ORIGINAL_CODE
56
FileIO *cFileHandle;
57
bool usingDataPack;
58
#endif
59
};
60
61
struct RSDKFileInfo {
62
uint hash[4];
63
int offset;
64
int filesize;
65
bool encrypted;
66
byte packID;
67
};
68
69
struct RSDKContainer {
70
RSDKFileInfo files[RETRO_PACKFILE_COUNT];
71
char packNames[RETRO_PACK_COUNT][0x400];
72
int fileCount;
73
int packCount;
74
};
75
76
extern RSDKContainer rsdkContainer;
77
78
extern char fileName[0x100];
79
extern byte fileBuffer[0x2000];
80
extern int fileSize;
81
extern int vFileSize;
82
extern int readPos;
83
extern int readSize;
84
extern int bufferPosition;
85
extern int virtualFileOffset;
86
extern bool useEncryption;
87
extern byte packID;
88
extern byte eStringPosA;
89
extern byte eStringPosB;
90
extern byte eStringNo;
91
extern byte eNybbleSwap;
92
extern byte encryptionStringA[0x10];
93
extern byte encryptionStringB[0x10];
94
95
extern FileIO *cFileHandle;
96
97
inline void CopyFilePath(char *dest, const char *src)
98
{
99
strcpy(dest, src);
100
for (int i = 0;; ++i) {
101
if (i >= strlen(dest)) {
102
break;
103
}
104
105
if (dest[i] == '/')
106
dest[i] = '\\';
107
}
108
}
109
bool CheckRSDKFile(const char *filePath);
110
inline void CloseRSDKContainers()
111
{
112
for (int i = 0; i < 4; ++i) {
113
strcpy(rsdkContainer.packNames[i], "");
114
}
115
rsdkContainer.packCount = 0;
116
rsdkContainer.fileCount = 0;
117
}
118
119
#if !RETRO_USE_ORIGINAL_CODE
120
int CheckFileInfo(const char *filepath);
121
#endif
122
123
bool LoadFile(const char *filePath, FileInfo *fileInfo);
124
inline bool CloseFile()
125
{
126
int result = 0;
127
if (cFileHandle)
128
result = fClose(cFileHandle);
129
130
cFileHandle = NULL;
131
return result;
132
}
133
134
void GenerateELoadKeys(uint key1, uint key2);
135
136
void FileRead(void *dest, int size);
137
void FileSkip(int count);
138
139
inline size_t FillFileBuffer()
140
{
141
if (readPos + 0x2000 <= fileSize)
142
readSize = 0x2000;
143
else
144
readSize = fileSize - readPos;
145
146
size_t result = fRead(fileBuffer, 1u, readSize, cFileHandle);
147
readPos += readSize;
148
bufferPosition = 0;
149
return result;
150
}
151
152
void GetFileInfo(FileInfo *fileInfo);
153
void SetFileInfo(FileInfo *fileInfo);
154
size_t GetFilePosition();
155
void SetFilePosition(int newPos);
156
bool ReachedEndOfFile();
157
158
#endif // !READER_H
159
160