1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-07-05 03:19:08 -05:00

Branch for IW4X practically everything refactored

This commit is contained in:
RaidMax
2017-05-26 17:49:27 -05:00
parent 85a658b987
commit 10075b0d3f
107 changed files with 7426 additions and 3995 deletions

View File

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2321A25F-7871-47C3-8440-02551918D6A1}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Auto_Restart_Plugin</RootNamespace>
<AssemblyName>AutorestartPlugin</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Management" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="Monitoring.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharedLibrary\SharedLibrary.csproj">
<Project>{d51eeceb-438a-47da-870f-7d7b41bc24d6}</Project>
<Name>SharedLibrary</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)Admin\bin\$(ConfigurationName)\plugins\AutoRestartPlugin.dll"
copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)BUILD\plugins\"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,72 @@
using System;
using SharedLibrary;
using SharedLibrary.Extensions;
using System.Threading.Tasks;
namespace Auto_Restart_Plugin
{
public class Main : IPlugin
{
public string Author
{
get
{
return "RaidMax";
}
}
public float Version
{
get
{
return 1.0f;
}
}
public string Name
{
get
{
return "Auto Restart Plugin";
}
}
public async Task OnLoad()
{
return;
}
public async Task OnUnload()
{
return;
}
public async Task OnTick(Server S)
{
switch (Monitoring.shouldRestart())
{
case 300:
await S.Broadcast("^1Server will be performing an ^5AUTOMATIC ^1restart in ^55 ^1minutes.");
break;
case 120:
await S.Broadcast("^1Server will be performing an ^5AUTOMATIC ^1restart in ^52 ^1minutes.");
break;
case 60:
await S.Broadcast("^1Server will be performing an ^5AUTOMATIC ^1restart in ^51 ^1minute.");
break;
case 30:
await S.Broadcast("^1Server will be performing an ^5AUTOMATIC ^1restart in ^530 ^1seconds.");
break;
case 0:
await S.Broadcast("^1Server now performing an ^5AUTOMATIC ^1restart ^5NOW ^1please reconnect.");
Monitoring.Restart(S);
break;
}
}
public async Task OnEvent(Event E, Server S)
{
return;
}
}
}

View File

@ -0,0 +1,127 @@
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Management;
using SharedLibrary;
namespace Auto_Restart_Plugin
{
static class Monitoring
{
[DllImport("kernel32")]
public static extern bool DeleteFile(string name);
public static void Restart(Server goodBye)
{
_Restart(goodBye);
}
private static void _Restart(Server goodBye)
{
try
{
string cmdLine = GetCommandLine(Process.GetProcessById(goodBye.pID()));
var info = new ProcessStartInfo();
// if we don't delete this, we get a prompt..
DeleteFile(Process.GetProcessById(goodBye.pID()).MainModule.FileName + ":Zone.Identifier");
//info.WorkingDirectory = goodBye.Basepath;
info.Arguments = cmdLine;
info.FileName = Process.GetProcessById(goodBye.pID()).MainModule.FileName;
// goodBye.executeCommand("killserver");
Process.GetProcessById(Process.GetProcessById(goodBye.pID()).Parent().Id).Kill();
Process.GetProcessById(goodBye.pID()).Kill();
Process.Start(info);
}
catch (Exception E)
{
goodBye.Log.Write("SOMETHING FUCKED UP BEYOND ALL REPAIR " + E.ToString());
}
}
public static int shouldRestart()
{
var curTime = DateTime.Now;
DateTime restartTime = new DateTime(curTime.Year, curTime.Month, curTime.Day, 4, 0, 0);
var a = Math.Floor((restartTime - curTime).TotalMilliseconds / 1000);
if (a > 0 && a < 2) // just in case of precision
return 0;
else
{
switch((int)a)
{
case 300:
return 300;
case 120:
return 120;
case 60:
return 60;
case 30:
return 30;
default:
return 1337;
}
}
}
//http://stackoverflow.com/questions/2633628/can-i-get-command-line-arguments-of-other-processes-from-net-c
private static string GetCommandLine(this Process process)
{
var commandLine = new StringBuilder();
commandLine.Append(" ");
using (var searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
{
foreach (var @object in searcher.Get())
{
if (@object["CommandLine"].ToString().Contains("iw4m"))
commandLine.Append(@object["CommandLine"].ToString().Substring(4));
else
commandLine.Append(@object["CommandLine"]);
commandLine.Append(" ");
}
}
return commandLine.ToString().Trim();
}
}
public static class ProcessExtensions
{
private static string FindIndexedProcessName(int pid)
{
var processName = Process.GetProcessById(pid).ProcessName;
var processesByName = Process.GetProcessesByName(processName);
string processIndexdName = null;
for (var index = 0; index < processesByName.Length; index++)
{
processIndexdName = index == 0 ? processName : processName + "#" + index;
var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
if ((int)processId.NextValue() == pid)
{
return processIndexdName;
}
}
return processIndexdName;
}
private static Process FindPidFromIndexedProcessName(string indexedProcessName)
{
var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
return Process.GetProcessById((int)parentId.NextValue());
}
public static Process Parent(this Process process)
{
return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
}
}
}