CSharpatron - the definitive Unityscript to C# converter!

I realize I’m probably somewhat behind the times with this, but I just completed work on what I’d like to think is the definitive Unityscript to C# converter… Prompted by my own need to convert a large, complex Unityscript code-base, I tried out some of the existing conversion tools and found that they didn’t take me too far. I thought I’d try a different approach…

CSharpatron parses script files utilizing type information gained via Reflection. It’s able to understand Unityscript files at a very low level, not only making it great at the ‘obvious’ conversions, but able to go much, much further to make really subtle fix-ups too, e.g. variable scoping adjustments and adding required type-casts. …In most cases the output C# files will compile right away with no manual work whatsoever.

There are many other features to help at all stages of project conversion:

  • Thorough log information for all fix-ups and errors.
  • A workspace view shows all converted files and their conversion status. You can enable or disable a C# file at will.
  • C# compile errors are captured and can be viewed from the workspace.
  • A special tool allows you to test compilation of a C# file even when the file has many dependencies that would otherwise block compilation.
  • Unityscript and C# files are switched in a way that won’t break any mapped game-objects.

CSharpatron can convert the old Penelope example project in less than 5 minutes without a single file-edit required. For my own project, it enabled me to move 107,000 lines of Unityscript to C# in just a few hours.

There’s a really thorough manual covering CSharpatron and the full process of .js to .cs conversion here.

CSharpatron is available on the Asset Store here.

All feedback welcome…

Chris

The workspace view show the status of a ‘mid-conversion’ project. It shows you any conversion issues, C# compile issues and external dependencies for a file…

Open up a file to see errors (with jump to line) or type/file dependencies. A number of other tools (including a ‘bonus’ .Net Reflection tool) exist to help you convert your files and projects.

1 Like

I’ve been staring at CSharpatron in my wishlist for weeks. I’ve looked at a few converters in the past and been very disappointed with the results. Going to take the plunge and buy this one (if it goes on sale in the next 6 months I am going to cry) simply because there are SO MANY tutorials that I have to ignore because they are written as .js (and not interested in spending time to learn to convert scripts manually)

trepan: I really hope this is the answer to my script conversion woes.

I really hope this is the answer to my script conversion woes

I hope so too. Let me know if you encounter any problems!

Indeed it has! This is by far the best conversion tool I’ve tried and a very worthwhile purchase :slight_smile: huzzah!!!

That’s good to hear :slight_smile:

Thanks for the feedback.

I’m having show-stopper problems with the package, which I just purchased today on 50% off sale. It’s hanging up my Unity Editor in an endless error loop that seems to be related to the CSharpatron editor window GUI.

FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
CSharpatron.SSCSharpatronRecord+ErrorInfo…ctor (System.String str)
CSharpatron.SSCSharpatronRecord…ctor (System.String _fn, System.IO.StreamReader reader)
CSharpatron.SSCSharpatronWorkspace.ReadFromFile ()
CSharpatron.SSCSharpatronWorkspace…ctor (System.String _fn)
CSharpatron.SSCSharpatron.InitializeWorkspace ()
CSharpatron.SSCSharpatron.OnEnable ()
CSharpatron.SSCSharpatron.OnInspectorUpdate ()
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[ ] parameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:241)
UnityEditor.HostView.Invoke (System.String methodName) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:234)
UnityEditor.HostView.OnInspectorUpdate () (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:199)

The only way I can stop the errors from continuing to accrue is to delete the CSharpatron dll.

I’m running Unity Pro 4.5.4f1.

It seems like there’s some bad text in the workspace file, most likely a captured error message of unexpected form. A quick fix will be to delete the offending error from the file record. I’ve sent you a PM with more information and request for a copy of the bad text so I can properly resolve the problem.

Sorry for the inconvenience :frowning:

Thanks for your quick response. :slight_smile: I will continue to work it out with you via email so as not to clutter this thread. I’m sure we can get this resolved. It looks like a great product!

Quick followup for the forums…

Apparently this was a small bug, due to Windows vs. Mac CR-LF end of line character differences. Trepan was very quick to provide me with an updated DLL and it seems to be working fine now. Excellent service!

Hi Trepan,

