2013-12-19 17:10:14 +00:00
|
|
|
#pragma once
|
|
|
|
#include "types.h"
|
2019-09-07 12:37:39 +00:00
|
|
|
#include <cstdlib>
|
2013-12-19 17:10:14 +00:00
|
|
|
#include <vector>
|
2019-09-07 12:37:39 +00:00
|
|
|
#include <cstring>
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifndef _WIN32
|
2013-12-19 17:10:14 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#else
|
2019-05-12 15:41:42 +00:00
|
|
|
#include <windows.h>
|
2013-12-19 17:10:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-08-25 16:38:36 +00:00
|
|
|
#ifdef __ANDROID__
|
2013-12-19 17:10:14 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#undef PAGE_MASK
|
|
|
|
#define PAGE_MASK (PAGE_SIZE-1)
|
|
|
|
#else
|
|
|
|
#define PAGE_SIZE 4096
|
|
|
|
#define PAGE_MASK (PAGE_SIZE-1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//Threads
|
|
|
|
|
2015-07-28 22:30:24 +00:00
|
|
|
#if !defined(HOST_NO_THREADS)
|
2013-12-19 17:10:14 +00:00
|
|
|
typedef void* ThreadEntryFP(void* param);
|
|
|
|
|
2019-04-27 12:08:43 +00:00
|
|
|
class cThread {
|
2013-12-19 17:10:14 +00:00
|
|
|
private:
|
2019-04-27 12:08:43 +00:00
|
|
|
ThreadEntryFP* entry;
|
2013-12-19 17:10:14 +00:00
|
|
|
void* param;
|
|
|
|
public :
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2019-04-27 12:08:43 +00:00
|
|
|
HANDLE hThread;
|
|
|
|
#else
|
|
|
|
pthread_t *hThread;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cThread(ThreadEntryFP* function, void* param)
|
|
|
|
:entry(function), param(param), hThread(NULL) {}
|
|
|
|
~cThread() { WaitToEnd(); }
|
2013-12-19 17:10:14 +00:00
|
|
|
void Start();
|
|
|
|
void WaitToEnd();
|
|
|
|
};
|
2015-07-28 22:30:24 +00:00
|
|
|
#endif
|
2019-04-27 12:08:43 +00:00
|
|
|
|
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
//Wait Events
|
|
|
|
typedef void* EVENTHANDLE;
|
|
|
|
class cResetEvent
|
|
|
|
{
|
|
|
|
private:
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2013-12-19 17:10:14 +00:00
|
|
|
EVENTHANDLE hEvent;
|
|
|
|
#else
|
|
|
|
pthread_mutex_t mutx;
|
|
|
|
pthread_cond_t cond;
|
2019-04-27 12:08:43 +00:00
|
|
|
bool state;
|
2013-12-19 17:10:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
public :
|
2019-04-27 12:08:43 +00:00
|
|
|
cResetEvent();
|
2013-12-19 17:10:14 +00:00
|
|
|
~cResetEvent();
|
|
|
|
void Set(); //Set state to signaled
|
|
|
|
void Reset(); //Set state to non signaled
|
2018-10-29 19:03:47 +00:00
|
|
|
bool Wait(u32 msec);//Wait for signal , then reset[if auto]. Returns false if timed out
|
2013-12-19 17:10:14 +00:00
|
|
|
void Wait(); //Wait for signal , then reset[if auto]
|
|
|
|
};
|
|
|
|
|
|
|
|
class cMutex
|
|
|
|
{
|
|
|
|
private:
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2013-12-19 17:10:14 +00:00
|
|
|
CRITICAL_SECTION cs;
|
|
|
|
#else
|
|
|
|
pthread_mutex_t mutx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public :
|
|
|
|
bool state;
|
|
|
|
cMutex()
|
|
|
|
{
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2013-12-19 17:10:14 +00:00
|
|
|
InitializeCriticalSection(&cs);
|
|
|
|
#else
|
2015-05-08 15:44:27 +00:00
|
|
|
pthread_mutex_init ( &mutx, NULL);
|
2013-12-19 17:10:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
~cMutex()
|
|
|
|
{
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2013-12-19 17:10:14 +00:00
|
|
|
DeleteCriticalSection(&cs);
|
|
|
|
#else
|
|
|
|
pthread_mutex_destroy(&mutx);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void Lock()
|
|
|
|
{
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2013-12-19 17:10:14 +00:00
|
|
|
EnterCriticalSection(&cs);
|
|
|
|
#else
|
|
|
|
pthread_mutex_lock(&mutx);
|
2018-09-02 13:49:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
bool TryLock()
|
|
|
|
{
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2018-09-02 13:49:23 +00:00
|
|
|
return TryEnterCriticalSection(&cs);
|
|
|
|
#else
|
|
|
|
return pthread_mutex_trylock(&mutx)==0;
|
2013-12-19 17:10:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void Unlock()
|
|
|
|
{
|
2019-08-25 17:29:56 +00:00
|
|
|
#ifdef _WIN32
|
2013-12-19 17:10:14 +00:00
|
|
|
LeaveCriticalSection(&cs);
|
|
|
|
#else
|
|
|
|
pthread_mutex_unlock(&mutx);
|
|
|
|
#endif
|
|
|
|
}
|
2020-02-07 15:55:32 +00:00
|
|
|
// std::BasicLockable so we can use std::lock_guard
|
|
|
|
void lock() { Lock(); }
|
|
|
|
void unlock() { Unlock(); }
|
2013-12-19 17:10:14 +00:00
|
|
|
};
|
|
|
|
|
2019-07-24 16:24:58 +00:00
|
|
|
#if !defined(TARGET_IPHONE)
|
|
|
|
#define DATA_PATH "/data/"
|
|
|
|
#else
|
|
|
|
#define DATA_PATH "/"
|
|
|
|
#endif
|
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
//Set the path !
|
2015-08-22 19:49:12 +00:00
|
|
|
void set_user_config_dir(const string& dir);
|
|
|
|
void set_user_data_dir(const string& dir);
|
|
|
|
void add_system_config_dir(const string& dir);
|
|
|
|
void add_system_data_dir(const string& dir);
|
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
//subpath format: /data/fsca-table.bit
|
2015-08-28 23:28:51 +00:00
|
|
|
string get_writable_config_path(const string& filename);
|
|
|
|
string get_writable_data_path(const string& filename);
|
|
|
|
string get_readonly_config_path(const string& filename);
|
|
|
|
string get_readonly_data_path(const string& filename);
|
2018-06-13 17:33:51 +00:00
|
|
|
bool file_exists(const string& filename);
|
2019-02-13 19:29:49 +00:00
|
|
|
bool make_directory(const string& path);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-11-12 16:54:38 +00:00
|
|
|
string get_game_save_prefix();
|
2018-11-17 11:29:59 +00:00
|
|
|
string get_game_basename();
|
2018-11-23 17:46:21 +00:00
|
|
|
string get_game_dir();
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-05-22 09:41:12 +00:00
|
|
|
bool mem_region_lock(void *start, size_t len);
|
|
|
|
bool mem_region_unlock(void *start, size_t len);
|
|
|
|
bool mem_region_set_exec(void *start, size_t len);
|
|
|
|
void *mem_region_reserve(void *start, size_t len);
|
|
|
|
bool mem_region_release(void *start, size_t len);
|
|
|
|
void *mem_region_map_file(void *file_handle, void *dest, size_t len, size_t offset, bool readwrite);
|
|
|
|
bool mem_region_unmap_file(void *start, size_t len);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-08-30 21:35:10 +00:00
|
|
|
class VArray2 {
|
2019-05-11 20:09:52 +00:00
|
|
|
public:
|
2013-12-19 17:10:14 +00:00
|
|
|
u8* data;
|
2019-05-11 20:09:52 +00:00
|
|
|
unsigned size;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-05-11 20:09:52 +00:00
|
|
|
void Zero() {
|
|
|
|
memset(data, 0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
INLINE u8& operator [](unsigned i) {
|
2013-12-19 17:10:14 +00:00
|
|
|
#ifdef MEM_BOUND_CHECK
|
2019-05-11 20:09:52 +00:00
|
|
|
if (i >= size)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2019-08-30 21:35:10 +00:00
|
|
|
ERROR_LOG(COMMON, "Error: VArray2 , index out of range (%d > %d)\n", i, size - 1);
|
2013-12-19 17:10:14 +00:00
|
|
|
MEM_DO_BREAK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return data[i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-31 22:51:12 +00:00
|
|
|
int msgboxf(const char* text,unsigned int type,...);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-06-30 19:06:46 +00:00
|
|
|
#define MBX_OK 0
|
|
|
|
#define MBX_ICONEXCLAMATION 0
|
|
|
|
#define MBX_ICONERROR 0
|