Raised This Month: $32 Target: $400
 8% 

[SNIPPET] Get Workshop Map Name


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-13-2014 , 13:10   [SNIPPET] Get Workshop Map Name
Reply With Quote #1

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.
Attached Files
File Type: sp Get Plugin or Get Source (map_workshop_functions_test.sp - 309 views - 3.8 KB)
File Type: inc map_workshop_functions.inc (5.5 KB, 569 views)
File Type: smx map_workshop_functions_test.smx (4.9 KB, 414 views)
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-21-2015 at 11:59.
Powerlord is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-14-2014 , 12:50   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #2

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.
Attached Files
File Type: inc map_workshop_functions.inc (5.5 KB, 236 views)
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 10-14-2014 , 16:17   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #3

so strcopy(destination, maxlen, ""); is faster/better than destination[0] = 0; ?
__________________
Leonardo is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-15-2014 , 11:10   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #4

Quote:
Originally Posted by Leonardo View Post
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.
Attached Files
File Type: inc map_workshop_functions.inc (5.5 KB, 367 views)
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-15-2014 at 11:20.
Powerlord is offline
SM9
Veteran Member
Join Date: Sep 2013
Location: United Kingdom
Old 04-27-2015 , 08:45   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #5

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
SM9 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-28-2015 , 14:24   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #6

Quote:
Originally Posted by xCoderx View Post
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.
Attached Files
File Type: inc map_workshop_functions.inc (3.9 KB, 359 views)
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 04-28-2015 at 14:27.
Powerlord is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 04-28-2015 , 16:06   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #7

Quote:
Originally Posted by Leonardo View Post
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.
Mathias. is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 04-30-2015 , 11:12   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #8

Why doesn't this (substring) follow the current convention of all string/format functions that is to return the cells written?
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-30-2015 , 16:21   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #9

Quote:
Originally Posted by Dr. Greg House View Post
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.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 06-13-2015 , 01:24   Re: [SNIPPET] Get Workshop Map Name
Reply With Quote #10

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.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 06-13-2015 at 01:27.
Powerlord is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:56.


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