Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box2D support #40

Closed
thomz12 opened this issue Apr 16, 2021 · 12 comments
Closed

Box2D support #40

thomz12 opened this issue Apr 16, 2021 · 12 comments

Comments

@thomz12
Copy link
Contributor

thomz12 commented Apr 16, 2021

In my project I'd like to use Box2D. Is it possible to port that over from Retyped.box2d?

Is that something I can look into? Are you decompiling the Retyped packages for that?

@theolivenbaum
Copy link
Collaborator

theolivenbaum commented Apr 16, 2021 via email

@thomz12
Copy link
Contributor Author

thomz12 commented Apr 16, 2021

Thanks for the info! I had I look just now and was able to decompile and get it to compile with my project.
Had a quick test that still didn't quite work, but I'll figure that out tomorrow. :)

@theolivenbaum
Copy link
Collaborator

theolivenbaum commented Apr 16, 2021 via email

@thomz12
Copy link
Contributor Author

thomz12 commented Apr 17, 2021

So unfortunatly the original package used for Retpyed.box2d is used just for node applications. (I failed to notice that before)
I can convert it so it can be loaded in a browser, but I can't get the retyped package to work with that. (in the browser)

Do you have any ideas?

@theolivenbaum
Copy link
Collaborator

To be honest my personal experience with Retyped isn't very good - for all the libraries we use, we just ended up writing our own wrapper classes - much easy to get a consistency experience than the autogenerated one from the typescript definition files used by Retyped. I'm not familiar with Box2D, but if their API is not too crazy might be worth giving it a try.

It's usually as simple as writing a C# method and directly call the javascript code with something like this:

public class MyLib
{ 
    public int DoSomething(string par1, int par2)
    {
        return Script.Write<int>("myLib.doSomething({0}, {1}", par1, par2);
    }
}

The trick is that the compiler will emit directly the parameter variables in the final javascript, so that becomes:

return myLib.doSomething(par1, par2);

If you need more complex data types, you can create lightweight classes using the [ObjectLiteral] attribute. These classes are compiled to plain javascript objects without any of the C# extra code.

@theolivenbaum
Copy link
Collaborator

Obviously, use static methods if it's a static library, or create an instance of the library and store it locally in the wrapper class if needed

@thomz12
Copy link
Contributor Author

thomz12 commented Apr 17, 2021

Thanks again for the tips!
I ended up using p2.js instead, and decompiled the retyped package for that. That seems to work now!

Would you like to make a nuget package for it? (p2) If you do, I'll open a PR tomorrow.

@theolivenbaum
Copy link
Collaborator

theolivenbaum commented Apr 17, 2021 via email

@thomz12
Copy link
Contributor Author

thomz12 commented Apr 18, 2021

Cool! I wanted to push a new branch, but it it looks like I'm not allowed to.

remote: Permission to theolivenbaum/h5.git denied to thomz12.

@theolivenbaum
Copy link
Collaborator

theolivenbaum commented Apr 18, 2021 via email

@thomz12
Copy link
Contributor Author

thomz12 commented Apr 18, 2021

Oh duh!
I've opened a PR now.

@theolivenbaum
Copy link
Collaborator

did that mistake in the past too 😄

Just merged the code, thanks for contributing!

For future reference, I'm leaving these two regexs that I used for cleaning the decompiled code:

  • Replace \s+{[\n\r]\s+get[\n\r]\s+{(.|[\s\n\r])*?}[\n\r]\s+set[\n\r]\s+{(.|[\s\n\r])*?}\s+} with { get; set; }
  • Replace \s+private .*?__BackingField;\r\n with

The final package should be published on nuget soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants