Skip to content

Commit

Permalink
xrCore: now build on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleivg committed May 24, 2018
1 parent 253aca9 commit 6772c65
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/xrCore/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ CVirtualFileRW::CVirtualFileRW(const char* cFileName)
::fstat(hSrcFile, &file_info);
Size = (int)file_info.st_size;
R_ASSERT3(Size, cFileName, xrDebug::ErrorToString(GetLastError()));
data = (char*)::mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, hSrcFile, 0);
data = (char*)::mmap(NULL, Size, PROT_READ | PROT_WRITE, MAP_SHARED, hSrcFile, 0);
#endif
#ifdef FS_DEBUG
register_file_mapping(data, Size, cFileName);
Expand Down Expand Up @@ -563,7 +563,7 @@ CVirtualFileReader::CVirtualFileReader(const char* cFileName)
::fstat(hSrcFile, &file_info);
Size = (int)file_info.st_size;
R_ASSERT3(Size, cFileName, xrDebug::ErrorToString(GetLastError()));
data = (char*)::mmap(NULL, 0, PROT_READ, MAP_SHARED, hSrcFile, 0);
data = (char*)::mmap(NULL, Size, PROT_READ, MAP_SHARED, hSrcFile, 0);
#endif
R_ASSERT3(data, cFileName, xrDebug::ErrorToString(GetLastError()));

Expand Down
3 changes: 1 addition & 2 deletions src/xrCore/FileSystem_borland.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//----------------------------------------------------
// file: FileSystem.cpp
//----------------------------------------------------
#if defined(WINDOWS)
#include "stdafx.h"
#pragma hdrstop

#include "FileSystem.h"

#ifdef WINDOWS
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
Expand Down
2 changes: 1 addition & 1 deletion src/xrCore/PostProcess/PPInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "StdAfx.h"
#include "stdafx.h"
#include "PPInfo.hpp"

SPPInfo& SPPInfo::add(const SPPInfo& ppi)
Expand Down
2 changes: 1 addition & 1 deletion src/xrCore/PostProcess/PostProcess.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "StdAfx.h"
#include "stdafx.h"
#include "PostProcess.hpp"

// postprocess value LOAD method implementation
Expand Down
14 changes: 14 additions & 0 deletions src/xrCore/Threading/Event.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "stdafx.h"
#include "Event.hpp"
#if defined(WINDOWS)
#include <windows.h>

Event::Event() noexcept { handle = (void*)CreateEvent(NULL, FALSE, FALSE, NULL); }
Event::~Event() noexcept { CloseHandle(handle); }
Expand All @@ -10,3 +12,15 @@ bool Event::Wait(u32 millisecondsTimeout) const noexcept
{
return WaitForSingleObject(handle, millisecondsTimeout) != WAIT_TIMEOUT;
}
#elif defined(LINUX)
Event::Event() noexcept { handle = (void*)malloc(1); }
Event::~Event() noexcept { free(handle); }
void Event::Reset() noexcept { memset(handle, 1, 1); }
void Event::Set() noexcept { memset(handle, 0, 1); }
void Event::Wait() const noexcept { Sleep(0); }
bool Event::Wait(u32 millisecondsTimeout) const noexcept
{
Sleep(millisecondsTimeout);
return true;
}
#endif
3 changes: 2 additions & 1 deletion src/xrCore/_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ using string4096 = char[4096];
using string_path = char[2 * max_path];

// XXX: Replace __interface with either struct or class. MS defines it as struct for COM, but this project is C++.
#if defined(WINDOWS)
#define xr_pure_interface __interface

#endif
#endif
2 changes: 1 addition & 1 deletion src/xrCore/stream_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void CStreamReader::map(const u32& new_offset)
(u8*)MapViewOfFile(m_file_mapping_handle, FILE_MAP_READ, 0, start_offset, m_current_window_size);
#elif defined(LINUX)
m_current_map_view_of_file =
(u8*)mmap(NULL, m_current_window_size, PROT_READ, MAP_SHARED, m_file_mapping_handle, start_offset); // TODO проверить не могу до полной сборки под Linux
(u8*)::mmap(NULL, m_current_window_size, PROT_READ, MAP_SHARED, m_file_mapping_handle, start_offset); // TODO проверить не могу до полной сборки под Linux
#endif
m_current_pointer = m_current_map_view_of_file;

Expand Down
5 changes: 4 additions & 1 deletion src/xrCore/stream_reader_inline.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifndef STREAM_READER_INLINE_H
#define STREAM_READER_INLINE_H
#ifdef LINUX
#include <sys/mman.h>
#endif

