[Cs:s] Automatic mapcycle.txt creator

A place for requesting new Source.Python plugins to be made for your server.

Please request only one plugin per thread.
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

[Cs:s] Automatic mapcycle.txt creator

Postby cssbestrpg » Mon Dec 28, 2020 2:12 pm

Hi, can some one make a plugin that automatically create mapcycle.txt from maps that server have.
User avatar
Kami
Global Moderator
Posts: 263
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

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

Postby Kami » Mon Dec 28, 2020 2:56 pm

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.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

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

Postby satoon101 » Mon Dec 28, 2020 3:11 pm

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')
Image
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

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

Postby cssbestrpg » Mon Dec 28, 2020 4:49 pm

Hi, the plugin works perfectly, thank you guys

Return to “Plugin Requests”

Who is online

Users browsing this forum: No registered users and 17 guests