Hacker News new | past | comments | ask | show | jobs | submit login

The real Terraria code isn't much better. Here's their 3MB [decompiled] NPC.cs: https://raw.githubusercontent.com/csnxs/Terraria/67de21a27e1...

They had serious problems implementing multiplayer because they had to synchronize objects with 50KB of state every frame.




Important to note is that this code is not the source code, but rather was generated by dotPeek, a C# decompiler, as mentioned in the repo's README: https://github.com/csnxs/Terraria/


Variable names and comments are lost, but the overall control flow structure of decompiled Java and C# code closely matches the original.


Even variable names are usually not lost.


Still - does that decompilation roll back any kind of array unrolling C# compiler may be doing (assuming it's doing it)? If not, it could explain those long chains of if/else seen in the decompilation. Maybe they're arrays of constants in the real code?


From the first couple hundred lines, they obviously know how to use a switch statement (as in how it works at least), but the odd cascade of if statements at the bottom makes me question whether they know when to use a switch statement (but then again, the stuff a the top makes me wonder that a bit as well).

Edit: As someone else noted, if this is a generated file, that might explain a lot of this.


I just scrolled to the bottom and all I see is } } } } } } } } } } } } } } } } } } } } } } } } } } } } } }

What the fuck


This is decompiled code, not the original source, so much/most/all of the craziness probably comes from that.


Decompiling .NET or Java binaries usually yields code very close if not identical to the original source code, at least unless an obfuscator was used but that seems not to be the case here. Chances are very good the original source code looks almost exactly like that code, maybe with some additional comments.


In this case, it's just that the decompiler didn't think of "else if" as a single control flow construct/keyword in the way that the original programmers did.

What was no doubt written like this:

  if(a < 1){
  }else if(a < 2){
  }else if(a < 3){
  }else{
  }
Decompiled into this:

  if(a < 1){
  }else{
    if(a < 2){
    }else{
      if(a < 3){
      }else{
      }
    }
  }


I will try this tomorrow, I am pretty sure dotPeek can recognize and output else if. Might be a coding style option though.


Wouldn’t “else if {“ and “else { if” compile to identical bytecode?


Sure, but the decompiler can choose to always decompile to "else if {", which is more readable and more likely to match the original source code.


That's my point. The compiler's choice of "else if {" or "else { if" is unrelated to the programmer's choice.


It does and at least the current dotPeek 2017.2.2 decompiles it to else if and does, other than I thought, not allow to customize it.


Obviously whatever decompiler was used here decided to do it the other way.


Mind you, a lot of Java 5 and later features like generics, switch on Strings, Iterable-based for, etc., are syntactic sugar implemented in the compiler, not part of Java bytecode, so those features don't decompile well.


I am not sure about Java decompilers, but the .NET decompilers I have used all perform pattern matching in order to detect and reverse language features implemented with compiler transformations.


At least the last time I used it (2015), IntelliJ's decompiler didn't.


Per the description on https://github.com/csnxs/Terraria/ , this isn't the Terraria source code -- it's a decompilation of the binary.


Those SetDefaults methods.. ouch.

On the plus side, it's fun to press the page down and see the code 'animate' from left to right


yes, also that it comes back to the left after it hit the right end lol


This looks as if this was a compile target, not the source code.

HitEffect is a quite the function.

This reminds me of working on CDDA[1] before many of the refactors hit.

CDDA is an interesting case, it stemmed from a situation similar to the original post (one person project, embarked upon it before knowing how to do so). It was a complete mess of macros, spaghetti code and data and code living happily side by side.

https://github.com/CleverRaven/Cataclysm-DDA


It was, or still is? Also, are you still working on it?

I'm currently having lots and lots of fun playing this game (experimental builds). It's pretty much halfway there to Dwarf Fortress...


I stopped many years ago. I was working on it in between sending out job applications, I was happy to gain experience working on a code base larger than I felt comfortable reading and one that is ugly but a rewrite is not an option.

I didn't add too many features, I was mostly reducing the amount of compiler noise and fixing any bugs I ran across.


Surely this file can't be edited by humans as is, right?


Wow, I just glanced at the how they're generating random names and it's incomprehensible. What a bizarre way to do something rather simple.


My guess is they are using partial classes and generating a bunch of the code from other, much simpler files.


Oh wow. That might explain a lot of my lag I’ve had when multiplaying on large maps (on LAN).


This gave me palpitations and then killed my browser.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: