TOGs SourcePawn Syntax Converter [MS Excel Based for now]
3 Attachment(s)
I wasnt sure which forum section to post this in, but since there are syntax highlighters and other indirectly related stuff to SP coding, I thought this the best fit.
I created some code in VBA in a Microsoft Excel environment to convert SourcePawn plugins from old syntax into new syntax. While the project is not yet finished, I thought I would release it for others to use, and for feedback (provided I have time to implement much feedback). It REALLY should be ported over to another language so that MS Excel isnt required. Despite all that, here it is. Perhaps in the future, myself or another will port my code to PHP and make a web version...
Also, it isnt perfect (and probably never will be). Here is the identified TO-DO's, should I find the time for them...the point is that the tool gets you most of the way there, taking a lot of the work out of it (if not all).
To Do and Stuff Not Yet Handled:
Find expressions or variables that are coerced to data types and replace with view_as<type>() [Not sure how to best detect this without false positives at this time...]
Break apart lines where variables of multiple types are declared.Done. Doesnt always work - need to revise.
Need to add dynamic size detection, similar to what is already done for char, for other variable types.Done.
May need to build up list of SM forwards again and push to 2D array with type to add them as (typically void) for checking if functions should be ints, check the type for fwds. They are only a problem when cross referenced in a recursive include, and added to the runtime list that makes them ints (typical for custom forwards from plugins, so it seems).Done.
Inclusions of includes - would need a defined path to include folder - DONT CONVERT INCLUDE, but use it to get defines, enums and forwardsDone.
Conversion of strings created with sizes set on run time (this would require pushing all defines to an array, including defines in includes, and checking if the size inside a bracket is a define or a variable - variable being dynamic)Done.
Add detection of multiple declarations of the same variable type (other than int), similar to what is already done for char, but for other types.Done.
Detection of functions that dont fit the several other lists but return something other than void, detectable via the return lines. I'm thinking of having it process entire functions at a time, allowing it to detect the text "return ....;" and adjust the type accordingly, and find the variable declare, if local, and its type. To support global variable types, those would need to be pushed to an array, since they are done processing.Done.
Processing of includes that are already in new syntax. Currently, it processes them, but might not work correctly on identifying some return type of natives, etc.
Some combinations of formatting can throw tool off, such as line breaks inserted in the middle of a single code statement, or possibly replacing some spaces with tabs.
Options that would be nice but aren't required to compile:
Replacing code with new SP 1.8 classes
Tasks to improve efficiency:
Remove function types that don't exist in SP 1.6- (1.8 classes)
Adding "int" in front of variable declarations that are not specified.
Adding void in front of function header parameters that don't have a type declared. This has overrides, noted below. Handling of optional parameters is now supported.
Detection of code that is in comments, including multi-line comments (/* */), even if mid-line of code. Anything inside comments or quotes is not processed.
Storing lists of function types, numeric defines (for processing of dynamic variable size declarations), enums (to identify variable "types"), forwards and their function types (for processing what their callback function types should be), and natives (same idea as forwards). These are also detected within includes.
Recursive reading of includes (and includes stated inside includes), both with default path (name inside <>), or string paths, and with exception to the <sourcemod> include. The sourcemod include is in most plugins, it's contents are already pre-added to lists, and this cuts down on processing time. In the future, this may include an option to also convert the includes (disabled by default), but not at this time. The processing looks for numeric defines, forwards, natives, and enums.
Skips most pre-compilation processing statements (starting with #), except for processing defines and includes.
Switching of where brackets go for char's any variables in function headers (i.e. after char, not after variable name). Variables with multiple brackets (dimensions) are now supported.
Replacement/removal of some depreciated functions or constants (FCVAR_PLUGIN, FindSendPropOffs, GetClientAuthString, etc.)
Menu detection for function header type declaration as int.
Removal of multiple of the same type declarations in a single line (i.e. char sSomeString[1], char sAnotherString[1]; --> char sSomeString[1], sAnotherString[1]; ). This applies to all variable types now.
Processing of entire functions to check return types in order to override the default "void" that would be added when not specified. Code detects type returned, and if it is a local or global variable, it looks it up to find that variables type and sets the function type accordingly. This does NOT process returning functions that return values at this time.
Splits declarations of multiple different variable types into as many separate lines as there are types. Note: This piece DOES NOT work if any of the multiple variables being declared set defaults in that line (e.g. iSomeInt = 1, Float:sSomeChar = 0.0;).
Converting of variable declarations with dynamic sizes to the new format.
Usage/Instructions:
It is a Microsoft Excel based tool for now. All you need to do to use it is:
Open the tool. If there is a yellow bar at the top asking if you want macros enabled, say yes (that allows the code to run). If there is a dialogue asking if ActiveX should run, say yes. This allows the button to work, though you could say no and run the code directly from the VBA window.
Make sure your include path is set. The includes arent edited, but are used to define variable types, natives, forwards, etc.
Click the button, and find the .sp file you want converted, and click ok.
The file will now be processed. The original file is left as is, and the new one is in the same location and has the text " newsyntax" added to the end. After this, the tool is done.
After processing, you will see the following comment and line of code at the top of the plugin's code:
PHP Code:
/* put the line below after all of the includes!
#pragma newdecls required
*/
Move the line of code (#pragma newdecls required) to be just past the last include. I may automate this step in the future as well.
Make final edits for anything not handled by the processor (see "To Do") until it compiles.
Note:The changelog is available with the spreadsheet in the "changelog" sheet.
I hope this is helpful! To see the code in Excel, press Alt+F11, or right click the tab and click "View Code". This tool will NOT edit the source file, but will create a new file with the same name then " newsyntax" appended. If a file already exists with that name, it will get a number appended in addition to that.
Timocop
11-24-2016 04:09
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
You could also use Visual Basic Script *.vbs as a single file. Works for all Windows versions and should work for Linux with Wine aswell, i think (i saw MS Scripting Control in winetricks).
ThatOneGuy
11-24-2016 07:17
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
Quote:
Originally Posted by Timocop
(Post 2472270)
You could also use Visual Basic Script *.vbs as a single file. Works for all Windows versions and should work for Linux with Wine aswell, i think (i saw MS Scripting Control in winetricks).
I actually started the conversion to vbs and havent finished it because of some of the differences. Ideally, I'd rather just port the logic to php and make a web page for people to use, but we'll see.
friagram
11-24-2016 10:11
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
convert back to old syntax would be useful
ThatOneGuy
11-24-2016 14:08
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
Quote:
Originally Posted by friagram
(Post 2472328)
convert back to old syntax would be useful
lol...but why would someone want to do that?! I'm sure there are times it would be useful though...
Miu
11-24-2016 15:12
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
what the fuck vba :psyduck:
ThatOneGuy
11-24-2016 15:35
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
Quote:
Originally Posted by Miu
(Post 2472393)
what the fuck vba :psyduck:
^This. lol. I already had a lot of code available from past projects I've done at work, so after a few drinks one night...
Anyways, when I finish the project, I'll probably make a PHP page to do the same. For now though, the development is easier in VBA, specifically for the includes processing, which I'm working on now. I dont know if you can easily follow a recursive include model from PHP without the user uploading all of those files as well...
ThatOneGuy
11-25-2016 04:02
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
Updated code and OP description.
shavit
11-25-2016 05:34
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]
great job!
headline
11-25-2016 15:38
Re: TOGs SourcePawn Syntax Converter [MS Excel Based for now]