Skip to content

Commit

Permalink
Merge pull request #33 from RaidcoreGG/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
DeltaGW2 authored Apr 7, 2024
2 parents 3530360 + 6b547c6 commit 8af9288
Show file tree
Hide file tree
Showing 61 changed files with 2,172 additions and 689 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/msbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
submodules: 'recursive'

- name: Add MSBuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/Resources/Locales"]
path = src/Resources/Locales
url = https://github.com/RaidcoreGG/Nexus-Translations
121 changes: 67 additions & 54 deletions src/DataLink/DataLink.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
///----------------------------------------------------------------------------------------------------
/// Copyright (c) Raidcore.GG - All rights reserved.
///
/// Name : DataLink.cpp
/// Description : Provides functions to share data and functions.
/// Authors : K. Bieniek
///----------------------------------------------------------------------------------------------------

#include "DataLink.h"

#include "Consts.h"
#include "Shared.h"
#include "State.h"

namespace DataLink
{
void* ADDONAPI_ShareResource(const char* aIdentifier, size_t aResourceSize)
{
ShareResource(aIdentifier, aResourceSize);
}
}

namespace DataLink
{
std::mutex Mutex;
Expand All @@ -14,27 +30,26 @@ namespace DataLink
if (State::Nexus == ENexusState::SHUTTING_DOWN)
{
const std::lock_guard<std::mutex> lock(Mutex);

while (Registry.size() > 0)
{
while (Registry.size() > 0)
{
const auto& it = Registry.begin();
const auto& it = Registry.begin();

if (it->second.Pointer)
{
UnmapViewOfFile((LPVOID)it->second.Pointer);
it->second.Pointer = nullptr;
}
if (it->second.Pointer)
{
UnmapViewOfFile((LPVOID)it->second.Pointer);
it->second.Pointer = nullptr;
}

if (it->second.Handle)
{
CloseHandle(it->second.Handle);
it->second.Handle = nullptr;
}
if (it->second.Handle)
{
CloseHandle(it->second.Handle);
it->second.Handle = nullptr;
}

LogInfo(CH_DATALINK, "Freed shared resource: \"%s\"", it->first.c_str());
LogInfo(CH_DATALINK, "Freed shared resource: \"%s\"", it->first.c_str());

Registry.erase(it);
}
Registry.erase(it);
}
}

Expand All @@ -47,12 +62,11 @@ namespace DataLink
void* result = nullptr;

const std::lock_guard<std::mutex> lock(Mutex);

const auto& it = Registry.find(str);
if (it != Registry.end())
{
const auto& it = Registry.find(str);
if (it != Registry.end())
{
result = it->second.Pointer;
}
result = it->second.Pointer;
}

return result;
Expand All @@ -70,50 +84,49 @@ namespace DataLink
void* result = nullptr;

const std::lock_guard<std::mutex> lock(Mutex);

/* resource already exists */
const auto& it = Registry.find(str);
if (it != Registry.end())
{
/* resource already exists */
const auto& it = Registry.find(str);
if (it != Registry.end())
if (it->second.Size == aResourceSize)
{
if (it->second.Size == aResourceSize)
{
return it->second.Pointer;
}
else
{
LogWarning(CH_DATALINK, "Resource with name \"%s\" already exists with size %u but size %u was requested.", str.c_str(), it->second.Size, aResourceSize);
return nullptr;
}
return it->second.Pointer;
}

/* allocate new resource */
LinkedResource resource{};
resource.Size = aResourceSize;

if (strOverride.empty())
else
{
strOverride = str + "_" + std::to_string(GetCurrentProcessId());
LogWarning(CH_DATALINK, "Resource with name \"%s\" already exists with size %u but size %u was requested.", str.c_str(), it->second.Size, aResourceSize);
return nullptr;
}
}

resource.UnderlyingName = strOverride;
/* allocate new resource */
LinkedResource resource{};
resource.Size = aResourceSize;

resource.Handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, strOverride.c_str());
if (!resource.Handle)
{
resource.Handle = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, static_cast<DWORD>(aResourceSize), strOverride.c_str());
}
if (strOverride.empty())
{
strOverride = str + "_" + std::to_string(GetCurrentProcessId());
}

