Skip to content

Commit f3ff919

Browse files
authored
Code clean up + replace some wstring instances with utf8 (#640)
1 parent ca79a6a commit f3ff919

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+163
-641
lines changed

src/Cafe/Account/Account.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "Account.h"
22
#include "util/helpers/helpers.h"
3-
#include "gui/CemuApp.h"
43
#include "util/helpers/SystemException.h"
5-
64
#include "config/ActiveSettings.h"
75
#include "Cafe/IOSU/legacy/iosu_crypto.h"
86
#include "Common/FileStream.h"
@@ -175,7 +173,7 @@ std::error_code Account::Load()
175173

176174
std::error_code Account::Save()
177175
{
178-
fs::path path = CemuApp::GetMLCPath(fmt::format(L"usr/save/system/act/{:08x}", m_persistent_id)).ToStdWstring();
176+
fs::path path = ActiveSettings::GetMlcPath(fmt::format(L"usr/save/system/act/{:08x}", m_persistent_id));
179177
if (!fs::exists(path))
180178
{
181179
std::error_code ec;
@@ -184,7 +182,7 @@ std::error_code Account::Save()
184182
return ec;
185183
}
186184

187-
path /= L"account.dat";
185+
path /= "account.dat";
188186

189187
try
190188
{
@@ -302,7 +300,7 @@ void Account::SetMiiName(std::wstring_view name)
302300
const std::vector<Account>& Account::RefreshAccounts()
303301
{
304302
std::vector<Account> result;
305-
const fs::path path = CemuApp::GetMLCPath(L"usr/save/system/act").ToStdWstring();
303+
const fs::path path = ActiveSettings::GetMlcPath("usr/save/system/act");
306304
if (fs::exists(path))
307305
{
308306
for (const auto& it : fs::directory_iterator(path))
@@ -417,7 +415,7 @@ fs::path Account::GetFileName(uint32 persistent_id)
417415
if (persistent_id < kMinPersistendId)
418416
throw std::invalid_argument(fmt::format("persistent id {:#x} is invalid", persistent_id));
419417

420-
return CemuApp::GetMLCPath(fmt::format(L"usr/save/system/act/{:08x}/account.dat", persistent_id)).ToStdWstring();
418+
return ActiveSettings::GetMlcPath(fmt::format("usr/save/system/act/{:08x}/account.dat", persistent_id));
421419
}
422420

423421
OnlineValidator Account::ValidateOnlineFiles() const

src/Cafe/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,6 @@ add_library(CemuCafe
454454
TitleList/BaseInfo.cpp
455455
TitleList/BaseInfo.h
456456
TitleList/GameInfo.h
457-
TitleList/MetaInfo.cpp
458-
TitleList/MetaInfo.h
459457
TitleList/ParsedMetaXml.h
460458
TitleList/SaveInfo.cpp
461459
TitleList/SaveInfo.h

src/Cafe/CafeSystem.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ uint32 loadSharedData()
337337
// advance write offset and pad to 16 byte alignment
338338
dataWritePtr += ((fileSize + 15) & ~15);
339339
}
340-
forceLog_printfW(L"COS: System fonts found. Generated shareddata (%dKB)", (uint32)(dataWritePtr - (uint8*)shareddataTable) / 1024);
340+
cemuLog_log(LogType::Force, "COS: System fonts found. Generated shareddata ({}KB)", (uint32)(dataWritePtr - (uint8*)shareddataTable) / 1024);
341341
return memory_getVirtualOffsetFromPointer(dataWritePtr);
342342
}
343343
// alternative method: load RAM dump

src/Cafe/GamePatch.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void hle_scan(uint8* data, sint32 dataLength, char* hleFunctionName)
139139
uint32 offset = (uint32)(scanCurrent - scanStart) + 0x01000000;
140140
debug_printf("HLE signature for '%s' found at 0x%08x\n", hleFunctionName, offset);
141141
uint32 opcode = (1<<26)|(functionIndex+0x1000); // opcode for HLE: 0x1000 + FunctionIndex
142-
memory_writeU32Direct(offset, opcode);
142+
memory_write<uint32>(offset, opcode);
143143
break;
144144
}
145145
scanCurrent += 4;
@@ -330,7 +330,7 @@ void GamePatch_scan()
330330
#endif
331331
sint32 functionIndex = hleIndex_h000000001;
332332
uint32 opcode = (1 << 26) | (functionIndex); // opcode for HLE: 0x1000 + FunctionIndex
333-
memory_writeU32Direct(hleAddr - 4, opcode);
333+
memory_write<uint32>(hleAddr - 4, opcode);
334334
}
335335
hleIndex_h000000002 = osLib_getFunctionIndex("hle", "h000000002");
336336
hleAddr = hle_locate(botw_busyLoopSignature2, botw_busyLoopMask2, sizeof(botw_busyLoopSignature2));
@@ -341,7 +341,7 @@ void GamePatch_scan()
341341
#endif
342342
sint32 functionIndex = hleIndex_h000000002;
343343
uint32 opcode = (1 << 26) | (functionIndex); // opcode for HLE: 0x1000 + FunctionIndex
344-
memory_writeU32Direct(hleAddr - 4, opcode);
344+
memory_write<uint32>(hleAddr - 4, opcode);
345345
}
346346

