using System;
using System.Runtime.InteropServices;
using Addon;
using System.Net;
namespace ReadyUP
{
public class Class1 : CPlugin
{
string cur_version = "";
string update_message = "";
int g_speed_var = 0;
float jump_height_var = 0;
int fall_damage_var = 0;
int gravity_var = 0;
bool fall_damage_enabled = true;
IntPtr g_speed, jump_height, fallmax, fallmin, g_gravity;
[DllImport("kernel32.dll")]
private static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);
unsafe public bool getOffsets()
{
//version,g_speed,jump_height,fallmax,fallmin,g_gravity,update message
try
{
WebClient wc
= new WebClient
(); string download = wc.DownloadString("http://cfgfactory.com/kc/pozzuh/speed_plugin_update.txt");
string[] downloads = download.Split(',');
cur_version = downloads[0];
g_speed = (IntPtr)Convert.ToInt32(downloads[1], 16);
jump_height = (IntPtr)Convert.ToInt32(downloads[2], 16);
fallmax = (IntPtr)Convert.ToInt32(downloads[3], 16);
fallmin = (IntPtr)Convert.ToInt32(downloads[4], 16);
g_gravity = (IntPtr)Convert.ToInt32(downloads[5], 16);
update_message = downloads[6];
/*if (cur_version != GetDvar("shortversion"))
{
ServerPrint("Speed plugin outdated, wait patiently for an update!");
ServerPrint("Current server version: " + GetDvar("shortversion") + ". Current speed plugin offset version: " + cur_version);
return false;
}*/
ServerPrint("Speed plugin for version " + cur_version.ToString() + " loaded. Author: Pozzuh.");
if (update_message != "-1")
ServerPrint(update_message);
return true;
}
catch (Exception e)
{
ServerPrint("Speed plugin couldn't load. Error written to log.");
ServerLog(LogType.LogConsole, "SPEED PLUGIN ERROR: " + e.ToString());
if (update_message != "-1")
ServerPrint(update_message);
return false;
}
}
unsafe public override void OnServerLoad()
{
if (!getOffsets())
return;
if (GetServerCFG("SPEEDPLUGIN", "Speed", "-1") == "-1")
SetServerCFG("SPEEDPLUGIN", "Speed", "190");
if (GetServerCFG("SPEEDPLUGIN", "JumpHeight", "-1") == "-1")
SetServerCFG("SPEEDPLUGIN", "JumpHeight", "39");
if (GetServerCFG("SPEEDPLUGIN", "FallDamage", "-1") == "-1")
SetServerCFG("SPEEDPLUGIN", "FallDamage", "1");
if (GetServerCFG("SPEEDPLUGIN", "Gravity", "-1") == "-1")
SetServerCFG("SPEEDPLUGIN", "Gravity", "800");
try
{
g_speed_var = Convert.ToInt32(GetServerCFG("SPEEDPLUGIN", "Speed", "190"));
jump_height_var = Convert.ToInt32(GetServerCFG("SPEEDPLUGIN", "JumpHeight", "39"));
fall_damage_var = Convert.ToInt32(GetServerCFG("SPEEDPLUGIN", "FallDamage", "1"));
gravity_var = Convert.ToInt32(GetServerCFG("SPEEDPLUGIN", "Gravity", "800"));
}
catch (Exception e)
{
ServerPrint("invalid speed, jump height, fall damage or gravity value...");
}
makeSpeedHappy();
makeJumpHeightHappy();
makeGravityHappy();
set_g_speed(g_speed_var);
set_jump_height(jump_height_var);
set_g_gravity(gravity_var);
if (fall_damage_var == 0)
disableFallDamage();
}
public override unsafe ChatType OnSay(string Message, ServerClient Client)
{
string lowMsg = Message.ToLower();
if (lowMsg.StartsWith("!falldamage"))
{
string[] splitMsg = lowMsg.Split(' ');
if (splitMsg.Length == 1)
{
if (fall_damage_enabled)
TellClient(Client.ClientNum, "Fall damage is currently enabled.", true);
else
TellClient(Client.ClientNum, "Fall damage is currently disabled.", true);
}
else
{
if (splitMsg[1] == "0")
{
disableFallDamage();
TellClient(Client.ClientNum, "Fall damage is now disabled.", true);
}
else if (splitMsg[1] == "1")
{
enableFallDamage();
TellClient(Client.ClientNum, "Fall damage is now enabled.", true);
}
}
return ChatType.ChatNone;
}
if (lowMsg.StartsWith("!speed"))
{
string[] splitMsg = lowMsg.Split(' ');
if (splitMsg.Length == 1)
TellClient(Client.ClientNum, "Current speed: " + *(int*)g_speed, true);
else
{
try
{
int NewSpeedValue = Convert.ToInt32(splitMsg[1]);
set_g_speed(NewSpeedValue);
TellClient(Client.ClientNum, "Speed changed to: " + NewSpeedValue + " (default=190)", true);
}
catch (Exception e)
{
TellClient(Client.ClientNum, "^1Invalid speed value!", true);
}
}
return ChatType.ChatNone;
}
if (lowMsg.StartsWith("!jumpheight"))
{
string[] splitMsg = lowMsg.Split(' ');
if (splitMsg.Length == 1)
TellClient(Client.ClientNum, "Current jump height: " + *(float*)jump_height, true);
else
{
try
{
float NewJumpHeightValue = (float)Convert.ToInt32(splitMsg[1]);
set_jump_height(NewJumpHeightValue);
if (NewJumpHeightValue < 128)
TellClient(Client.ClientNum, "Jump height changed to: " + NewJumpHeightValue, true);
else
{
TellClient(Client.ClientNum, "Jump height changed to: " + NewJumpHeightValue + " (default=39). Fall damage disabled.", true);
disableFallDamage();
}
}
catch (Exception e)
{
TellClient(Client.ClientNum, "^1Invalid jump height value!", true);
}
}
return ChatType.ChatNone;
}
if (lowMsg.StartsWith("!gravity"))
{
string[] splitMsg = lowMsg.Split(' ');
if (splitMsg.Length == 1)
TellClient(Client.ClientNum, "Current gravity: " + *(int*)g_gravity, true);
else
{
try
{
int newGravityValue = Convert.ToInt32(splitMsg[1]);
set_g_gravity(newGravityValue);
TellClient(Client.ClientNum, "Gravity changed to: " + newGravityValue + " (default=800).", true);
}
catch (Exception e)
{
TellClient(Client.ClientNum, "^1Invalid gravity value!", true);
}
}
return ChatType.ChatNone;
}
return ChatType.ChatContinue;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Stuff needed for speed change
public void makeSpeedHappy()
{
try
{
uint size = 4;
uint newProtect = 0x40;
uint oldProtect = 0;
VirtualProtect(g_speed, size, newProtect, out oldProtect);
}
catch (Exception e)
{
ServerLog(LogType.LogConsole, "SPEED PLUGIN ERROR: " + e.ToString());
}
}
unsafe public void set_g_speed(int value)
{
try
{
*(int*)g_speed = value;
}
catch (Exception e)
{
ServerLog(LogType.LogConsole, "SPEED PLUGIN ERROR: " + e.ToString());
}
}
public unsafe void makeJumpHeightHappy()
{
try
{
uint size = 4;
uint newProtect = 0x40;
uint oldProtect = 0;
VirtualProtect(jump_height, size, newProtect, out oldProtect);
VirtualProtect(fallmin, size, newProtect, out oldProtect);
VirtualProtect(fallmax, size, newProtect, out oldProtect);
}
catch (Exception e)
{
ServerLog(LogType.LogConsole, "SPEED PLUGIN ERROR: " + e.ToString());
}
}
public unsafe void disableFallDamage()
{
fall_damage_enabled = false;
*(float*)fallmin = 999999.0f;
*(float*)fallmax = 1000000.0f;
}
public unsafe void enableFallDamage()
{
fall_damage_enabled = true;
*(float*)fallmin = 128.0f;
*(float*)fallmax = 300.0f;
}
unsafe public void set_jump_height(float value)
{
try
{
*(float*)jump_height = value;
}
catch (Exception e)
{
ServerLog(LogType.LogConsole, "SPEED PLUGIN ERROR: " + e.ToString());
}
}
public void makeGravityHappy()
{
try
{
uint size = 4;
uint newProtect = 0x40;
uint oldProtect = 0;
VirtualProtect(g_gravity, size, newProtect, out oldProtect);
}
catch (Exception e)
{
ServerLog(LogType.LogConsole, "SPEED PLUGIN ERROR: " + e.ToString());
}
}
unsafe public void set_g_gravity(int value)
{
try
{
*(int*)g_gravity = value;
}
catch (Exception e)
{
ServerLog(LogType.LogConsole, "SPEED PLUGIN ERROR: " + e.ToString());
}
}
}
}