Browse Source

win+mac: When counters file is c0rrupt, just rename that shit to '*.rip.<00nix time>' and move on (instead of quitting lol)

Wazakindjes 1 year ago
parent
commit
ada0b200bc

BIN
release/str0kem-mac.zip


BIN
release/str0kem-win.zip


+ 2 - 1
sauce/common/include/muhdefs.h

@@ -40,8 +40,9 @@
 #define JM_ERRMSG_TIMER_FLUSH "Unable to install flush timer"
 #define JM_ERRMSG_TIMER_WRITE "Unable to install writecounters timer"
 #define JM_ERRMSG_READCOUNTERS "Unable to read stored counters file, not proceeding"
-#define JM_ERRMSG_PARSECOUNTERS "Unable to parse stored counters file"
+#define JM_ERRMSG_PARSECOUNTERS "Unable to parse stored counters file, renaming and ignoring em"
 #define JM_ERRMSG_DELCOUNTERS "[WARNING] Unable to remove stored counters file after parsing"
+#define JM_ERRMSG_RENAMECOUNTERS "[WARNING] Unable to rename stored counters file after parsing"
 
 #define JM_MSG_INIT "Ayy successfully started up lol"
 

+ 27 - 14
sauce/mac/src/str0kemAppDelegate.m

@@ -22,11 +22,13 @@ NSFileManager *fileman;
 -(void)applicationDidFinishLaunching: (NSNotification *)aNotification {
 	// Initialise some shit lol
 	NSError *error;
-	NSString *errTitle;
+	NSString *errTitle, *errMsg;
 	NSString *bundleIdentifier;
 	NSFileHandle *fh;
 	NSData *rawJSON;
 	NSDictionary *countersJSON;
+	NSURL *countersCorrupt;
+	NSString *ripExtension;
 	int keypress, leftmouse, rightmouse;
 
 	// Let's default to using SSL lol
@@ -116,20 +118,31 @@ NSFileManager *fileman;
 			[fh closeFile];
 		}
 		countersJSON = [NSJSONSerialization JSONObjectWithData: rawJSON options: NSJSONReadingAllowFragments error: &error];
-		if(error)
-			[self fuckoff: @JM_ERRMSG_PARSECOUNTERS : [error localizedDescription]];
-		keypress = [[countersJSON valueForKeyPath: @JM_JSON_KEYPRESS] intValue];
-		leftmouse = [[countersJSON valueForKeyPath: @JM_JSON_LEFTMOUSE] intValue];
-		rightmouse = [[countersJSON valueForKeyPath: @JM_JSON_RIGHTMOUSE] intValue];
-		keypressCounter = [NSNumber numberWithInt: keypress + [keypressCounter intValue]];
-		leftmouseCounter = [NSNumber numberWithInt: leftmouse + [leftmouseCounter intValue]];
-		rightmouseCounter = [NSNumber numberWithInt: rightmouse + [rightmouseCounter intValue]];
-		[fileman removeItemAtPath: counterPath error: &error];
-		if(error)
-			[self alertem: @JM_ERRMSG_DELCOUNTERS : [error localizedDescription]]; // Reading succeeded but deleting didn't, just throw a warning imo
+		if(error) {
+			// Save error for later, try to rename first ;]
+			errMsg = [error localizedDescription];
+			error = NULL;
+			ripExtension = [NSString stringWithFormat: @"rip.%d", [[NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970]] intValue]];
+			countersCorrupt = [counterPathURL URLByAppendingPathExtension: ripExtension];
+			[fileman replaceItemAtURL: countersCorrupt withItemAtURL: counterPathURL backupItemName: NULL options: 0 resultingItemURL: NULL error: &error];
+			[self alertem: @JM_ERRMSG_PARSECOUNTERS : errMsg];
+			if(error) // Also throw a warning about renaming imo tbh lmao
+				[self alertem: @JM_ERRMSG_RENAMECOUNTERS : [error localizedDescription]];
+		}
+		else {
+			keypress = [[countersJSON valueForKeyPath: @JM_JSON_KEYPRESS] intValue];
+			leftmouse = [[countersJSON valueForKeyPath: @JM_JSON_LEFTMOUSE] intValue];
+			rightmouse = [[countersJSON valueForKeyPath: @JM_JSON_RIGHTMOUSE] intValue];
+			keypressCounter = [NSNumber numberWithInt: keypress + [keypressCounter intValue]];
+			leftmouseCounter = [NSNumber numberWithInt: leftmouse + [leftmouseCounter intValue]];
+			rightmouseCounter = [NSNumber numberWithInt: rightmouse + [rightmouseCounter intValue]];
+			[fileman removeItemAtPath: counterPath error: &error];
+			if(error)
+				[self alertem: @JM_ERRMSG_DELCOUNTERS : [error localizedDescription]]; // Reading succeeded but deleting didn't, just throw a warning imo
 
-		if(muhcfg.debug)
-			[muhapp loggem: [NSString stringWithFormat: @JM_DEBUG_FMT_READCOUNTERS(keypress, leftmouse, rightmouse)]];
+			if(muhcfg.debug)
+				[muhapp loggem: [NSString stringWithFormat: @JM_DEBUG_FMT_READCOUNTERS(keypress, leftmouse, rightmouse)]];
+		}
 	}
 
 	signal(SIGTERM, muhsighandler); // Normal kill signal, which is always sent to the pr0cess itself to handle shutdown requests properly

