xpenatan / gdx-teavm

Run Libgdx in a webbrowser with teavm
Apache License 2.0
108 stars 16 forks source link

Obfuscation not Working #63

Closed noblemaster closed 1 year ago

noblemaster commented 1 year ago

When I enable obfuscation, I get the following error:

Uncaught SyntaxError: Unexpected token 'do' (index):8 Uncaught ReferenceError: main is not defined

Is that a bug? I checked the code, but couldn't quite figure out how it got that way? It seems the main function/entry point is not generated (or obfuscated)?

image

EDIT: more details provided!

xpenatan commented 1 year ago

Is your code something close to this? using VMTool to set obfuscated?

` @SkipClass public class BuildUITest {

public static void main(String[] args) {
    TeaBuildConfiguration teaBuildConfiguration = new TeaBuildConfiguration();
    teaBuildConfiguration.assetsPath.add(new File("../desktop/assets"));
    teaBuildConfiguration.webappPath = new File(".").getAbsolutePath();

    TeaVMTool tool = TeaBuilder.config(teaBuildConfiguration);
    tool.setObfuscated(true);
    tool.setMainClass(UITestLauncher.class.getName());
    TeaBuilder.build(tool);
}

}

`

Possible to reproduce in of the examples ? https://github.com/xpenatan/gdx-teavm/tree/master/examples/core/core/src/main/java/com/github/xpenatan/gdx/examples/tests

noblemaster commented 1 year ago

Yes, that's what I use. Setting tool.setObfuscated(...); to true simply produces a JavaScript file that doesn't run. I'll probably try the examples later.

I think that's something on TeaVM's end (not gdx-teavm). I was just wondering if it's just me or everyone doesn't get it to work with obfuscation?

xpenatan commented 1 year ago

That's interesting, the last time I check all examples was working with Obfuscation true.

