Skip to content

sp update #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use Boost.Filesystem instead if IFileSystem
IFileSystem had some problems with not seeing all files on Linux.
  • Loading branch information
Ayuto committed Mar 10, 2018
commit 961058ce3b16cf2e4970b49f40e9be9276673fdb
19 changes: 9 additions & 10 deletions src/loader/loader_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
#include "interface.h"
#include "eiface.h"
#include "strtools.h"
#include "filesystem.h"
#ifdef _WIN32
# include <windows.h>
#endif

#include "../core/utilities/shared_utils.h"
#include <exception>

//---------------------------------------------------------------------------------
// Disable warnings.
Expand All @@ -53,7 +53,6 @@
// Interfaces.
//---------------------------------------------------------------------------------
ICvar* g_pCVar = NULL; // This is required for linux linking..
IFileSystem* filesystem = NULL;
IVEngineServer* engine = NULL;

//
Expand Down Expand Up @@ -175,13 +174,6 @@ bool CSourcePython::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn
return false;
}

filesystem = (IFileSystem*) interfaceFactory(FILESYSTEM_INTERFACE_VERSION, NULL);
if (!filesystem)
{
Msg(MSG_PREFIX "Unable to retrieve IFileSystem interface.\n");
return false;
}

// ------------------------------------------------------------------
// Build path to python engines directory.
// ------------------------------------------------------------------
Expand All @@ -197,7 +189,14 @@ bool CSourcePython::Load( CreateInterfaceFn interfaceFactory, CreateInterfaceFn

if (UpdateAvailable())
{
ApplyUpdateStage2();
try
{
ApplyUpdateStage2();
}
catch (const std::exception& e)
{
Msg(MSG_PREFIX "An error occured during update stage 2:\n%s\n", e.what());
}
}

// ------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions src/loader/loader_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
//---------------------------------------------------------------------------------
#include "definitions.h"

class IFileSystem;
extern IFileSystem* filesystem;


//---------------------------------------------------------------------------------
// Purpose: a sample 3rd party plugin class
Expand Down
165 changes: 42 additions & 123 deletions src/loader/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,169 +36,88 @@
#endif

// SDK
#include "filesystem.h"
#include "eiface.h"

// Source.Python
#include "updater.h"
#include "definitions.h"

// Boost
#include "boost/filesystem.hpp"
namespace bfs = boost::filesystem;


//---------------------------------------------------------------------------------
// Declarations
//---------------------------------------------------------------------------------
extern IFileSystem* filesystem;
extern IVEngineServer* engine;


//---------------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------------
void DeleteDirectory(const char* szDirectory)
bool UpdateAvailable()
{
char szFilePattern[MAX_PATH_LENGTH];
V_snprintf(szFilePattern, sizeof(szFilePattern), "%s/*", szDirectory);

FileFindHandle_t findHandle;
const char *pRelativeFileName = filesystem->FindFirst(szFilePattern, &findHandle);
while( pRelativeFileName )
{
// Skip . and ..
if (strcmp(pRelativeFileName, ".") == 0 || strcmp(pRelativeFileName, "..") == 0)
{
pRelativeFileName = filesystem->FindNext( findHandle );
continue;
}
DevMsg(1, MSG_PREFIX "Checking if update stage 2 can be applied... ");

// Construct an absolute path
char tmpFile[MAX_PATH_LENGTH];
V_snprintf(tmpFile, sizeof(tmpFile), "%s/%s", szDirectory, pRelativeFileName);

if( filesystem->FindIsDirectory( findHandle ) )
{
DeleteDirectory(tmpFile);
}
else
{
DevMsg(5, MSG_PREFIX "Deleting \"%s\"...\n", tmpFile);
filesystem->RemoveFile(tmpFile);
}
char szGameDir[MAX_PATH_LENGTH];
engine->GetGameDir(szGameDir, MAX_PATH_LENGTH);

pRelativeFileName = filesystem->FindNext( findHandle );
}
bfs::path updateDir = bfs::path(szGameDir) / SP_UPDATE_PATH;
bool result = bfs::is_directory(updateDir) && !bfs::is_empty(updateDir);

filesystem->FindClose(findHandle);

DevMsg(5, MSG_PREFIX "Deleting \"%s\"...\n", szDirectory);
RemoveEmptyDir(szDirectory);
DevMsg(1, "%s.\n", result ? "Yes" : "No");
return result;
}

bool UpdateAvailable()
static void DeleteDir(bfs::path dir)
{
char szGameDir[MAX_PATH_LENGTH];
engine->GetGameDir(szGameDir, MAX_PATH_LENGTH);
DevMsg(1, MSG_PREFIX "Deleting %s...\n", dir.string().c_str());
bfs::remove_all(dir);
}

char szFilePattern[MAX_PATH_LENGTH];
V_snprintf(szFilePattern, sizeof(szFilePattern), "%s/%s/*", szGameDir, SP_UPDATE_PATH);
static void MergeDirectories(const bfs::path& src, const bfs::path& dst)
{
if (bfs::is_directory(src)) {
bfs::create_directories(dst);

bool found = false;
FileFindHandle_t findHandle;
const char *pRelativeFileName = filesystem->FindFirst(szFilePattern, &findHandle);
while( pRelativeFileName )
{
// Skip . and ..
if (strcmp(pRelativeFileName, ".") == 0 || strcmp(pRelativeFileName, "..") == 0)
bfs::directory_iterator end_iter;
for (bfs::directory_iterator iter(src); iter != end_iter; ++iter)
{
pRelativeFileName = filesystem->FindNext( findHandle );
continue;
MergeDirectories(iter->path(), dst/iter->path().filename());
}

found = true;
break;
}
else if (bfs::is_regular_file(src))
{
DevMsg(5, MSG_PREFIX "Merging %s into %s...\n", src.string().c_str(), dst.string().c_str());
bfs::rename(src, dst);
}
else
{
Msg(MSG_PREFIX "%s is not a file or directory. Doing nothing...\n", dst.string().c_str());
}

filesystem->FindClose(findHandle);
return found;
}

void ApplyUpdateStage2()
{
Msg(MSG_PREFIX "Applying Source.Python update stage 2...\n");
Msg(MSG_PREFIX "Applying update stage 2...\n");

char szGameDir[MAX_PATH_LENGTH];
engine->GetGameDir(szGameDir, MAX_PATH_LENGTH);

// Delete SP package dir
char szSPPackageDir[MAX_PATH_LENGTH];
V_snprintf(szSPPackageDir, sizeof(szSPPackageDir), "%s/%s", szGameDir, SP_PACKAGE_PATH);

DevMsg(1, MSG_PREFIX "Deleting %s...\n", szSPPackageDir);
DeleteDirectory(szSPPackageDir);

// Delete SP data dir
char szSPDataDir[MAX_PATH_LENGTH];
V_snprintf(szSPDataDir, sizeof(szSPDataDir), "%s/%s", szGameDir, SP_DATA_PATH);

DevMsg(1, MSG_PREFIX "Deleting %s...\n", szSPDataDir);
DeleteDirectory(szSPPackageDir);
// Delete old directories
DeleteDir(bfs::path(szGameDir) / SP_PACKAGE_PATH);
DeleteDir(bfs::path(szGameDir) / SP_DATA_PATH);
DeleteDir(bfs::path(szGameDir) / SP_DOCS_PATH);
DeleteDir(bfs::path(szGameDir) / PYTHON3_PATH);

// Move files from update dir to real dir
char szUpdateDir[MAX_PATH_LENGTH];
V_snprintf(szUpdateDir, sizeof(szUpdateDir), "%s/%s", szGameDir, SP_UPDATE_PATH);

DevMsg(1, MSG_PREFIX "Merging \"%s\" into \"%s\"...\n", szUpdateDir, szGameDir);
MergeDirectories(szUpdateDir, szGameDir);
bfs::path updateDir = bfs::path(szGameDir) / SP_UPDATE_PATH;
DevMsg(1, MSG_PREFIX "Merging \"%s\" into \"%s\"...\n", updateDir.string().c_str(), szGameDir);
MergeDirectories(updateDir, szGameDir);

// Delete update dir, because it now contains a bunch of empty directories
DevMsg(1, MSG_PREFIX "Deleting %s...\n", szUpdateDir);
DeleteDirectory(szUpdateDir);
DeleteDir(updateDir);

Msg(MSG_PREFIX "Stage 2 has been applied.\n");
}

void MergeDirectories(const char* szSourceDir, const char* szDestDir)
{
char szFilePattern[MAX_PATH_LENGTH];
V_snprintf(szFilePattern, sizeof(szFilePattern), "%s/*", szSourceDir);

FileFindHandle_t findHandle;
const char *pRelativeFileName = filesystem->FindFirst(szFilePattern, &findHandle);
while( pRelativeFileName )
{
// Skip . and ..
if (strcmp(pRelativeFileName, ".") == 0 || strcmp(pRelativeFileName, "..") == 0)
{
pRelativeFileName = filesystem->FindNext( findHandle );
continue;
}

// Construct an absolute path
char tmpUpdateFile[MAX_PATH_LENGTH];
V_snprintf(tmpUpdateFile, sizeof(tmpUpdateFile), "%s/%s", szSourceDir, pRelativeFileName);

char tmpRealFile[MAX_PATH_LENGTH];
V_snprintf(tmpRealFile, sizeof(tmpRealFile), "%s/%s", szDestDir, pRelativeFileName);

if( filesystem->FindIsDirectory( findHandle ) )
{
MergeDirectories(tmpUpdateFile, tmpRealFile);
}
else
{
DevMsg(5, MSG_PREFIX "Moving \"%s\" to \"%s\"...\n", tmpUpdateFile, tmpRealFile);
if (filesystem->FileExists(tmpRealFile))
{
filesystem->RemoveFile(tmpRealFile);
}

if (!filesystem->RenameFile(tmpUpdateFile, tmpRealFile))
{
Msg(MSG_PREFIX "Failed to move file \"%s\" to \"%s\"!\n", tmpUpdateFile, tmpRealFile);
}
}

pRelativeFileName = filesystem->FindNext( findHandle );
}

filesystem->FindClose(findHandle);
}
8 changes: 2 additions & 6 deletions src/loader/updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,17 @@
#define SP_UPDATE_PATH "addons/source-python/update"
#define SP_PACKAGE_PATH "addons/source-python/packages/source-python"
#define SP_DATA_PATH "addons/source-python/data/source-python"
#define SP_DOCS_PATH "addons/source-python/docs/source-python"
#define PYTHON3_PATH "addons/source-python/Python3"


//---------------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------------
// Recursively delete a directory.
void DeleteDirectory(const char* szDirectory);

// Returns true if there is at least one file in the update directory
bool UpdateAvailable();

// Applies stage 2 of the update process
void ApplyUpdateStage2();

// Merge two directories
void MergeDirectories(const char* szSourceDir, const char* szDestDir);

#endif // _UPDATER_H
10 changes: 6 additions & 4 deletions src/makefiles/linux/linux.base.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ Include_Directories(
# ------------------------------------------------------------------
Set(SOURCEPYTHON_LINK_LIBRARIES
pthread dl util
${DYNCALLSDK_LIB}/libdyncall_s.a
${DYNCALLSDK_LIB}/libdyncallback_s.a
${DYNCALLSDK_LIB}/libdynload_s.a
${BOOSTSDK_LIB}/libboost_filesystem.a
${BOOSTSDK_LIB}/libboost_system.a
)


Expand Down Expand Up @@ -135,8 +134,11 @@ Set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_NDEBUG")
# ------------------------------------------------------------------
Set(SOURCEPYTHON_LINK_LIBRARIES_RELEASE
${PYTHONSDK_LIB}/libpython3.6m.a
${BOOSTSDK_LIB}/libboost_python.a
${PYTHONSDK_LIB}/libpython3.6m.so.1.0
${BOOSTSDK_LIB}/libboost_python.a
${DYNAMICHOOKSSDK_LIB}/libDynamicHooks.a
${ASMJITSDK_LIB}/libAsmJit.a
${DYNCALLSDK_LIB}/libdyncall_s.a
${DYNCALLSDK_LIB}/libdyncallback_s.a
${DYNCALLSDK_LIB}/libdynload_s.a
)
2 changes: 2 additions & 0 deletions src/makefiles/win32/win32.base.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Set(SOURCEPYTHON_LINK_LIBRARIES
${DYNCALLSDK_LIB}/libdynload_s.lib
${ASMJITSDK_LIB}/AsmJit.lib
${DYNAMICHOOKSSDK_LIB}/DynamicHooks.lib
${BOOSTSDK_LIB}/libboost_filesystem-vc100-mt-s-1_64.lib
${BOOSTSDK_LIB}/libboost_system-vc100-mt-s-1_64.lib
)

# CSGO Engine adds in interfaces.lib
Expand Down
Binary file not shown.
Binary file added src/thirdparty/boost/lib/libboost_filesystem.a
Binary file not shown.
Binary file not shown.
Binary file added src/thirdparty/boost/lib/libboost_system.a
Binary file not shown.