mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-09-10 12:02:53 +02:00
Remove Windows API emulation
This commit is contained in:
parent
ff25161b10
commit
95e17244fd
13 changed files with 2 additions and 1339 deletions
|
@ -1066,12 +1066,6 @@ static void mriBindingExecute() {
|
|||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(MKXPZ_ESSENTIALS_DEBUG) && !defined(__WIN32__)
|
||||
char *tmpdir = getenv("TMPDIR");
|
||||
if (tmpdir)
|
||||
setenv("TEMP", tmpdir, false);
|
||||
#endif
|
||||
|
||||
VALUE lpaths = rb_gv_get(":");
|
||||
if (!conf.rubyLoadpaths.empty()) {
|
||||
/* Setup custom load paths */
|
||||
|
|
|
@ -335,11 +335,6 @@ void graphicsBindingInit()
|
|||
_rb_define_module_function(module, "fadein", graphicsFadein);
|
||||
_rb_define_module_function(module, "snap_to_bitmap", graphicsSnapToBitmap);
|
||||
_rb_define_module_function(module, "resize_screen", graphicsResizeScreen);
|
||||
#ifdef MKXPZ_ESSENTIALS_DEBUG
|
||||
// The other two are overridden by default, which is super
|
||||
_rb_define_module_function(module, "mkxp_snap_to_bitmap", graphicsSnapToBitmap);
|
||||
_rb_define_module_function(module, "mkxp_resize_screen", graphicsResizeScreen);
|
||||
#endif
|
||||
_rb_define_module_function(module, "center", graphicsCenter);
|
||||
|
||||
INIT_GRA_PROP_BIND( Brightness, "brightness" );
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Most of the MiniFFI class was taken from Ruby 1.8's Win32API.c,
|
||||
// it's just as basic but should work fine for the moment
|
||||
|
||||
#include "system/fake-api.h"
|
||||
#include <SDL.h>
|
||||
#include <cstdint>
|
||||
|
||||
|
@ -39,40 +38,6 @@ DEF_ALLOCFUNC_CUSTOMFREE(MiniFFI, SDL_UnloadObject);
|
|||
#endif
|
||||
|
||||
static void *MiniFFI_GetFunctionHandle(void *libhandle, const char *func) {
|
||||
#ifdef MKXPZ_ESSENTIALS_DEBUG
|
||||
#define CAPTURE(n) \
|
||||
if (!strcmp(#n, func)) \
|
||||
return (void *)&MKXP_##n
|
||||
CAPTURE(GetCurrentThreadId);
|
||||
CAPTURE(GetWindowThreadProcessId);
|
||||
CAPTURE(FindWindowEx);
|
||||
CAPTURE(GetForegroundWindow);
|
||||
CAPTURE(GetClientRect);
|
||||
CAPTURE(GetCursorPos);
|
||||
CAPTURE(ScreenToClient);
|
||||
CAPTURE(SetWindowPos);
|
||||
CAPTURE(SetWindowTextA);
|
||||
CAPTURE(GetWindowRect);
|
||||
CAPTURE(GetKeyboardState);
|
||||
#ifndef __WIN32__
|
||||
// Functions only needed on Linux and macOS go here
|
||||
CAPTURE(RtlMoveMemory);
|
||||
CAPTURE(LoadLibrary);
|
||||
CAPTURE(FreeLibrary);
|
||||
CAPTURE(GetAsyncKeyState);
|
||||
CAPTURE(GetSystemPowerStatus);
|
||||
CAPTURE(ShowWindow);
|
||||
CAPTURE(GetSystemMetrics);
|
||||
CAPTURE(SetCapture);
|
||||
CAPTURE(ReleaseCapture);
|
||||
CAPTURE(ShowCursor);
|
||||
CAPTURE(GetPrivateProfileString);
|
||||
CAPTURE(GetUserDefaultLangID);
|
||||
CAPTURE(GetUserName);
|
||||
CAPTURE(RegisterHotKey);
|
||||
CAPTURE(SetWindowLong);
|
||||
#endif
|
||||
#endif
|
||||
if (!libhandle)
|
||||
return 0;
|
||||
return SDL_LoadFunction(libhandle, func);
|
||||
|
|
|
@ -4,9 +4,6 @@
|
|||
// Created by ゾロアーク on 1/2/21.
|
||||
//
|
||||
|
||||
// Uncomment for Essentials crap
|
||||
// MKXPZ_EXTRA_ARGS = MKXPZ_ESSENTIALS_DEBUG
|
||||
|
||||
// -----------------------------
|
||||
// Don't change any of this
|
||||
// -----------------------------
|
||||
|
|
|
@ -49,7 +49,6 @@
|
|||
3B10EDAC2568E95E00372D13 /* sharedstate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED512568E95D00372D13 /* sharedstate.cpp */; };
|
||||
3B10EDAD2568E95E00372D13 /* filesystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED542568E95D00372D13 /* filesystem.cpp */; };
|
||||
3B10EDAF2568E95E00372D13 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED562568E95D00372D13 /* main.cpp */; };
|
||||
3B10EDB12568E95E00372D13 /* fake-api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED5A2568E95D00372D13 /* fake-api.cpp */; };
|
||||
3B10EDB32568E95E00372D13 /* midisource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED5E2568E95D00372D13 /* midisource.cpp */; };
|
||||
3B10EDB42568E95E00372D13 /* alstream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED5F2568E95D00372D13 /* alstream.cpp */; };
|
||||
3B10EDB52568E95E00372D13 /* fluid-fun.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED602568E95D00372D13 /* fluid-fun.cpp */; };
|
||||
|
@ -186,7 +185,6 @@
|
|||
3B1C23BB25A19C600075EF5D /* graphics.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED7B2568E95D00372D13 /* graphics.cpp */; };
|
||||
3B1C23BC25A19C600075EF5D /* font.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED772568E95D00372D13 /* font.cpp */; };
|
||||
3B1C23BF25A19C600075EF5D /* filesystemImplApple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3B5A840C2569BE7C00BAF2E5 /* filesystemImplApple.mm */; };
|
||||
3B1C23C025A19C600075EF5D /* fake-api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED5A2568E95D00372D13 /* fake-api.cpp */; };
|
||||
3B1C23C125A19C600075EF5D /* sharedstate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED512568E95D00372D13 /* sharedstate.cpp */; };
|
||||
3B1C23C325A19C600075EF5D /* libSDL2_ttf.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BE080EF256879FD0006849F /* libSDL2_ttf.a */; };
|
||||
3B1C23C425A19C600075EF5D /* libvorbisenc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BE080FB256879FE0006849F /* libvorbisenc.a */; };
|
||||
|
@ -364,7 +362,6 @@
|
|||
3BC65D132584EDC60063AFF1 /* graphics.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED7B2568E95D00372D13 /* graphics.cpp */; };
|
||||
3BC65D142584EDC60063AFF1 /* font.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED772568E95D00372D13 /* font.cpp */; };
|
||||
3BC65D172584EDC60063AFF1 /* filesystemImplApple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3B5A840C2569BE7C00BAF2E5 /* filesystemImplApple.mm */; };
|
||||
3BC65D182584EDC60063AFF1 /* fake-api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED5A2568E95D00372D13 /* fake-api.cpp */; };
|
||||
3BC65D192584EDC60063AFF1 /* sharedstate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED512568E95D00372D13 /* sharedstate.cpp */; };
|
||||
3BC65D282584EDC60063AFF1 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BD2B47A256534BA003DAD8A /* IOKit.framework */; };
|
||||
3BC65D2A2584EDC60063AFF1 /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BE081552568D3A60006849F /* Carbon.framework */; };
|
||||
|
@ -458,7 +455,6 @@
|
|||
3BC65DD42584F3AD0063AFF1 /* graphics.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED7B2568E95D00372D13 /* graphics.cpp */; };
|
||||
3BC65DD52584F3AD0063AFF1 /* font.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED772568E95D00372D13 /* font.cpp */; };
|
||||
3BC65DD82584F3AD0063AFF1 /* filesystemImplApple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3B5A840C2569BE7C00BAF2E5 /* filesystemImplApple.mm */; };
|
||||
3BC65DD92584F3AD0063AFF1 /* fake-api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED5A2568E95D00372D13 /* fake-api.cpp */; };
|
||||
3BC65DDA2584F3AD0063AFF1 /* sharedstate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED512568E95D00372D13 /* sharedstate.cpp */; };
|
||||
3BC65DEB2584F3AD0063AFF1 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BD2B47A256534BA003DAD8A /* IOKit.framework */; };
|
||||
3BC65DED2584F3AD0063AFF1 /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BE081552568D3A60006849F /* Carbon.framework */; };
|
||||
|
@ -822,8 +818,6 @@
|
|||
3B10ED532568E95D00372D13 /* filesystem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = filesystem.h; sourceTree = "<group>"; };
|
||||
3B10ED542568E95D00372D13 /* filesystem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = filesystem.cpp; sourceTree = "<group>"; };
|
||||
3B10ED562568E95D00372D13 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
|
||||
3B10ED5A2568E95D00372D13 /* fake-api.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "fake-api.cpp"; sourceTree = "<group>"; };
|
||||
3B10ED5C2568E95D00372D13 /* fake-api.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "fake-api.h"; sourceTree = "<group>"; };
|
||||
3B10ED5E2568E95D00372D13 /* midisource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = midisource.cpp; sourceTree = "<group>"; };
|
||||
3B10ED5F2568E95D00372D13 /* alstream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = alstream.cpp; sourceTree = "<group>"; };
|
||||
3B10ED602568E95D00372D13 /* fluid-fun.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "fluid-fun.cpp"; sourceTree = "<group>"; };
|
||||
|
@ -980,7 +974,6 @@
|
|||
3B5A840C2569BE7C00BAF2E5 /* filesystemImplApple.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = filesystemImplApple.mm; sourceTree = "<group>"; };
|
||||
3B5A84132569C28B00BAF2E5 /* filesystemImpl.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = filesystemImpl.cpp; sourceTree = "<group>"; };
|
||||
3B5A84142569C28B00BAF2E5 /* filesystemImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = filesystemImpl.h; sourceTree = "<group>"; };
|
||||
3B5A842B2569E8BA00BAF2E5 /* mINI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = mINI.h; sourceTree = "<group>"; };
|
||||
3B5A8444256A0F6300BAF2E5 /* libopenal.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libopenal.a; path = "Dependencies/build-macosx-x86_64/lib/libopenal.a"; sourceTree = "<group>"; };
|
||||
3B5A845C256A465700BAF2E5 /* system.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = system.h; sourceTree = "<group>"; };
|
||||
3B5A845D256A465700BAF2E5 /* systemImpl.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = systemImpl.cpp; sourceTree = "<group>"; };
|
||||
|
@ -1358,7 +1351,6 @@
|
|||
3B10EE1F2569348E00372D13 /* json5pp.hpp */,
|
||||
3B1BC0DF266F7C0C00794D22 /* iniconfig.h */,
|
||||
3B1BC0E0266F7C0C00794D22 /* iniconfig.cpp */,
|
||||
3B5A842B2569E8BA00BAF2E5 /* mINI.h */,
|
||||
);
|
||||
path = util;
|
||||
sourceTree = "<group>";
|
||||
|
@ -1402,8 +1394,6 @@
|
|||
3B10ED582568E95D00372D13 /* system */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
3B10ED5A2568E95D00372D13 /* fake-api.cpp */,
|
||||
3B10ED5C2568E95D00372D13 /* fake-api.h */,
|
||||
3B5A845C256A465700BAF2E5 /* system.h */,
|
||||
3B5A8463256A46B200BAF2E5 /* systemImplApple.mm */,
|
||||
3B5A845D256A465700BAF2E5 /* systemImpl.cpp */,
|
||||
|
@ -2138,7 +2128,6 @@
|
|||
3B1C242B25A1AA1F0075EF5D /* steamshim_child.c in Sources */,
|
||||
3B3F7D2D25B1A73A00EA5F1C /* SettingsMenuController.mm in Sources */,
|
||||
3B1C23BF25A19C600075EF5D /* filesystemImplApple.mm in Sources */,
|
||||
3B1C23C025A19C600075EF5D /* fake-api.cpp in Sources */,
|
||||
3B1BC0E4266F7C2800794D22 /* iniconfig.cpp in Sources */,
|
||||
3B1C23C125A19C600075EF5D /* sharedstate.cpp in Sources */,
|
||||
);
|
||||
|
@ -2225,7 +2214,6 @@
|
|||
3BC65D142584EDC60063AFF1 /* font.cpp in Sources */,
|
||||
3B1BC0E3266F7C2700794D22 /* iniconfig.cpp in Sources */,
|
||||
3BC65D172584EDC60063AFF1 /* filesystemImplApple.mm in Sources */,
|
||||
3BC65D182584EDC60063AFF1 /* fake-api.cpp in Sources */,
|
||||
3BC65D192584EDC60063AFF1 /* sharedstate.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -2304,7 +2292,6 @@
|
|||
3BC65DD52584F3AD0063AFF1 /* font.cpp in Sources */,
|
||||
3B1BC0E1266F7C2600794D22 /* iniconfig.cpp in Sources */,
|
||||
3BC65DD82584F3AD0063AFF1 /* filesystemImplApple.mm in Sources */,
|
||||
3BC65DD92584F3AD0063AFF1 /* fake-api.cpp in Sources */,
|
||||
3BC65DDA2584F3AD0063AFF1 /* sharedstate.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -2383,7 +2370,6 @@
|
|||
3B10EDC02568E95E00372D13 /* font.cpp in Sources */,
|
||||
3B1BC0E2266F7C2700794D22 /* iniconfig.cpp in Sources */,
|
||||
3B5A840D2569BE7C00BAF2E5 /* filesystemImplApple.mm in Sources */,
|
||||
3B10EDB12568E95E00372D13 /* fake-api.cpp in Sources */,
|
||||
3B10EDAC2568E95E00372D13 /* sharedstate.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
|
|
@ -100,10 +100,6 @@ if get_option('workdir_current')
|
|||
global_args += '-DWORKDIR_CURRENT'
|
||||
endif
|
||||
|
||||
if get_option('easypoke') == true and miniffi == true
|
||||
global_args += '-DMKXPZ_ESSENTIALS_DEBUG'
|
||||
endif
|
||||
|
||||
if get_option('cxx11_experimental') == true
|
||||
global_args += '-DMKXPZ_EXP_FS'
|
||||
endif
|
||||
|
|
|
@ -8,7 +8,6 @@ option('cxx11_experimental', type: 'boolean', value: false, description: 'Toggle
|
|||
option('shared_fluid', type: 'boolean', value: true, description: 'Dynamically link fluidsynth at build time')
|
||||
option('cjk_fallback_font', type: 'boolean', value: false, description: 'Use WenQuanYi Micro Hei as the fallback font')
|
||||
option('use_miniffi', type: 'boolean', value: true, description: 'Enable MiniFFI Ruby module (Win32API)')
|
||||
option('easypoke', type: 'boolean', value: false, description: '"Fix" any incompatibilities with Pkmn Essentials.')
|
||||
option('enable-https', type: 'boolean', value: true, description: 'Support HTTPS for get/post requests. Requires OpenSSL.')
|
||||
option('default_framerate', type: 'boolean', value: false, description: 'Disable syncToRefreshrate and fixedFramerate configuration options')
|
||||
option('no_preload_scripts', type: 'boolean', value: false, description: 'Disable the preloadScript configuration option')
|
||||
|
|
|
@ -17,9 +17,8 @@
|
|||
#include "util/debugwriter.h"
|
||||
#include "util/sdl-util.h"
|
||||
#include "util/util.h"
|
||||
#include "util/json5pp.hpp"
|
||||
#include "util/mINI.h"
|
||||
|
||||
#include "util/json5pp.hpp"
|
||||
#include "util/iniconfig.h"
|
||||
|
||||
#if defined(MKXPZ_BUILD_XCODE) || defined(MKXPZ_INI_ENCODING)
|
||||
|
@ -401,7 +400,7 @@ void Config::readGameINI() {
|
|||
Debug() << iniFileName << ": failed to convert game title to UTF-8";
|
||||
}
|
||||
#else
|
||||
Debug() << iniFileName << ": Game title isn't valid UTF-8"
|
||||
Debug() << iniFileName << ": Game title isn't valid UTF-8";
|
||||
#endif
|
||||
}
|
||||
else
|
||||
|
|
|
@ -659,11 +659,6 @@ void FileSystem::openReadRaw(SDL_RWops &ops, const char *filename,
|
|||
return;
|
||||
}
|
||||
|
||||
// RGSS normalizes paths for at least audio
|
||||
// Essentials kind of takes this for granted
|
||||
// (`Audio/BGM/../../Audio/ME/[file]`)
|
||||
|
||||
// SDL_SaveBMP wants absolute paths
|
||||
std::string FileSystem::normalize(const char *pathname, bool preferred,
|
||||
bool absolute) {
|
||||
return filesystemImpl::normalizePath(pathname, preferred, absolute);
|
||||
|
|
|
@ -168,7 +168,6 @@ main_source = files(
|
|||
'net/LUrlParser.cpp',
|
||||
'net/net.cpp',
|
||||
|
||||
'system/fake-api.cpp',
|
||||
'system/systemImpl.cpp'
|
||||
)
|
||||
|
||||
|
|
|
@ -1,374 +0,0 @@
|
|||
#ifdef MKXPZ_ESSENTIALS_DEBUG
|
||||
#include <SDL.h>
|
||||
|
||||
#ifdef __WIN32__
|
||||
#include <SDL_syswm.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <cstring>
|
||||
#endif
|
||||
|
||||
|
||||
#include "util/mINI.h"
|
||||
#include "eventthread.h"
|
||||
#include "system/fake-api.h"
|
||||
#include "filesystem/filesystem.h"
|
||||
#include "input/input.h"
|
||||
#include "system/system.h"
|
||||
#include "sharedstate.h"
|
||||
|
||||
namespace ini = mINI;
|
||||
|
||||
// Essentials, without edits, needs Win32API. Two problems with that:
|
||||
|
||||
// 1. Not all Win32API functions work here, and
|
||||
// 2. It uses Windows libraries.
|
||||
|
||||
// So, even on Windows, we need to switch out some of those functions
|
||||
// with our own in order to trick the game into doing what we want.
|
||||
|
||||
// On Windows, we just have to worry about functions that involve
|
||||
// thread IDs and window handles mostly. On anything else, every
|
||||
// function that an Essentials game uses has to be impersonated.
|
||||
|
||||
// If you're lucky enough to be using a crossplatform library, you
|
||||
// could just load that since MiniFFI should work anywhere, but
|
||||
// every single system or windows-specific function you use has
|
||||
// to be intercepted.
|
||||
|
||||
// This would also apply if you were trying to load a macOS .dylib
|
||||
// from Windows or a linux .so from macOS. It's just not gonna work,
|
||||
// chief.
|
||||
|
||||
// There's probably not much reason to change this outside of
|
||||
// improving compatibility with games in general, since if you just
|
||||
// want to fix one specific game and you're smart enough to add to
|
||||
// this then you're also smart enough to either use MiniFFI on a
|
||||
// library made for your platform or, better yet, just write the
|
||||
// functionality into MKXP and don't use MiniFFI/Win32API at all.
|
||||
|
||||
// A lot of functions here will probably be bound directly to Ruby
|
||||
// eventually (so that you can just call Graphics.screenshot or
|
||||
// something instead of having to make a messy Win32API call
|
||||
// that *now* has to run entirely different function to even work)
|
||||
|
||||
PREFABI DWORD MKXP_GetCurrentThreadId(void) NOP_VAL(DUMMY_VAL)
|
||||
|
||||
PREFABI DWORD
|
||||
MKXP_GetWindowThreadProcessId(HWND hWnd, LPDWORD lpdwProcessId)
|
||||
NOP_VAL(DUMMY_VAL)
|
||||
|
||||
PREFABI HWND MKXP_FindWindowEx(HWND hWnd, HWND hWndChildAfter,
|
||||
LPCSTR lpszClass, LPCSTR lpszWindow)
|
||||
#ifdef __WIN32__
|
||||
{
|
||||
SDL_SysWMinfo wm;
|
||||
SDL_GetWindowWMInfo(shState->sdlWindow(), &wm);
|
||||
return wm.info.win.window;
|
||||
}
|
||||
#else
|
||||
NOP_VAL((HWND)DUMMY_VAL)
|
||||
#endif
|
||||
|
||||
PREFABI DWORD MKXP_GetForegroundWindow(void) {
|
||||
if (SDL_GetWindowFlags(shState->sdlWindow()) & SDL_WINDOW_INPUT_FOCUS) {
|
||||
return DUMMY_VAL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_GetClientRect(HWND hWnd, LPRECT lpRect) {
|
||||
SDL_GetWindowSize(shState->sdlWindow(), (int *)&lpRect->right,
|
||||
(int *)&lpRect->bottom);
|
||||
return true;
|
||||
}
|
||||
|
||||
// You would think that you could just call GetCursorPos
|
||||
// and ScreenToClient with the window handle and lppoint
|
||||
// and be fine, but nope
|
||||
PREFABI BOOL MKXP_GetCursorPos(LPPOINT lpPoint) {
|
||||
SDL_GetGlobalMouseState((int *)&lpPoint->x, (int *)&lpPoint->y);
|
||||
return true;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_ScreenToClient(HWND hWnd, LPPOINT lpPoint) {
|
||||
lpPoint->x = shState->input().mouseX();
|
||||
lpPoint->y = shState->input().mouseY();
|
||||
return true;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y,
|
||||
int cx, int cy, UINT uFlags) {
|
||||
// The game calls resize_screen which will automatically
|
||||
// ... well, resize the screen
|
||||
// shState->eThread().requestWindowResize(cx, cy);
|
||||
|
||||
shState->eThread().requestWindowReposition(X, Y);
|
||||
return true;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_SetWindowTextA(HWND hWnd, LPCSTR lpString) {
|
||||
shState->eThread().requestWindowRename((const char *)lpString);
|
||||
return true;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_GetWindowRect(HWND hWnd, LPRECT lpRect) {
|
||||
int cur_x, cur_y, cur_w, cur_h;
|
||||
SDL_GetWindowPosition(shState->sdlWindow(), &cur_x, &cur_y);
|
||||
SDL_GetWindowSize(shState->sdlWindow(), &cur_w, &cur_h);
|
||||
lpRect->left = cur_x;
|
||||
lpRect->right = cur_x + cur_w + 1;
|
||||
lpRect->top = cur_y;
|
||||
lpRect->bottom = cur_y + cur_h + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Shift key with GetKeyboardState doesn't work for whatever reason,
|
||||
// so Windows needs this too
|
||||
#define ks(sc) shState->eThread().keyStates[SDL_SCANCODE_##sc]
|
||||
PREFABI BOOL MKXP_GetKeyboardState(PBYTE lpKeyState) {
|
||||
#ifdef __WIN32__
|
||||
bool rc = GetKeyboardState(lpKeyState);
|
||||
if (rc) {
|
||||
lpKeyState[VK_LSHIFT] = ks(LSHIFT) << 7;
|
||||
lpKeyState[VK_RSHIFT] = ks(RSHIFT) << 7;
|
||||
lpKeyState[VK_SHIFT] =
|
||||
(lpKeyState[VK_LSHIFT] || lpKeyState[VK_RSHIFT]) ? 0x80 : 0;
|
||||
}
|
||||
return rc;
|
||||
#else
|
||||
for (int i = 254; i > 0; i--) {
|
||||
lpKeyState[i] = (MKXP_GetAsyncKeyState(i)) ? 0x80 : 0;
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
// =========================================
|
||||
// macOS / Linux only stuff starts here
|
||||
// =========================================
|
||||
|
||||
#ifndef __WIN32__
|
||||
|
||||
PREFABI VOID MKXP_RtlMoveMemory(VOID *Destination, VOID *Source,
|
||||
SIZE_T Length) {
|
||||
// I have no idea if this is a good idea or not
|
||||
// or why it's even necessary in the first place,
|
||||
// getting rid of this is priority #1
|
||||
memcpy(Destination, Source, Length);
|
||||
}
|
||||
|
||||
// I don't know who's more crazy, them for writing this stuff
|
||||
// or me for being willing to put it here.
|
||||
|
||||
// Probably me.
|
||||
|
||||
PREFABI HMODULE MKXP_LoadLibrary(LPCSTR lpLibFileName) {
|
||||
return SDL_LoadObject(lpLibFileName);
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_FreeLibrary(HMODULE hLibModule) {
|
||||
SDL_UnloadObject(hLibModule);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Luckily, Essentials only cares about the high-order bit,
|
||||
// so SDL's keystates will work perfectly fine
|
||||
PREFABI SHORT MKXP_GetAsyncKeyState(int vKey) {
|
||||
SHORT result;
|
||||
switch (vKey) {
|
||||
case 0x10: // Any Shift
|
||||
result = (ks(LSHIFT) || ks(RSHIFT)) ? 0x8000 : 0;
|
||||
break;
|
||||
|
||||
case 0x11: // Any Ctrl
|
||||
result = (ks(LCTRL) || ks(RCTRL)) ? 0x8000 : 0;
|
||||
break;
|
||||
|
||||
case 0x12: // Any Alt
|
||||
result = (ks(LALT) || ks(RALT)) ? 0x8000 : 0;
|
||||
break;
|
||||
|
||||
case 0x1: // Mouse button 1
|
||||
result = (SDL_GetMouseState(0, 0) & SDL_BUTTON(1)) ? 0x8000 : 0;
|
||||
break;
|
||||
|
||||
case 0x2: // Mouse button 2
|
||||
result = (SDL_GetMouseState(0, 0) & SDL_BUTTON(3)) ? 0x8000 : 0;
|
||||
break;
|
||||
|
||||
case 0x4: // Middle mouse
|
||||
result = (SDL_GetMouseState(0, 0) & SDL_BUTTON(2)) ? 0x8000 : 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
try {
|
||||
// Use EventThread instead of Input because
|
||||
// Input.update typically gets overridden
|
||||
result = shState->eThread().keyStates[vKeyToScancode[vKey]] << 15;
|
||||
} catch (...) {
|
||||
result = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#undef ks
|
||||
|
||||
PREFABI BOOL
|
||||
MKXP_GetSystemPowerStatus(LPSYSTEM_POWER_STATUS lpSystemPowerStatus) {
|
||||
int seconds, percent;
|
||||
SDL_PowerState ps;
|
||||
ps = SDL_GetPowerInfo(&seconds, &percent);
|
||||
|
||||
// Setting ACLineStatus
|
||||
if (ps == SDL_POWERSTATE_UNKNOWN) {
|
||||
lpSystemPowerStatus->ACLineStatus = 0xFF;
|
||||
} else {
|
||||
lpSystemPowerStatus->ACLineStatus =
|
||||
(ps == SDL_POWERSTATE_ON_BATTERY) ? 1 : 0;
|
||||
}
|
||||
|
||||
// Setting BatteryFlag
|
||||
if (ps == SDL_POWERSTATE_ON_BATTERY) {
|
||||
if (percent == -1) {
|
||||
lpSystemPowerStatus->BatteryFlag = 0xFF;
|
||||
} else if (percent >= 33) {
|
||||
lpSystemPowerStatus->BatteryFlag = 1;
|
||||
} else if (4 < percent && percent < 33) {
|
||||
lpSystemPowerStatus->BatteryFlag = 2;
|
||||
} else {
|
||||
lpSystemPowerStatus->BatteryFlag = 4;
|
||||
}
|
||||
} else if (ps == SDL_POWERSTATE_CHARGING) {
|
||||
lpSystemPowerStatus->BatteryFlag = 8;
|
||||
} else if (ps == SDL_POWERSTATE_NO_BATTERY) {
|
||||
lpSystemPowerStatus->BatteryFlag = (BYTE)128;
|
||||
} else {
|
||||
lpSystemPowerStatus->BatteryFlag = 0xFF;
|
||||
}
|
||||
|
||||
// Setting BatteryLifePercent
|
||||
lpSystemPowerStatus->BatteryLifePercent =
|
||||
(percent != -1) ? (BYTE)percent : 0xFF;
|
||||
|
||||
// Setting SystemStatusFlag
|
||||
lpSystemPowerStatus->SystemStatusFlag = 0;
|
||||
|
||||
// Setting BatteryLifeTime
|
||||
lpSystemPowerStatus->BatteryLifeTime = seconds;
|
||||
|
||||
// Setting BatteryFullLifeTime
|
||||
lpSystemPowerStatus->BatteryFullLifeTime = -1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_ShowWindow(HWND hWnd, int nCmdShow) NOP_VAL(true);
|
||||
|
||||
// This only currently supports getting screen width/height
|
||||
// Not really motivated to do the other ones when I'll be
|
||||
// extending Ruby at a later time anyway
|
||||
PREFABI int MKXP_GetSystemMetrics(int nIndex) {
|
||||
SDL_DisplayMode dm = {0};
|
||||
int rc = SDL_GetDesktopDisplayMode(
|
||||
SDL_GetWindowDisplayIndex(shState->sdlWindow()), &dm);
|
||||
if (!rc) {
|
||||
switch (nIndex) {
|
||||
case 0:
|
||||
return dm.w;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
return dm.h;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
PREFABI HWND MKXP_SetCapture(HWND hWnd) NOP_VAL((HWND)DUMMY_VAL);
|
||||
|
||||
PREFABI BOOL MKXP_ReleaseCapture(void) NOP_VAL(true);
|
||||
|
||||
PREFABI int MKXP_ShowCursor(BOOL bShow) NOP_VAL(DUMMY_VAL);
|
||||
|
||||
PREFABI DWORD MKXP_GetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName,
|
||||
LPCTSTR lpDefault,
|
||||
LPTSTR lpReturnedString, DWORD nSize,
|
||||
LPCTSTR lpFileName) {
|
||||
if (mkxp_fs::fileExists(lpFileName)) {
|
||||
ini::INIStructure data;
|
||||
|
||||
if (ini::INIFile(lpFileName).read(data)) {
|
||||
if (data.has(lpAppName) && data[lpAppName].has(lpKeyName))
|
||||
strncpy(lpReturnedString, data[lpAppName][lpKeyName].c_str(), nSize);
|
||||
} else {
|
||||
strncpy(lpReturnedString, lpDefault, nSize);
|
||||
}
|
||||
}
|
||||
|
||||
return strlen(lpReturnedString);
|
||||
}
|
||||
|
||||
// Does not handle sublanguages, only returns a few
|
||||
// common languages
|
||||
|
||||
// Use MKXP.user_language instead
|
||||
|
||||
PREFABI short // I know it's a LANGID but I don't care
|
||||
MKXP_GetUserDefaultLangID(void) {
|
||||
char buf[50];
|
||||
strncpy(buf, mkxp_sys::getSystemLanguage().c_str(), sizeof(buf));
|
||||
|
||||
for (int i = 0; i < strlen(buf); i++) {
|
||||
if (buf[i] == '_') {
|
||||
buf[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#define MATCH(l, c) \
|
||||
if (!strcmp(l, buf)) \
|
||||
return (c & 0x3ff);
|
||||
MATCH("ja", 0x11);
|
||||
MATCH("en", 0x09);
|
||||
MATCH("fr", 0x0c);
|
||||
MATCH("it", 0x10);
|
||||
MATCH("de", 0x07);
|
||||
MATCH("es", 0x0a);
|
||||
MATCH("ko", 0x12);
|
||||
MATCH("pt", 0x16);
|
||||
MATCH("zh", 0x04);
|
||||
#undef MATCH
|
||||
return 0x09;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_GetUserName(LPSTR lpBuffer, LPDWORD pcbBuffer) {
|
||||
if (*pcbBuffer < 1)
|
||||
return false;
|
||||
strncpy(lpBuffer, mkxp_sys::getUserName().c_str(), *pcbBuffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
PREFABI BOOL MKXP_RegisterHotKey(HWND hWnd, int id, UINT fsModifiers, UINT vk)
|
||||
NOP_VAL(true);
|
||||
|
||||
PREFABI LONG MKXP_SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong) {
|
||||
if (nIndex == -16) {
|
||||
if (dwNewLong == 0) {
|
||||
shState->eThread().requestFullscreenMode(true);
|
||||
} else if (dwNewLong == 0x14ca0000) {
|
||||
shState->eThread().requestFullscreenMode(false);
|
||||
}
|
||||
}
|
||||
return DUMMY_VAL;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -1,130 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#define ABI(x) __attribute__((x))
|
||||
#if defined(__WIN32__)
|
||||
|
||||
// Before, it needed to be ensured that functions called from
|
||||
// MiniFFI/Win32API were using stdcall as a calling convention,
|
||||
// but now that the stack pointer is automatically reset after
|
||||
// getting the result, cdecl should work just fine too
|
||||
|
||||
// ... Oh, and by the way, Windows was a mistake
|
||||
|
||||
// #define PREFABI ABI(stdcall)
|
||||
#define PREFABI
|
||||
#else
|
||||
#define PREFABI
|
||||
#endif
|
||||
|
||||
#ifdef MKXPZ_ESSENTIALS_DEBUG
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __WIN32__
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#ifndef __WIN32__
|
||||
typedef unsigned int DWORD, UINT, *LPDWORD;
|
||||
typedef char BYTE, *LPSTR, *LPCSTR, *LPCTSTR, *LPTSTR, *PBYTE;
|
||||
typedef short SHORT;
|
||||
typedef int LONG;
|
||||
|
||||
#ifdef __APPLE__
|
||||
typedef signed char BOOL;
|
||||
#else
|
||||
typedef bool BOOL;
|
||||
#endif
|
||||
|
||||
typedef void VOID, *LPVOID, *HANDLE, *HMODULE, *HWND;
|
||||
typedef size_t SIZE_T;
|
||||
|
||||
typedef struct {
|
||||
LONG x;
|
||||
LONG y;
|
||||
} POINT, *LPPOINT;
|
||||
|
||||
typedef struct {
|
||||
LONG left;
|
||||
LONG top;
|
||||
LONG right;
|
||||
LONG bottom;
|
||||
} RECT, *PRECT, *NPRECT, *LPRECT;
|
||||
|
||||
typedef struct {
|
||||
BYTE ACLineStatus;
|
||||
BYTE BatteryFlag;
|
||||
BYTE BatteryLifePercent;
|
||||
BYTE SystemStatusFlag;
|
||||
DWORD BatteryLifeTime;
|
||||
DWORD BatteryFullLifeTime;
|
||||
} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;
|
||||
#endif
|
||||
|
||||
#define DUMMY_VAL 571
|
||||
#define NOP \
|
||||
{ return; }
|
||||
#define NOP_VAL(x) \
|
||||
{ return x; }
|
||||
|
||||
PREFABI DWORD MKXP_GetCurrentThreadId(void);
|
||||
|
||||
PREFABI DWORD MKXP_GetWindowThreadProcessId(HWND hWnd, LPDWORD lpdwProcessId);
|
||||
|
||||
PREFABI HWND MKXP_FindWindowEx(HWND hWnd, HWND hWndChildAfter, LPCSTR lpszClass,
|
||||
LPCSTR lpszWindow);
|
||||
|
||||
PREFABI DWORD MKXP_GetForegroundWindow(void);
|
||||
|
||||
PREFABI BOOL MKXP_GetClientRect(HWND hWnd, LPRECT lpRect);
|
||||
|
||||
PREFABI BOOL MKXP_GetCursorPos(LPPOINT lpPoint);
|
||||
|
||||
PREFABI BOOL MKXP_ScreenToClient(HWND hWnd, LPPOINT lpPoint);
|
||||
|
||||
PREFABI BOOL MKXP_SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y,
|
||||
int cx, int cy, UINT uFlags);
|
||||
|
||||
PREFABI BOOL MKXP_SetWindowTextA(HWND hWnd, LPCSTR lpString);
|
||||
|
||||
PREFABI BOOL MKXP_GetWindowRect(HWND hWnd, LPRECT lpRect);
|
||||
|
||||
PREFABI BOOL MKXP_GetKeyboardState(PBYTE lpKeyState);
|
||||
|
||||
#ifndef __WIN32__
|
||||
|
||||
PREFABI VOID MKXP_RtlMoveMemory(VOID *Destination, VOID *Source, SIZE_T Length);
|
||||
|
||||
PREFABI HMODULE MKXP_LoadLibrary(LPCSTR lpLibFileName);
|
||||
|
||||
PREFABI BOOL MKXP_FreeLibrary(HMODULE hLibModule);
|
||||
|
||||
PREFABI SHORT MKXP_GetAsyncKeyState(int vKey);
|
||||
|
||||
PREFABI BOOL
|
||||
MKXP_GetSystemPowerStatus(LPSYSTEM_POWER_STATUS lpSystemPowerStatus);
|
||||
|
||||
PREFABI BOOL MKXP_ShowWindow(HWND hWnd, int nCmdShow);
|
||||
|
||||
PREFABI int MKXP_GetSystemMetrics(int nIndex);
|
||||
|
||||
PREFABI HWND MKXP_SetCapture(HWND hWnd);
|
||||
|
||||
PREFABI BOOL MKXP_ReleaseCapture(void);
|
||||
|
||||
PREFABI int MKXP_ShowCursor(BOOL bShow);
|
||||
|
||||
PREFABI DWORD MKXP_GetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName,
|
||||
LPCTSTR lpDefault,
|
||||
LPTSTR lpReturnedString, DWORD nSize,
|
||||
LPCTSTR lpFileName);
|
||||
|
||||
PREFABI short MKXP_GetUserDefaultLangID(void);
|
||||
|
||||
PREFABI BOOL MKXP_GetUserName(LPSTR lpBuffer, LPDWORD pcbBuffer);
|
||||
|
||||
PREFABI BOOL MKXP_RegisterHotKey(HWND hWnd, int id, UINT fsModifiers, UINT vk);
|
||||
|
||||
PREFABI LONG MKXP_SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong);
|
||||
|
||||
#endif
|
||||
#endif
|
758
src/util/mINI.h
758
src/util/mINI.h
|
@ -1,758 +0,0 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
* Copyright (c) 2018 Danijel Durakovic
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is furnished to do
|
||||
* so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// /mINI/ v0.9.7
|
||||
// An INI file reader and writer for the modern age.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// A tiny utility library for manipulating INI files with a straightforward
|
||||
// API and a minimal footprint. It conforms to the (somewhat) standard INI
|
||||
// format - sections and keys are case insensitive and all leading and
|
||||
// trailing whitespace is ignored. Comments are lines that begin with a
|
||||
// semicolon. Trailing comments are allowed on section lines.
|
||||
//
|
||||
// Files are read on demand, upon which data is kept in memory and the file
|
||||
// is closed. This utility supports lazy writing, which only writes changes
|
||||
// and updates to a file and preserves custom formatting and comments. A lazy
|
||||
// write invoked by a write() call will read the output file, find what
|
||||
// changes have been made and update the file accordingly. If you only need to
|
||||
// generate files, use generate() instead. Section and key order is preserved
|
||||
// on read, write and insert.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// /* BASIC USAGE EXAMPLE: */
|
||||
//
|
||||
// /* read from file */
|
||||
// mINI::INIFile file("myfile.ini");
|
||||
// mINI::INIStructure ini;
|
||||
// file.read(ini);
|
||||
//
|
||||
// /* read value; gets a reference to actual value in the structure.
|
||||
// if key or section don't exist, a new empty value will be created */
|
||||
// std::string& value = ini["section"]["key"];
|
||||
//
|
||||
// /* read value safely; gets a copy of value in the structure.
|
||||
// does not alter the structure */
|
||||
// std::string value = ini.get("section").get("key");
|
||||
//
|
||||
// /* set or update values */
|
||||
// ini["section"]["key"] = "value";
|
||||
//
|
||||
// /* set multiple values */
|
||||
// ini["section2"].set({
|
||||
// {"key1", "value1"},
|
||||
// {"key2", "value2"}
|
||||
// });
|
||||
//
|
||||
// /* write updates back to file, preserving comments and formatting */
|
||||
// file.write(ini);
|
||||
//
|
||||
// /* or generate a file (overwrites the original) */
|
||||
// file.generate(ini);
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Long live the INI file!!!
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef MINI_INI_H_
|
||||
#define MINI_INI_H_
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace mINI
|
||||
{
|
||||
namespace INIStringUtil
|
||||
{
|
||||
const std::string whitespaceDelimiters = " \t\n\r\f\v";
|
||||
inline void trim(std::string& str)
|
||||
{
|
||||
str.erase(str.find_last_not_of(whitespaceDelimiters) + 1);
|
||||
str.erase(0, str.find_first_not_of(whitespaceDelimiters));
|
||||
}
|
||||
#ifndef MINI_CASE_SENSITIVE
|
||||
inline void toLower(std::string& str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
||||
}
|
||||
#endif
|
||||
inline void replace(std::string& str, std::string const& a, std::string const& b)
|
||||
{
|
||||
if (!a.empty())
|
||||
{
|
||||
std::size_t pos = 0;
|
||||
while ((pos = str.find(a, pos)) != std::string::npos)
|
||||
{
|
||||
str.replace(pos, a.size(), b);
|
||||
pos += b.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef _WIN32
|
||||
const std::string endl = "\r\n";
|
||||
#else
|
||||
const std::string endl = "\n";
|
||||
#endif
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class INIMap
|
||||
{
|
||||
private:
|
||||
using T_DataIndexMap = std::unordered_map<std::string, std::size_t>;
|
||||
using T_DataItem = std::pair<std::string, T>;
|
||||
using T_DataContainer = std::vector<T_DataItem>;
|
||||
using T_MultiArgs = typename std::vector<std::pair<std::string, T>>;
|
||||
|
||||
T_DataIndexMap dataIndexMap;
|
||||
T_DataContainer data;
|
||||
|
||||
inline std::size_t setEmpty(std::string& key)
|
||||
{
|
||||
std::size_t index = data.size();
|
||||
dataIndexMap[key] = index;
|
||||
data.emplace_back(key, T());
|
||||
return index;
|
||||
}
|
||||
|
||||
public:
|
||||
using const_iterator = typename T_DataContainer::const_iterator;
|
||||
|
||||
INIMap() { }
|
||||
|
||||
INIMap(INIMap const& other)
|
||||
{
|
||||
std::size_t data_size = other.data.size();
|
||||
for (std::size_t i = 0; i < data_size; ++i)
|
||||
{
|
||||
auto const& key = other.data[i].first;
|
||||
auto const& obj = other.data[i].second;
|
||||
data.emplace_back(key, obj);
|
||||
}
|
||||
dataIndexMap = T_DataIndexMap(other.dataIndexMap);
|
||||
}
|
||||
|
||||
T& operator[](std::string key)
|
||||
{
|
||||
INIStringUtil::trim(key);
|
||||
#ifndef MINI_CASE_SENSITIVE
|
||||
INIStringUtil::toLower(key);
|
||||
#endif
|
||||
auto it = dataIndexMap.find(key);
|
||||
bool hasIt = (it != dataIndexMap.end());
|
||||
std::size_t index = (hasIt) ? it->second : setEmpty(key);
|
||||
return data[index].second;
|
||||
}
|
||||
T get(std::string key) const
|
||||
{
|
||||
INIStringUtil::trim(key);
|
||||
#ifndef MINI_CASE_SENSITIVE
|
||||
INIStringUtil::toLower(key);
|
||||
#endif
|
||||
auto it = dataIndexMap.find(key);
|
||||
if (it == dataIndexMap.end())
|
||||
{
|
||||
return T();
|
||||
}
|
||||
return T(data[it->second].second);
|
||||
}
|
||||
bool has(std::string key) const
|
||||
{
|
||||
INIStringUtil::trim(key);
|
||||
#ifndef MINI_CASE_SENSITIVE
|
||||
INIStringUtil::toLower(key);
|
||||
#endif
|
||||
return (dataIndexMap.count(key) == 1);
|
||||
}
|
||||
void set(std::string key, T obj)
|
||||
{
|
||||
INIStringUtil::trim(key);
|
||||
#ifndef MINI_CASE_SENSITIVE
|
||||
INIStringUtil::toLower(key);
|
||||
#endif
|
||||
auto it = dataIndexMap.find(key);
|
||||
if (it != dataIndexMap.end())
|
||||
{
|
||||
data[it->second].second = obj;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataIndexMap[key] = data.size();
|
||||
data.emplace_back(key, obj);
|
||||
}
|
||||
}
|
||||
void set(T_MultiArgs const& multiArgs)
|
||||
{
|
||||
for (auto const& it : multiArgs)
|
||||
{
|
||||
auto const& key = it.first;
|
||||
auto const& obj = it.second;
|
||||
set(key, obj);
|
||||
}
|
||||
}
|
||||
bool remove(std::string key)
|
||||
{
|
||||
INIStringUtil::trim(key);
|
||||
#ifndef MINI_CASE_SENSITIVE
|
||||
INIStringUtil::toLower(key);
|
||||
#endif
|
||||
auto it = dataIndexMap.find(key);
|
||||
if (it != dataIndexMap.end())
|
||||
{
|
||||
std::size_t index = it->second;
|
||||
data.erase(data.begin() + index);
|
||||
dataIndexMap.erase(it);
|
||||
for (auto& it2 : dataIndexMap)
|
||||
{
|
||||
auto& vi = it2.second;
|
||||
if (vi > index)
|
||||
{
|
||||
vi--;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
data.clear();
|
||||
dataIndexMap.clear();
|
||||
}
|
||||
std::size_t size() const
|
||||
{
|
||||
return data.size();
|
||||
}
|
||||
const_iterator begin() const { return data.begin(); }
|
||||
const_iterator end() const { return data.end(); }
|
||||
};
|
||||
|
||||
using INIStructure = INIMap<INIMap<std::string>>;
|
||||
|
||||
namespace INIParser
|
||||
{
|
||||
using T_ParseValues = std::pair<std::string, std::string>;
|
||||
|
||||
enum class PDataType : char
|
||||
{
|
||||
PDATA_NONE,
|
||||
PDATA_COMMENT,
|
||||
PDATA_SECTION,
|
||||
PDATA_KEYVALUE,
|
||||
PDATA_UNKNOWN
|
||||
};
|
||||
|
||||
inline PDataType parseLine(std::string line, T_ParseValues& parseData)
|
||||
{
|
||||
parseData.first.clear();
|
||||
parseData.second.clear();
|
||||
INIStringUtil::trim(line);
|
||||
if (line.empty())
|
||||
{
|
||||
return PDataType::PDATA_NONE;
|
||||
}
|
||||
char firstCharacter = line[0];
|
||||
if (firstCharacter == ';')
|
||||
{
|
||||
return PDataType::PDATA_COMMENT;
|
||||
}
|
||||
if (firstCharacter == '[')
|
||||
{
|
||||
auto commentAt = line.find_first_of(';');
|
||||
if (commentAt != std::string::npos)
|
||||
{
|
||||
line = line.substr(0, commentAt);
|
||||
}
|
||||
auto closingBracketAt = line.find_last_of(']');
|
||||
if (closingBracketAt != std::string::npos)
|
||||
{
|
||||
auto section = line.substr(1, closingBracketAt - 1);
|
||||
INIStringUtil::trim(section);
|
||||
parseData.first = section;
|
||||
return PDataType::PDATA_SECTION;
|
||||
}
|
||||
}
|
||||
auto lineNorm = line;
|
||||
INIStringUtil::replace(lineNorm, "\\=", " ");
|
||||
auto equalsAt = lineNorm.find_first_of('=');
|
||||
if (equalsAt != std::string::npos)
|
||||
{
|
||||
auto key = line.substr(0, equalsAt);
|
||||
INIStringUtil::trim(key);
|
||||
INIStringUtil::replace(key, "\\=", "=");
|
||||
auto value = line.substr(equalsAt + 1);
|
||||
INIStringUtil::trim(value);
|
||||
parseData.first = key;
|
||||
parseData.second = value;
|
||||
return PDataType::PDATA_KEYVALUE;
|
||||
}
|
||||
return PDataType::PDATA_UNKNOWN;
|
||||
}
|
||||
};
|
||||
|
||||
class INIReader
|
||||
{
|
||||
public:
|
||||
using T_LineData = std::vector<std::string>;
|
||||
using T_LineDataPtr = std::shared_ptr<T_LineData>;
|
||||
|
||||
private:
|
||||
std::ifstream fileReadStream;
|
||||
T_LineDataPtr lineData;
|
||||
|
||||
T_LineData readFile()
|
||||
{
|
||||
std::string fileContents;
|
||||
fileReadStream.seekg(0, std::ios::end);
|
||||
fileContents.resize(fileReadStream.tellg());
|
||||
fileReadStream.seekg(0, std::ios::beg);
|
||||
std::size_t fileSize = fileContents.size();
|
||||
fileReadStream.read(&fileContents[0], fileSize);
|
||||
fileReadStream.close();
|
||||
T_LineData output;
|
||||
if (fileSize == 0)
|
||||
{
|
||||
return output;
|
||||
}
|
||||
std::string buffer;
|
||||
buffer.reserve(50);
|
||||
for (std::size_t i = 0; i < fileSize; ++i)
|
||||
{
|
||||
char& c = fileContents[i];
|
||||
if (c == '\n')
|
||||
{
|
||||
output.emplace_back(buffer);
|
||||
buffer.clear();
|
||||
continue;
|
||||
}
|
||||
if (c != '\0' && c != '\r')
|
||||
{
|
||||
buffer += c;
|
||||
}
|
||||
}
|
||||
output.emplace_back(buffer);
|
||||
return output;
|
||||
}
|
||||
|
||||
public:
|
||||
INIReader(std::string const& filename, bool keepLineData = false)
|
||||
{
|
||||
fileReadStream.open(filename, std::ios::in | std::ios::binary);
|
||||
if (keepLineData)
|
||||
{
|
||||
lineData = std::make_shared<T_LineData>();
|
||||
}
|
||||
}
|
||||
~INIReader() { }
|
||||
|
||||
bool operator>>(INIStructure& data)
|
||||
{
|
||||
if (!fileReadStream.is_open())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
T_LineData fileLines = readFile();
|
||||
std::string section;
|
||||
bool inSection = false;
|
||||
INIParser::T_ParseValues parseData;
|
||||
for (auto const& line : fileLines)
|
||||
{
|
||||
auto parseResult = INIParser::parseLine(line, parseData);
|
||||
if (parseResult == INIParser::PDataType::PDATA_SECTION)
|
||||
{
|
||||
inSection = true;
|
||||
data[section = parseData.first];
|
||||
}
|
||||
else if (inSection && parseResult == INIParser::PDataType::PDATA_KEYVALUE)
|
||||
{
|
||||
auto const& key = parseData.first;
|
||||
auto const& value = parseData.second;
|
||||
data[section][key] = value;
|
||||
}
|
||||
if (lineData && parseResult != INIParser::PDataType::PDATA_UNKNOWN)
|
||||
{
|
||||
if (parseResult == INIParser::PDataType::PDATA_KEYVALUE && !inSection)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
lineData->emplace_back(line);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
T_LineDataPtr getLines()
|
||||
{
|
||||
return lineData;
|
||||
}
|
||||
};
|
||||
|
||||
class INIGenerator
|
||||
{
|
||||
private:
|
||||
std::ofstream fileWriteStream;
|
||||
|
||||
public:
|
||||
bool prettyPrint = false;
|
||||
|
||||
INIGenerator(std::string const& filename)
|
||||
{
|
||||
fileWriteStream.open(filename, std::ios::out | std::ios::binary);
|
||||
}
|
||||
~INIGenerator() { }
|
||||
|
||||
bool operator<<(INIStructure const& data)
|
||||
{
|
||||
if (!fileWriteStream.is_open())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!data.size())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
auto it = data.begin();
|
||||
for (;;)
|
||||
{
|
||||
auto const& section = it->first;
|
||||
auto const& collection = it->second;
|
||||
fileWriteStream
|
||||
<< "["
|
||||
<< section
|
||||
<< "]";
|
||||
if (collection.size())
|
||||
{
|
||||
fileWriteStream << INIStringUtil::endl;
|
||||
auto it2 = collection.begin();
|
||||
for (;;)
|
||||
{
|
||||
auto key = it2->first;
|
||||
INIStringUtil::replace(key, "=", "\\=");
|
||||
auto value = it2->second;
|
||||
INIStringUtil::trim(value);
|
||||
fileWriteStream
|
||||
<< key
|
||||
<< ((prettyPrint) ? " = " : "=")
|
||||
<< value;
|
||||
if (++it2 == collection.end())
|
||||
{
|
||||
break;
|
||||
}
|
||||
fileWriteStream << INIStringUtil::endl;
|
||||
}
|
||||
}
|
||||
if (++it == data.end())
|
||||
{
|
||||
break;
|
||||
}
|
||||
fileWriteStream << INIStringUtil::endl;
|
||||
if (prettyPrint)
|
||||
{
|
||||
fileWriteStream << INIStringUtil::endl;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class INIWriter
|
||||
{
|
||||
private:
|
||||
using T_LineData = std::vector<std::string>;
|
||||
using T_LineDataPtr = std::shared_ptr<T_LineData>;
|
||||
|
||||
std::string filename;
|
||||
|
||||
T_LineData getLazyOutput(T_LineDataPtr const& lineData, INIStructure& data, INIStructure& original)
|
||||
{
|
||||
T_LineData output;
|
||||
INIParser::T_ParseValues parseData;
|
||||
std::string sectionCurrent;
|
||||
bool parsingSection = false;
|
||||
bool continueToNextSection = false;
|
||||
bool discardNextEmpty = false;
|
||||
bool writeNewKeys = false;
|
||||
std::size_t lastKeyLine = 0;
|
||||
for (auto line = lineData->begin(); line != lineData->end(); ++line)
|
||||
{
|
||||
if (!writeNewKeys)
|
||||
{
|
||||
auto parseResult = INIParser::parseLine(*line, parseData);
|
||||
if (parseResult == INIParser::PDataType::PDATA_SECTION)
|
||||
{
|
||||
if (parsingSection)
|
||||
{
|
||||
writeNewKeys = true;
|
||||
parsingSection = false;
|
||||
--line;
|
||||
continue;
|
||||
}
|
||||
sectionCurrent = parseData.first;
|
||||
if (data.has(sectionCurrent))
|
||||
{
|
||||
parsingSection = true;
|
||||
continueToNextSection = false;
|
||||
discardNextEmpty = false;
|
||||
output.emplace_back(*line);
|
||||
lastKeyLine = output.size();
|
||||
}
|
||||
else
|
||||
{
|
||||
continueToNextSection = true;
|
||||
discardNextEmpty = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (parseResult == INIParser::PDataType::PDATA_KEYVALUE)
|
||||
{
|
||||
if (continueToNextSection)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (data.has(sectionCurrent))
|
||||
{
|
||||
auto& collection = data[sectionCurrent];
|
||||
auto const& key = parseData.first;
|
||||
auto const& value = parseData.second;
|
||||
if (collection.has(key))
|
||||
{
|
||||
auto outputValue = collection[key];
|
||||
if (value == outputValue)
|
||||
{
|
||||
output.emplace_back(*line);
|
||||
}
|
||||
else
|
||||
{
|
||||
INIStringUtil::trim(outputValue);
|
||||
auto lineNorm = *line;
|
||||
INIStringUtil::replace(lineNorm, "\\=", " ");
|
||||
auto equalsAt = lineNorm.find_first_of('=');
|
||||
auto valueAt = lineNorm.find_first_not_of(
|
||||
INIStringUtil::whitespaceDelimiters,
|
||||
equalsAt + 1
|
||||
);
|
||||
std::string outputLine = line->substr(0, valueAt);
|
||||
if (prettyPrint && equalsAt + 1 == valueAt)
|
||||
{
|
||||
outputLine += " ";
|
||||
}
|
||||
outputLine += outputValue;
|
||||
output.emplace_back(outputLine);
|
||||
}
|
||||
lastKeyLine = output.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (discardNextEmpty && line->empty())
|
||||
{
|
||||
discardNextEmpty = false;
|
||||
}
|
||||
else if (parseResult != INIParser::PDataType::PDATA_UNKNOWN)
|
||||
{
|
||||
output.emplace_back(*line);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (writeNewKeys || std::next(line) == lineData->end())
|
||||
{
|
||||
T_LineData linesToAdd;
|
||||
if (data.has(sectionCurrent) && original.has(sectionCurrent))
|
||||
{
|
||||
auto const& collection = data[sectionCurrent];
|
||||
auto const& collectionOriginal = original[sectionCurrent];
|
||||
for (auto const& it : collection)
|
||||
{
|
||||
auto key = it.first;
|
||||
if (collectionOriginal.has(key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
auto value = it.second;
|
||||
INIStringUtil::replace(key, "=", "\\=");
|
||||
INIStringUtil::trim(value);
|
||||
linesToAdd.emplace_back(
|
||||
key + ((prettyPrint) ? " = " : "=") + value
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!linesToAdd.empty())
|
||||
{
|
||||
output.insert(
|
||||
output.begin() + lastKeyLine,
|
||||
linesToAdd.begin(),
|
||||
linesToAdd.end()
|
||||
);
|
||||
}
|
||||
if (writeNewKeys)
|
||||
{
|
||||
writeNewKeys = false;
|
||||
--line;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto const& it : data)
|
||||
{
|
||||
auto const& section = it.first;
|
||||
if (original.has(section))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (prettyPrint && output.size() > 0 && !output.back().empty())
|
||||
{
|
||||
output.emplace_back();
|
||||
}
|
||||
output.emplace_back("[" + section + "]");
|
||||
auto const& collection = it.second;
|
||||
for (auto const& it2 : collection)
|
||||
{
|
||||
auto key = it2.first;
|
||||
auto value = it2.second;
|
||||
INIStringUtil::replace(key, "=", "\\=");
|
||||
INIStringUtil::trim(value);
|
||||
output.emplace_back(
|
||||
key + ((prettyPrint) ? " = " : "=") + value
|
||||
);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public:
|
||||
bool prettyPrint = false;
|
||||
|
||||
INIWriter(std::string const& filename)
|
||||
: filename(filename)
|
||||
{
|
||||
}
|
||||
~INIWriter() { }
|
||||
|
||||
bool operator<<(INIStructure& data)
|
||||
{
|
||||
struct stat buf;
|
||||
bool fileExists = (stat(filename.c_str(), &buf) == 0);
|
||||
if (!fileExists)
|
||||
{
|
||||
INIGenerator generator(filename);
|
||||
generator.prettyPrint = prettyPrint;
|
||||
return generator << data;
|
||||
}
|
||||
INIStructure originalData;
|
||||
T_LineDataPtr lineData;
|
||||
bool readSuccess = false;
|
||||
{
|
||||
INIReader reader(filename, true);
|
||||
if ((readSuccess = reader >> originalData))
|
||||
{
|
||||
lineData = reader.getLines();
|
||||
}
|
||||
}
|
||||
if (!readSuccess)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
T_LineData output = getLazyOutput(lineData, data, originalData);
|
||||
std::ofstream fileWriteStream(filename, std::ios::out | std::ios::binary);
|
||||
if (fileWriteStream.is_open())
|
||||
{
|
||||
if (output.size())
|
||||
{
|
||||
auto line = output.begin();
|
||||
for (;;)
|
||||
{
|
||||
fileWriteStream << *line;
|
||||
if (++line == output.end())
|
||||
{
|
||||
break;
|
||||
}
|
||||
fileWriteStream << INIStringUtil::endl;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class INIFile
|
||||
{
|
||||
private:
|
||||
std::string filename;
|
||||
|
||||
public:
|
||||
INIFile(std::string const& filename)
|
||||
: filename(filename)
|
||||
{ }
|
||||
|
||||
~INIFile() { }
|
||||
|
||||
bool read(INIStructure& data) const
|
||||
{
|
||||
if (data.size())
|
||||
{
|
||||
data.clear();
|
||||
}
|
||||
if (filename.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
INIReader reader(filename);
|
||||
return reader >> data;
|
||||
}
|
||||
bool generate(INIStructure const& data, bool pretty = false) const
|
||||
{
|
||||
if (filename.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
INIGenerator generator(filename);
|
||||
generator.prettyPrint = pretty;
|
||||
return generator << data;
|
||||
}
|
||||
bool write(INIStructure& data, bool pretty = false) const
|
||||
{
|
||||
if (filename.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
INIWriter writer(filename);
|
||||
writer.prettyPrint = pretty;
|
||||
return writer << data;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MINI_INI_H_
|
Loading…
Add table
Reference in a new issue