
SimCity UI Code + DRM - _fs
https://gist.github.com/anonymous/5133829
======
oinksoft
Good to see people using the Closure Tools. It really is a powerful toolkit.

Just a reminder that I (and some others, hopefully) are in ##closure-tools on
Freenode to help you out if you have questions. Make sure to stay in the
channel if you ask, as it's low-volume. I don't believe I've let a question go
unanswered in well over a year, unless the person left right away and there
was no point in answering it.

The channel itself is in no way affiliated with Google, nor am I.

------
gee_totes
What parts of SimCity were written in Javascript?

(Before you say "The UI, obvs", I'm wondering if it's a common practice to
write UI code in _games_ in javascript. I know that Gnome has a lot of JS
that's tied into UI, I didn't expect the practice to make itself into game
dev).

And if this code is actually from SimCity, does SimCity have some type of v8
or other js engine packaged with it?

~~~
Bjartr
>I'm wondering if it's a common practice to write UI code in games in
javascript

It's becoming more common. Libraries like Awesomium[1] make it trivial to
render browser content to an in-game texture and the V8 engine, while not
'easy' to integrate, is quite empowering in that you can have the UI code call
directly into your compiled code.

>does SimCity have some type of v8 or other js engine packaged with it?

Very probably. Most games have some scripting layer in their engines to
simplify implementing non-performance-critical game logic. Lua and JS are two
of the most popular languages for achieving that these days.

[1] <http://awesomium.com/>

~~~
notdan
FWIW, we were much happier with Chromium Embedded [1] than Awesomium.
Awesomium is not free for non-commercial use (something like $8,000+ if you
wanted source code included, the price of the software isn't even listed on
the site now), the support was terrible, and the product seemed to be not
nearly as fleshed out at Chromium Embedded. The website is prettier though...

We switched to using Chromium Embedded 6-8 months ago and haven't regretted it
a bit.

[1] <https://code.google.com/p/chromiumembedded/>

~~~
TheMakeA
We are starting to use Chromium Embedded for all of our in-game UIs, after
going through Flash and (for a brief period) CEGUI. No major regrets here. In
fact, we like HTML so much that we are considering moving our game tools
(level editors, etc) to HTML as well, ala Insomniac [1].

[1] [http://www.insomniacgames.com/new-generation-of-
insomniacgam...](http://www.insomniacgames.com/new-generation-of-
insomniacgames-tools-as-webapp/)

------
asveikau
How is it cool to put someone else's non-free copyrighted work on github?

I'm not a fan of DRM or what they did to Sim City... I've also got nothing
against reverse engineering, excerpting code snippets, and that sort of thing.
But violating the terms of GitHub, in addition to someone's copyright? Not
cool.

~~~
belorn
The uploader has a decent argument of fair use.

Its uploaded in a nonprofit way, for educational purposes. It is a very small
portion of the complete work. The effect of the use upon the potential market
is either small or non-existant.

That is three out of the four-factor balancing test regarding fair use. If we
guess that this is about 0.3% of the complete work (3MB of a very very large
final product), that would be comparable to use 30s clip of a film.