347347
// FFL library float array endian conversion
@@ -353,7 +353,7 @@ void GamePatch_scan()
353353
forceLogDebug_printf("HLE: Hook FFL float array endian swap function at 0x%08x", hleAddr);
354354
sint32 functionIndex = hleIndex_h000000003;
355355
uint32 opcode = (1 << 26) | (functionIndex); // opcode for HLE: 0x1000 + FunctionIndex
356-
memory_writeU32Direct(hleAddr, opcode);
356+
memory_write<uint32>(hleAddr, opcode);
357357
}
358358

359359
// XCX freeze workaround

src/Cafe/GraphicPack/GraphicPack2.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,14 @@ GraphicPack2::GraphicPack2(std::wstring filename, IniParser& rules)
500500
}
501501
}
502502

503+
// returns true if enabling, disabling (changeEnableState) or changing presets (changePreset) for the graphic pack requires restarting if the game is already running
504+
bool GraphicPack2::RequiresRestart(bool changeEnableState, bool changePreset)
505+
{
506+
if (!GetTextureRules().empty())
507+
return true;
508+
return false;
509+
}
510+
503511
bool GraphicPack2::Reload()
504512
{
505513
Deactivate();

src/Cafe/GraphicPack/GraphicPack2.h

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class GraphicPack2
105105
sint32 GetVersion() const { return m_version; }
106106
const std::wstring& GetFilename() const { return m_filename; }
107107
const fs::path GetFilename2() const { return fs::path(m_filename); }
108+
bool RequiresRestart(bool changeEnableState, bool changePreset);
108109
bool Reload();
109110

110111
bool HasName() const { return !m_name.empty(); }
@@ -172,6 +173,7 @@ class GraphicPack2
172173

173174
static void ActivateForCurrentTitle();
174175
static void Reset();
176+
175177
private:
176178
bool Activate();
177179
bool Deactivate();

src/Cafe/GraphicPack/GraphicPack2Patches.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ bool GraphicPack2::LoadCemuPatches()
102102
// load Cemu style patch file
103103
if (!ParseCemuPatchesTxtInternal(patchesStream))
104104
{
105-
forceLog_printfW(L"Error while processing \"%s\". No patches for this graphic pack will be applied.", path.c_str());
105+
cemuLog_log(LogType::Force, "Error while processing \"{}\". No patches for this graphic pack will be applied.", _pathToUtf8(path));
106106
cemu_assert_debug(list_patchGroups.empty());
107107
return true; // return true since a .asm patch was found even if we could not parse it
108108
}
109109
}
110110
else
111111
{
112-
forceLog_printfW(L"Unable to load patch file \"%s\"", path.c_str());
112+
cemuLog_log(LogType::Force, "Unable to load patch file \"{}\"", _pathToUtf8(path));
113113
}
114114
foundPatches = true;
115115
}

