From 20cd7d51c63ab8664668d30068de9de8bc4a838b Mon Sep 17 00:00:00 2001 From: Marcel Naeve Date: Wed, 13 Nov 2024 21:21:09 +0100 Subject: [PATCH] started implementing download method for mods and authentication for download. --- scripts/class.FactorioMod.php | 53 ++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/scripts/class.FactorioMod.php b/scripts/class.FactorioMod.php index aed31a9..2dd51d3 100644 --- a/scripts/class.FactorioMod.php +++ b/scripts/class.FactorioMod.php @@ -44,6 +44,12 @@ class FactorioMod { */ private $mod_folder = null; + /** + * @var string $game_folder - Path to the Factorio game folder. + */ + private $game_folder = "factorio"; + + /** * Reads all Mods as FactorioMod objects to an array. * @param string $modFolder Path to the mods folder. @@ -223,7 +229,14 @@ class FactorioMod { * @return bool True if the mod was downloaded, false otherwise. */ private function download() : bool { - // TODO: implement + + // TODO: continue implementing.. where player-data.json.. + $player_data = json_decode(file_get_contents($this->game_folder. "/player-data.json"), true); + $token = $player_data["service-token"]; + $username = $player_data["service-username"]; + + $url = "https://mods.factorio.com" . $this->getLastVersionInfo()->download_url . "?username=$username&token=$token"; + return false; } @@ -259,7 +272,11 @@ class FactorioMod { * @return bool True if the mod settings have been loaded successfully, false otherwise. */ private function loadModSettings() : bool { - // TODO: implement + + $this->mod_settings = json_decode( + file_get_contents($this->mod_folder. "/settings.json") + ); + return false; } @@ -271,6 +288,28 @@ class FactorioMod { return $this->mod_settings; } + /** + * Sets the path to the game folder. + * @param string $game_folder Path to the game folder. + * @return FactorioMod The instance of FactorioMod for chaining. + */ + public function setGameFolder(string $game_folder) : FactorioMod { + + $this->game_folder = $game_folder; + return $this; + + } + + /** + * Returns the path to the game folder. + * @return string Path to the game folder. + */ + public function getGameFolder() : string { + + return $this->game_folder; + + } + /** * Saves the changes in the mod settings for all mods (enabled/disabled) to the settings.json file. * @return bool True if the mod settings have been saved successfully, false otherwise. @@ -278,9 +317,15 @@ class FactorioMod { private function saveModSettings() : bool { $j = json_encode($this->mod_settings); - // TODO: implement + + $r = file_put_contents($this->mod_folder. "/settings.json", $j); + + if($r === false) { + return false; + } + + return ( $r === strlen($j) ); - return false; } /**