Try adding main class name to classesToPreserve in TeaBuildConfiguration.classesToPreserve.add("x.y.z.Class') or in tool.getClassesToPreserve().add(..).

Maybe there is a bug when its a bigger project 🤔

I think if you generate your game jar obfuscating with proguard only your game classes and then use this jar in teavm it will work.

noblemaster commented 1 year ago

Try adding main class name to classesToPreserve in TeaBuildConfiguration.classesToPreserve.add("x.y.z.Class') or in tool.getClassesToPreserve().add(..).

I did, but there error didn't change. The main method should automatically preserved for what I gather.

I'll ask over at teavm later than, as it seems it might not be related to gdx-teavm.

xpenatan commented 1 year ago

Maybe updating teavm will fix it. The backend is using version 1200. The last time I tried to update it was giving errors and then I reverted back to v1200.

noblemaster commented 1 year ago

I tried that, but it didn't work. I'm not sure, but I don't think teavm-cli will still be available in the future? It currently only goes to dev-1209, while e.g. teavm-core already goes to dev-1212. Also, I wasn't able to build it anymore via gradle. I cloned the teavm project.

Something like this will work in your build.gradle:

includeDep "org.teavm:teavm-tooling:$project.teaVMVersion"
includeDep "org.teavm:teavm-core:$project.teaVMVersion"
includeDep "org.teavm:teavm-classlib:$project.teaVMVersion"
includeDep "org.teavm:teavm-jso:$project.teaVMVersion"
includeDep "org.teavm:teavm-jso-apis:$project.teaVMVersion"
includeDep "org.teavm:teavm-jso-impl:$project.teaVMVersion"
konsoletyper commented 1 year ago

@noblemaster can you please share short piece of generated JavaScript, so that I could analyze it and fix the issue?

@xpenatan, you can always reach me directly to ask for help in fixing TeaVM issue in your project. What was the issue with 0.7.0-dev-1212? Is there a way for me to reproduce it?

noblemaster commented 1 year ago

That's the beginning of the code (probably fine).

"use strict";
var main;(function($rt_globals){
var $rt_seed=2463534242;function $rt_nextId(){var x=$rt_seed;x^=x<<13;x^=x>>17;x^=x<<5;$rt_seed=x;return x;}function $rt_compare(a,b){return a>b?1:a<b? -1:a===b?0:1;}function $rt_isInstance(obj,cls){return obj!==null&&!!obj.constructor.$meta&&$rt_isAssignable(obj.constructor,cls);}function $rt_isAssignable(from,to){if(from===to){return true;}if(to.$meta.item!==null){return from.$meta.item!==null&&$rt_isAssignable(from.$meta.item,to.$meta.item);}var supertypes=from.$meta.supertypes;for(var i=0;i<supertypes.length;i
...

Here is the line where it fails. There is a variable with name do which is a reserved keyword in JavaScript (for the do-while loop). I believe the variable name generator should probably prevent creating variables called do.

...
A.FJs=function(a,b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,ba,bb,bc,bd,be,bf,bg,bh,bi,bj,bk,bl,bm,bn,bo,bp,bq,br,bs,bt,bu,bv,bw,bx,by,bz,bA,bB,bC,bD,bE,bF,bG,bH,bI,bJ,bK,bL,bM,bN,bO,bP,bQ,bR,bS,bT,bU,bV,bW,bX,bY,bZ,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b$,b_,ca,cb,cc,cd,ce,cf,cg,ch,ci,cj,ck,cl,cm,cn,co,cp,cq,cr,cs,ct,cu,cv,cw,cx,cy,cz,cA,cB,cC,cD,cE,cF,cG,cH,cI,cJ,cK,cL,cM,cN,cO,cP,cQ,cR,cS,cT,cU,cV,cW,cX,cY,cZ,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c$,c_,da,db,dc,dd,de,df,dg,dh,di,dj,dk,dl,dm,dn,do,dp,dq,dr,ds,
...

I had another look at teavm and removed the letter o from the abcdefghijklmnopqrstuvwxyz strings that generate the variable names. Actually removing o from the following the strings in the following files works now. I can now obfuscate the game.

I could submit another pull request, but I still have that other one pending (same branch, so it would mix in).

xpenatan commented 1 year ago

What was the issue with 0.7.0-dev-1212? Is there a way for me to reproduce it?

Wrong gradle include and old jetty plugin I guess.

Using @noblemaster gradle info build is working fine now.

noblemaster commented 1 year ago

From what I saw, team-cli is gone in the latest releases. Not sure if that's intentional. The above list is what I saw was used in another project, so I'll assume that's it?

konsoletyper commented 1 year ago

@noblemaster teavm-cli is gone for purpose. For what do you need it?

noblemaster commented 1 year ago

@konsoletyper I don't really needed. It's just what was included in gdx-teavm. I wasn't quite sure which to pick instead of teavm-cli. I assume the following is it. Or just one of them?

includeDep "org.teavm:teavm-tooling:$project.teaVMVersion"
includeDep "org.teavm:teavm-core:$project.teaVMVersion"
includeDep "org.teavm:teavm-classlib:$project.teaVMVersion"
includeDep "org.teavm:teavm-jso:$project.teaVMVersion"
includeDep "org.teavm:teavm-jso-apis:$project.teaVMVersion"
includeDep "org.teavm:teavm-jso-impl:$project.teaVMVersion"
konsoletyper commented 1 year ago

@noblemaster assuming that I don't know what includeDep is and what actually you are doing with these dependencies, I can't answer your question.

noblemaster commented 1 year ago

My bad, I thought includeDep was yet another gradle functionality I'm not familiar with. However, looking into it, it's something that exists solely in gdx-teavm.

@xpenatan maybe you can explain it?

xpenatan commented 1 year ago

includeDep is a custom solution to add the libraries to pom when uploading snapshots or release version. When you import backend-teavm gradle implementation, it also download/include all required teavm libraries.

Without this you will need to manually add teavm sdk implementation in all projects using backend-teavm.

implementation "org.teavm:teavm-tooling:$project.teaVMVersion"
implementation "org.teavm:teavm-core:$project.teaVMVersion"
implementation "org.teavm:teavm-classlib:$project.teaVMVersion"
implementation "org.teavm:teavm-jso:$project.teaVMVersion"
implementation "org.teavm:teavm-jso-apis:$project.teaVMVersion"
implementation "org.teavm:teavm-jso-impl:$project.teaVMVersion"
xpenatan commented 1 year ago

Hey @noblemaster Is this fixed with 0.8.0-dev-1 ?

noblemaster commented 1 year ago

Yes, that's fixed, I just forgot to close it.