Search Unity

[OPEN SOURCE] TypeSafe - Replace 'magic' strings with automatically generated code

Discussion in 'Assets and Asset Store' started by Simie, Aug 3, 2015.

  1. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456

    One principle advantage of type safety is catching errors at compile time.
    TypeSafe brings this same advantage to your Unity-powered game when loading resources, scenes or using layers and tags.

    Key Features
    • Automatically scans your project and generates strong-typed classes than can be used to replace raw strings in your code.
    • Using these safe classes ensures that any resource, layer or scene you try and use in code always exists.
    • Moving, renaming or deleting a resource will cause compile errors at the place in code they were used.
    • Read more on the website, download on github.


     
    Last edited: Apr 30, 2022
    MostHated and mimminito like this.
  2. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    Hey all, I have 3 voucher codes to give away. If you're interested just reply here and I'll PM you one, first come first served!
     
    mimminito likes this.
  3. mimminito

    mimminito

    Joined:
    Feb 10, 2010
    Posts:
    780
    Very interesting product! Would love to give it a go!
     
    Simie likes this.
  4. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @mimminito, PM sent! Thanks for your interest!
     
    mimminito likes this.
  5. iamsam

    iamsam

    Joined:
    Dec 22, 2013
    Posts:
    233
    Would love to test it as I was planning on doing something similar for my current project. Thanks.
     
    Simie likes this.
  6. movra

    movra

    Joined:
    Feb 16, 2013
    Posts:
    566
    Curious, would this solve the magic string problem with WPF PropertyChangedEventHandler?

    Code (CSharp):
    1. private string _name;
    2. public string Name
    3. {
    4.    get { return _name; }
    5.    set
    6.    {
    7.       _name = value;
    8.       PropertyChanged("Name");
    9.    }
    10. }
    11. private void PropertyChanged(string prop)
    12. {
    13.    if( PropertyChanged != null )
    14.    {
    15.       PropertyChanged(this, new PropertyChangedEventArgs(prop);
    16.    }
    17. }
    In C# 5 that's solved with [CallerMemberName], but that's a long way off on the Unity roadmap.

    edit: Oops, now that I read the description better it seems I got a bit ahead of myself when reading "replace raw strings". :p
     
    Last edited: Aug 4, 2015
  7. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @iamsam, voucher sent via PM. Thanks for your interest!

    @movra, unfortunately TypeSafe won't help there :( In C#6 there is the nameof() operator which is perfect, but we'll have to wait for Unity to upgrade their version of Mono for that. Alas! Were you interested in having a voucher?
     
  8. movra

    movra

    Joined:
    Feb 16, 2013
    Posts:
    566
    @Simie Sure, I want to try using TypeSafe with Entitas. It's a nifty framework but you still have code like this to load prefabs:

    Code (CSharp):
    1.   public void Start() {
    2.         const string resourceName = "Opponent";
    3.         for (int i = 1; i < 10; i++) {
    4.             var speed = Random.value * 0.02f;
    5.             _pool.CreateEntity()
    6.                 .AddResource(resourceName)
    7.                 .AddPosition(i + i, 0, 0)
    8.                 .AddMove(speed, speed);
    9.         }
    10.     }
    which connects to

    Code (CSharp):
    1.  
    2. public void Execute(List<Entity> entities) {
    3.         foreach (var e in entities) {
    4.             var res = Resources.Load<GameObject>(e.resource.name);
    5.             var gameObject = Object.Instantiate(res);
    6.             gameObject.transform.parent = _viewContainer;
    7.             e.AddView(gameObject);
    8.         }
    9.     }
    TypeSafe can make that more robust, right?
     
    iamsam likes this.
  9. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @movra, Entitas looks very interesting. I'll be sure to check that out in the future. I'll be sending you a PM in a moment with a voucher :)

    @Everyone Else
    I've got just one voucher left, so next person to ask for one will be the last, for now!
     
    movra likes this.
  10. movra

    movra

    Joined:
    Feb 16, 2013
    Posts:
    566
    I have used it for only half an hour just replacing the resource path magic strings, but I can already tell TypeSafe makes development in Unity a lot more robust. Looking at the feature list I've barely scraped the surface.

    The asset itself is also very polished, just looking at details like filtering folders and the ability to customize the naming scheme.

    Thumbs up!
     
    Simie likes this.
  11. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @movra, Thanks very much for the review you left on the asset store, I'm glad you like it!
     
  12. electroflame

    electroflame

    Joined:
    May 7, 2014
    Posts:
    177
    Hey, I'd love to give this a shot if you've still got the voucher left!

    I've got something that I cooked up for myself that works pretty well on a basic level, but it's nowhere near as feature-complete (or customizable) as TypeSafe.
     
    Last edited: Aug 4, 2015
  13. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @electroflame, PM sent with a voucher code for you. Thanks for your interest!

    That's the last of my voucher codes for now, folks. Thanks for checking out TypeSafe!
     
  14. iamsam

    iamsam

    Joined:
    Dec 22, 2013
    Posts:
    233
    @Simie thanks for the voucher, will check it out today and surely leave a review in a day or two. Thanks again.

    @movra thanks for the info on Entitas, looks like a useful system and something much needed for Unity.
     
  15. electroflame

    electroflame

    Joined:
    May 7, 2014
    Posts:
    177
    I said thanks in my return PM, but I totally spaced and forgot to thank you in this thread. Thanks @Simie!
     
  16. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
  17. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    Hey all, I just submitted a quick bug fix release for approval on the asset store. Should be up in a few days.

    Changelog:

    1.0.1
    ----------

    New:
    - Added documentation comments to generated resource folder methods (GetContents, etc).

    Fixes:
    - Welcome window no longer shows up after the first time
    - Fixed Editor-only resources being included in scan
     
    movra likes this.
  18. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    1.0.1 is been approved and is now available on the asset store.

    The next version, 1.1.0, will have a system for hooking external data into TypeSafe so you can generate classes with your own data. This should be submitted within a few days and be available shortly after.
     
    iamsam and movra like this.
  19. iamsam

    iamsam

    Joined:
    Dec 22, 2013
    Posts:
    233
    This is an extremely polished asset store product which will really make your code robust and bug free. Extremely easy to use and is very polished. I was working on something similar but after trying this last week, I can say a million thanks to the author for creating the plugin. I fully recommend it :).

    PS. I am sorry for the late update, will put a review on the asset store too.
     
    Simie likes this.
  20. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @iamsam, thanks for the kind words! I'm really pleased you have found TypeSafe useful.

    I've just submitted version 1.1.0 to the asset store. A big thanks to @electroflame for helping me test the custom data source part of this release. Changelog is below, and it should be approved and available on the asset store within about a week.

    1.1.0
    ----------

    New:
    - Added ITypeSafeDataSource interface for using TypeSafe for custom data. See documentation for details.
    - Added TypeSafeApi static class for invoking TypeSafe scan/compile process via script.
    - PrefabResource.Instantiate(Vector3) overload added (no rotation)

    Fixes:
    - Fixed "Object has been cleaned up" warning when saving a scene with TypeSafe enabled.
     
    Last edited: Aug 13, 2015
    movra likes this.
  21. electroflame

    electroflame

    Joined:
    May 7, 2014
    Posts:
    177
    Hey now, just to be clear, @Simie did all of the hard work -- I just told him when it didn't work, or when I'd broken it. :)
     
  22. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    Hey folks, 1.1.0 has been approved and is now available on the asset store for download! Documentation is live on my website for the new features.
     
    Last edited: Aug 18, 2015
    mimminito likes this.
  23. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    TypeSafe 1.1.1 has just been submitted to the asset store. As always, changelog is attached below.

    Be sure to check out our other asset, SRDebugger, for on-device development and diagnostics.

    1.1.1
    ----------

    New:
    - Added Unload() method to resources.
    - Added UnloadAll() and UnloadAllRecursive() methods to resource folders.

    Improvements:
    - Resource wrappers now cache with a weak reference, so they do not prevent Resources.UnloadUnusedResources() working.
     
    movra likes this.
  24. Dan2013

    Dan2013

    Joined:
    May 24, 2013
    Posts:
    200
    @Simie
    I just buy your SRDebugger and use it for several hours.
    It is really the best debugging-and-tweaking-on-real-device tool I can find.

    With high expectation, I also have a look at this TypeSafe.
    TypeSafe will have a pro/plus version that may allow paid users to access its source code?
    I think using TypeSafe makes some strong dependencies on it. Accessing source code may make users feel a little bit more comfortable.
     
    Last edited: Sep 8, 2015
  25. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    Hey @Dan2013, thanks for purchasing SRDebugger. I'm really glad you like it!

    Source code to TypeSafe is available for free to anyone who has purchased. Simply send me an email at contact@stompyrobot.uk with your TypeSafe invoice number and I will send over the latest source code snapshot. I'm hoping to automate this in the future, but for now it's a manual process.
     
  26. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    This looks amazing. Is there any easy way to filter out object types? As in, I would not want to have every model file, every texture, added into the code generation, but only have prefabs and materials. Maybe textures, but anyways, a selective filter by type. I didn't see any mention of it, but I have thousands of assets in my folder when you include everything, whereas only a hundred or so prefabs.
     
  27. Dan2013

    Dan2013

    Joined:
    May 24, 2013
    Posts:
    200
    @Simie
    Awesome! I will definitely buy TypeSafe. Hope that automatic process can be setup soon. :)
     
    Simie likes this.
  28. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    Hi @Plutoman, there isn't currently a way to filter object types. Nice idea though, and I'm pretty inspired for a GUI to configure that. I've added it to the todo list!

    Keep in mind that only assets in Resources folders are included by TypeSafe, so it won't be all your assets included in the generated code.
     
  29. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    Obviously not, but my game is fully procedural, thus everything is in the resources folder for it to actually be included in the game build. That also makes asset management even more important; every asset we use has to be linked in code => prefab. Heck, we have to keep shaders in there even because no materials / shaders are ever referenced by the scene, just a starting manager that instantiates our GUI and goes from there, and camera scripts.

    I went ahead and picked it up, but if you could either add that in, or I could do it with source code. Editing always sucks though 'cause then I'm merging conflicts later.
     
  30. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @Plutoman, I'll be adding it in the next update that I'll start work on tomorrow. Shoot me an email at contact@stompyrobot.uk if you'd like to take a look at the source code yourself in the mean time.

    Thanks for purchasing!

    EDIT: It also occurs to me that with a system like yours you will need the new version (currently pending approval, will probably be approved in the next few days). It uses WeakReferences to cache loaded resources so they aren't counted as "used" when using Resources.UnloadUnusedResources(). If you send me an email at the above address I'll send you the latest version.
     
  31. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    That's fine, I'm going to heading off to a wedding for the next week, so today and tomorrow are my last work-days for the next week. I'll just check in then and implement it into my system afterwards! I currently have a make-shift AssetLink class where I linked everything in a singular manager by string, and referenced from that manager. This will be far cleaner.
     
  32. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    Version 1.1.1 has been approved and is now available on the Asset Store!

    Changelog:

    1.1.1
    ----------

    New:
    - Added Unload() method to resources.
    - Added UnloadAll() and UnloadAllRecursive() methods to resource folders.

    Improvements:
    - Resource wrappers now cache with a weak reference, so they do not prevent Resources.UnloadUnusedResources() working.
     
  33. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    Hey guys, TypeSafe is participating in the Sensible Sale this month. 35% off the usual price! Don't forget to check out some of the other great assets in the sale also.
     
  34. mwgray

    mwgray

    Joined:
    Feb 18, 2015
    Posts:
    5
    Hello! I just grabbed TypeSafe during the sale. So far it seems like a great product, but there's some usability issues I have with it that prevents me from using it.
    • The main issue is the unclear way in which the scanner determines an item to be wrapped. When I first imported the project, all the generated files were empty. After ~30 minutes I learned about the required places for files(Resources in Resources, Prefabs in Prefabs, Scenes in build settings). This was rather frustrating as it forces a required structure on the project. In addition there was no real feedback (in the tool) as to why the generated files were empty. The project I'm working in doesn't use Resource/Prefab folders, so nothing was included. Some feedback during the scan would be helpful but ideally, I think it would be solved in conjunction with the next issue.
    • Lack of inclusion filter. I'm using ~10-15 different asset store packages, so I need to have an exclusion filter for each one. Rather than do that I'd prefer to say which folder to include. Additionally, all of my project resources are under one folder at the base, _assets/. All third party items stay out of that folder. Ideally I should just let TypeSafe know to scan only this folder, rather than exclude all others. In conjunction with the previous issue, I'd prefer just to have folder lists for each scannable item type to scan. That way the rules are clear from the beginning where the scanner is getting the items. It seems like this feature is in the pipes, I just hope in the process it can help remedy the issue above as well.
    • Another oddity I noticed is that Prefabs must be in a prefabs folder. However if the prefab is in a resource folder, it appears under the Resources namespace as a prefab.
    • Finally, much to my dismay, I found out TypeSafe doesn't actually do what I was expecting it to. The main reason I purchased it is to get rid of magic strings, and for the most part it does just that. However, I was hoping to get rid of magic strings within scenes as well. When binding code to assets at runtime, I do a lot of GameObject.Find calls. It'd be nice if there were a way to 'deep scan' of a scene so that the scanner generates id files for a given scene. Even better would be to have a way to bind a scene at runtime so all the references are pointing to objects in an actual scene.
    Despite all this I think it's an asset with great potential!
     
  35. cavila

    cavila

    Joined:
    Jun 4, 2013
    Posts:
    67
    Hello,

    I'm encountering issues after moving this asset to the /Plugin folder. It seems the settings.asset file gets reset quite frequently, which causes the welcome screen to keep popping up. Would it be possible to configure the resource location for the settings or allow TypeSafe to work from the /Plugin folder?
     
  36. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @mwgray, thanks for the incredibly detailed feedback of your first impressions, it's extremely helpful. I'll likely make some changes to the product page to make things bit clearer. I'll go over a few of your points...

    Resources
    TypeSafe is designed only to replace usages of Resources, not all assets in your project. (See the Resources page in the Unity docs). This is why only assets in resources folders are picked up by the TypeSafe scan, they are the only assets that can be loaded dynamically at runtime (usually via a string path, which is what TypeSafe seeks to replace). I intend to make this clearer on the product page.

    Prefabs Folder
    I'm not sure why prefabs would only work if in Prefabs folders, they should work if they are within a Resources folder. This might be a bug.

    Scenes
    TypeSafe only includes scenes listed in the Editor Build Settings as they are the only scenes that can be loaded at runtime. (i.e. included in the final build). Listing scenes that are not included in the editor build settings would result in a lot of errors at runtime.

    Scene Hierarchy
    This is a feature I considered, but I felt like it would be too problematic if the scene hierarchy changed too much at runtime and the generated code no longer matched up with the resulting scene structure. I won't rule it out for a future version, but it's not looking like I can do it in a way that feels robust.

    Inclusion Filter
    Yes, a white-list is on the roadmap! This has been requested a few times, and I definitely see the value of it.

    Thanks again for your feedback.

    @cavila,

    I've just tested moving the TypeSafe plugin around a project, and into a Plugins directory, and it works as expected for me. Could you take a screenshot of the folder structure around the TypeSafe plugin in the project browser so I can see if you've found an edge-case bug?



    Note: I'm going to be away over this weekend from Friday morning to Monday evening. I may be slow to reply to support requests until I get back. My apologies for any inconvenience.
     
    Last edited: Sep 17, 2015
  37. cavila

    cavila

    Joined:
    Jun 4, 2013
    Posts:
    67
    Hi @Simie,

    Here's how the tree looks like:

    Screen Shot 2015-09-17 at 12.58.40 PM.png

    An easy way to reproduce the issue is to have a blank project with the above and do Assets -> Reimport All. It triggers the welcome window and resets the settings every time.
     
  38. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @cavila, it looks like it has put the usr/ folder in an Editor directory. This shouldn't happen.

    Can you change the hierarchy so that the Editor folder for TypeSafe is in the same directory as TypeSafe.dll? The plugin automatically derives it's location in your project from TypeSafe.Editor.dll, so requires TypeSafe.dll to be in a non-editor directory one directory up. Basically just copying the StompyRobot folder into the Plugins directory should work fine. Is there a reason you can't use that structure?
     
  39. cavila

    cavila

    Joined:
    Jun 4, 2013
    Posts:
    67
    It exhibits the same issue with the following structure:

    Screen Shot 2015-09-17 at 1.17.42 PM.png

    that's how you are suggesting it right?

    The reason why I had it the other way is because Unity requires that /Plugins and /Plugins/Editor contain different sets of scripts, as explained in the Compilation Order article. Editor scripts in /Plugins/{Asset}/Editor don't compile. The fact that TypeSafe works this way at all is because the editor code is already compiled.

    I keep all my other Asset Store products in the /Plugins folder. Cuts compile time by up to 80%.
     
  40. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @cavila

    I can't seem to reproduce the settings reset issue (with Reimport All) with any of the structures you have posted. Can I ask which version of Unity and TypeSafe you are using? I've tested with Unity 5.0.4 and 5.2.0 and can't reproduce. Also, if you could send me the TypeSafe.log from {YourProject}/Temp/TypeSafe.log so I can see if there is anything strange going on (either via email or pastebin).

    I wasn't aware of the special case regarding editor folders and Plugins/, I assumed it behaved the same as other Editor folders. I will modify the usr/ folder to derive its position from TypeSafe.dll instead of relative to TypeSafe.Editor.dll to allow you to have the correct structure for the next update.

    Unfortunately I'm going to be away from tomorrow until Monday evening, so I might not be able to take a good look at this until I get back. My apologies for that.
     
  41. cavila

    cavila

    Joined:
    Jun 4, 2013
    Posts:
    67
    That's interesting, I'm using unity 5.1.3f1 so that might be it. I'll paste the output later on.

    Have a safe trip :)
     
  42. cavila

    cavila

    Joined:
    Jun 4, 2013
    Posts:
    67
    Here's the log file after an initial import (installation) and a subsequent "Reimport All" action that triggered a reset and a welcome screen for a second time:

    http://pastebin.com/DLVt58cQ
     
  43. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    How's the performance on very very big projects? > 20GB have u tested with the scanning?
     
  44. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    Regarding Scenes, it would be nice to have the option, to not throw errors if a missing scene is referenced.

    When projects get large, and contain many scenes, build times can really add up. Sometimes, when adding new features, testing ui, measuring performance, or testing new platforms, it can be useful to remove all the scenes you don't really need, to get faster build times. It would be nice if you could do this, without the project lighting up :)

    Bonus points if it's configurable via code:
    TypeSafe.IncludeMissingScenes = true;

    Other than that, awesome plugin, thanks for making it!
     
    Last edited: Sep 22, 2015
  45. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @imtrobin, performance on large projects should be good. I've tested TypeSafe with various large asset packs on the Asset Store and have gotten very good performance.

    @cavila, I'll get down to trying to sort out what is going on here soon. Thanks for the log file.

    @shawnblais, to workaround this problem you could disable automatic regeneration, so TypeSafe doesn't trigger a regen when it detects your active scenes changing. I'll look into adding an option to select what changes trigger a regeneration for a future update. I'm hesitant to include non-active scenes in the scenes list since it would break at runtime.
     
  46. gskinner

    gskinner

    Joined:
    Aug 28, 2014
    Posts:
    43
    I think as long as it's optional, and throws a big fat warning it should be ok.
     
  47. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @gskinner, I could add an option include unchecked scenes but have them marked with [ObsoleteAttribute] so they create compiler warnings. Would that work for you?
     
  48. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    Back to work and checking back in - how close is an exclusion filter? Ending up with quite a lot of updates and work to set up, so again, no rush.
     
  49. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @Plutoman, it will be in the next update that I'm working on right now. I can send you the latest build when I submit to the asset store so you can get it a bit sooner, if you like.

    @cavila, I've been looking into the Settings bug but I'm at a bit of a loss as to what might be causing it. I'm including some extra logging in the next version, as well as changing how the paths are worked out so your Plugins directory changes work. Hopefully that will either give some insight into the problem or fix it altogether.
     
  50. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    456
    @cavila, I've been able to reproduce that settings bug. It looks like it only occurs on the Mac Unity Editor. I'll make sure it's fixed for the next version.