Skip to content

Commit

Permalink
DEV9: Unicode support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastRar authored and refractionpcsx2 committed Jan 17, 2021
1 parent 5640ee8 commit b76abc3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
22 changes: 16 additions & 6 deletions pcsx2/DEV9/DEV9.cpp
Expand Up @@ -138,12 +138,15 @@ void __Log(int level, const char* fmt, ...)

void LogInit()
{
const std::string LogFile(s_strLogPath + "/dev9Log.txt");
if (logFile)
{
DEV9Log.WriteToFile = true;
DEV9Log.Open(LogFile);
}
const char* logName = "dev9Log.txt";

//GHC uses UTF8 on all platforms
ghc::filesystem::path path(GetLogFolder().ToUTF8().data());
path /= logName;
std::string strPath = path.u8string();

DEV9Log.WriteToFile = true;
DEV9Log.Open(strPath.c_str());
}

s32 DEV9init()
Expand Down Expand Up @@ -239,7 +242,14 @@ s32 DEV9open(void* pDsp)
{
DEV9_LOG("DEV9open\n");
LoadConf();
#ifdef _WIN32
//Convert to utf8
char mbHdd[sizeof(config.Hdd)] = {0};
WideCharToMultiByte(CP_UTF8, 0, config.Hdd, -1, mbHdd, sizeof(mbHdd) - 1, nullptr, nullptr);
DEV9_LOG("open r+: %s\n", mbHdd);
#else
DEV9_LOG("open r+: %s\n", config.Hdd);
#endif
config.HddSize = 8 * 1024;

#ifdef ENABLE_ATA
Expand Down
8 changes: 8 additions & 0 deletions pcsx2/DEV9/DEV9.h
Expand Up @@ -58,12 +58,20 @@ void rx_process(NetPacket* pk);
bool rx_fifo_can_rx();

#define ETH_DEF "eth0"
#ifdef _WIN32
#define HDD_DEF L"DEV9hdd.raw"
#else
#define HDD_DEF "DEV9hdd.raw"
#endif

typedef struct
{
char Eth[256];
#ifdef _WIN32
wchar_t Hdd[256];
#else
char Hdd[256];
#endif
int HddSize;

int hddEnable;
Expand Down
54 changes: 36 additions & 18 deletions pcsx2/DEV9/Win32/DEV9WinConfig.cpp
Expand Up @@ -17,42 +17,60 @@
#include "PrecompiledHeader.h"
#include <stdlib.h>

#include <fstream>

//#include <winsock2.h>
#include "..\DEV9.h"
#include "AppConfig.h"

BOOL WritePrivateProfileInt(LPCSTR lpAppName, LPCSTR lpKeyName, int intvar, LPCSTR lpFileName)
BOOL WritePrivateProfileInt(LPCWSTR lpAppName, LPCWSTR lpKeyName, int intvar, LPCWSTR lpFileName)
{
return WritePrivateProfileStringA(lpAppName, lpKeyName, std::to_string(intvar).c_str(), lpFileName);
return WritePrivateProfileString(lpAppName, lpKeyName, std::to_wstring(intvar).c_str(), lpFileName);
}
bool FileExists(std::string szPath)
bool FileExists(std::wstring szPath)
{
DWORD dwAttrib = GetFileAttributesA(szPath.c_str());
DWORD dwAttrib = GetFileAttributes(szPath.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

void SaveConf()
{
const std::string file(GetSettingsFolder().Combine(wxString("DEV9.cfg")).GetFullPath());
DeleteFileA(file.c_str());

WritePrivateProfileStringA("DEV9", "Eth", config.Eth, file.c_str());
WritePrivateProfileStringA("DEV9", "Hdd", config.Hdd, file.c_str());
WritePrivateProfileInt("DEV9", "HddSize", config.HddSize, file.c_str());
WritePrivateProfileInt("DEV9", "ethEnable", config.ethEnable, file.c_str());
WritePrivateProfileInt("DEV9", "hddEnable", config.hddEnable, file.c_str());
const std::wstring file(GetSettingsFolder().Combine(wxString("DEV9.cfg")).GetFullPath());
DeleteFile(file.c_str());

//Create file with UT16 BOM to allow PrivateProfile to save unicode data
int bom = 0xFEFF;
std::fstream nfile = std::fstream(file, std::ios::out | std::ios::binary);
nfile.write((char*)&bom, 2);
//Write header to avoid empty line
nfile.write((char*)L"[DEV9]", 14);
nfile.close();

wchar_t wEth[sizeof(config.Eth)] = {0};
mbstowcs(wEth, config.Eth, sizeof(config.Eth) - 1);
WritePrivateProfileString(L"DEV9", L"Eth", wEth, file.c_str());
WritePrivateProfileString(L"DEV9", L"Hdd", config.Hdd, file.c_str());

WritePrivateProfileInt(L"DEV9", L"HddSize", config.HddSize, file.c_str());
WritePrivateProfileInt(L"DEV9", L"ethEnable", config.ethEnable, file.c_str());
WritePrivateProfileInt(L"DEV9", L"hddEnable", config.hddEnable, file.c_str());
}

void LoadConf()
{
const std::string file(GetSettingsFolder().Combine(wxString("DEV9.cfg")).GetFullPath());
const std::wstring file(GetSettingsFolder().Combine(wxString("DEV9.cfg")).GetFullPath());
if (FileExists(file.c_str()) == false)
return;

GetPrivateProfileStringA("DEV9", "Eth", ETH_DEF, config.Eth, sizeof(config.Eth), file.c_str());
GetPrivateProfileStringA("DEV9", "Hdd", HDD_DEF, config.Hdd, sizeof(config.Hdd), file.c_str());
config.HddSize = GetPrivateProfileIntA("DEV9", "HddSize", config.HddSize, file.c_str());
config.ethEnable = GetPrivateProfileIntA("DEV9", "ethEnable", config.ethEnable, file.c_str());
config.hddEnable = GetPrivateProfileIntA("DEV9", "hddEnable", config.hddEnable, file.c_str());
wchar_t wEth[sizeof(config.Eth)] = {0};
mbstowcs(wEth, ETH_DEF, sizeof(config.Eth) - 1);
GetPrivateProfileString(L"DEV9", L"Eth", wEth, wEth, sizeof(config.Eth), file.c_str());
wcstombs(config.Eth, wEth, sizeof(config.Eth) - 1);

GetPrivateProfileString(L"DEV9", L"Hdd", HDD_DEF, config.Hdd, sizeof(config.Hdd), file.c_str());

config.HddSize = GetPrivateProfileInt(L"DEV9", L"HddSize", config.HddSize, file.c_str());
config.ethEnable = GetPrivateProfileInt(L"DEV9", L"ethEnable", config.ethEnable, file.c_str());
config.hddEnable = GetPrivateProfileInt(L"DEV9", L"hddEnable", config.hddEnable, file.c_str());
}
6 changes: 4 additions & 2 deletions pcsx2/DEV9/Win32/Win32.cpp
Expand Up @@ -18,6 +18,8 @@
//#include <windows.h>
//#include <windowsx.h>

#include <filesystem>

#include "..\Config.h"
#include "resource.h"
#include "..\DEV9.h"
Expand Down Expand Up @@ -73,7 +75,7 @@ void OnInitDialog(HWND hW)
}
}

SetWindowTextA(GetDlgItem(hW, IDC_HDDFILE), config.Hdd);
SetWindowText(GetDlgItem(hW, IDC_HDDFILE), config.Hdd);

Button_SetCheck(GetDlgItem(hW, IDC_ETHENABLED), config.ethEnable);
Button_SetCheck(GetDlgItem(hW, IDC_HDDENABLED), config.hddEnable);
Expand Down Expand Up @@ -106,7 +108,7 @@ void OnOk(HWND hW)
strcpy(config.Eth, ptr);
}

GetWindowTextA(GetDlgItem(hW, IDC_HDDFILE), config.Hdd, 256);
GetWindowText(GetDlgItem(hW, IDC_HDDFILE), config.Hdd, 256);

config.ethEnable = Button_GetCheck(GetDlgItem(hW, IDC_ETHENABLED));
config.hddEnable = Button_GetCheck(GetDlgItem(hW, IDC_HDDENABLED));
Expand Down

0 comments on commit b76abc3

Please sign in to comment.