-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
275 lines (239 loc) · 8.54 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "about.h"
#include "patches.h"
#include "ROM.h"
#include "functions.cpp"
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrsMain *frsMain;
__fastcall TfrsMain::TfrsMain(TComponent* Owner) : TForm(Owner){
Application->Title = frsMain->Caption;
AppPath = ExtractFilePath(Application->ExeName);
LastDir = AppPath;
}
// EVENTS
void __fastcall TfrsMain::ev_LoadROM(TObject *Sender)
{
TOpenDialog *OpenFile = new TOpenDialog(Application);
OpenFile->Filter = "SNES ROM Files|*.smc;*.sfc";
OpenFile->Options.Clear();
OpenFile->Options << ofFileMustExist;
if(OpenFile->Execute() && FileExists(OpenFile->FileName)){
Levels->Clear();
FileName = OpenFile->FileName;
ROM = new ROMFile(OpenFile->FileName);
fn_Log("Loaded: " + ROM->Title);
ROMInfo->Strings->Clear();
ROMInfo->Strings->Add("Title=" + ROM->Title);
ROMInfo->Strings->Add("Code=" + ROM->GameCode);
ROMInfo->Strings->Add("Region=" + ROM->Region);
ROMInfo->Strings->Add("Version=" + ROM->Version);
ROMInfo->Strings->Add("ROM Size=" + ROM->ROMSize);
ROMInfo->Strings->Add("RAM Size=" + ROM->RAMSize);
ROMInfo->Strings->Add("SRAM Size=" + ROM->ExRAMSize);
ROMInfo->Strings->Add(sprintf("Headered=%s", (ROM->HasHeader ? "Yes" : "No")));
if(!ROM->CheckROM() && MessageBoxA(NULL, "Invalid ROM, you need a Yoshi's Island ROM file. Do you wanna try load the level data anyway?", "Invalid Yoshi's Island ROM", MB_YESNO | MB_ICONWARNING) == ID_YES){
ROM->BypassCheck = true;
}
btn_ClearLevels->Enabled = ROM->CheckROM();
btn_ExportLevels->Enabled = ROM->CheckROM();
btn_ImportLevels->Enabled = ROM->CheckROM();
btn_ExportEntrances->Enabled = ROM->CheckROM();
btn_ImportEntrances->Enabled = ROM->CheckROM();
LoadLevels();
}
btn_SaveROM->Enabled = ROM ? true : false;
btn_Patches->Enabled = ROM ? true : false;
}
void __fastcall TfrsMain::ev_SaveROM(TObject *Sender)
{
if(ROM){
int MSG = MessageBoxA(Handle, "Warning!\nClick on 'Yes' if you want to overwrite the opened ROM.\nIf you don't want to overwrite, click on 'No' and save as another file.", "Save Changes", MB_YESNOCANCEL | MB_ICONWARNING);
bool Saved = false;
if(MSG == ID_NO){
TSaveDialog *SaveFile = new TSaveDialog(Application);
SaveFile->Filter = "SNES ROM Files|*.smc;*.sfc";
SaveFile->FileName = ExtractFileName(FileName);
SaveFile->InitialDir = ExtractFileDir(FileName);
if(SaveFile->Execute()){
ROM->SaveToFile(SaveFile->FileName);
Saved = true;
}
} else if(MSG == ID_YES){
ROM->SaveToFile(FileName);
Saved = true;
}
if(Saved){
ShowMessage("All changes were successfully saved :)");
}
}
}
void __fastcall TfrsMain::ev_ToggleDebugInfo(TObject *Sender)
{
DebugInfo->Visible = chk_DebugInfo->Checked;
}
void __fastcall TfrsMain::ev_OpenAbout(TObject *Sender)
{
frsAbout->ShowModal();
}
void __fastcall TfrsMain::ev_ExportLevels(TObject *Sender)
{
if(Levels->SelCount == 0)
Levels->SelectAll();
if(!ROM || !ROM->CheckROM()) ShowMessage("You need to load a valid ROM first...");
else if(SelectDirectory("Directory to export the levels", "", LastDir, TSelectDirExtOpts() << sdNewUI << sdNewFolder, NULL)){
Progress->Position = 0; Progress->Max = Levels->SelCount;
for(int ID = 0; ID < Levels->Items->Count; ID++){
if(Levels->Items->Item[ID]->Selected){
Progress->Position++;
Levels->Items->Item[ID]->SubItems->Text = ROM->ExportLevel(ID, LastDir + sprintf("/level_%02X.ylt", ID));
}
}
ShowMessage("All levels were successfully exported! :D");
if(MessageBoxA(Handle, "Do you want to export all levels entrances data too?", "Export Levels Entrances", MB_YESNO | MB_ICONINFORMATION) == ID_YES){
ROM->ExportEntrances(LastDir + "/entrances.yet");
}
}
}
void __fastcall TfrsMain::ev_ImportLevels(TObject *Sender)
{
if(ROM && ROM->CheckROM() && SelectDirectory("Directory to import the levels", "", LastDir, TSelectDirExtOpts() << sdNewUI << sdNewFolder, NULL)){
TSearchRec FILE;
FileList = new TStringList();
if(FindFirst(LastDir + "\\*.ylt", faArchive, FILE) == 0){
do {
FileList->Add(LastDir + "/" + FILE.Name);
} while(FindNext(FILE) == 0);
FindClose(FILE);
// Clear All Levels First
for(int ID = 0; ID < 0xDE; ID++){
//Levels->Items->Item[ID]->SubItems->Text =
ROM->ClearLevel(ID);
}
ROM->FreeSpaceBlocks();
try {
Progress->Position = 0; Progress->Max = FileList->Count;
for(int X = 0; X < FileList->Count; X++){
Progress->Position++;
ROM->ImportLevel(0xFF, FileList->Strings[X]);
}
LoadLevels();
ShowMessage("All levels were successfully imported! :D");
if(FileExists(LastDir + "/entrances.yet"))
if(MessageBoxA(Handle, "I've found a levels entrances file on that folder. Do you wanna import this too, now?", "Import Levels Entrances", MB_YESNO | MB_ICONINFORMATION) == ID_YES){
ROM->ImportEntrances(LastDir + "/entrances.yet");
}
} catch (Exception &e) {
ShowMessage("Something wrong happened :(\n" + e.Message);
}
} else {
ShowMessage("This directory doesn't have any level files! :(");
}
}
}
void __fastcall TfrsMain::ev_SelectAllLevels(TObject *Sender)
{
Levels->SelectAll();
}
void __fastcall TfrsMain::ev_ImportLevel(TObject *Sender)
{
TOpenDialog *OpenFile = new TOpenDialog(Application);
OpenFile->Filter = "Yoshi's Island Level Files|*.ylt";
OpenFile->Options.Clear();
OpenFile->Options << ofFileMustExist;
if(ROM && ROM->CheckROM())
if(Levels->SelCount == 1){
if(OpenFile->Execute()){
Levels->Selected->SubItems->Text = ROM->ImportLevel(Levels->Selected->Index, OpenFile->FileName);
ShowMessage("Level successfully imported :)");
}
}
}
void __fastcall TfrsMain::ev_ExportLevel(TObject *Sender)
{
TSaveDialog *SaveFile = new TSaveDialog(Application);
SaveFile->Filter = "Yoshi's Island Levels|*.ylt";
SaveFile->InitialDir = LastDir;
if(ROM && ROM->CheckROM())
if(Levels->SelCount == 1){
SaveFile->FileName = sprintf("level_%02X.ylt", Levels->Selected->Index);
if(SaveFile->Execute()){
Levels->Selected->SubItems->Text = ROM->ExportLevel(Levels->Selected->Index, ReplaceStr(SaveFile->FileName, ".ylt", "") + ".ylt");
ShowMessage("Level exported successfully :)");
}
} else if(Levels->SelCount > 1){
ev_ExportLevels(Sender);
}
}
void __fastcall TfrsMain::ev_ClearLevel(TObject *Sender)
{
if(ROM && ROM->CheckROM())
if(Levels->SelCount == 1){
if(MessageBoxA(Handle, "Are you sure you wanna clear the level data and sprites?", "Clear Level", MB_YESNO | MB_ICONWARNING) == ID_YES){
Levels->Selected->SubItems->Text = ROM->ClearLevel(Levels->Selected->Index);
ShowMessage("Level cleared successfully :)");
ROM->FreeSpaceBlocks();
}
} else if(Levels->SelCount > 1){
ev_ExportLevels(Sender);
}
}
void __fastcall TfrsMain::ev_ClearLevels(TObject *Sender)
{
if(Levels->SelCount == 0)
Levels->SelectAll();
if(ROM || ROM->CheckROM() && MessageBoxA(Handle, "Are you sure you wanna clear all levels data and sprites?", "Clear Levels", MB_YESNO | MB_ICONWARNING) == ID_YES){
for(int ID = 0; ID < Levels->Items->Count; ID++){
if(Levels->Items->Item[ID]->Selected){
Levels->Items->Item[ID]->SubItems->Text = ROM->ClearLevel(ID);
}
}
ROM->FreeSpaceBlocks();
ShowMessage("All levels were successfully cleared! :D");
}
}
void __fastcall TfrsMain::ev_ExportEntrances(TObject *Sender)
{
TSaveDialog *SaveFile = new TSaveDialog(Application);
SaveFile->Filter = "Yoshi's Island Levels Entrances|*.yet";
SaveFile->FileName = "entrances.yet";
SaveFile->InitialDir = LastDir;
if(ROM && ROM->CheckROM() && SaveFile->Execute()){
ROM->ExportEntrances(ReplaceStr(SaveFile->FileName, ".yet", "") + ".yet");
ShowMessage("Level Entrances successfully exported! :)");
}
}
void __fastcall TfrsMain::ev_ImportEntrances(TObject *Sender)
{
TOpenDialog *OpenFile = new TOpenDialog(Application);
OpenFile->Filter = "Yoshi's Island Levels Entrances|*.yet";
OpenFile->Options.Clear();
OpenFile->Options << ofFileMustExist;
if(ROM && ROM->CheckROM() && OpenFile->Execute()){
ROM->ImportEntrances(OpenFile->FileName);
ShowMessage("Levels Entrances successfully imported! :)");
}
}
void __fastcall TfrsMain::ev_Patches(TObject *Sender)
{
dlgPatches->ShowModal();
}
// FUNCTIONS
void __fastcall TfrsMain::LoadLevels(){
if(ROM && ROM->CheckROM()){
ROM->FetchLevelsEntrances();
Levels->Clear();
TListItem *ITEM;
Progress->Position = 0; Progress->Max = 0xDD;
for(int ID = 0; ID <= 0xDD; ID++){
ITEM = Levels->Items->Add();
ITEM->Caption = sprintf("%02X", ID);
ITEM->ImageIndex = ROM->LevelEntrance(ID);
ITEM->SubItems->Text = ROM->FetchLevel(ID);
Progress->Position++;
}
Levels->Width++;
}
}