IC CStreamReader::CStreamReader() {}
IC CStreamReader::CStreamReader(const CStreamReader& object)
Expand All @@ -19,7 +22,7 @@ IC const HANDLE& CStreamReader::file_mapping_handle() const { return (m_file_map
#if defined(WINDOWS)
IC void CStreamReader::unmap() { UnmapViewOfFile(m_current_map_view_of_file); }
#else
IC void CStreamReader::unmap() { ; }
IC void CStreamReader::unmap() { ::munmap(const_cast<u8*>(m_current_map_view_of_file), m_current_window_size); }
#endif
IC void CStreamReader::remap(const u32& new_offset)
{
Expand Down
2 changes: 2 additions & 0 deletions src/xrCore/xrMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
Поэтому всё-же стоит переопределять для большинства случаев операторы new и delete.
А для остального мы будем полагать (и надеяться), что прокси справится без проблем.
*/
#if defined(WINDOWS) // Не знаю, как это на виндах работает, но на линуксе ломается линковка из-за множественного объявления __TBB_malloc_proxy_helper_object
#include "tbb/tbbmalloc_proxy.h"
#endif

class XRCORE_API xrMemory
{
Expand Down
201 changes: 201 additions & 0 deletions src/xrCore/xr_ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,207 @@
XRCORE_API CInifile const* pSettings = nullptr;
XRCORE_API CInifile const* pSettingsAuth = nullptr;

#if defined(LINUX)
#include <stdint.h>
#define MSVCRT_EINVAL 22
#define MSVCRT_ERANGE 34

#define MSVCRT_UI64_MAX (((uint64_t)0xffffffff << 32) | 0xffffffff)

/**
* from wine@dlls/msvcrt/string.c
*
* @param fileName
* @param readOnly
* @return
*/
int _cdecl _i64toa_s(int64_t value, char *str, size_t size, int radix){
uint64_t val;
unsigned int digit;
int is_negative;
char buffer[65], *pos;
size_t len;

if (!(str != NULL))
return MSVCRT_EINVAL;
if (!(size > 0))
return MSVCRT_EINVAL;
if (!(radix >= 2 && radix <= 36)) {
str[0] = '\0';
return MSVCRT_EINVAL;
}

if (value < 0 && radix == 10) {
is_negative = 1;
val = -value;
} else {
is_negative = 0;
val = value;
}

pos = buffer + 64;
*pos = '\0';

do {
digit = val % radix;
val /= radix;

if (digit < 10)
*--pos = '0' + digit;
else
*--pos = 'a' + digit - 10;
} while (val != 0);

if (is_negative)
*--pos = '-';

len = buffer + 65 - pos;
if (len > size) {
size_t i;
char *p = str;

/* Copy the temporary buffer backwards up to the available number of
* characters. Don't copy the negative sign if present. */

if (is_negative) {
p++;
size--;
}

for (pos = buffer + 63, i = 0; i < size; i++)
*p++ = *pos--;

str[0] = '\0';
return MSVCRT_ERANGE;
}

memcpy(str, pos, len);
return 0;
}

int _cdecl _ui64toa_s(uint64_t value, char *str, size_t size, int radix) {
char buffer[65], *pos;
int digit;

if (!(str != NULL))
return MSVCRT_EINVAL;
if (!(size > 0))
return MSVCRT_EINVAL;
if (!(radix >= 2 && radix <= 36)) {
str[0] = '\0';
return MSVCRT_EINVAL;
}

pos = buffer + 64;
*pos = '\0';

do {
digit = value % radix;
value /= radix;

if (digit < 10)
*--pos = '0' + digit;
else
*--pos = 'a' + digit - 10;
} while (value != 0);

if (buffer - pos + 65 > size) {
return MSVCRT_EINVAL;
}

memcpy(str, pos, buffer - pos + 65);
return 0;
}

LARGE_INTEGER _cdecl _atoi64( const char *str )
{
ULARGE_INTEGER RunningTotal = 0;
char bMinus = 0;

while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
str++;
} /* while */

if (*str == '+') {
str++;
} else if (*str == '-') {
bMinus = 1;
str++;
} /* if */

while (*str >= '0' && *str <= '9') {
RunningTotal = RunningTotal * 10 + *str - '0';
str++;
} /* while */

return bMinus ? -RunningTotal : RunningTotal;
}

uint64_t _cdecl _strtoui64_l(const char *nptr, char **endptr, int base, locale_t locale)
{
BOOL negative = FALSE;
uint64_t ret = 0;


if (!(nptr != NULL)) return 0;
if (!(base == 0 || base >= 2)) return 0;
if (!(base <= 36)) return 0;

while(isspace(*nptr)) nptr++;

if(*nptr == '-') {
negative = TRUE;
nptr++;
} else if(*nptr == '+')
nptr++;

if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
base = 16;
nptr += 2;
}

if(base == 0) {
if(*nptr=='0')
base = 8;
else
base = 10;
}

while(*nptr) {
char cur = tolower(*nptr);
int v;

if(isdigit(cur)) {
if(cur >= '0'+base)
break;
v = *nptr-'0';
} else {
if(cur<'a' || cur>='a'+base-10)
break;
v = cur-'a'+10;
}

nptr++;

if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
ret = MSVCRT_UI64_MAX;
} else
ret = ret*base + v;
}

if(endptr)
*endptr = (char*)nptr;

return negative ? -ret : ret;
}

uint64_t _cdecl _strtoui64(const char *nptr, char **endptr, int base)
{
return _strtoui64_l(nptr, endptr, base, NULL);
}
#endif


CInifile* CInifile::Create(pcstr fileName, bool readOnly)
{
return new CInifile(fileName, readOnly);
Expand Down

0 comments on commit 6772c65

Please sign in to comment.