src/Cafe/HW/Latte/Core/LatteCommandProcessor.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -559,19 +559,19 @@ LatteCMDPtr LatteCP_itLoadReg(LatteCMDPtr cmd, uint32 nWords, uint32 regBase)
559559
MPTR physAddressRegArea = LatteReadCMD();
560560
uint32 waitForIdle = LatteReadCMD();
561561
uint32 loadEntries = (nWords - 2) / 2;
562-
uint32 regIndex = 0;
562+
uint32 regShadowMemAddr = physAddressRegArea;
563563
for (uint32 i = 0; i < loadEntries; i++)
564564
{
565565
uint32 regOffset = LatteReadCMD();
566566
uint32 regCount = LatteReadCMD();
567567
cemu_assert_debug(regCount != 0);
568+
uint32 regAddr = regBase + regOffset;
568569
for (uint32 f = 0; f < regCount; f++)
569570
{
570-
uint32 regAddr = regBase + regOffset + f;
571-
uint32 regShadowMemAddr = physAddressRegArea + regIndex * 4;
572571
LatteGPUState.contextRegisterShadowAddr[regAddr] = regShadowMemAddr;
573-
LatteGPUState.contextRegister[regAddr] = memory_readU32Direct(regShadowMemAddr);
574-
regIndex++;
572+
LatteGPUState.contextRegister[regAddr] = memory_read<uint32>(regShadowMemAddr);
573+
regAddr++;
574+
regShadowMemAddr += 4;
575575
}
576576
}
577577
return cmd;
@@ -716,7 +716,7 @@ LatteCMDPtr LatteCP_itHLESampleTimer(LatteCMDPtr cmd, uint32 nWords)
716716
{
717717
cemu_assert_debug(nWords == 1);
718718
MPTR timerMPTR = (MPTR)LatteReadCMD();
719-
memory_writeU64Slow(timerMPTR, coreinit::coreinit_getTimerTick());
719+
memory_writeU64(timerMPTR, coreinit::coreinit_getTimerTick());
720720
return cmd;
721721
}
722722

src/Cafe/HW/Latte/Core/LatteShaderCache.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void LatteShaderCache_load()
219219
if(!fc_shaderCacheGeneric)
220220
{
221221
// no shader cache available yet
222-
forceLog_printfW(L"Unable to open or create shader cache file \"%s\"", pathGeneric.c_str());
222+
cemuLog_log(LogType::Force, "Unable to open or create shader cache file \"{}\"", _pathToUtf8(pathGeneric));
223223
LatteShaderCache_finish();
224224
return;
225225
}

src/Cafe/HW/Latte/Core/LatteTextureLoader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ void LatteTextureLoader_UpdateTextureSliceData(LatteTexture* tex, sint32 texture
695695
if (textureLoader.dump)
696696
{
697697
wchar_t path[1024];
698-
swprintf(path, 1024, L"dump\\textures\\%08x_fmt%04x_slice%d_mip%02d_%dx%d_tm%02d.tga", physImagePtr, (uint32)tex->format, sliceIndex, mipIndex, tex->width, tex->height, tileMode);
698+
swprintf(path, 1024, L"dump/textures/%08x_fmt%04x_slice%d_mip%02d_%dx%d_tm%02d.tga", physImagePtr, (uint32)tex->format, sliceIndex, mipIndex, tex->width, tex->height, tileMode);
699699
tga_write_rgba(path, textureLoader.width, textureLoader.height, textureLoader.dumpRGBA);
700700
free(textureLoader.dumpRGBA);
701701
}

src/Cafe/HW/MMU/MMU.cpp

-17
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,6 @@ uint8* memory_getPointerFromVirtualOffsetAllowNull(uint32 virtualOffset)
293293
return memory_getPointerFromVirtualOffset(virtualOffset);
294294
}
295295

296-
void memory_writeU32Direct(uint32 address, uint32 v)
297-
{
298-
*(uint32*)(memory_getPointerFromVirtualOffset(address)) = CPU_swapEndianU32(v);
299-
}
300-
301296
// write access
302297
void memory_writeDouble(uint32 address, double vf)
303298
{
@@ -320,12 +315,6 @@ void memory_writeU32(uint32 address, uint32 v)
320315
*(uint32*)(memory_getPointerFromVirtualOffset(address)) = CPU_swapEndianU32(v);
321316
}
322317

