Skip to content

Commit

Permalink
Make FS use new allocator API
Browse files Browse the repository at this point in the history
  • Loading branch information
elasota committed Apr 29, 2021
1 parent a2d374f commit 7442b92
Show file tree
Hide file tree
Showing 8 changed files with 522 additions and 69 deletions.
2 changes: 2 additions & 0 deletions Aerofoil/Aerofoil.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
<ClInclude Include="..\GpCommon\GpDisplayDriverTickStatus.h" />
<ClInclude Include="..\GpCommon\GpFileCreationDisposition.h" />
<ClInclude Include="..\GpCommon\GpInputDriverProperties.h" />
<ClInclude Include="..\GpCommon\GpString.h" />
<ClInclude Include="..\GpCommon\GpVector.h" />
<ClInclude Include="..\GpCommon\IGpCursor.h" />
<ClInclude Include="..\GpCommon\IGpAudioChannelCallbacks.h" />
<ClInclude Include="..\GpCommon\IGpDisplayDriverSurface.h" />
Expand Down
6 changes: 6 additions & 0 deletions Aerofoil/Aerofoil.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
<ClInclude Include="GpBWCursor_Win32.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\GpCommon\GpVector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\GpCommon\GpString.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="ConvertedResources\Large128.ico">
Expand Down
152 changes: 99 additions & 53 deletions Aerofoil/GpFileSystem_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "IGpAllocator.h"
#include "IGpDirectoryCursor.h"

#include <string>

#include <Shlwapi.h>
#include <ShlObj.h>
#include <commdlg.h>
Expand Down Expand Up @@ -105,49 +105,70 @@ GpDirectoryCursor_Win32::~GpDirectoryCursor_Win32()
FindClose(m_handle);
}

GpFileSystem_Win32::GpFileSystem_Win32()
: m_alloc(GpAllocator_C::GetInstance())
GpFileSystem_Win32::GpFileSystem_Win32(IGpAllocator *alloc)
: m_alloc(alloc)
, m_prefsDir(alloc)
, m_scoresDir(alloc)
, m_packagedDir(alloc)
, m_housesDir(alloc)
, m_logsDir(alloc)
, m_userHousesDir(alloc)
, m_userSavesDir(alloc)
, m_resourcesDir(alloc)
{
// GP TODO: This shouldn't be static init since it allocates memory
m_executablePath[0] = 0;
}

void GpFileSystem_Win32::Destroy()
{
IGpAllocator *alloc = m_alloc;
this->~GpFileSystem_Win32();
alloc->Release(this);
}

bool GpFileSystem_Win32::Init()
{
PWSTR docsPath;
if (!FAILED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &docsPath)))
{
try
{
m_prefsDir = docsPath;
}
catch(...)
{
CoTaskMemFree(docsPath);
throw;
}
if (FAILED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &docsPath)))
return false;

m_prefsDir.append(L"\\" GP_APPLICATION_NAME_W);

m_userHousesDir = m_prefsDir + L"\\Houses";
m_userSavesDir = m_prefsDir + L"\\SavedGames";
m_scoresDir = m_prefsDir + L"\\Scores";
m_logsDir = m_prefsDir + L"\\Logs";
m_fontCacheDir = m_prefsDir + L"\\FontCache";

CreateDirectoryW(m_prefsDir.c_str(), nullptr);
CreateDirectoryW(m_scoresDir.c_str(), nullptr);
CreateDirectoryW(m_userHousesDir.c_str(), nullptr);
CreateDirectoryW(m_userSavesDir.c_str(), nullptr);
CreateDirectoryW(m_logsDir.c_str(), nullptr);
CreateDirectoryW(m_fontCacheDir.c_str(), nullptr);

m_prefsDir.append(L"\\");
m_scoresDir.append(L"\\");
m_userHousesDir.append(L"\\");
m_userSavesDir.append(L"\\");
m_logsDir.append(L"\\");
m_fontCacheDir.append(L"\\");
m_resourcesDir.append(L"\\");
if (!m_prefsDir.Set(docsPath))
{
CoTaskMemFree(docsPath);
return false;
}

CoTaskMemFree(docsPath);

if (!m_prefsDir.Append(L"\\" GP_APPLICATION_NAME_W))
return false;

if (!m_userHousesDir.Set(m_prefsDir) || !m_userHousesDir.Append(L"\\Houses"))
return false;

if (!m_userSavesDir.Set(m_prefsDir) || !m_userSavesDir.Append(L"\\SavedGames"))
return false;

if (!m_scoresDir.Set(m_prefsDir) || !m_scoresDir.Append(L"\\Scores"))
return false;

