Commit 83be5723 authored by Libretro-Admin's avatar Libretro-Admin
Browse files

Get rid of Win32-specific error handling code; NativeVFS/VirtualVFS

does not need write operations, etc
parent 5fdceeed
Pipeline #44406 passed with stage
in 4 minutes and 47 seconds
......@@ -30,8 +30,7 @@ SOURCES_CXX := \
$(MEDNAFEN_DIR)/MTStreamReader.cpp
ifeq ($(THREADING_TYPE),Win32)
SOURCES_CXX += $(MEDNAFEN_DIR)/mthreading/MThreading_Win32.cpp \
$(MEDNAFEN_DIR)/win32-common.cpp
SOURCES_CXX += $(MEDNAFEN_DIR)/mthreading/MThreading_Win32.cpp
else
SOURCES_CXX += $(MEDNAFEN_DIR)/mthreading/MThreading_POSIX.cpp
endif
......
......@@ -116,13 +116,6 @@ void ExtMemStream::write(const void *data, uint64 count)
position += count;
}
void ExtMemStream::truncate(uint64 length)
{
grow_if_necessary(length, length);
data_buffer_size = length;
}
void ExtMemStream::seek(int64 offset, int whence)
{
uint64 new_position;
......
......@@ -44,7 +44,6 @@ class ExtMemStream : public Stream
virtual uint64 read(void *data, uint64 count, bool error_on_eos = true);
virtual void write(const void *data, uint64 count);
virtual void truncate(uint64 length);
virtual void seek(int64 offset, int whence);
virtual uint64 tell(void);
virtual uint64 size(void);
......
......@@ -102,22 +102,6 @@ FileStream::FileStream(const std::string& path, const uint32 mode) : OpenedMode(
fpom = "rb";
open_flags = O_RDONLY;
break;
case MODE_READ_WRITE:
fpom = "r+b";
open_flags = O_RDWR | O_CREAT;
break;
case MODE_WRITE: // Truncation is handled near the end of the constructor.
case MODE_WRITE_INPLACE:
fpom = "wb";
open_flags = O_WRONLY | O_CREAT;
break;
case MODE_WRITE_SAFE:
fpom = "wb";
open_flags = O_WRONLY | O_CREAT | O_EXCL;
break;
}
#ifdef O_BINARY
......@@ -174,22 +158,10 @@ FileStream::FileStream(const std::string& path, const uint32 mode) : OpenedMode(
throw MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError());
}
if(mode == MODE_WRITE)
{
truncate(0);
}
}
FileStream::~FileStream()
{
#if 0
if(fp && (attributes() & ATTRIBUTE_WRITEABLE))
{
MDFN_printf(_("FileStream::close() not explicitly called for file \"%s\" opened for writing!\n"), path_save.c_str());
}
#endif
close();
}
......@@ -202,16 +174,6 @@ uint64 FileStream::attributes(void)
case MODE_READ:
ret |= ATTRIBUTE_READABLE;
break;
case MODE_READ_WRITE:
ret |= ATTRIBUTE_READABLE | ATTRIBUTE_WRITEABLE;
break;
case MODE_WRITE_INPLACE:
case MODE_WRITE_SAFE:
case MODE_WRITE:
ret |= ATTRIBUTE_WRITEABLE;
break;
}
return ret;
......@@ -318,62 +280,6 @@ void FileStream::write(const void *data, uint64 count)
prev_was_write = 1;
}
void FileStream::truncate(uint64 length)
{
#ifdef WIN32
if(fflush(fp) == EOF)
{
ErrnoHolder ene(errno);
throw MDFN_Error(ene.Errno(), _("Error truncating opened file \"%s\": %s"), path_save.c_str(), ene.StrError());
}
LARGE_INTEGER pos_set;
LARGE_INTEGER pos_save;
HANDLE fhand = (HANDLE)_get_osfhandle(fileno(fp));
pos_set.QuadPart = 0;
if(!SetFilePointerEx(fhand, pos_set, &pos_save, FILE_CURRENT))
{
const uint32 ec = GetLastError();
throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str());
}
pos_set.QuadPart = length;
if(!SetFilePointerEx(fhand, pos_set, nullptr, FILE_BEGIN))
{
const uint32 ec = GetLastError();
throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str());
}
if(!SetEndOfFile(fhand))
{
const uint32 ec = GetLastError();
SetFilePointerEx(fhand, pos_save, nullptr, FILE_BEGIN);
throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str());
}
if(!SetFilePointerEx(fhand, pos_save, nullptr, FILE_BEGIN))
{
// Can SetFilePointerEx() fail here?
const uint32 ec = GetLastError();
throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str());
}
#else
if(fflush(fp) == EOF || ftruncate(fileno(fp), length) != 0)
{
ErrnoHolder ene(errno);
throw MDFN_Error(ene.Errno(), _("Error truncating opened file \"%s\": %s"), path_save.c_str(), ene.StrError());
}
#endif
}
void FileStream::seek(int64 offset, int whence)
{
if(fseeko(fp, offset, whence) == -1)
......
......@@ -51,12 +51,6 @@ class FileStream : public Stream
enum
{
MODE_READ = VirtualFS::MODE_READ,
MODE_READ_WRITE = VirtualFS::MODE_READ_WRITE,
MODE_WRITE = VirtualFS::MODE_WRITE,
MODE_WRITE_SAFE = VirtualFS::MODE_WRITE_SAFE, // Will throw an exception instead of overwriting an existing file.
MODE_WRITE_INPLACE = VirtualFS::MODE_WRITE_INPLACE, // Like MODE_WRITE, but won't truncate the file if it already exists.
};
FileStream(const std::string& path, const uint32 mode);
......@@ -70,7 +64,6 @@ class FileStream : public Stream
virtual uint64 read(void *data, uint64 count, bool error_on_eos = true);
virtual void write(const void *data, uint64 count);
virtual void truncate(uint64 length);
virtual void seek(int64 offset, int whence);
virtual uint64 tell(void);
virtual uint64 size(void);
......@@ -109,7 +102,7 @@ class FileStream : public Stream
void* mapping;
uint64 mapping_size;
int prev_was_write; // -1 for no state, 0 for last op was read, 1 for last op was write(used for MODE_READ_WRITE)
int prev_was_write; // -1 for no state, 0 for last op was read
};
}
......
......@@ -21,12 +21,6 @@
#include "MemoryStream.h"
#ifdef WIN32
// For mswin_utf8_convert_kludge()
#include "string/string.h"
#include "win32-common.h"
#endif
namespace Mednafen
{
......@@ -242,17 +236,6 @@ void MemoryStream::write(const void *data, uint64 count)
position += count;
}
//
// Don't add code to reduce the amount of memory allocated(when possible) without providing a
// per-stream setting to disable that behavior.
//
void MemoryStream::truncate(uint64 length)
{
grow_if_necessary(length, length);
data_buffer_size = length;
}
void MemoryStream::seek(int64 offset, int whence)
{
uint64 new_position;
......@@ -329,30 +312,4 @@ int MemoryStream::get_line(std::string &str)
}
void MemoryStream::mswin_utf8_convert_kludge(void)
{
#ifdef WIN32
if(!UTF8_validate(map_size(), (char*)map(), true) && map_size() <= INT_MAX)
{
int req;
if((req = MultiByteToWideChar(CP_ACP, 0, (char*)map(), map_size(), NULL, 0)) > 0)
{
std::unique_ptr<char16_t[]> ws(new char16_t[req]);
if(MultiByteToWideChar(CP_ACP, 0, (char*)map(), map_size(), (wchar_t*)ws.get(), req) == req)
{
size_t fpnr = 0;
if(UTF16_to_UTF8(ws.get(), req, (char*)map(), &fpnr, true))
{
truncate(fpnr);
UTF16_to_UTF8(ws.get(), req, (char*)map(), &fpnr, true);
}
}
}
}
#endif
}
}
......@@ -61,7 +61,6 @@ class MemoryStream : public Stream
virtual uint64 read(void *data, uint64 count, bool error_on_eos = true);
virtual void write(const void *data, uint64 count);
virtual void truncate(uint64 length);
virtual void seek(int64 offset, int whence);
virtual uint64 tell(void);
virtual uint64 size(void);
......@@ -72,8 +71,6 @@ class MemoryStream : public Stream
void shrink_to_fit(void); // Minimizes alloced memory.
void mswin_utf8_convert_kludge(void);
// No methods on the object may be called externally(other than the destructor) after steal_malloced_ptr()
INLINE void* steal_malloced_ptr(void)
{
......
......@@ -88,28 +88,29 @@ void NativeVFS::readdirentries(const std::string& path, std::function<bool(const
{
if(!(dp = FindFirstFileW((const wchar_t*)u16path.c_str(), &ded)))
{
const uint32 ec = GetLastError();
throw MDFN_Error(0, _("Error reading directory entries from \"%s\": %s"), path.c_str(), Win32Common::ErrCodeToString(ec).c_str());
#if 0
printf("Error reading directory entries from %s\n", path.c_str());
#endif
return;
}
for(;;)
{
//printf("%s\n", UTF16_to_UTF8((const char16_t*)ded.cFileName, nullptr, true).c_str());
if(!callb(UTF16_to_UTF8((const char16_t*)ded.cFileName, nullptr, true)))
break;
//
if(!FindNextFileW(dp, &ded))
{
const uint32 ec = GetLastError();
if(!callb(UTF16_to_UTF8((const char16_t*)ded.cFileName, nullptr, true)))
break;
if(!FindNextFileW(dp, &ded))
{
const uint32 ec = GetLastError();
if(ec == ERROR_NO_MORE_FILES)
break;
else
throw MDFN_Error(0, _("Error reading directory entries from \"%s\": %s"), path.c_str(), Win32Common::ErrCodeToString(ec).c_str());
}
if(ec == ERROR_NO_MORE_FILES)
break;
#if 0
printf("Error rading directory entries from %s\n", path.c_str());
#endif
return;
}
}
//
FindClose(dp);
dp = nullptr;
}
......
......@@ -229,88 +229,4 @@ int Stream::get_line(std::string &str)
return(str.length() ? 256 : -1);
}
/*
StreamPosFilter::StreamPosFilter(std::shared_ptr<Stream> s_) : s(s_), pos(0)
{
}
uint64 StreamPosFilter::read(void *data, uint64 count, bool error_on_eos)
{
if(s->tell() != pos)
s->seek(pos, SEEK_SET);
uint64 ret = s->read(data, count, error_on_eos);
pos += ret;
return ret;
}
void StreamPosFilter::write(const void *data, uint64 count)
{
if(s->tell() != pos)
s->seek(pos, SEEK_SET);
try
{
s->write(data, count);
}
catch(...)
{
try { pos = s->tell(); } catch(...) { }
throw;
}
pos += count;
return ret;
}
void StreamPosFilter::seek(int64 offset, int whence)
{
try
{
s->seek(offset, whence);
}
catch(...)
{
try { pos = s->tell(); } catch(...) { }
throw;
}
pos = s->tell();
}
uint64 StreamPosFilter::tell(void)
{
return pos;
}
uint64 StreamPosFilter::size(void)
{
return s->size();
}
void StreamPosFilter::close(void)
{
s->close();
}
uint64 StreamPosFilter::attributes(void)
{
return s->attributes();
}
void StreamPosFilter::truncate(uint64 length)
{
s->trruncate(length);
}
void StreamPosFilter::flush(void)
{
s->flush();
}
*/
}
......@@ -83,8 +83,6 @@ class Stream
virtual uint64 read(void *data, uint64 count, bool error_on_eos = true) = 0;
virtual void write(const void *data, uint64 count) = 0;
virtual void truncate(uint64 length) = 0; // Should have ftruncate()-like semantics; but avoid using it to extend files.
virtual void seek(int64 offset, int whence = SEEK_SET) = 0;
inline void rewind(void)
{
......@@ -256,27 +254,5 @@ class Stream
//
//
//
/*
class StreamPosFilter final : public Stream
{
public:
StreamPosFilter(std::shared_ptr<Stream> s_);
virtual uint64 read(void *data, uint64 count, bool error_on_eos = true);
virtual void write(const void *data, uint64 count);
virtual void seek(int64 offset, int whence);
virtual uint64 tell(void);
virtual uint64 size(void);
virtual void close(void);
virtual uint64 attributes(void);
virtual void truncate(uint64 length);
virtual void flush(void);
private:
uint64 pos;
std::shared_ptr<Stream> s;
};
*/
}
#endif
......@@ -39,20 +39,6 @@ class VirtualFS
{
MODE_READ = 0x0D46323C,
// Will create file if it doesn't already exist. Will not truncate existing file.
// Any necessary synchronization when switching between read and write operations is handled internally in
// Stream implementation.
MODE_READ_WRITE = 0x193AAA56,
// Will create file if it doesn't already exist, and truncate file to 0-length if it does.
MODE_WRITE = 0xA587267C,
// Will throw an exception if the file already exists(to prevent overwriting).
MODE_WRITE_SAFE = 0xB8E75994,
// Like MODE_WRITE, but won't truncate the file if it already exists.
MODE_WRITE_INPLACE = 0xE7B2EC69,
// Custom modes for derived classes after this.
MODE__CUSTOM_BEGIN = 0xF0000000,
};
......
......@@ -33,16 +33,14 @@
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <mednafen/win32-common.h>
#include <windows.h>
#include <io.h>
namespace Mednafen
{
namespace MThreading
{
using namespace Win32Common;
struct Thread
{
HANDLE thr;
......@@ -91,7 +89,7 @@ Thread* Thread_Create(int (*fn)(void *), void *data, const char* debug_name)
free(ret);
throw MDFN_Error(0, _("%s failed: %s"), "_beginthreadex()", ene.StrError());
return NULL;
}
return ret;
......@@ -161,18 +159,17 @@ bool Mutex_Unlock(Mutex* mutex)
Cond* Cond_Create(void)
{
Cond* ret;
Cond* ret;
if(!(ret = (Cond*)calloc(1, sizeof(Cond))))
return NULL;
if(!(ret->evt = CreateEvent(NULL, FALSE, FALSE, NULL)))
{
free(ret);
if(!(ret = (Cond*)calloc(1, sizeof(Cond))))
return NULL;
if(!(ret->evt = CreateEvent(NULL, FALSE, FALSE, NULL)))
{
free(ret);
return NULL;
}
throw MDFN_Error(0, _("%s failed: %s"), "CreateEvent()", ErrCodeToString(GetLastError()).c_str());
}
return ret;
return ret;
}
void Cond_Destroy(Cond* cond)
......@@ -241,7 +238,7 @@ Sem* Sem_Create(void)
if(!(ret->sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
{
free(ret);
throw MDFN_Error(0, _("%s failed: %s"), "CreateSemaphore()", ErrCodeToString(GetLastError()).c_str());
return NULL;
}
return ret;
......@@ -292,12 +289,12 @@ uint64 Thread_SetAffinity(Thread* thread, uint64 mask)
if(mask > ~(DWORD_PTR)0)
{
throw MDFN_Error(0, _("Setting affinity to 0x%016llx failed: %s"), (unsigned long long)mask, ErrCodeToString(ERROR_INVALID_PARAMETER).c_str());
return 0;
}
if(!(ret = SetThreadAffinityMask(thread ? thread->thr : GetCurrentThread(), mask)))
{
throw MDFN_Error(0, _("Setting affinity to 0x%016llx failed: %s"), (unsigned long long)mask, ErrCodeToString(GetLastError()).c_str());
return 0;
}
return ret;
......
......@@ -20,17 +20,12 @@
*/
#define MDFN_SNES_FAUST_SPC700_IPL_HLE 1
//#define MDFN_SNES_FAUST_SPC700_IPL_EFFECTS_ANALYZE 1
#include "snes.h"
#include "apu.h"
#include <mednafen/sound/OwlResampler.h>
#ifdef MDFN_SNES_FAUST_SPC700_IPL_EFFECTS_ANALYZE
#include <mednafen/FileStream.h>
#endif
namespace MDFN_IEN_SNES_FAUST
{
......
......@@ -462,45 +462,11 @@ bool CART_Init(Stream* fp, uint8 id[16], const int32 cx4_ocmultiplier, const int
bool CART_LoadNV(void)
{
#ifndef MDFN_SNES_FAUST_SUPAFAUST
if(Cart.RAM_Size)
{
try
{
const std::string path = MDFN_MakeFName(MDFNMKF_SAV, 0, "srm");
FileStream fp(path, FileStream::MODE_READ);
const uint64 fp_size_tmp = fp.size();
if(Cart.RAM_Size != fp_size_tmp) // Check before reading any data.
throw MDFN_Error(0, _("Save game memory file \"%s\" is an incorrect size(%llu bytes). The correct size is %llu bytes."), path.c_str(),
(unsigned long long)fp_size_tmp, (unsigned long long)Cart.RAM_Size);
fp.read(Cart.RAM, Cart.RAM_Size);
return true;
}
catch(MDFN_Error &e)
{
if(e.GetErrno() != ENOENT)
throw;
}
}
#endif
return false;
}
void CART_SaveNV(void)
{
#ifndef MDFN_SNES_FAUST_SUPAFAUST
if(Cart.RAM_Size)
{
const std::string path = MDFN_MakeFName(MDFNMKF_SAV, 0, "srm");
FileStream fp(path, FileStream::MODE_WRITE_INPLACE);
fp.write(Cart.RAM, Cart.RAM_Size);
fp.close();
}
#endif
}
void CART_Kill(void)
......
......@@ -461,16 +461,6 @@ void MSU1_Init(GameFile* gf, double* IdealSoundRate, uint64 affinity_audio, uint
//printf("Phase: %3u, Coeff: %2u --- % f --- % f : % f\n", phase, coeff, Impulse[phase][coeff], Impulse[phase][coeff] - Impulse[std::max<int>(0, phase - 1)][coeff], Impulse[std::max<int>(0, phase - 1)][coeff] - Impulse[std::max<int>(0, phase - 2)][coeff]);
}
}
#if 0
{
std::unique_ptr<Stream> fp(NVFS.open("/tmp/dump.raw", VirtualFS::MODE_WRITE));
float goat[65536] = { 0 };
memcpy(&goat[32768 - NumCoeffs / 2], &Impulse[NumPhases / 2][0], NumCoeffs * sizeof(float));
fp->write(goat, 65536 * sizeof(float));
}
#endif
}
ResampBufPos = 0;
......
......@@ -289,38 +289,17 @@ static void (*SPC700_IOHandler)(void);