AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [SNIPPET] Get Workshop Map Name (https://forums.alliedmods.net/showthread.php?t=249866)

Powerlord 10-13-2014 13:10

[SNIPPET] Get Workshop Map Name
 
5 Attachment(s)
SourceMod 1.8 has a function GetMapDisplayName(const char[] map, char[] displayName, int maxlength) that should be used instead of this functionality.

So, I wrote this just recently and was thinking about submitting it for inclusion in SourceMod.

Note that if this is included in SourceMod, it likely would end up split into multiple include files instead of just the one (likely string.inc for SubString, mapchooser.inc for the others.)

Changelog
  • 1.1.1 - Minor optimization to SubString in case you pass a 0 for len.
  • 1.1 - RemoveMapPath will now copy the original text to destination when it doesn't remove anything.
  • 1.0 - Initial release

Functions in this file:

Code:

/**
 * Copy a substring from source to destination
 *
 * @param source                String to copy from
 * @param start                        position to start at, 0 numbered. Negative means to start that many characters from the end.
 * @param len                        number of characters to copy.  Negative means to not copy that many characters from the end.
 * @param destination                String to copy to
 * @param maxlen                Length of destination string.  Must be 1 or greater.
 *
 * @return                                True if success, false if number of characters copied would be negative.
 * NOTE:                                There is no mechanism to get the remaining characters of a string.
 *                                        Instead, use strcopy with source[start] for that.
 */

bool:SubString(const String:source[], start, len, String:destination[], maxlen)

Code:

/**
 * Remove the path from the map name
 * This was added to remove workshop paths.
 *
 * @param map                        Map name
 * @param destination                String to copy map name to
 * @param maxlen                Length of destination string
 *
 * @return                                true if path was removed, false if map and destination are the same
 */
bool:RemoveMapPath(const String:map[], String:destination[], maxlen)

Code:

/**
 * Check if two maps are the same map.
 * Takes workshop paths into account.
 *
 * @param map                First map
 * @param map2                Second map
 *
 * @return                        true if they're the same map, false if not.
 */
bool:MapEqual(const String:map[], const String:map2[])

Code:

/**
 * Returns the index for the first occurrence of the provided map. If the map cannot be located, -1 will be returned.
 * Takes workshop maps into account.
 *
 * @param array                Array Handle.
 * @param map                Map to search for
 *
 * @return                        Array index, or -1 on failure
 */
FindMapStringInMapArray(Handle:array, const String:map[])

Note that these were primarily written for use in a MapChooser and were tested using the attached plugin.

Powerlord 10-14-2014 12:50

Re: [SNIPPET] Get Workshop Map Name
 
1 Attachment(s)
Here's version 1.1. It changes RemoveMapPath so that non-workshop maps will put the original string into destination instead of an empty string.

Leonardo 10-14-2014 16:17

Re: [SNIPPET] Get Workshop Map Name
 
so strcopy(destination, maxlen, ""); is faster/better than destination[0] = 0; ?

Powerlord 10-15-2014 11:10

Re: [SNIPPET] Get Workshop Map Name
 
1 Attachment(s)
Quote:

Originally Posted by Leonardo (Post 2211112)
so strcopy(destination, maxlen, ""); is faster/better than destination[0] = 0; ?

Yes1. Unless you meant than destination[0] = '\0'; in which case, no. I missed that I did a strcopy of "", whoops.

The comment optimization above it was there because earlier in development, it didn't check so it kept going all the way down the function when the copy length was 0.

To be honest, the entire SubString function is unnecessary as long as its args are positive, as it's just a fancy wrapper for strcopy.

1Because destination is tagged as String and that will toss warnings/errors.

SM9 04-27-2015 08:45

Re: [SNIPPET] Get Workshop Map Name
 
Am I correct to think this makes maps that look like "workshop/14235453/mapname" look like "mapname" if so, how would I go about adding this to MapChooserExtended so that it displays the clean map name in voting?

Thanks

Powerlord 04-28-2015 14:24

Re: [SNIPPET] Get Workshop Map Name
 
1 Attachment(s)
Quote:

Originally Posted by xCoderx (Post 2290735)
Am I correct to think this makes maps that look like "workshop/14235453/mapname" look like "mapname" if so, how would I go about adding this to MapChooserExtended so that it displays the clean map name in voting?

Thanks

RemoveMapPath would probably be your best bet. If you use the version in this post (or my previous post), the input string will be copied to the output string if they're the same.

Because it uses strcopy internally, I think it's safe to use the same string as the input and output.

One of my MapChooser Extended branches has this already in place, but I forget what other changes I've made to it.

I also forgot there was a separate thread for this. I've revamped this .inc file a bit since I originally posted it.

Here's a 1.7 API version that should be considerably cleaner than the version in the first post. Note that SubString has completely gone away in this version.

Mathias. 04-28-2015 16:06

Re: [SNIPPET] Get Workshop Map Name
 
Quote:

Originally Posted by Leonardo (Post 2211112)
so strcopy(destination, maxlen, ""); is faster/better than destination[0] = 0; ?

destination[0] = '\0' or 0 is still faster but not at a point it really matter even if you had to call the function into a large loop.

Dr. Greg House 04-30-2015 11:12

Re: [SNIPPET] Get Workshop Map Name
 
Why doesn't this (substring) follow the current convention of all string/format functions that is to return the cells written?

Powerlord 04-30-2015 16:21

Re: [SNIPPET] Get Workshop Map Name
 
Quote:

Originally Posted by Dr. Greg House (Post 2291859)
Why doesn't this (substring) follow the current convention of all string/format functions that is to return the cells written?

Newer versions remove SubString as it's just a wrapper for strcopy anyway.

Powerlord 06-13-2015 01:24

Re: [SNIPPET] Get Workshop Map Name
 
This include is completely obsolete now. Not only does it not cover all the use cases in CS:GO, but now TF2 supports workshop maps... in a somewhat different way than CS:GO does.

Incidentally, it'd be a huge help if someone could tell me which names can be used in CS:GO servers for workshop maps. I think someone mentioned the map workshop ID worked in addition to the map's name... I was wondering if partial filenames also worked.


All times are GMT -4. The time now is 18:30.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.