+ 1 - 1
sauce/mac/str0kem-Info.plist

@@ -17,7 +17,7 @@
 	<key>CFBundlePackageType</key>
 	<string>APPL</string>
 	<key>CFBundleShortVersionString</key>
-	<string>1.1.0.15</string>
+	<string>1.1.0.16</string>
 	<key>JMSQLVersion</key>
 	<string>6.1.11</string>
 	<key>LSApplicationCategoryType</key>

+ 44 - 24
sauce/win/src/str0kem.cpp

@@ -12,6 +12,7 @@
 #include <shellapi.h>
 #include <shlobj.h>
 #include <stdlib.h>
+#include <time.h>
 
 #include <C:/Program Files/MySQL/MySQL Connector C 6.1/include/mysql.h>
 
@@ -41,7 +42,6 @@ int WinMain(HINSTANCE hinst, HINSTANCE hprevinst, LPSTR cmdline, int showcmd) {
 	char rawJSON[2048];
 	picojson::value pv;
 	std::string serrJSON;
-	const char *errJSON;
 	picojson::value::object countersJSON;
 	picojson::value::object::const_iterator pvi;
 	const char *k;
@@ -50,6 +50,9 @@ int WinMain(HINSTANCE hinst, HINSTANCE hprevinst, LPSTR cmdline, int showcmd) {
 	HWND hwnd;
 	HICON muhicon;
 	WNDCLASSEX wc;
+	bool ripCounters;
+	char countersCorrupt[_MAX_PATH];
+	const char *errMsg;
 
 	// Attempt to attach to le console in case we runnin' from a cmd =]
 	if(AttachConsole(ATTACH_PARENT_PROCESS))
@@ -186,35 +189,52 @@ int WinMain(HINSTANCE hinst, HINSTANCE hprevinst, LPSTR cmdline, int showcmd) {
 			fuckoff(errTitle2, const_cast<char *>(JM_ERRMSG_FILE_READLEN));
 		rawJSON[rwcount] = '\0';
 
+		ripCounters = false;
+		errMsg = NULL;
 		if(!((serrJSON = picojson::parse(pv, rawJSON)).empty())) {
-			errJSON = serrJSON.c_str();
-			fuckoff(errTitle2, const_cast<char *>(errJSON));
+			errMsg = serrJSON.c_str();
+			ripCounters = true;
 		}
-		if(!pv.is<picojson::object>())
-			fuckoff(errTitle2, const_cast<char *>(JM_ERRMSG_JSON_NOOBJ));
-
-		countersJSON = pv.get<picojson::object>();
-		for(pvi = countersJSON.begin(); pvi != countersJSON.end(); pvi++) {
-			if(pvi->second.is<double>()) {
-				k = pvi->first.c_str();
-				if(!strcmp(k, JM_JSON_KEYPRESS))
-					keypressCounter += pvi->second.get<double>();
-				else if(!strcmp(k, JM_JSON_LEFTMOUSE))
-					leftmouseCounter += pvi->second.get<double>();
-				else if(!strcmp(k, JM_JSON_RIGHTMOUSE))
-					rightmouseCounter += pvi->second.get<double>();
-			}
+		else if(!pv.is<picojson::object>()) {
+			errMsg = JM_ERRMSG_JSON_NOOBJ;
+			ripCounters = true;
 		}
-		if(!DeleteFile(counterPath)) {
-			// Reading succeeded but deleting didn't, just throw a warning imo
+
+		if(ripCounters) {
+			// Save error for later, try to rename first ;]
+			snprintf(countersCorrupt, sizeof(countersCorrupt), "%s.rip.%lld", counterPath, time(NULL));
+			MoveFileEx(counterPath, countersCorrupt, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
 			error = GetLastError();
-			JM_GIBERRSTR(error, lpbuf);
-			alertem(errTitle2, lpbuf);
+			alertem(const_cast<char *>(JM_ERRMSG_PARSECOUNTERS), const_cast<char *>(errMsg));
+			if(error != ERROR_SUCCESS) { // Also throw a warning about renaming imo tbh lmao
+				JM_GIBERRSTR(error, lpbuf);
+				alertem(const_cast<char *>(JM_ERRMSG_RENAMECOUNTERS), lpbuf);
+			}
 		}
+		else {
+			countersJSON = pv.get<picojson::object>();
+			for(pvi = countersJSON.begin(); pvi != countersJSON.end(); pvi++) {
+				if(pvi->second.is<double>()) {
+					k = pvi->first.c_str();
+					if(!strcmp(k, JM_JSON_KEYPRESS))
+						keypressCounter += pvi->second.get<double>();
+					else if(!strcmp(k, JM_JSON_LEFTMOUSE))
+						leftmouseCounter += pvi->second.get<double>();
+					else if(!strcmp(k, JM_JSON_RIGHTMOUSE))
+						rightmouseCounter += pvi->second.get<double>();
+				}
+			}
+			if(!DeleteFile(counterPath)) {
+				// Reading succeeded but deleting didn't, just throw a warning imo
+				error = GetLastError();
+				JM_GIBERRSTR(error, lpbuf);
+				alertem(errTitle2, lpbuf);
+			}
 
-		if(muhcfg.debug) {
-			snprintf(buf, sizeof(buf), JM_DEBUG_FMT_READCOUNTERS(keypressCounter, leftmouseCounter, rightmouseCounter));
-			loggem(buf);
+			if(muhcfg.debug) {
+				snprintf(buf, sizeof(buf), JM_DEBUG_FMT_READCOUNTERS(keypressCounter, leftmouseCounter, rightmouseCounter));
+				loggem(buf);
+			}
 		}
 	}
 

+ 4 - 4
sauce/win/str0kem.rc

@@ -70,8 +70,8 @@ IDR_MYSQLDLL            DLL MOVEABLE PURE       "lib\\libmysql.dll"
 //
 
 VS_VERSION_INFO VERSIONINFO
- FILEVERSION 1,1,0,35
- PRODUCTVERSION 1,1,0,35
+ FILEVERSION 1,1,0,36
+ PRODUCTVERSION 1,1,0,36
  FILEFLAGSMASK 0x3fL
 #ifdef _DEBUG
  FILEFLAGS 0x1L
@@ -87,11 +87,11 @@ BEGIN
         BLOCK "040904b0"
         BEGIN
             VALUE "FileDescription", "str0kem"
-            VALUE "FileVersion", "1.1.0.35"
+            VALUE "FileVersion", "1.1.0.36"
             VALUE "InternalName", "str0kem"
             VALUE "LegalCopyright", "Copyright © 2019 Wazakindjes"
             VALUE "ProductName", "str0kem"
-            VALUE "ProductVersion", "1.1.0.35"
+            VALUE "ProductVersion", "1.1.0.36"
         END
     END
     BLOCK "VarFileInfo"