LCEMP/Minecraft.Client/Platform_Libs/Dev/Storage/STO_DLC.cpp
2026-04-21 01:27:06 -04:00

347 lines
9 KiB
C++

/*
MIT License
Copyright (c) 2026 Patoke
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if !defined(__linux__)
#include "stdafx.h"
#endif
#include "STO_DLC.h"
#include "STO_Main.h"
#ifdef __linux__
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#endif
XCONTENT_DATA &CDLC::GetDLC(DWORD dw)
{
return m_vInstalledDLCs[dw];
}
CDLC::CDLC(void) : m_vInstalledDLCs(), m_szMountPath(), m_vDLCDriveMappings()
{
m_iHasNewInstalledDLCs = false;
dword0 = 0;
dwordC0 = 0;
m_iHasNewMountedDLCs = false; // @Patoke fix
ZeroMemory(m_szDLCProductCode, sizeof(m_szDLCProductCode));
ZeroMemory(m_szProductUpgradeKey, sizeof(m_szProductUpgradeKey));
}
C4JStorage::EDLCStatus CDLC::GetOffers(int iPad, int (*Func)(LPVOID, int, DWORD, int), LPVOID lpParam, DWORD dwOfferTypesBitmask)
{
return C4JStorage::EDLC_NoOffers;
}
void CDLC::ClearOffers()
{
;
}
C4JStorage::EDLCStatus CDLC::GetInstalledDLC(int iPad, int (*Func)(LPVOID, int, int), LPVOID lpParam)
{
if (m_iHasNewInstalledDLCs)
{
return C4JStorage::EDLC_Pending;
}
m_pInstalledDLCFunc = Func;
m_pInstalledDLCParam = lpParam;
m_iHasNewInstalledDLCs = true;
#ifdef __linux__
const char *dlcDir = NULL;
struct stat stDir;
if (stat("Windows64Media/DLC", &stDir) == 0 && S_ISDIR(stDir.st_mode))
{
dlcDir = "Windows64Media/DLC";
}
else if (stat("Windows64/DLC", &stDir) == 0 && S_ISDIR(stDir.st_mode))
{
dlcDir = "Windows64/DLC";
}
if (!dlcDir)
{
InternalStorageManager.DebugPrintf("No DLC directory, can't have any DLC installed\n");
return C4JStorage::EDLC_Error;
}
DIR *dir = opendir(dlcDir);
if (dir)
{
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_name[0] == '.')
continue;
char fullPath[512];
snprintf(fullPath, sizeof(fullPath), "%s/%s", dlcDir, entry->d_name);
struct stat stEntry;
if (stat(fullPath, &stEntry) == 0 && S_ISDIR(stEntry.st_mode))
{
XCONTENT_DATA data;
snprintf(data.szFileName, sizeof(data.szFileName), "%s/%s", dlcDir, entry->d_name);
swprintf(data.szDisplayName, XCONTENT_MAX_DISPLAYNAME_LENGTH, L"%hs", entry->d_name);
data.DeviceID = 0;
data.dwContentType = 0;
AddInstalled(&data);
}
}
closedir(dir);
}
#else
bool ret = false;
DWORD atts = GetFileAttributesA("Windows64Media/DLC");
if (atts == -1)
{
atts = GetFileAttributesA("Windows64/DLC");
ret = true;
}
bool validDir = atts != -1 && (atts & FILE_ATTRIBUTE_DIRECTORY);
if (!validDir)
{
InternalStorageManager.DebugPrintf("No DLC directory, can't have any DLC installed\n");
return C4JStorage::EDLC_Error;
}
_WIN32_FIND_DATAA hFind;
HANDLE hFindFile;
if (ret)
{
hFindFile = FindFirstFileA("Windows64/DLC/*", &hFind);
}
else
{
hFindFile = FindFirstFileA("Windows64Media/DLC/*", &hFind);
}
if (hFindFile != (HANDLE)-1LL)
{
do
{
atts = hFind.dwFileAttributes;
bool isArt = hFind.dwFileAttributes != -1 && (hFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
if (isArt && hFind.cFileName[0] != '.')
{
XCONTENT_DATA data;
if (ret)
{
sprintf(data.szFileName, "Windows64/DLC/%s", hFind.cFileName);
}
else
{
sprintf(data.szFileName, "Windows64Media/DLC/%s", hFind.cFileName);
}
swprintf(data.szDisplayName, XCONTENT_MAX_DISPLAYNAME_LENGTH, L"%hs", hFind.cFileName);
data.DeviceID = 0;
data.dwContentType = 0;
AddInstalled(&data);
}
} while (FindNextFileA(hFindFile, &hFind));
FindClose(hFindFile);
}
#endif
return C4JStorage::EDLC_Idle;
}
DWORD CDLC::MountInstalledDLC(int iPad, DWORD dwDLC, int (*Func)(LPVOID, int, DWORD, DWORD), LPVOID lpParam, LPCSTR szMountDrive)
{
this->m_pMountedDLCFunc = Func;
this->m_pMountedDLCParam = lpParam;
if (szMountDrive)
{
m_szMountPath = szMountDrive;
}
else
{
m_szMountPath = this->m_szPackageRoot;
}
this->m_uiCurrentMappedDLC = dwDLC;
char *dlcdirPath = m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName;
m_vDLCDriveMappings.push_back(DriveMapping(m_szMountPath, dlcdirPath));
dword94 = 0xFFFFFFFF;
m_iHasNewMountedDLCs = true;
return 997;
}
DWORD CDLC::UnmountInstalledDLC(LPCSTR szMountDrive)
{
LPCSTR szDrive = nullptr;
if (szMountDrive)
{
szDrive = szMountDrive;
}
else
{
szDrive = this->m_szPackageRoot;
}
for (int i = 0; i < this->m_vDLCDriveMappings.size(); i++)
{
if (m_vDLCDriveMappings[i].m_szDirectoryPath == szDrive)
{
m_vDLCDriveMappings.erase(m_vDLCDriveMappings.begin() + i);
return 0;
}
}
return 0;
}
void CDLC::GetMountedDLCFileList(const char *szMountDrive, std::vector<std::string> &fileList)
{
#ifdef __linux__
const char *basePath = m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName;
DIR *dir = opendir(basePath);
if (dir)
{
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_name[0] == '.')
continue;
char fullPath[256];
snprintf(fullPath, sizeof(fullPath), "%s/%s", basePath, entry->d_name);
struct stat st;
if (stat(fullPath, &st) == 0 && !S_ISDIR(st.st_mode))
{
fileList.push_back(fullPath);
}
}
closedir(dir);
}
#else
char *dlcdirPath = new char[256];
sprintf(dlcdirPath, "%s/*", m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName);
_WIN32_FIND_DATAA atts;
HANDLE hFind = FindFirstFileA(dlcdirPath, &atts);
if (hFind != (HANDLE)-1LL)
{
do
{
if (atts.dwFileAttributes == -1 || (atts.dwFileAttributes & 0x10) != 0x10)
{
char dir[256];
sprintf(dir, "%s/%s", m_vInstalledDLCs[m_uiCurrentMappedDLC].szFileName, atts.cFileName);
fileList.push_back(dir);
}
} while (FindNextFileA(hFind, &atts));
FindClose(hFind);
}
delete[] dlcdirPath;
#endif
}
std::string CDLC::GetMountedPath(std::string szMount)
{
for (int ch = 0; ch < szMount.size(); ++ch)
{
if (szMount[ch] == '/' || szMount[ch] == '\\')
{
return "";
}
if (szMount[ch] == ':')
{
std::string driveName = szMount.substr(0, ch);
for (int i = 0; i < m_vDLCDriveMappings.size(); ++i)
{
if (m_vDLCDriveMappings[i].m_szDirectoryPath == driveName)
{
std::string newPath = m_vDLCDriveMappings[i].m_szMountPath;
newPath.append(szMount.substr(ch + 1, -1));
return newPath;
}
}
break;
}
}
return "";
}
void CDLC::SetDLCProductCode(const char *szProductCode)
{
strcpy(m_szDLCProductCode, szProductCode);
}
void CDLC::SetProductUpgradeKey(const char *szProductCode)
{
strcpy(m_szProductUpgradeKey, szProductCode);
}
int CDLC::GetAvailableDLCCount(int iPad)
{
return 0;
}
void CDLC::SetPackageRoot(char *pszDLCRoot)
{
strcpy(this->m_szPackageRoot, pszDLCRoot);
}
void CDLC::Tick(void)
{
if (m_iHasNewInstalledDLCs)
{
m_iHasNewInstalledDLCs = false;
m_pInstalledDLCFunc(m_pInstalledDLCParam, m_vInstalledDLCs.size(), 0);
}
if (m_iHasNewMountedDLCs)
{
m_iHasNewMountedDLCs = false;
m_pMountedDLCFunc(m_pMountedDLCParam, 0, 0, dword94);
}
}
void CDLC::AddInstalled(XCONTENT_DATA *data)
{
m_vInstalledDLCs.push_back(*data);
}
DWORD CDLC::CancelOffers(void)
{
return 0;
}