if (resource.Handle)
{
resource.Pointer = MapViewOfFile(resource.Handle, FILE_MAP_ALL_ACCESS, 0, 0, static_cast<DWORD>(aResourceSize));
resource.UnderlyingName = strOverride;

Registry[str] = resource;
result = resource.Pointer;
}
resource.Handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, strOverride.c_str());
if (!resource.Handle)
{
resource.Handle = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, static_cast<DWORD>(aResourceSize), strOverride.c_str());
}

if (resource.Handle)
{
resource.Pointer = MapViewOfFile(resource.Handle, FILE_MAP_ALL_ACCESS, 0, 0, static_cast<DWORD>(aResourceSize));

LogInfo(CH_DATALINK, "Created shared resource: \"%s\" (with underlying name \"%s\")", str.c_str(), strOverride.c_str());
Registry[str] = resource;
result = resource.Pointer;
}

LogInfo(CH_DATALINK, "Created shared resource: \"%s\" (with underlying name \"%s\")", str.c_str(), strOverride.c_str());

return result;
}
}
}
46 changes: 42 additions & 4 deletions src/DataLink/DataLink.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
///----------------------------------------------------------------------------------------------------
/// Copyright (c) Raidcore.GG - All rights reserved.
///
/// Name : DataLink.h
/// Description : Provides functions to share data and functions.
/// Authors : K. Bieniek
///----------------------------------------------------------------------------------------------------

#ifndef DATALINK_H
#define DATALINK_H

Expand All @@ -7,19 +15,49 @@

#include "LinkedResource.h"

///----------------------------------------------------------------------------------------------------
/// DataLink Namespace
///----------------------------------------------------------------------------------------------------
namespace DataLink
{
///----------------------------------------------------------------------------------------------------
/// ADDONAPI_ShareResource:
/// Addon API wrapper function for ShareResource.
///----------------------------------------------------------------------------------------------------
void* ADDONAPI_ShareResource(const char* aIdentifier, size_t aResourceSize);
}
///----------------------------------------------------------------------------------------------------
/// DataLink Namespace
///----------------------------------------------------------------------------------------------------
namespace DataLink
{
extern std::mutex Mutex;
extern std::unordered_map<std::string, LinkedResource> Registry;

/* Frees all remaining resources */
///----------------------------------------------------------------------------------------------------
/// Free:
/// Frees all remaining resources.
///----------------------------------------------------------------------------------------------------
void Free();

/* Retrieves the resource with the given identifier */
///----------------------------------------------------------------------------------------------------
/// GetResource:
/// Retrieves the resource with the given identifier.
///----------------------------------------------------------------------------------------------------
void* GetResource(const char* aIdentifier);
/* Allocates new memory of the given size and returns a pointer to it and shares it via the given identifier */

///----------------------------------------------------------------------------------------------------
/// ShareResource:
/// Allocates memory of the given size, accessible via the provided identifier.
///----------------------------------------------------------------------------------------------------
void* ShareResource(const char* aIdentifier, size_t aResourceSize);

///----------------------------------------------------------------------------------------------------
/// ShareResource:
/// Allocates memory of the given size, accessible via the provided identifier,
/// but with a different internal/underlying name.
///----------------------------------------------------------------------------------------------------
void* ShareResource(const char* aIdentifier, size_t aResourceSize, const char* aUnderlyingName);
}

#endif
#endif
14 changes: 12 additions & 2 deletions src/DataLink/LinkedResource.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
///----------------------------------------------------------------------------------------------------
/// Copyright (c) Raidcore.GG - All rights reserved.
///
/// Name : LinkedResource.h
/// Description : Contains the Texture data struct definition.
/// Authors : K. Bieniek
///----------------------------------------------------------------------------------------------------

#ifndef LINKEDRESOURCE_H
#define LINKEDRESOURCE_H

#include <Windows.h>
#include <string>

/* A structure holding information about an allocated resource. */
///----------------------------------------------------------------------------------------------------
/// LinkedResource data struct
///----------------------------------------------------------------------------------------------------
struct LinkedResource
{
HANDLE Handle; /* The handle of the resource. */
Expand All @@ -13,4 +23,4 @@ struct LinkedResource
std::string UnderlyingName; /* The real name of the memory mapped file.*/
};

#endif
#endif
Loading

0 comments on commit 8af9288

Please sign in to comment.