Page 1 of 1

[Cs:s] Automatic mapcycle.txt creator

Posted: Mon Dec 28, 2020 2:12 pm
by cssbestrpg
Hi, can some one make a plugin that automatically create mapcycle.txt from maps that server have.

Re: [Cs:s] Automatic mapcycle.txt creator

Posted: Mon Dec 28, 2020 2:56 pm
by Kami
Hey, you can try this:

Syntax: Select all

from commands.server import ServerCommand

from os import listdir
from paths import GAME_PATH

MAP_PATH = GAME_PATH+"\maps"
CFG_PATH = GAME_PATH+"\cfg"

@ServerCommand('mapcycle_create')
def _mapcycle_create(command):
mapcycle_file = open(CFG_PATH+"\mapcycle.txt", "w")

for map in listdir(MAP_PATH):
if ".bsp" in map:
map = map.replace(".bsp","")
mapcycle_file.write(map+"\n")

mapcycle_file.close()


Use the command "mapcycle_create" to create or overwrite the mapcycle file with every map name you currently have in your maps folder.

Everytime you use this, it will overwrite all content in the mapcycle.txt. This means maps you don't have in your maps folder anymore will be replaced once you use mapcycle_create.

Re: [Cs:s] Automatic mapcycle.txt creator

Posted: Mon Dec 28, 2020 3:11 pm
by satoon101
I have a few tips when it comes to using the 'path' package included with SP.

  1. Using the __div__ method to automatically add directory using proper os.sep:

    Syntax: Select all

    MAP_PATH = GAME_PATH / 'maps'
  2. Open a file with the .open method along with the 'with' statement (which automatically closes the file on __exit__):

    Syntax: Select all

    MAPCYCLE_FILE = CFG_PATH / 'mapcycle.txt'
    with MAPCYCLE_FILE.open('w') as open_file:
    open_file.write()
  3. Loop through files using a wildcard (along with using f-strings):

    Syntax: Select all

    for bsp_file in MAP_PATH.files('*.bsp'):
    mapcycle_file.write(f'{bsp_file.namebase}\n')

Put them all together, and that plugin could look like:

Syntax: Select all

from commands.server import ServerCommand

from paths import GAME_PATH

MAP_PATH = GAME_PATH / 'maps'
MAPCYCLE_FILE = GAME_PATH / 'cfg' / 'mapcycle.txt'


@ServerCommand('mapcycle_create')
def _mapcycle_create(command):
with MAPCYCLE_FILE.open('w') as open_file:
for bsp_file in MAP_PATH.files('*.bsp'):
open_file.write(f'{bsp_file.namebase}\n')

Re: [Cs:s] Automatic mapcycle.txt creator

Posted: Mon Dec 28, 2020 4:49 pm
by cssbestrpg
Hi, the plugin works perfectly, thank you guys