bsnes/nall/inode.hpp

164 lines
5.1 KiB
C++
Raw Normal View History

#pragma once
//generic abstraction layer for common storage operations against both files and directories
//these functions are not recursive; use directory::create() and directory::remove() for recursion
#include <nall/platform.hpp>
#include <nall/string.hpp>
namespace nall {
struct inode {
enum class time : uint { create, modify, access };
inode() = delete;
inode(const inode&) = delete;
auto operator=(const inode&) -> inode& = delete;
static auto exists(const string& name) -> bool {
return access(name, F_OK) == 0;
}
static auto readable(const string& name) -> bool {
return access(name, R_OK) == 0;
}
static auto writable(const string& name) -> bool {
return access(name, W_OK) == 0;
}
static auto executable(const string& name) -> bool {
return access(name, X_OK) == 0;
}
static auto hidden(const string& name) -> bool {
#if defined(PLATFORM_WINDOWS)
auto attributes = GetFileAttributes(utf16_t(name));
return attributes & FILE_ATTRIBUTE_HIDDEN;
#else
//todo: is this really the best way to do this? stat doesn't have S_ISHIDDEN ...
return name.split("/").last().beginsWith(".");
#endif
}
static auto mode(const string& name) -> uint {
struct stat data{};
stat(name, &data);
return data.st_mode;
}
Update to v098r11 release. byuu says: Changelog: - fixed nall/path.hpp compilation issue - fixed ruby/audio/xaudio header declaration compilation issue (again) - cleaned up xaudio2.hpp file to match my coding syntax (12.5% of the file was whitespace overkill) - added null terminator entry to nall/windows/utf8.hpp argc[] array - nall/windows/guid.hpp uses the Windows API for generating the GUID - this should stop all the bug reports where two nall users were generating GUIDs at the exact same second - fixed hiro/cocoa compilation issue with uint# types - fixed major higan/sfc Super Game Boy audio latency issue - fixed higan/sfc CPU core bug with pei, [dp], [dp]+y instructions - major cleanups to higan/processor/r65816 core - merged emulation/native-mode opcodes - use camel-case naming on memory.hpp functions - simplify address masking code for memory.hpp functions - simplify a few opcodes themselves (avoid redundant copies, etc) - rename regs.* to r.* to match modern convention of other CPU cores - removed device.order<> concept from Emulator::Interface - cores will now do the translation to make the job of the UI easier - fixed plurality naming of arrays in Emulator::Interface - example: emulator.ports[p].devices[d].inputs[i] - example: vector<Medium> media - probably more surprises Major show-stoppers to the next official release: - we need to work on GB core improvements: LY=153/0 case, multiple STAT IRQs case, GBC audio output regs, etc. - we need to re-add software cursors for light guns (Super Scope, Justifier) - after the above, we need to fix the turbo button for the Super Scope I really have no idea how I want to implement the light guns. Ideally, we'd want it in higan/video, so we can support the NES Zapper with the same code. But this isn't going to be easy, because only the SNES knows when its output is interlaced, and its resolutions can vary as {256,512}x{224,240,448,480} which requires pixel doubling that was hard-coded to the SNES-specific behavior, but isn't appropriate to be exposed in higan/video.
2016-05-25 11:13:02 +00:00
static auto uid(const string& name) -> uint {
struct stat data{};
stat(name, &data);
return data.st_uid;
}
Update to v098r11 release. byuu says: Changelog: - fixed nall/path.hpp compilation issue - fixed ruby/audio/xaudio header declaration compilation issue (again) - cleaned up xaudio2.hpp file to match my coding syntax (12.5% of the file was whitespace overkill) - added null terminator entry to nall/windows/utf8.hpp argc[] array - nall/windows/guid.hpp uses the Windows API for generating the GUID - this should stop all the bug reports where two nall users were generating GUIDs at the exact same second - fixed hiro/cocoa compilation issue with uint# types - fixed major higan/sfc Super Game Boy audio latency issue - fixed higan/sfc CPU core bug with pei, [dp], [dp]+y instructions - major cleanups to higan/processor/r65816 core - merged emulation/native-mode opcodes - use camel-case naming on memory.hpp functions - simplify address masking code for memory.hpp functions - simplify a few opcodes themselves (avoid redundant copies, etc) - rename regs.* to r.* to match modern convention of other CPU cores - removed device.order<> concept from Emulator::Interface - cores will now do the translation to make the job of the UI easier - fixed plurality naming of arrays in Emulator::Interface - example: emulator.ports[p].devices[d].inputs[i] - example: vector<Medium> media - probably more surprises Major show-stoppers to the next official release: - we need to work on GB core improvements: LY=153/0 case, multiple STAT IRQs case, GBC audio output regs, etc. - we need to re-add software cursors for light guns (Super Scope, Justifier) - after the above, we need to fix the turbo button for the Super Scope I really have no idea how I want to implement the light guns. Ideally, we'd want it in higan/video, so we can support the NES Zapper with the same code. But this isn't going to be easy, because only the SNES knows when its output is interlaced, and its resolutions can vary as {256,512}x{224,240,448,480} which requires pixel doubling that was hard-coded to the SNES-specific behavior, but isn't appropriate to be exposed in higan/video.
2016-05-25 11:13:02 +00:00
static auto gid(const string& name) -> uint {
struct stat data{};
stat(name, &data);
return data.st_gid;
}
static auto owner(const string& name) -> string {
#if !defined(PLATFORM_WINDOWS)
struct passwd* pw = getpwuid(uid(name));
if(pw && pw->pw_name) return pw->pw_name;
#endif
return {};
}
static auto group(const string& name) -> string {
#if !defined(PLATFORM_WINDOWS)
struct group* gr = getgrgid(gid(name));
if(gr && gr->gr_name) return gr->gr_name;
#endif
return {};
}
Update to v100r16 release. byuu says: (Windows users may need to include <sys/time.h> at the top of nall/chrono.hpp, not sure.) Unchangelog: - forgot to add the Scheduler clock=0 fix because I have the memory of a goldfish Changelog: - new icarus database with nine additional games - hiro(GTK,Qt) won't constantly write its settings.bml file to disk anymore - added latency simulator for fun (settings.bml => Input/Latency in milliseconds) So the last one ... I wanted to test out nall::chrono, and I was also thinking that by polling every emulated frame, it's pretty wasteful when you are using Fast Forward and hitting 200+fps. As I've said before, calls to ruby::input::poll are not cheap. So to get around this, I added a limiter so that if you called the hardware poll function within N milliseconds, it'll return without doing any actual work. And indeed, that increases my framerate of Zelda 3 uncapped from 133fps to 142fps. Yay. But it's not a "real" speedup, as it only helps you when you exceed 100% speed (theoretically, you'd need to crack 300% speed since the game itself will poll at 16ms at 100% speed, but yet it sped up Zelda 3, so who am I to complain?) I threw the latency value into the settings file. It should be 16, but I set it to 5 since that was the lowest before it started negatively impacting uncapped speeds. You're wasting your time and CPU cycles setting it lower than 5, but if people like placebo effects it might work. Maybe I should let it be a signed integer so people can set it to -16 and think it's actually faster :P (I'm only joking. I took out the 96000hz audio placebo effect as well. Not really into psychological tricks anymore.) But yeah seriously, I didn't do this to start this discussion again for the billionth time. Please don't go there. And please don't tell me this WIP has higher/lower latency than before. I don't want to hear it. The only reason I bring it up is for the fun part that is worth discussing: put up or shut up time on how sensitive you are to latency! You can set the value above 5 to see how games feel. I personally can't really tell a difference until about 50. And I can't be 100% confident it's worse until about 75. But ... when I set it to 150, games become "extra difficult" ... the higher it goes, the worse it gets :D For this WIP, I've left no upper limit cap. I'll probably set a cap of something like 500ms or 1000ms for the official release. Need to balance user error/trolling with enjoyability. I'll think about it. [...] Now, what I worry about is stupid people seeing it and thinking it's an "added latency" setting, as if anyone would intentionally make things worse by default. This is a limiter. So if 5ms have passed since the game last polled, and that will be the case 99.9% of the time in games, the next poll will happen just in time, immediately when the game polls the inputs. Thus, a value below 1/<framerate>ms is not only pointless, if you go too low it will ruin your fast forward max speeds. I did say I didn't want to resort to placebo tricks, but I also don't want to spark up public discussion on this again either. So it might be best to default Input/Latency to 0ms, and internally have a max(5, latency) wrapper around the value.
2016-08-03 12:32:40 +00:00
static auto timestamp(const string& name, time mode = time::modify) -> uint64_t {
struct stat data{};
stat(name, &data);
switch(mode) {
#if defined(PLATFORM_WINDOWS)
//on Windows, the last status change time (ctime) holds the file creation time instead
case time::create: return data.st_ctime;
#elif defined(PLATFORM_BSD) || defined(PLATFORM_MACOS)
//st_birthtime may return -1 or st_atime if it is not supported by the file system
//the best that can be done in this case is to return st_mtime if it's older
case time::create: return min((uint)data.st_birthtime, (uint)data.st_mtime);
#else
//Linux simply doesn't support file creation time at all
//this is also our fallback case for unsupported operating systems
case time::create: return data.st_mtime;
#endif
case time::modify: return data.st_mtime;
//for performance reasons, last access time is usually not enabled on various filesystems
//ensure that the last access time is not older than the last modify time (eg for NTFS)
case time::access: return max((uint)data.st_atime, data.st_mtime);
}
return 0;
}
static auto setMode(const string& name, uint mode) -> bool {
#if !defined(PLATFORM_WINDOWS)
return chmod(name, mode) == 0;
#else
return _wchmod(utf16_t(name), (mode & 0400 ? _S_IREAD : 0) | (mode & 0200 ? _S_IWRITE : 0)) == 0;
#endif
}
static auto setOwner(const string& name, const string& owner) -> bool {
#if !defined(PLATFORM_WINDOWS)
struct passwd* pwd = getpwnam(owner);
if(!pwd) return false;
return chown(name, pwd->pw_uid, inode::gid(name)) == 0;
#else
return true;
#endif
}
static auto setGroup(const string& name, const string& group) -> bool {
#if !defined(PLATFORM_WINDOWS)
struct group* grp = getgrnam(group);
if(!grp) return false;
return chown(name, inode::uid(name), grp->gr_gid) == 0;
#else
return true;
#endif
}
static auto setTimestamp(const string& name, uint64_t value, time mode = time::modify) -> bool {
struct utimbuf timeBuffer;
timeBuffer.modtime = mode == time::modify ? value : inode::timestamp(name, time::modify);
timeBuffer.actime = mode == time::access ? value : inode::timestamp(name, time::access);
return utime(name, &timeBuffer) == 0;
}
//returns true if 'name' already exists
Update to v098r11 release. byuu says: Changelog: - fixed nall/path.hpp compilation issue - fixed ruby/audio/xaudio header declaration compilation issue (again) - cleaned up xaudio2.hpp file to match my coding syntax (12.5% of the file was whitespace overkill) - added null terminator entry to nall/windows/utf8.hpp argc[] array - nall/windows/guid.hpp uses the Windows API for generating the GUID - this should stop all the bug reports where two nall users were generating GUIDs at the exact same second - fixed hiro/cocoa compilation issue with uint# types - fixed major higan/sfc Super Game Boy audio latency issue - fixed higan/sfc CPU core bug with pei, [dp], [dp]+y instructions - major cleanups to higan/processor/r65816 core - merged emulation/native-mode opcodes - use camel-case naming on memory.hpp functions - simplify address masking code for memory.hpp functions - simplify a few opcodes themselves (avoid redundant copies, etc) - rename regs.* to r.* to match modern convention of other CPU cores - removed device.order<> concept from Emulator::Interface - cores will now do the translation to make the job of the UI easier - fixed plurality naming of arrays in Emulator::Interface - example: emulator.ports[p].devices[d].inputs[i] - example: vector<Medium> media - probably more surprises Major show-stoppers to the next official release: - we need to work on GB core improvements: LY=153/0 case, multiple STAT IRQs case, GBC audio output regs, etc. - we need to re-add software cursors for light guns (Super Scope, Justifier) - after the above, we need to fix the turbo button for the Super Scope I really have no idea how I want to implement the light guns. Ideally, we'd want it in higan/video, so we can support the NES Zapper with the same code. But this isn't going to be easy, because only the SNES knows when its output is interlaced, and its resolutions can vary as {256,512}x{224,240,448,480} which requires pixel doubling that was hard-coded to the SNES-specific behavior, but isn't appropriate to be exposed in higan/video.
2016-05-25 11:13:02 +00:00
static auto create(const string& name, uint permissions = 0755) -> bool {
if(exists(name)) return true;
if(name.endsWith("/")) return mkdir(name, permissions) == 0;
int fd = open(name, O_CREAT | O_EXCL, permissions);
if(fd < 0) return false;
return close(fd), true;
}
//returns false if 'name' and 'targetname' are on different file systems (requires copy)
static auto rename(const string& name, const string& targetname) -> bool {
return ::rename(name, targetname) == 0;
}
//returns false if 'name' is a directory that is not empty
static auto remove(const string& name) -> bool {
#if defined(PLATFORM_WINDOWS)
if(name.endsWith("/")) return _wrmdir(utf16_t(name)) == 0;
return _wunlink(utf16_t(name)) == 0;
#else
if(name.endsWith("/")) return rmdir(name) == 0;
return unlink(name) == 0;
#endif
}
};
}