I am getting similar error each time I modify a new converted file. I will PM you my details and would appreciate the update.

Also is it possible to make a video demonstrating the conversion process. I tried to convert the standard assets js scripts and although the errors were corrected I could not get the finalize button to show up.

Thanks,
Sam

Hi Sam,

Sorry about the error :(. I have learnt that although Unity shields us from most platform differences, line-feeds in compiler output are in fact different between Mac/PC!

I’ve replied to your PM regarding the update and your standard assets test (need a little more info to tell if this is a bug or incomplete process).

For anyone else seeing the Int32.Parse/GUI bug please contact my support email (see ‘Support’ in manual) and I’ll reply with a fixed interim version. NOTE: I’m currently finishing work on a new build of CSharpatron that fixes this bug and also addresses another problem seen by another user when projects contain vars of mixed type (e.g. int[,][ ]). Hopefully I’ll be submitting this later today so it’ll be live in 3-4 days.

As for a video demo, yes, that might be a good idea…

Regards,

Chris

Hi Chris,

Thanks a lot for the outstanding support.

I just wanted to let the readers know that the errors were my fault as I did not see the finalize button which appears in blue when the csharp files compiles 100%. I successfully converted the character controller scripts in the standard assets folder to csharp equivalent files in a matter of few minutes using this plugin. Really amazing and well designed asset. Looking forward for the video for first time users like me to quickly dive in and use it to its full potential.

Good luck with future updates, this is a must have plugin for Unity.
Sam.

Well I’m glad about that! Thanks so much for the positive feedback.

v1.1 will be submitted to the Asset Store very soon now…

Just a quick update to let you know that I created a demo/tutorial video for CSharpatron. You can see the video here.

I also submitted v1.1 to the Asset Store last night, so hopefully it will be live in a few days. As soon as it is, I’ll post full details of the update (there’s a lot of stuff, both bug fixes and new features).

Many thanks to those of you that have purchased CSharpatron - I really appreciate your support and feedback.

Chris

Thanks Chris for the outstanding support, the video was exactly what I was looking for… really helps in using this asset to its full potential. Patiently waiting for v1.1 :slight_smile:

Sam

The new version of CSharpatron was just approved. Sorry it took a little longer than anticipated (the first submission failed to send and I had to resubmit). On the plus side there are now a couple more improvements…

Here’s the change-log for v1.11:

  • Fix for error case when reading workspace file when running under Windows (causing ‘Int32.Parse’ crash cycle).
  • Improved support for multi-var arrays declared in Unityscript (e.g. int[,])
  • Support for mixed format multi-dimensional arrays declared in a C# assembly (e.g. int[,][ ])
  • Fix for a freeze bug that could occur if source contained an abbreviated float constant of the form ‘1f’.
  • Reworked handling for auto fix-up of assignment to value type return. New method works in all cases barring multi var assignment.
  • Prevented an unhelpful type-cast to ‘IEnumerable’ that could be added in some circumstances.
  • Fixed a case where a var declared as a Unityscript Array could fail to yield an error.
  • Fixed possible confusing error message pending assignment of a Unityscript Array to an otherwise untyped var.
  • Fixed potential parsing bug when processing initialized arrays (could have caused bad type-cast bracketing).
  • Added a debug ‘line-step’ feature to help one user to track down a 'lock-up’ bug.
  • Added support for Javascript ‘Number’ type (converts to C# 'double’)
  • UnityEngine.Object.Instantiate return type now implicitly set to match first param (see details in manual)
  • @CustomEditor is now respected and used to infer the type of the editor class var ‘target’. This works wonders for conversion error counts in editor related files.
  • Extension methods (from C# assemblies) are now parsed correctly when used in Unityscript.
  • Added a ‘DumpExtensionMethods’ debug tool (under Extras).
  • Fixed bogus ‘read-only’ error when script was writing to ‘this.member’.
  • Fixed possibility for negative numeric constants to be misinterpreted.
  • Improved detection of implicitly convertible types in binary ops to fix some spurious casts.
  • ‘Rebuild stubs file’ is now blocked if there are outstanding fatal errors (to avoid generation of an incomplete file).
  • Reworked intParse and floatParse to just insert type-casts as appropriate.
  • Improved wording for a couple of common warnings.
  • Improved handling for typeof() operator. If supplied a non-class name, CSharpatron now auto-converts to var.GetType()
  • Added new ‘About’ callout in Control Center panel to make version and contact details a little more accessible.
  • Added support for anonymous methods.
  • Major optimization to conversion times, now approximately twice as fast as v1.03.
  • Fixed parsing problem in ‘for( var x:type in y)’ style loops where ‘type’ included template brackets.
  • Fixed fatal error parsing nested ‘if’ statements of the form ‘if (x) if (y) {}’.
  • Added ability to ‘DestroyWorkspace’ after ‘Finalize’.
  • Better handling of attempted conversion of scripts in ‘Standard Assets’, ‘Editor’ or ‘Plugins’ folders (direct conversion blocked, workaround detailed in manual).
  • @script AddComponentMenu’ is now relocated prior to class body as required by C#.

Many thanks to users who logged bug reports and supplied code snippets that helped me to identify and resolve problems. Special thanks to Tanuki Digital for granting me full source access to an entire project - great for prompting some of the new conversion improvements!

You can see the manual, updated for the new version, here.

Chris

Hi Chris,

I’m getting this error for a JS file that imports System.Security:

js/ApiManager.js[-1]. Fatal:System.Exception: Failed to find component ‘Security’ of path: System.Security.Cryptography. Is this library in the ‘exclusions’ list?
at CSharpatron.SSTypeDatabase.LookupContainer (System.String path, Boolean errorOnFail, CSharpatron.SSDBContainer baseContainer) [0x00000] in :0
at CSharpatron.SSTypeDatabase.LookupContainer (System.String path, CSharpatron.SSDBContainer baseContainer) [0x00000] in :0
at CSharpatron.SSSourceConverter.AddLib (System.String libName, Boolean addUsing) [0x00000] in :0
at CSharpatron.SSSourceConverter.Convert (System.String className, System.String source) [0x00000] in :0
at CSharpatron.SSCSharpatron.Convert (UnityEditor.MonoScript scriptObj) [0x00000] in :0
LAST CONVERSION LINE: 1
UnityEngine.Debug:LogError(Object)
CSharpatron.SSCSharpatron:EmitError(String, ErrorType, Int32)
CSharpatron.SSCSharpatron:Convert(MonoScript)
CSharpatron.SSCSharpatron:ConvertAll(MonoScript[ ])
CSharpatron.SSCSharpatron:OnInspectorUpdate()
UnityEditor.HostView:OnInspectorUpdate()

That should be a simple thing to fix… Look in the configuration panel for ‘Conversion/Filter libs list’ and un-tick this option. Your file will hopefully now convert without issue.

(What’s going on here is that when CSharpatron starts up it has to generate a large database of type information and to reduce the time this takes, I deliberately filter out a number of namespaces that don’t seem like things many people would be using. …Of course this is a subjective thing and perhaps I’m wrong in the case of System.Security - sorry! …Anyhow, the option is there to stop the filtering from occurring so that the converter has access to all available namespaces at the expense of slightly slower startup).

Hope this resolves your problem…

Hey i just purchased CSharpatron, but for me it’s freezing when im trying to make the workspace for selected files. I let it process the files for over an hour but it never moved further from 16/99. There was three errors so i suppose those have something to do with it. Any help here?

Hi Kajo,

Thanks for your purchase. I’m sorry to hear you’re having problems.

If you look at the progress dialog it should tell you the name of the file being processed at the point that the freeze occurs (look in the bottom right corner). We’ll have to figure out what in that file is causing trouble. If you’re able to send me the file I can take a look. Alternatively you can investigate further yourself by ticking the option ‘Line step (for debugging)’ in the control panels ‘Debug’ section. This will make it so that you have to click a dialog button before each attempted line of conversion - very tedious, but the best tool for diagnosing this kind of problem!

If you look in the control panel ‘About’ section you’ll see a direct email contact which is the best way to send me the file and/or any more information you can gain by line-stepping. Once we track down the source line that’s causing the freeze it can probably be commented out as a quick work-around, and I can fix the issue and a send you a new CSharpatron .dll.

Regards,

Chris