if (!m_logsDir.Set(m_prefsDir) || !m_logsDir.Append(L"\\Logs"))
return false;

CreateDirectoryW(m_prefsDir.Buffer(), nullptr);
CreateDirectoryW(m_scoresDir.Buffer(), nullptr);
CreateDirectoryW(m_userHousesDir.Buffer(), nullptr);
CreateDirectoryW(m_userSavesDir.Buffer(), nullptr);
CreateDirectoryW(m_logsDir.Buffer(), nullptr);

if (!m_prefsDir.Append(L"\\") ||
!m_scoresDir.Append(L"\\") ||
!m_userHousesDir.Append(L"\\") ||
!m_userSavesDir.Append(L"\\") ||
!m_logsDir.Append(L"\\") ||
!m_resourcesDir.Append(L"\\"))
return false;

DWORD modulePathSize = GetModuleFileNameW(nullptr, m_executablePath, MAX_PATH);
if (modulePathSize == MAX_PATH || modulePathSize == 0)
m_executablePath[0] = 0;
Expand Down Expand Up @@ -182,12 +203,19 @@ GpFileSystem_Win32::GpFileSystem_Win32()
currentPathLength--;
}

if (currentPathLength > 0)
{
m_packagedDir = std::wstring(m_executablePath) + L"Packaged\\";
m_housesDir = std::wstring(m_executablePath) + L"Packaged\\Houses\\";
m_resourcesDir = std::wstring(m_executablePath) + L"Resources\\";
}
if (currentPathLength == 0)
return false;

if (!m_packagedDir.Set(m_executablePath) || !m_packagedDir.Append(L"Packaged\\"))
return false;

if (!m_housesDir.Set(m_executablePath) || !m_housesDir.Append(L"Packaged\\Houses\\"))
return false;

if (!m_resourcesDir.Set(m_executablePath) || !m_resourcesDir.Append(L"Resources\\"))
return false;

return true;
}

bool GpFileSystem_Win32::FileExists(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path)
Expand Down Expand Up @@ -428,9 +456,27 @@ const wchar_t *GpFileSystem_Win32::GetBasePath() const
return m_executablePath;
}

GpFileSystem_Win32 *GpFileSystem_Win32::CreateInstance(IGpAllocator *alloc)
{
void *storage = alloc->Alloc(sizeof(GpFileSystem_Win32));
if (!storage)
return nullptr;

GpFileSystem_Win32 *fs = new (storage) GpFileSystem_Win32(alloc);
if (!fs->Init())
{
fs->Destroy();
return nullptr;
}

ms_instance = fs;

return fs;
}

GpFileSystem_Win32 *GpFileSystem_Win32::GetInstance()
{
return &ms_instance;
return ms_instance;
}

