2020-08-12 16:20:34 -06:00
|
|
|
/*
|
2024-06-15 09:01:19 -06:00
|
|
|
Copyright 2016-2024 melonDS team
|
2020-08-12 16:20:34 -06:00
|
|
|
|
|
|
|
This file is part of melonDS.
|
|
|
|
|
|
|
|
melonDS is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with melonDS. If not, see http://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "ARCodeFile.h"
|
|
|
|
#include "Platform.h"
|
|
|
|
|
2023-11-25 10:32:09 -07:00
|
|
|
namespace melonDS
|
|
|
|
{
|
2023-08-18 14:50:57 -06:00
|
|
|
using namespace Platform;
|
2020-08-12 16:20:34 -06:00
|
|
|
|
|
|
|
// TODO: import codes from other sources (usrcheat.dat, ...)
|
|
|
|
// TODO: more user-friendly error reporting
|
|
|
|
|
|
|
|
|
2023-04-28 09:05:34 -06:00
|
|
|
ARCodeFile::ARCodeFile(const std::string& filename)
|
2020-08-12 16:20:34 -06:00
|
|
|
{
|
2022-01-07 06:00:43 -07:00
|
|
|
Filename = filename;
|
2020-08-12 16:20:34 -06:00
|
|
|
|
|
|
|
Error = false;
|
|
|
|
|
|
|
|
Categories.clear();
|
|
|
|
|
2020-08-14 16:14:05 -06:00
|
|
|
if (!Load())
|
|
|
|
Error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ARCodeFile::~ARCodeFile()
|
|
|
|
{
|
|
|
|
Categories.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARCodeFile::Load()
|
|
|
|
{
|
2023-08-18 14:50:57 -06:00
|
|
|
FileHandle* f = OpenFile(Filename, FileMode::ReadText);
|
2020-08-14 16:14:05 -06:00
|
|
|
if (!f) return true;
|
|
|
|
|
|
|
|
Categories.clear();
|
2020-08-12 16:20:34 -06:00
|
|
|
|
|
|
|
bool isincat = false;
|
|
|
|
ARCodeCat curcat;
|
|
|
|
|
|
|
|
bool isincode = false;
|
|
|
|
ARCode curcode;
|
|
|
|
|
|
|
|
char linebuf[1024];
|
2023-08-18 14:50:57 -06:00
|
|
|
while (!IsEndOfFile(f))
|
2020-08-12 16:20:34 -06:00
|
|
|
{
|
2023-08-18 14:50:57 -06:00
|
|
|
if (!FileReadLine(linebuf, 1024, f))
|
2021-05-03 09:02:38 -06:00
|
|
|
break;
|
2021-05-03 06:36:21 -06:00
|
|
|
|
2020-08-12 16:20:34 -06:00
|
|
|
linebuf[1023] = '\0';
|
|
|
|
|
|
|
|
char* start = linebuf;
|
|
|
|
while (start[0]==' ' || start[0]=='\t')
|
|
|
|
start++;
|
|
|
|
|
|
|
|
if (start[0]=='#' || start[0]=='\r' || start[0]=='\n' || start[0]=='\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strncasecmp(start, "CAT", 3))
|
|
|
|
{
|
|
|
|
char catname[128];
|
2020-08-14 16:14:05 -06:00
|
|
|
int ret = sscanf(start, "CAT %127[^\r\n]", catname);
|
2020-08-12 16:20:34 -06:00
|
|
|
catname[127] = '\0';
|
|
|
|
|
|
|
|
if (ret < 1)
|
|
|
|
{
|
2023-03-23 11:04:38 -06:00
|
|
|
Log(LogLevel::Error, "AR: malformed CAT line: %s\n", start);
|
2023-08-18 14:50:57 -06:00
|
|
|
CloseFile(f);
|
2020-08-14 16:14:05 -06:00
|
|
|
return false;
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isincode) curcat.Codes.push_back(curcode);
|
|
|
|
isincode = false;
|
|
|
|
|
|
|
|
if (isincat) Categories.push_back(curcat);
|
|
|
|
isincat = true;
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
curcat.Name = catname;
|
2020-08-12 16:20:34 -06:00
|
|
|
curcat.Codes.clear();
|
|
|
|
}
|
|
|
|
else if (!strncasecmp(start, "CODE", 4))
|
|
|
|
{
|
|
|
|
int enable;
|
|
|
|
char codename[128];
|
2020-08-14 16:14:05 -06:00
|
|
|
int ret = sscanf(start, "CODE %d %127[^\r\n]", &enable, codename);
|
2020-08-12 16:20:34 -06:00
|
|
|
codename[127] = '\0';
|
|
|
|
|
|
|
|
if (ret < 2)
|
|
|
|
{
|
2023-03-23 11:04:38 -06:00
|
|
|
Log(LogLevel::Error, "AR: malformed CODE line: %s\n", start);
|
2023-08-18 14:50:57 -06:00
|
|
|
CloseFile(f);
|
2020-08-14 16:14:05 -06:00
|
|
|
return false;
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isincat)
|
|
|
|
{
|
2023-03-23 11:04:38 -06:00
|
|
|
Log(LogLevel::Error, "AR: encountered CODE line with no category started\n");
|
2023-08-18 14:50:57 -06:00
|
|
|
CloseFile(f);
|
2020-08-14 16:14:05 -06:00
|
|
|
return false;
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isincode) curcat.Codes.push_back(curcode);
|
|
|
|
isincode = true;
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
curcode.Name = codename;
|
2020-08-12 16:20:34 -06:00
|
|
|
curcode.Enabled = enable!=0;
|
2023-08-27 05:34:11 -06:00
|
|
|
curcode.Code.clear();
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u32 c0, c1;
|
|
|
|
int ret = sscanf(start, "%08X %08X", &c0, &c1);
|
|
|
|
|
|
|
|
if (ret < 2)
|
|
|
|
{
|
2023-03-23 11:04:38 -06:00
|
|
|
Log(LogLevel::Error, "AR: malformed data line: %s\n", start);
|
2023-08-18 14:50:57 -06:00
|
|
|
CloseFile(f);
|
2020-08-14 16:14:05 -06:00
|
|
|
return false;
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isincode)
|
|
|
|
{
|
2023-03-23 11:04:38 -06:00
|
|
|
Log(LogLevel::Error, "AR: encountered data line with no code started\n");
|
2023-08-18 14:50:57 -06:00
|
|
|
CloseFile(f);
|
2020-08-14 16:14:05 -06:00
|
|
|
return false;
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
|
2023-08-27 05:34:11 -06:00
|
|
|
curcode.Code.push_back(c0);
|
|
|
|
curcode.Code.push_back(c1);
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isincode) curcat.Codes.push_back(curcode);
|
|
|
|
if (isincat) Categories.push_back(curcat);
|
|
|
|
|
2023-08-18 14:50:57 -06:00
|
|
|
CloseFile(f);
|
2020-08-14 16:14:05 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARCodeFile::Save()
|
|
|
|
{
|
2023-08-18 14:50:57 -06:00
|
|
|
FileHandle* f = Platform::OpenFile(Filename, FileMode::WriteText);
|
2020-08-14 16:14:05 -06:00
|
|
|
if (!f) return false;
|
2020-08-12 16:20:34 -06:00
|
|
|
|
|
|
|
for (ARCodeCatList::iterator it = Categories.begin(); it != Categories.end(); it++)
|
|
|
|
{
|
|
|
|
ARCodeCat& cat = *it;
|
2020-08-14 16:14:05 -06:00
|
|
|
|
2023-11-02 05:40:49 -06:00
|
|
|
if (it != Categories.begin()) FileWriteFormatted(f, "\n");
|
|
|
|
FileWriteFormatted(f, "CAT %s\n\n", cat.Name.c_str());
|
2020-08-12 16:20:34 -06:00
|
|
|
|
|
|
|
for (ARCodeList::iterator jt = cat.Codes.begin(); jt != cat.Codes.end(); jt++)
|
|
|
|
{
|
|
|
|
ARCode& code = *jt;
|
2023-11-02 05:40:49 -06:00
|
|
|
FileWriteFormatted(f, "CODE %d %s\n", code.Enabled, code.Name.c_str());
|
2020-08-12 16:20:34 -06:00
|
|
|
|
2023-08-27 05:34:11 -06:00
|
|
|
for (size_t i = 0; i < code.Code.size(); i+=2)
|
2020-08-12 16:20:34 -06:00
|
|
|
{
|
2023-11-02 05:40:49 -06:00
|
|
|
FileWriteFormatted(f, "%08X %08X\n", code.Code[i], code.Code[i + 1]);
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
2020-08-14 16:14:05 -06:00
|
|
|
|
2023-11-02 05:40:49 -06:00
|
|
|
FileWriteFormatted(f, "\n");
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 14:50:57 -06:00
|
|
|
CloseFile(f);
|
2020-08-14 16:14:05 -06:00
|
|
|
return true;
|
2020-08-12 16:20:34 -06:00
|
|
|
}
|
2023-11-25 10:32:09 -07:00
|
|
|
|
|
|
|
}
|