323-
void memory_writeU64Slow(uint32 address, uint64 v)
324-
{
325-
memory_writeU32(address+0, (v>>32)&0xFFFFFFFF);
326-
memory_writeU32(address+4, (v)&0xFFFFFFFF);
327-
}
328-
329318
void memory_writeU64(uint32 address, uint64 v)
330319
{
331320
*(uint64*)(memory_getPointerFromVirtualOffset(address)) = CPU_swapEndianU64(v);
@@ -372,12 +361,6 @@ uint32 memory_readU32(uint32 address)
372361
return CPU_swapEndianU32(v);
373362
}
374363

375-
uint32 memory_readU32Direct(uint32 address)
376-
{
377-
uint32 v = *(uint32*)(memory_getPointerFromVirtualOffset(address));
378-
return CPU_swapEndianU32(v);
379-
}
380-
381364
uint16 memory_readU16(uint32 address)
382365
{
383366
uint16 v = *(uint16*)(memory_getPointerFromVirtualOffset(address));

src/Cafe/HW/MMU/MMU.h

+12-38
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ uint8* memory_getPointerFromPhysicalOffset(uint32 physicalOffset);
1515
uint32 memory_virtualToPhysical(uint32 virtualOffset);
1616
uint32 memory_physicalToVirtual(uint32 physicalOffset);
1717

18-
extern uint8* memory_base; // points to 0x00000000
18+
extern uint8* memory_base; // points to base of PowerPC address space
1919

2020
enum class MMU_MEM_AREA_ID
2121
{
@@ -171,33 +171,26 @@ bool memory_isAddressRangeAccessible(MPTR virtualAddress, uint32 size);
171171
#define MEMORY_SHAREDDATA_AREA_ADDR (0xF8000000)
172172
#define MEMORY_SHAREDDATA_AREA_SIZE (0x02000000) // 32MB
173173

174-
static uint16 CPU_swapEndianU16(uint16 v)
175-
{
176-
return (v>>8)|(v<<8);
177-
}
178-
179174
#if BOOST_OS_WINDOWS
180175
#define CPU_swapEndianU64(_v) _byteswap_uint64((uint64)(_v))
181176
#define CPU_swapEndianU32(_v) _byteswap_ulong((uint32)(_v))
177+
#define CPU_swapEndianU16(_v) _byteswap_ushort((uint16)(_v))
182178
#elif BOOST_OS_LINUX
183179
#define CPU_swapEndianU64(_v) bswap_64((uint64)(_v))
184180
#define CPU_swapEndianU32(_v) bswap_32((uint32)(_v))
181+
#define CPU_swapEndianU16(_v) bswap_16((uint16)(_v))
185182
#elif BOOST_OS_MACOS
186183
#define CPU_swapEndianU64(_v) OSSwapInt64((uint64)(_v))
187184
#define CPU_swapEndianU32(_v) OSSwapInt32((uint32)(_v))
185+
#define CPU_swapEndianU16(_v) OSSwapInt16((uint16)(_v))
188186
#endif
189187

190-
// direct memory access (no hardware interface access)
191-
void memory_writeU32Direct(uint32 address, uint32 v);
192-
uint32 memory_readU32Direct(uint32 address);
193-
194-
// memory access (includes hardware interface, slower)
188+
// C-style memory access, deprecated. Use memory_read<> and memory_write<> templates instead
195189
void memory_writeDouble(uint32 address, double vf);
196190
void memory_writeFloat(uint32 address, float vf);
197191
void memory_writeU32(uint32 address, uint32 v);
198192
void memory_writeU16(uint32 address, uint16 v);
199193
void memory_writeU8(uint32 address, uint8 v);
200-
void memory_writeU64Slow(uint32 address, uint64 v);
201194
void memory_writeU64(uint32 address, uint64 v);
202195

203196
double memory_readDouble(uint32 address);
@@ -210,43 +203,24 @@ uint8 memory_readU8(uint32 address);
210203
void memory_createDump();
211204

212205
template<size_t count>
213-
void memory_readBytes(uint32 address, std::array<uint8, count>& buffer)
206+
void memory_readBytes(VAddr address, std::array<uint8, count>& buffer)
214207
{
215208
memcpy(buffer.data(), memory_getPointerFromVirtualOffset(address), count);
216209
}
217210

218-
template <typename T> T memory_read(uint32 address)
211+
template <typename T> inline T memory_read(VAddr address)
219212
{
220-
if constexpr(std::is_floating_point<T>::value)
221-
{
222-
if constexpr(sizeof(T) == sizeof(float))
223-
return memory_readFloat(address);
224-
else
225-
return memory_readDouble(address);
226-
}
227-
else if(std::is_integral<T>::value)
228-
{
229-
if constexpr (sizeof(T) == sizeof(uint8))
230-
return (T)memory_readU8(address);
231-
else if constexpr (sizeof(T) == sizeof(uint16))
232-
return (T)memory_readU16(address);
233-
else if constexpr (sizeof(T) == sizeof(uint32))
234-
return (T)memory_readU32(address);
235-
else if constexpr (sizeof(T) == sizeof(uint64))
236-
return (T)memory_readU64(address);
237-
}
213+
return *(betype<T>*)(memory_base + address);
214+
}
238215

239-
debugBreakpoint();
240-
return {};
216+
template <typename T> inline void memory_write(VAddr address, T value)
217+
{
218+
*(betype<T>*)(memory_base + address) = value;
241219
}
242220

243221
// LLE implementation
244222
void memory_initPhysicalLayout();
245223

246-
// updated code
247-
using EAddr = uint32; // effective address
248-
using PAddr = uint32; // physical address
249-
250224
namespace MMU
251225
{
252226
using MMIOFuncWrite32 = void (*)(PAddr addr, uint32 value);

src/Cafe/OS/RPL/rpl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ uint32 RPLLoader_MakePPCCallable(void(*ppcCallableExport)(PPCInterpreter_t* hCPU
734734
sint32 functionIndex = PPCInterpreter_registerHLECall(ppcCallableExport);
735735
MPTR codeAddr = memory_getVirtualOffsetFromPointer(RPLLoader_AllocateTrampolineCodeSpace(4));
736736
uint32 opcode = (1 << 26) | functionIndex;
737-
memory_writeU32Direct(codeAddr, opcode);
737+
memory_write<uint32>(codeAddr, opcode);
738738
g_map_callableExports[ppcCallableExport] = codeAddr;
739739
return codeAddr;
740740
}
@@ -772,7 +772,7 @@ uint32 rpl_mapHLEImport(RPLModule* rplLoaderContext, const char* rplName, const
772772
{
773773
MPTR codeAddr = memory_getVirtualOffsetFromPointer(RPLLoader_AllocateTrampolineCodeSpace(4));
774774
uint32 opcode = (1 << 26) | functionIndex;
775-
memory_writeU32Direct(codeAddr, opcode);
775+
memory_write<uint32>(codeAddr, opcode);
776776
// register mapped import
777777
mappedFunctionImport_t newImport;
778778
newImport.hash1 = mappedImportHash1;
@@ -792,8 +792,8 @@ uint32 rpl_mapHLEImport(RPLModule* rplLoaderContext, const char* rplName, const
792792
uint32 codeStart = memory_getVirtualOffsetFromPointer(RPLLoader_AllocateTrampolineCodeSpace(256));
793793
uint32 currentAddress = codeStart;
794794
uint32 opcode = (1 << 26) | (0xFFD0); // opcode for HLE: Unsupported import
795-
memory_writeU32Direct(codeStart + 0, opcode);
796-
memory_writeU32Direct(codeStart + 4, 0x4E800020);
795+
memory_write<uint32>(codeStart + 0, opcode);
796+
memory_write<uint32>(codeStart + 4, 0x4E800020);
797797
currentAddress += 8;
798798
// write name of lib/function
799799
sint32 libNameLength = std::min(128, (sint32)strlen(libName));

0 commit comments

Comments
 (0)