From 5b1a034aa98a9f139bbe639391a0d7caa4437f85 Mon Sep 17 00:00:00 2001 From: zeromus Date: Mon, 28 Nov 2016 21:44:32 -0600 Subject: [PATCH] cleanup fsnitro (clean portability hazards and use libretro-common parts) --- .../src/addons/slot1_retail_mcrom_debug.cpp | 17 +++-- desmume/src/utils/fsnitro.cpp | 70 +++++++------------ desmume/src/utils/fsnitro.h | 31 ++++---- 3 files changed, 47 insertions(+), 71 deletions(-) diff --git a/desmume/src/addons/slot1_retail_mcrom_debug.cpp b/desmume/src/addons/slot1_retail_mcrom_debug.cpp index 82a4c0666..f679e307c 100644 --- a/desmume/src/addons/slot1_retail_mcrom_debug.cpp +++ b/desmume/src/addons/slot1_retail_mcrom_debug.cpp @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2015 DeSmuME team + Copyright (C) 2013-2016 DeSmuME team This file is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,6 +15,11 @@ along with the this software. If not, see . */ +//This module implements a device which is capable of building a nitro FS on the fly +//(and REBUILDING it! -- Q: under what conditions?) +//so that you can test your homebrew games and patches without having to rebuild the ROM and restart it. +//Q. can it handle resized files? + #include "slot1comp_mc.h" #include "slot1comp_rom.h" #include "slot1comp_protocol.h" @@ -27,12 +32,6 @@ #include "../NDSSystem.h" #include "../utils/fsnitro.h" -//quick architecture overview: -//MCROM receives GC bus commands from MMU.cpp -//those are passed on to the protocol component for parsing -//protocol calls back into MCROM via ISlot1Comp_Protocol_Client interface for things the protocol doesnt know about (the contents of the rom, chiefly) -//MCROM utilizes the rom component for address logic and delivering data - class Slot1_Retail_DEBUG : public ISlot1Interface, public ISlot1Comp_Protocol_Client { private: @@ -41,7 +40,7 @@ private: FILE *fpROM; FS_NITRO *fs; u16 curr_file_id; - string pathData; + std::string pathData; public: @@ -131,7 +130,7 @@ public: { if (file_id != curr_file_id) { - string tmp = fs->getFullPathByFileID(file_id); + std::string tmp = fs->getFullPathByFileID(file_id); printf("%04X:[%08X, ofs %08X] %s\n", file_id, protocol.address, offset, tmp.c_str()); if (fpROM) diff --git a/desmume/src/utils/fsnitro.cpp b/desmume/src/utils/fsnitro.cpp index e46b34df6..7dd0dfd45 100644 --- a/desmume/src/utils/fsnitro.cpp +++ b/desmume/src/utils/fsnitro.cpp @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 DeSmuME team + Copyright (C) 2013-2016 DeSmuME team This file is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,30 +15,12 @@ along with the this software. If not, see . */ -#include "types.h" - #include #include -#ifdef HOST_WINDOWS -#include -#include -#define __mkdir(x) mkdir(x, 0777) -#else -#include -#include -#include -#include -#define __mkdir(x) mkdir(x, 0777) -#endif + +#include "types.h" #include "fsnitro.h" - -using namespace std; - -#ifdef HOST_WINDOWS -#define FS_DIRECTORY_DELIMITER_CHAR "\\" -#else -#define FS_DIRECTORY_DELIMITER_CHAR "/" -#endif +#include "file/file_path.h" FS_NITRO::FS_NITRO(u8 *cart_rom) { @@ -215,7 +197,7 @@ bool FS_NITRO::loadFileTables() if (!store) return false; memset(store, 0, sizeof(uintptr_t) * numDirs); - fnt[0].filename = FS_DIRECTORY_DELIMITER_CHAR; + fnt[0].filename = path_default_slash(); fnt[0].parentID = 0xF000; //printf("FNT F000: Sub:%08Xh, 1st ID:%04xh, parentID:%04Xh <%s>\n", fnt[0].offset, fnt[0].firstID, fnt[0].parentID, fnt[0].filename); @@ -278,7 +260,7 @@ bool FS_NITRO::loadFileTables() } // ======================= tools -bool FS_NITRO::rebuildFAT(u32 addr, u32 size, string pathData) +bool FS_NITRO::rebuildFAT(u32 addr, u32 size, std::string pathData) { if (!inited) return false; if (size == 0) return false; @@ -294,7 +276,7 @@ bool FS_NITRO::rebuildFAT(u32 addr, u32 size, string pathData) for (u32 i = startID; i < endID; i++) { if (i >= numFiles) break; - string path = pathData + getFullPathByFileID(i); + std::string path = pathData + getFullPathByFileID(i); //printf("%04Xh - %s (%d)\n", i, path.c_str(), fat[i].size); fat[i].file = false; FILE *fp = fopen(path.c_str(), "rb"); @@ -317,7 +299,7 @@ bool FS_NITRO::rebuildFAT(u32 addr, u32 size, string pathData) return true; } -bool FS_NITRO::rebuildFAT(string pathData) +bool FS_NITRO::rebuildFAT(std::string pathData) { return rebuildFAT(FATOff, FATSize, pathData); } @@ -390,7 +372,7 @@ bool FS_NITRO::getFileIdByAddr(u32 addr, u16 &id, u32 &offset) return false; } -string FS_NITRO::getDirNameByID(u16 id) +std::string FS_NITRO::getDirNameByID(u16 id) { if (!inited) return ""; if ((id & 0xF000) != 0xF000) return "|file|"; @@ -408,7 +390,7 @@ u16 FS_NITRO::getDirParrentByID(u16 id) return fnt[id & 0x0FFF].parentID; } -string FS_NITRO::getFileNameByID(u16 id) +std::string FS_NITRO::getFileNameByID(u16 id) { if (!inited) return ""; if ((id & 0xF000) == 0xF000) return ""; @@ -426,27 +408,27 @@ u16 FS_NITRO::getFileParentById(u16 id) return fat[id].parentID; } -string FS_NITRO::getFullPathByFileID(u16 id, bool addRoot) +std::string FS_NITRO::getFullPathByFileID(u16 id, bool addRoot) { if (!inited) return ""; if (id > numFiles) return ""; - string res = ""; + std::string res = ""; if (!fat[id].isOverlay) { u32 parentID = (fat[id].parentID & 0x0FFF); while (parentID) { - res = fnt[parentID].filename + string(FS_DIRECTORY_DELIMITER_CHAR) + res; + res = fnt[parentID].filename + path_default_slash() + res; parentID = (fnt[parentID].parentID & 0x0FFF); } if (addRoot) - res = string(FS_DIRECTORY_DELIMITER_CHAR) + string("data") + string(FS_DIRECTORY_DELIMITER_CHAR) + res; + res = (std::string)path_default_slash() + "data" + path_default_slash() + res; } else { if (addRoot) - res = string(FS_DIRECTORY_DELIMITER_CHAR) + string("overlay") + string(FS_DIRECTORY_DELIMITER_CHAR); + res = (std::string)path_default_slash() + "overlay" + path_default_slash(); } res += fat[id].filename; @@ -477,7 +459,7 @@ u32 FS_NITRO::getEndAddrById(u16 id) return (fat[id].end); } -bool FS_NITRO::extract(u16 id, string to) +bool FS_NITRO::extract(u16 id, std::string to) { printf("Extract to %s\n", to.c_str()); @@ -492,7 +474,7 @@ bool FS_NITRO::extract(u16 id, string to) return false; } -bool FS_NITRO::extractFile(u16 id, string to) +bool FS_NITRO::extractFile(u16 id, std::string to) { if (!inited) return false; if (id > numFiles) return false; @@ -506,14 +488,14 @@ bool FS_NITRO::extractFile(u16 id, string to) return true; } -bool FS_NITRO::extractAll(string to, void (*callback)(u32 current, u32 num)) +bool FS_NITRO::extractAll(std::string to, void (*callback)(u32 current, u32 num)) { if (!inited) return false; - string dataDir = to + "data" + FS_DIRECTORY_DELIMITER_CHAR; - string overlayDir = to + "overlay" + FS_DIRECTORY_DELIMITER_CHAR; - __mkdir(dataDir.c_str()); - __mkdir(overlayDir.c_str()); + std::string dataDir = to + "data" + path_default_slash(); + std::string overlayDir = to + "overlay" + path_default_slash(); + path_mkdir(dataDir.c_str()); + path_mkdir(overlayDir.c_str()); char curr_dir[MAX_PATH] = {0}; getcwd(curr_dir, sizeof(curr_dir)); @@ -521,22 +503,22 @@ bool FS_NITRO::extractAll(string to, void (*callback)(u32 current, u32 num)) for (u32 i = 0; i < numDirs; i++) { - string tmp = fnt[i].filename; + std::string tmp = fnt[i].filename; u16 parent = (fnt[i].parentID) & 0x0FFF; while (parent) { - tmp = fnt[parent].filename + string(FS_DIRECTORY_DELIMITER_CHAR) + tmp; + tmp = fnt[parent].filename + path_default_slash() + tmp; parent = (fnt[parent].parentID) & 0x0FFF; } - __mkdir(tmp.c_str()); + path_mkdir(tmp.c_str()); } chdir(dataDir.c_str()); for (u32 i = 0; i < numFiles; i++) { if (fat[i].isOverlay) continue; - string fname = getFullPathByFileID(i, false); + std::string fname = getFullPathByFileID(i, false); extract(i, fname); if (callback) callback(i, numFiles); diff --git a/desmume/src/utils/fsnitro.h b/desmume/src/utils/fsnitro.h index 80c33f47a..519eb6ca3 100644 --- a/desmume/src/utils/fsnitro.h +++ b/desmume/src/utils/fsnitro.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 DeSmuME team + Copyright (C) 2013-2016 DeSmuME team This file is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,14 +17,9 @@ #ifndef _FS_NITRO_H_ #define _FS_NITRO_H_ -#include -#ifndef _MSC_VER -#include -#include -#endif -#include "../types.h" -using namespace std; +#include +#include "types.h" enum FNT_TYPES { @@ -56,7 +51,7 @@ struct FAT_NITRO bool file; u32 sizeFile; u16 parentID; - string filename; + std::string filename; }; struct FNT_MAIN @@ -77,7 +72,7 @@ struct FNT_NITRO u32 offset; u16 firstID; u16 parentID; - string filename; + std::string filename; }; struct OVR_NITRO @@ -132,7 +127,7 @@ private: FNT_TYPES getFNTType(u8 type); bool loadFileTables(); - bool extract(u16 id, string to); + bool extract(u16 id, std::string to); void destroy(); public: @@ -142,11 +137,11 @@ public: bool getFileIdByAddr(u32 addr, u16 &id); bool getFileIdByAddr(u32 addr, u16 &id, u32 &offset); - string getDirNameByID(u16 id); + std::string getDirNameByID(u16 id); u16 getDirParrentByID(u16 id); - string getFileNameByID(u16 id); - string getFullPathByFileID(u16 id, bool addRoot = true); + std::string getFileNameByID(u16 id); + std::string getFullPathByFileID(u16 id, bool addRoot = true); u16 getFileParentById(u16 id); u32 getFileSizeById(u16 id); @@ -158,12 +153,12 @@ public: bool isARM7(u32 addr) { return ((addr >= ARM7exeStart) && (addr < ARM7exeEnd)); } bool isFAT(u32 addr) { return ((addr >= FATOff) && (addr < FATEnd)); } - bool rebuildFAT(u32 addr, u32 size, string pathData); - bool rebuildFAT(string pathData); + bool rebuildFAT(u32 addr, u32 size, std::string pathData); + bool rebuildFAT(std::string pathData); u32 getFATRecord(u32 addr); - bool extractFile(u16 id, string to); - bool extractAll(string to, void (*callback)(u32 current, u32 num) = NULL); + bool extractFile(u16 id, std::string to); + bool extractAll(std::string to, void (*callback)(u32 current, u32 num) = NULL); u32 getNumDirs() { return numDirs; } u32 getNumFiles() { return numFiles; }