2024-11-22 00:03:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Namespace for classes and functions related to the game Factorio.
|
|
|
|
* @package NAE\Factorio
|
|
|
|
*/
|
|
|
|
namespace NAE\Factorio;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for managing a headless server for the game Factorio.
|
|
|
|
* @author "Marcel Naeve" <php@naeve.info>
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit)
|
|
|
|
*/
|
|
|
|
class FactorioServer {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FactorioVersion Current Version of Server
|
|
|
|
*/
|
|
|
|
private $version;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FactorioMod For Managing Server Mods
|
|
|
|
*/
|
|
|
|
private $mods;
|
|
|
|
|
2024-11-23 03:33:42 +01:00
|
|
|
/**
|
|
|
|
* @var ValveRcon for handling Valve Rcon to server
|
|
|
|
*/
|
|
|
|
private $rcon;
|
|
|
|
|
2024-11-22 00:03:29 +01:00
|
|
|
/**
|
|
|
|
* @var string Name of the Save to load when starting the server
|
|
|
|
*/
|
2024-11-23 03:33:42 +01:00
|
|
|
private $save_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Path to the Server Directory
|
|
|
|
*/
|
|
|
|
private $server_dir = INDEX_DIR . "/factorio";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Path to the Mods Directory
|
|
|
|
*/
|
|
|
|
private $mod_dir = INDEX_DIR . "/factorio/mods";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Path to the Save-File Directory
|
|
|
|
*/
|
|
|
|
private $save_dir = INDEX_DIR . "/factorio/saves";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Name of the user who owns the files and runs this server
|
|
|
|
*/
|
|
|
|
private $user_name = "factorio";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Name of the user-group who owns the files and runs this server
|
|
|
|
*/
|
|
|
|
private $user_group = "factorio";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Name of the Service in systemd/systemctl
|
|
|
|
*/
|
|
|
|
private $service_name = "factorio";
|
2024-11-22 00:03:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callcable Custom function for stopping the server (return bool)
|
|
|
|
*/
|
|
|
|
private $customStop = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable Custom function for starting the server (return bool)
|
|
|
|
*/
|
|
|
|
private $customStart = null;
|
|
|
|
|
2024-11-23 03:33:42 +01:00
|
|
|
/**
|
|
|
|
* @var callable Custom function for restarting the server (return bool)
|
|
|
|
*/
|
|
|
|
private $customReStart = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable Custom function for checking the server status (return bool - true means server is running, false means server is not running)
|
|
|
|
*/
|
|
|
|
private $customIsRunning = null;
|
|
|
|
|
2024-11-22 00:03:29 +01:00
|
|
|
/**
|
|
|
|
* @var array Server Settings
|
|
|
|
*/
|
|
|
|
private $settings = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Environment Variables for the Server
|
|
|
|
*/
|
|
|
|
private $env = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Server Admin List
|
|
|
|
*/
|
2024-11-23 03:33:42 +01:00
|
|
|
private $admin_list = [];
|
2024-11-22 00:03:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param array $settings Settings for the server
|
|
|
|
*/
|
|
|
|
public function __construct(array $settings) {
|
|
|
|
// TODO: Implement
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the Server with optional feedback on cli.
|
|
|
|
* @param bool $silent Feedback on cli wanted? (default: false)
|
|
|
|
* @return bool Returns true on success, false on failure
|
|
|
|
*/
|
|
|
|
public function updateCli(bool $silent=false) : bool {
|
|
|
|
// TODO: Implement
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install a new Server with the current Settings with optional feedback on cli.
|
|
|
|
* @param string $version Which version should be installed? (default: latest)
|
|
|
|
* @param bool $silent Feedback on cli wanted? (default: false)
|
|
|
|
* @return bool Returns true on success, false on failure
|
|
|
|
*/
|
|
|
|
public function installCli(string $version="latest", bool $silent=false) : bool {
|
|
|
|
// TODO: Implement
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a custom function for stopping the server.
|
|
|
|
* @param callable function that is beeing called for stopping the server
|
|
|
|
* @return FactorioServer $this for chaining.
|
|
|
|
*/
|
|
|
|
public function setCustomStop(callable $customStop) : FactorioServer {
|
|
|
|
|
|
|
|
$this->customStop = $customStop;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a custom function for starting the server.
|
|
|
|
* @param callable function that is beeing called for starting the server
|
|
|
|
* @return FactorioServer $this for chaining.
|
|
|
|
*/
|
|
|
|
public function setCustomStart(callable $customStart) : FactorioServer {
|
|
|
|
|
|
|
|
$this->customStart = $customStart;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the server.
|
|
|
|
* @return bool true on success, false on failure
|
|
|
|
*/
|
|
|
|
public function stop() : bool {
|
|
|
|
|
|
|
|
if(is_callable($this->customStop)) {
|
|
|
|
return call_user_func($this->customStop);
|
|
|
|
}
|
|
|
|
|
|
|
|
$command = "systemctl stop factorio-server";
|
|
|
|
exec($command, $output, $return);
|
|
|
|
|
|
|
|
return ( $return === 0 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the server.
|
|
|
|
* @return bool true on success, false on failure
|
|
|
|
*/
|
|
|
|
public function start() : bool {
|
|
|
|
|
|
|
|
if(is_callable($this->customStart)) {
|
|
|
|
return call_user_func($this->customStart);
|
|
|
|
}
|
|
|
|
|
2024-11-23 03:33:42 +01:00
|
|
|
$command = "systemctl start ".$this->getServiceName();
|
2024-11-22 00:03:29 +01:00
|
|
|
exec($command, $output, $return);
|
|
|
|
|
|
|
|
return ( $return === 0 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restarts the server.
|
|
|
|
* @return bool true on success, false on failure
|
|
|
|
*/
|
|
|
|
public function restart() : bool {
|
|
|
|
|
2024-11-23 03:33:42 +01:00
|
|
|
if(is_callable($this->customReStart)) {
|
|
|
|
return call_user_func($this->customReStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
exec("systemctl restart ".$this->getServiceName()." > /dev/null", $output, $return);
|
2024-11-22 00:03:29 +01:00
|
|
|
|
|
|
|
return ( $return === 0 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Service is running
|
|
|
|
*
|
|
|
|
* @return bool True if the service is running, false otherwise
|
|
|
|
*/
|
|
|
|
public function isRunning() : bool {
|
|
|
|
|
2024-11-23 03:33:42 +01:00
|
|
|
if(is_callable($this->customIsRunning)) {
|
|
|
|
return call_user_func($this->customIsRunning);
|
|
|
|
}
|
|
|
|
|
|
|
|
exec("systemctl is-active ".$this->getServiceName(), $output, $return);
|
2024-11-22 00:03:29 +01:00
|
|
|
|
|
|
|
return ( $return === 0 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-23 03:33:42 +01:00
|
|
|
/**
|
|
|
|
* Get the Name of the Service in systemd/systemctl.
|
|
|
|
* @return string Name of the service
|
|
|
|
*/
|
|
|
|
public function getServiceName() : string {
|
|
|
|
|
|
|
|
return $this->service_name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Name of the Service in systemd/systemctl.
|
|
|
|
* @param string $serviceName Name of the Service
|
|
|
|
* @return FactorioServer $this for chaining.
|
|
|
|
*/
|
|
|
|
public function setServiceName(string $serviceName) : FactorioServer {
|
|
|
|
|
|
|
|
$this->service_name = $serviceName;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-22 00:03:29 +01:00
|
|
|
}
|