bool GpFileSystem_Win32::ResolvePath(PortabilityLayer::VirtualDirectory_t virtualDirectory, char const* const* paths, size_t numPaths, wchar_t *outPath)
Expand All @@ -440,28 +486,28 @@ bool GpFileSystem_Win32::ResolvePath(PortabilityLayer::VirtualDirectory_t virtua
switch (virtualDirectory)
{
case PortabilityLayer::VirtualDirectories::kApplicationData:
baseDir = m_packagedDir.c_str();
baseDir = m_packagedDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kGameData:
baseDir = m_housesDir.c_str();
baseDir = m_housesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kUserData:
baseDir = m_userHousesDir.c_str();
baseDir = m_userHousesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kUserSaves:
baseDir = m_userSavesDir.c_str();
baseDir = m_userSavesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kPrefs:
baseDir = m_prefsDir.c_str();
baseDir = m_prefsDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kFonts:
baseDir = m_resourcesDir.c_str();
baseDir = m_resourcesDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kHighScores:
baseDir = m_scoresDir.c_str();
baseDir = m_scoresDir.Buffer();
break;
case PortabilityLayer::VirtualDirectories::kLogs:
baseDir = m_logsDir.c_str();
baseDir = m_logsDir.Buffer();
break;
default:
return false;
Expand Down Expand Up @@ -507,4 +553,4 @@ bool GpFileSystem_Win32::ResolvePath(PortabilityLayer::VirtualDirectory_t virtua
return true;
}

GpFileSystem_Win32 GpFileSystem_Win32::ms_instance;
GpFileSystem_Win32 *GpFileSystem_Win32::ms_instance;
29 changes: 16 additions & 13 deletions Aerofoil/GpFileSystem_Win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

#include "GpCoreDefs.h"
#include "GpWindows.h"

#include <string>
#include "GpString.h"

class GpFileSystem_Win32 final : public IGpFileSystem
{
public:
GpFileSystem_Win32();
explicit GpFileSystem_Win32(IGpAllocator *alloc);

void Destroy();

bool FileExists(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path) override;
bool FileLocked(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool &exists) override;
Expand All @@ -25,23 +26,25 @@ class GpFileSystem_Win32 final : public IGpFileSystem

const wchar_t *GetBasePath() const;

static GpFileSystem_Win32 *CreateInstance(IGpAllocator *alloc);
static GpFileSystem_Win32 *GetInstance();

private:
bool Init();

bool ResolvePath(PortabilityLayer::VirtualDirectory_t virtualDirectory, char const* const* paths, size_t numPaths, wchar_t *outPath);

std::wstring m_prefsDir;
std::wstring m_scoresDir;
std::wstring m_packagedDir;
std::wstring m_housesDir;
std::wstring m_logsDir;
std::wstring m_userHousesDir;
std::wstring m_userSavesDir;
std::wstring m_resourcesDir;
std::wstring m_fontCacheDir;
GpWString m_prefsDir;
GpWString m_scoresDir;
GpWString m_packagedDir;
GpWString m_housesDir;
GpWString m_logsDir;
GpWString m_userHousesDir;
GpWString m_userSavesDir;
GpWString m_resourcesDir;
wchar_t m_executablePath[MAX_PATH];

IGpAllocator *m_alloc;

static GpFileSystem_Win32 ms_instance;
static GpFileSystem_Win32 *ms_instance;
};
10 changes: 9 additions & 1 deletion Aerofoil/GpMain_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,20 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
int nArgs;
LPWSTR *cmdLineArgs = CommandLineToArgvW(cmdLine, &nArgs);

IGpAllocator *alloc = GpAllocator_C::GetInstance();

// Init file system first since logging may depend on it
GpFileSystem_Win32 *fs = GpFileSystem_Win32::CreateInstance(alloc);
if (!fs)
return -1;

for (int i = 1; i < nArgs; i++)
{
if (!wcscmp(cmdLineArgs[i], L"-diagnostics"))
GpLogDriver_Win32::Init();
}

IGpLogDriver *logger = GpLogDriver_Win32::GetInstance();
IGpAllocator *alloc = GpAllocator_C::GetInstance();
IGpSystemServices *sysServices = GpSystemServices_Win32::GetInstance();

GpDriverCollection *drivers = GpAppInterface_Get()->PL_GetDriverCollection();
Expand Down Expand Up @@ -469,5 +475,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

LocalFree(cmdLineArgs);

fs->Destroy();

return returnCode;
}
12 changes: 10 additions & 2 deletions AerofoilSDL/GpMain_SDL_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ IGpInputDriver *GpDriver_CreateInputDriver_SDL2_Gamepad(const GpInputDriverPrope

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
IGpAllocator *alloc = GpAllocator_C::GetInstance();

GpFileSystem_Win32 *fs = GpFileSystem_Win32::CreateInstance(alloc);
if (!fs)
return -1;

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0)
return -1;

Expand All @@ -57,7 +63,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
IGpLogDriver *logger = GpLogDriver_Win32::GetInstance();
GpDriverCollection *drivers = GpAppInterface_Get()->PL_GetDriverCollection();

drivers->SetDriver<GpDriverIDs::kFileSystem>(GpFileSystem_Win32::GetInstance());
drivers->SetDriver<GpDriverIDs::kFileSystem>(fs);
drivers->SetDriver<GpDriverIDs::kSystemServices>(GpSystemServices_Win32::GetInstance());
drivers->SetDriver<GpDriverIDs::kLog>(GpLogDriver_Win32::GetInstance());
drivers->SetDriver<GpDriverIDs::kAlloc>(GpAllocator_C::GetInstance());
Expand Down Expand Up @@ -88,7 +94,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
g_gpGlobalConfig.m_osGlobals = &g_gpWindowsGlobals;
g_gpGlobalConfig.m_logger = logger;
g_gpGlobalConfig.m_systemServices = GpSystemServices_Win32::GetInstance();
g_gpGlobalConfig.m_allocator = GpAllocator_C::GetInstance();
g_gpGlobalConfig.m_allocator = alloc;

GpDisplayDriverFactory::RegisterDisplayDriverFactory(EGpDisplayDriverType_SDL_GL2, GpDriver_CreateDisplayDriver_SDL_GL2);
GpAudioDriverFactory::RegisterAudioDriverFactory(EGpAudioDriverType_SDL2, GpDriver_CreateAudioDriver_SDL);
Expand All @@ -105,5 +111,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

LocalFree(cmdLineArgs);

fs->Destroy();

return returnCode;
}
Loading

0 comments on commit 7442b92

Please sign in to comment.