~~~
asveikau
Do you feel confident enough in this assessment that you'd feel comfortable
slapping a GPL license on this thing and using it in a project? (Nevermind for
a moment that this code isn't at all re-usable.) I feel like that's more or
less the standard that should apply to github. It's kind of a disservice to
people who are using github for open source but are making effort to avoid
this kind of taint. Maybe it's hyperbole but it makes it one step closer to
github being "that place where I pirate simcity".

~~~
belorn
I would feel the same confidence regarding a 30s clip of a movie. That is to
say, I would not feel confident at all, but that has nothing to do with the
law and all to do how it is interpreted. I do however think that 30s clip of a
3hr movie, or 0.3% of the source code for a game should be small enough that
fair use arguments can be made.

While github is often used as a publishing tool, it is also commonly used as a
place where work-in-progress is placed. Thus, if someone wanted to create a
commentary about a piece of copyrighted material (say, the DRM part of a
game), I would personally consider copying the related code into a github
repository for refinement to be fair use. So long the four-factor balancing
test is fulfilled, why limit the available tools? Should the workplace where
such refinement is taking place be limited only as private repositories where
each party has a copyright license?

I would also like to note that nowhere in the law does it say that fair use
must minimize their usage of the copyrighted work. The law says under its
four-factor balancing test:

    
    
      the amount and substantiality of the portion used in relation to
      the copyrighted work as a whole.
    

How it is interpreted has changed widely in the last 15 years. Some talk about
the minimal necessary amount for his or her intended use. Other talks about
"the heart of the work". For a time, _De minimis_ was considered, through was
later reversed in 2005.

------
comex
Misleading title. This is obviously a formatted version of minified JS,
presumably extracted from the shipping game, not a leak.

~~~
toufka
obviously? care to share for those of us who don't read JS fluently?

~~~
lmkg
In general, the code is dense and difficult to read. If you know what to look
for, there are a few idioms scattered around that stand out as awkward, but
save a few characters here and there. There are some pretty obvious examples
right at the beginning, lines 2 and 4:

    
    
      var COMPILED = !0
      goog.DEBUG = !1
    

As you would expect in a weakly-typed C-style language, !0 == true, and !1 ==
false. JavaScript has first-class booleans, and there's not much to be gained
by using one of these expressions instead of the literal it evaluates to. It
only serves to save character count.

Here's another example. Starting at line 91, you get the following:

    
    
      !COMPILED && goog.ENABLE_DEBUG_LOADER && (goog.included_ = {}, goog.dependencies_ = { // ... and so forth
    

The above code uses commas in a particular way that usually means it comes
from a minifier. JavaScript has a comma operator, which is basically just a
statement separator, but it has the interesting property that it joins two
statements as a single expression. As in many C-style languages, control flow
operators like _if_ and _while_ don't require curly braces, and apply to the
immediately following statement if they are absent. The combination of these
two means that

    
    
      if(!foo){bar();baz();qux()}
    

is equivalent to

    
    
      if(!foo)bar(),baz(),qux();
    

which is shorter by a single character. Because the line of code joined by
commas is now an expression, this also lets us abuse the short-circuit
property of boolean operators, re-writing the above like so:

    
    
      foo&&(bar(),baz(),qux());
    

for a further savings of one more character.

You pretty much never see the comma operator in code written by humans,
because it's not useful for anything. Seeing them in production code,
especially the way shown above, almost always means the code came out of a
minifier. That actually broke the Opera browser a few months back--apparently
the parser ran out of stack space when handling a single expression consisting
of over 1,400 comma-separated statements.

~~~
bdc
On the subject of minification, I see a lot of lines that look like this[1]:

    
    
      fireToken : 1 === f.tradeTokens.data.fireToken ? !0 : !1,
    

Why might that be a desired output from a minifier? Why not simply

    
    
      fireToken : 1 === f.tradeTokens.data.fireToken,
    

[1] [https://gist.github.com/anonymous/5133829#file-simcityui-
js-...](https://gist.github.com/anonymous/5133829#file-simcityui-js-L11890)

~~~
ajanuary
The original code could have been written as "condition ? true : false" and
it's just not a pattern the minifier picks up on.

------
mike503
Curious. Decided to explore.

<http://telemetry.simcity.com/simcity/rest/user/1> \- user ID 1

<http://telemetry.simcity.com/simcity/rest/user/1/friends> \- their friends

but I thought friends can only seem to see each other on their respective
server region. this seems to be storing centralized data?

playing around with other nouns...

<http://telemetry.simcity.com/simcity/rest/region/1> \- even includes some of
their S3 URLs.

<http://telemetry.simcity.com/simcity/rest/box/2> \- seems list CITY and
REGION

i'm trying to think of other nouns to throw in there.

<http://telemetry.simcity.com/simcity/rest/user/1027/regions> \- a list of
user ID 1027's regions

<http://telemetry.simcity.com/simcity/rest/user/10278/regions> \- this guy has
4 regions!

(I love RESTful services)

------
SchizoDuckie
3.5 mb of Javascript. Glug. Looks like just the game ui. I cannot really find
references to DRM like the title says.

Lots of nice references to cheats though! They all rely on simcity.DEBUG being
true,

    
    
      if (simcity.DEBUG)
          switch (a) {
          case scrui.kKeyCodeDigit1:
            c && (e && !d) && scrui.RunCheat("scgameui -unlockalltools");
            break;
          case scrui.kKeyCodeLetterW:
            c && (e && !d) && simcity.CreateUnit("T_RCI_AddHourlyIncomeCheat")
          }
    

Also pretty nifty: There's a rest api built into the game for communication,
(as wel as websockets) and the name and email of one of the programmers are
still in the code.

    
    
      simcity.TutorialFakeUserData.creator = {
        uri : "/rest/user/0",
        tutorialState : 1,
        id : 0,
        screenName : "Chet Keno",
        email : "mayorkeno@simcity.com",
        nucleusId : 0,
        lastLogin : 0,
        isOnline : "true",
        avatarImage :     "Graphics/Tutorial/tutorial_avatar_keno.png",
        cities_count : 1
      };
    

Also interesting is that it seems that this is not just a stripped v8 engine.
There's things in the code that make me think the whole game UI is actually a
pretty decent webkit engine:

    
    
      simcity.gEventManager = new simcity.cEventManger;
      simcity.gEventTypes = {
        CLICK : "click",
        MOUSEUP : "mouseUp",
        MOUSEDOWN : "mouseDown",
        MOUSEOVER : "mouseOver",
        MOUSEOUT : "mouseOut",
        ITEMSELECTED : "itemSelected",
        KEYUP : "keyup",
        KEYDOWN : "keydown",
        DATACHANGED : "dataChanged",
        HASHCHANGED : "hashChanged",
        GENERICEVENT : "genericEvent",
        SOCKETEVENT : "socketEvent",
        SOCKETCONNECT : "socketConnect",
        SOCKETDISCONNECT : "socketDisconnect"
      };
      simcity.SocketManager = {};
      simcity.cSocketManager = function () {
        this.mSocketListeners = [];
        this.mSocketRequests = [];
    
    

_update:_ It could be leftovers, but this indicates that this is code from a
prerelease/debug build.

    
    
        simcity.SetUpDemoScreen = function () {
          simcity.persistentLayout = gUIManager.LoadLayout("Layouts/PersistentPreAlpha.js", gUIManager.GetRootWindow());
          simcity.SetTextOnElement(simcity.DemoExitButtonTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7bc9", "EXIT"));
          simcity.SetTextOnElement(simcity.DemoAvailableReleaseDateTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7bb2", "AVAILABLE FEBRUARY 2013"));
          simcity.SetTextOnElement(simcity.DemoThanksForPlayingTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7b4b",
          "THANKS for PLAYING"));
          simcity.SetTextOnElement(simcity.DemoPreOrderTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7bb1", "PRE-ORDER AT SIMCITY.COM"));
          simcity.SetTextOnElement(simcity.DemoNotFinalSoftwareTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7b3c", "Not Final Software"))
        };

~~~
kevingadd
Yeah, it's not just v8. It's EAWebKit, a WebKit fork EA has been using in
various games (You can find the source releases on their site).

------
devindotcom
It would be an interesting project to offline the game - but I do wonder how
hardcore the calculations are that run it. Would it be realistic to run the
game itself and the server-side simulation stuff (nulling input from other
cities) on an ordinary desktop computer? Or would you definitely need a
secondary machine?

~~~
nwh
I highly doubt there's any significant calculation. If there was, EA would
have to rent hundreds of thousands of EC2 instances to cover the running of
the game.

~~~
thristian
On the other hand, if there's no significant calculation on the server, why
would EA pretend they were having massive load problems? Sure, maybe they
wanted to get some headlines about 'SimCity is so popular it's having launch
shortages', but there's got to be a happy medium between "friendly headlines
talking about SimCity's popularity" and "seething EA hatred plastered across
the Internet".

~~~
thethimble
I think it was just poor scalability engineering. They didn't expect that many
users on launch and they didn't implement a system that could scale upwards.

~~~
bluedanieru
They knew exactly how many users to expect, I'm sure. But you aren't going to
pay for a solution to handle all of them when you expect the demand to taper
off after a few days.

------
MartinZhel
I'm wondering if they are going to do the same with other games. I'm really
happy EA decided to use JS to build the UI for the new Sim City game.

At the moment my team is working on a solution that allows other game dev
companies to do the same thing and I'm glad to see big players taking a step
in that direction.

------
pooriaazimi
There's lots of instances like

    
    
        if (something....)
            return !0;
        else
            return !1;
    

is there any reason why they aren't simply using `true` and `false`?

(because !1 is defined to return literally `false` and !0 to return `true`)

~~~
esrauch
Likely was "true" in the original source (and the source in separate files)
and was replaced by some minifier. Not clear if these are the files verbatim
or if it was automatically formatted from minified js.

~~~
jschorr
It is one of the peephole optimizations that Closure Compiler performs to save
some space (read: 2-3 characters) in the final output:
[http://code.google.com/p/closure-
compiler/source/browse/src/...](http://code.google.com/p/closure-
compiler/source/browse/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java).
The compiler actually has a lot of cool little tricks like that to save space
(since saving even a few bytes for a property like, say, Gmail, can translate
to very large numbers very quickly)

------
jtheoof
I've created a gist containing a resume of the architecture used for simcity
object if anyone is interested. <http://goo.gl/eExRF>

------
mandariini
so there is some browser based simcity or is this ui for the last released(and
controversial) game?

------
Mahn
Is that all of the server-side logic of the game?

~~~
SleekoNiko
No, that's Javascript that handles the user interface.

------
drivebyacct2
At the very least it seems cut off...

~~~
ghayes
Github lists a non-truncated version [1]

[1]
[https://gist.github.com/anonymous/5133829/raw/9340a6fdc35453...](https://gist.github.com/anonymous/5133829/raw/9340a6fdc354537b878291f591987e27c1605335/SimCityUI.js)

------
goggles99
Perfect example of how JavaScript is not good for app programming. It is great
for little tasks, but does not scale well. Please create a standard for a
browser VM instead of this JS as ASM workaround that everyone is starting to
use.

~~~
espadrine
That's the exact contrary.

What didn't scale was the server-side. The client-side is just fine.

As for the idea of a browser VM, it is a world we live in. Welcome to the
wonders of Java applets.

