Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Dec 17, 2024
0 parents commit 4cc0eff
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Mods.txt
Both.txt
WorkshopItems.txt
composer.lock
/vendor
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# PZConfigHelper

PZConfigHelper is a PHP tool designed to generate mod lists for Project Zomboid from a specified directory structure. It scans directories to create lists of mod IDs and workshop items, and saves these lists to text files.

## Installation

Clone the repository to your local machine:

```sh
git clone https://github.com/yourusername/PZConfigHelper.git
```

Navigate to the project directory:

```sh
cd PZConfigHelper
```

## Usage

Run the script with the `--path` option to specify the directory containing the mods:

```sh
php src/PZConfigHelper/PZConfigHelper.php --path='D:\\SteamLibrary\\steamapps\\workshop\\content\\108600'
```

This will generate three files in the current working directory:
- `Mods.txt`: Contains a list of mod names.
- `WorkshopItems.txt`: Contains a list of workshop item IDs.
- `Both.txt`: Contains a combined list of mod names and workshop item IDs.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

## Author

Valithor Obsidion <[email protected]>
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "valgorithms/pzconfighelper",
"description": "PZConfigHelper is a PHP tool designed to generate mod lists for Project Zomboid.",
"keywords": ["project-zomboid", "mod-list", "workshop-items"],
"license": "MIT",
"authors": [
{
"name": "Valithor Obsidion",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.0"
},
"autoload": {
"psr-4": {
"PZConfigHelper\\": "src/PZConfigHelper/"
}
}
}
11 changes: 11 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once 'vendor/autoload.php';

use PZConfigHelper\PZConfigHelper;

$options = getopt("", ["path:"]);
if (!isset($options['path'])) die("Usage: php example.php --path='D:\\SteamLibrary\\steamapps\\workshop\\content\\108600'");

$helper = new PZConfigHelper($options['path']);
$helper->saveToFile(getcwd());
14 changes: 14 additions & 0 deletions src/PZConfigHelper/PZConfigHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is a part of the PZConfigHelper project.
*
* Copyright (c) 2024-present Valithor Obsidion <[email protected]>
*/

namespace PZConfigHelper;

class PZConfigHelper
{
use PZConfigHelperTrait;
}
16 changes: 16 additions & 0 deletions src/PZConfigHelper/PZConfigHelperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is a part of the PZConfigHelper project.
*
* Copyright (c) 2024-present Valithor Obsidion <[email protected]>
*/

namespace PZConfigHelper;

interface PZConfigHelperInterface
{
public function __construct(string $path = __DIR__);
public function generateModLists(?string $directory = null, bool $isRootDirectory = true): void;
public function saveToFile(string $directory = __DIR__): void;
};
93 changes: 93 additions & 0 deletions src/PZConfigHelper/PZConfigHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/*
* This file is a part of the PZConfigHelper project.
*
* Copyright (c) 2024-present Valithor Obsidion <[email protected]>
*/

namespace PZConfigHelper;

trait PZConfigHelperTrait
{
private string $mods = '';
private string $ids = '';
private string $both = '';

public function __construct(private string $path = __DIR__) {
$this->generateModLists();
}

public function generateModLists(?string $directory = null, bool $isRootDirectory = true): void
{
$directory = $directory ?? $this->path;

foreach (scandir($directory) as $file) {
if ($file === '.' || $file === '..') continue;
if (!is_dir($filePath = $directory . '/' . $file)) continue;
if ($isRootDirectory) { // Root level directory, should contain Mod IDs
$this->ids .= $file . ';';
$this->both .= $file . '=';
$this->generateModLists($filePath, false);
$this->both .= PHP_EOL;
} else {
// Nested directory, should contain Workshop Item names
if (basename($filePath) === 'mods') {
foreach (scandir($filePath) as $modFile) {
if ($modFile === '.' || $modFile === '..') continue;
if (is_dir($filePath . '/' . $modFile)) {
$this->mods .= $modFile . ';';
$this->both .= $modFile . ';';
}
}
} else {
$this->generateModLists($filePath, false);
}
}
}
}

public function saveToFile(string $directory = __DIR__): void
{
file_put_contents($directory . '/Mods.txt', $this->mods);
file_put_contents($directory . '/WorkshopItems.txt', $this->ids);
file_put_contents($directory . '/Both.txt', $this->both);
}

public function __get(string $name)
{
return $this->$name ?? null;
}

public function serialize(): string
{
return serialize($this->__toArray());
}

public function unserialize($data): void
{
$array = unserialize($data);
$this->mods = $array['mods'];
$this->ids = $array['ids'];
$this->both = $array['both'];
}

public function __toArray(): array
{
return [
'mods' => $this->mods,
'ids' => $this->ids,
'both' => $this->both,
];
}

public function __toString(): string
{
return "$this->both";
}

public function __debugInfo(): array
{
return $this->__toArray();
}
}

0 comments on commit 4cc0eff

Please sign in to comment.