View Single Post
eyal282
Veteran Member
Join Date: Aug 2011
Old 04-02-2019 , 08:34   Re: [CSGO] IsCookieTrue !!
Reply With Quote #4

I'm not done with these functions and they aren't working properly. I didn't use any syntax highlighting or anything so it might look terrible. I'll fix this NON WORKING CODE later, but the only issue I can see right now is the fact that overwriting a value doesn't do anything at all.

Code:
#include <sourcemod>

new const String:ServerValuesPath[] = "data/ServerValues.txt"

public OnPluginStart()
{
	StoreServerValue("test", "thr", false);
	new bool:tr1 = StoreServerValue("bruh", "moment", false);
	StoreServerValue("tester", "hgr", false);
	new bool:tr2 = StoreServerValue("bruh", "momentnum2", true);
	new bool:fls1 = StoreServerValue("bruh", "mome", false);
	
	new String:Buffer[128];
	ReadServerValue("bruh", Buffer, sizeof(Buffer));
	
	LogAction(-1, -1, "%i %i %i - [momentnum2] %s", tr1, tr2, fls1, Buffer);
}

// returns true if successfully stored, false if value already exists.
stock bool:StoreServerValue(const String:Key[], const String:Value[], bool:replace=true)
{
	LogAction(-1, -1, "nnn");
	new String:FilePath[PLATFORM_MAX_PATH];
	
	BuildPath(Path_SM, FilePath, sizeof(FilePath), ServerValuesPath);
	
	if(!FileExists(FilePath))
		CreateEmptyFile(FilePath);
	
	//RemoveLines(FilePath);
	
	new Handle:hFile = OpenFile(FilePath, "r+");
	
	new String:LineBuffer[256], String:KeyBuffer[128];
	while(!IsEndOfFile(hFile))
	{
		ReadFileLine(hFile, LineBuffer, sizeof(LineBuffer));
		
		BreakString(LineBuffer, KeyBuffer, sizeof(KeyBuffer));
		
		if(StrEqual(Key, KeyBuffer, true))
		{
			if(!replace)
			{
				LogAction(-1, -1, "a");
				CloseHandle(hFile);
				return false;
			}
			
			LogAction(-1, -1, "b");
			// We don't need LineBuffer anymore, we can use it to replace the data.
			Format(LineBuffer, sizeof(LineBuffer), "\"%s\" \"%s\"", Key, Value);
			WriteFileLine(hFile, LineBuffer);
			
			CloseHandle(hFile);
			return true;
		}
	}

	LogAction(-1, -1, "c");
	Format(LineBuffer, sizeof(LineBuffer), "\"%s\" \"%s\"", Key, Value);
	WriteFileLine(hFile, LineBuffer);	
	
	CloseHandle(hFile);
	
	return true;
}

// Removes lines that aren't in two arguments ( "key" "value" ) and empty lines.

// returns amount of lines deleted.
stock RemoveLines(const String:Path[])
{
	new count = 0;
	
	new Handle:hFile = OpenFile(Path, "r");
	
	new String:TempFilePath[PLATFORM_MAX_PATH];
	Format(TempFilePath, sizeof(TempFilePath), Path);
	
	new pathlen = strlen(TempFilePath);
	
	while(TempFilePath[pathlen] != '/' && TempFilePath[pathlen] != '\\')
		pathlen--;
	
	TempFilePath[pathlen+1] = EOS;
	Format(TempFilePath, sizeof(TempFilePath), "%slineremoval_temp.txt", TempFilePath);
	
	// If it's called temp, I'm legally allowed to delete it :D
	DeleteFile(TempFilePath);
	
	new Handle:hTempFile = OpenFile(TempFilePath, "a+");
	
	new String:LineBuffer[256];
	while(!IsEndOfFile(hFile))
	{
		ReadFileLine(hFile, LineBuffer, sizeof(LineBuffer));
		
		if(LineBuffer[0] == EOS)
		{
			count++;
			continue;
		}	
		else if(GetStringArgumentCount(LineBuffer, sizeof(LineBuffer)) != 2)
		{
			count++;
			continue;
		}	
		
		WriteFileLine(hTempFile, LineBuffer);
	}
	
	CloseHandle(hFile);
	CloseHandle(hTempFile);
	DeleteFile(Path);
	RenameFile(Path, TempFilePath);
	
	return count;
}

stock GetStringArgumentCount(const String:source[], length)
{
	new String:dummy_str[length];
	new count = 0, arglen;
	
	while((arglen = BreakString(source[arglen], dummy_str, length)) != -1)
		count++;
		
	if(count > 0)
		count++; // It always rushes one count ahead as it gives index to next argument, if a string has two arguments, only one arglen will be taken in a loop.
		
	return count;
}

// returns true if found, false otherwise.

stock bool:ReadServerValue(const String:Key[], String:buffer[], bufferlen)
{
	new String:FilePath[PLATFORM_MAX_PATH];
	
	BuildPath(Path_SM, FilePath, sizeof(FilePath), ServerValuesPath);
	
	if(!FileExists(FilePath))
		return false;
	
	new Handle:hFile = OpenFile(FilePath, "r");
	
	if(hFile == INVALID_HANDLE)
		return false;
	
	new String:LineBuffer[256], len, String:KeyBuffer[128];
	while(!IsEndOfFile(hFile))
	{
		ReadFileLine(hFile, LineBuffer, sizeof(LineBuffer));
		
		len = BreakString(LineBuffer, KeyBuffer, sizeof(KeyBuffer));
		
		if(StrEqual(Key, KeyBuffer))
		{
			BreakString(LineBuffer[len], buffer, bufferlen);
			
			CloseHandle(hFile);
			return true;
		}
	}

	CloseHandle(hFile);
	return false;
}

stock CreateEmptyFile(const String:Path[])
{
	CloseHandle(OpenFile(Path, "a"));
}
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline