简体   繁体   English

生成代码与文件格式

[英]Generating code vs file format

So my friend and I had an argument that we couldn't resolve. 所以我和我的朋友有一个争论,我们无法解决。 He is writing a general purpose web game library and a map editor. 他正在编写通用网络游戏库和地图编辑器。 The map editor saves maps as XML, but the map editor can also load a Lua script that exports the details of the map into a Javascript file that looks something like this(he didn't want to post the code, so this is just a snippet with the names changed): 地图编辑器将地图另存为XML,但是地图编辑器也可以加载Lua脚本,该脚本会将地图的详细信息导出到看起来像这样的Javascript文件中(他不想发布代码,因此这只是一个名称已更改的代码段):

// This probably isn't valid code, but this is the idea of the code generator
(function() {
Game.Level1 = function (state) {
    GameEngine.Group.call(this);
            var Object0 = new Game.Lo(new GameEngine.Point(654 , 975.13), 15, state);
            var slot123 = new GameEngine.TimeSlot(123);        //Start
            slot123.addEvent(new GameEngine.Event(Object0, "x", "current", 15, 200));
...

The idea is that the game library would just run this code instead of having to parse a map file and generate objects on the fly. 这个想法是,游戏库将只运行此代码,而不必解析地图文件并即时生成对象。 And the Lua script in the map editor that generates the code could be modified by anyone who wanted to output code in a different language for a different library. 想要以不同语言为不同库输出代码的任何人都可以修改在地图编辑器中生成代码的Lua脚本。 (not limited to scripting languages). (不限于脚本语言)。

I've never heard of this idea, usually i'd expect the map data to be in a standard format like JSON or XML and have the game library parse it. 我从未听说过这个主意,通常我希望地图数据采用JSON或XML之类的标准格式,并让游戏库对其进行解析。

So given that his library is written in javascript and his map can generate javascript to load files, what are the tradeoffs between running the generated code vs parsing JSON/XML and generating objects from that? 因此,假设他的库是用javascript编写的,并且他的地图可以生成javascript以加载文件,那么在运行生成的代码与解析JSON / XML和从中生成对象之间的权衡是什么?

In a generic sense loading the metadata from another script, can give added flexibility to the script generator about how the data is sent, displayed etc. For example, you can have complete math expressions, conditionals etc as the part of the loaded script, that will be parsed and loaded seamlessly by the script parser(interpreter). 从一般意义上来说,从另一个脚本加载元数据可以使脚本生成器在数据发送,显示等方面具有更大的灵活性。例如,可以将完整的数学表达式,条件等作为加载脚本的一部分,将由脚本解析器(解释器)无缝解析和加载。 It might be harder to do the same thing by using XML or JSON ( imagine sending an expression to do Miller Cylindrical Projection via XML, on the fly). 使用XML或JSON来做同样的事情可能会更困难(想象一下,通过XML发送一个表达式来执行Miller圆柱投影 )。

I've seen many situations where the app creates its own scripting language (MAXscript, MEL for Maya) to provide flexibility to the user. 我已经看到许多情况下,应用程序会创建自己的脚本语言(MAXscript,Maya的MEL)来为用户提供灵活性。 These are probably not analogous to your friend's usage of Java script to load metadata. 这些可能与您朋友使用Java脚本加载元数据不相似。 But IMHO, it is a continuous spectrum, starting from metadata text files, to XML,JSON, expression parsing, to full fledged script parsing. 但是恕我直言,这是一个连续的过程,从元数据文本文件开始,到XML,JSON,表达式解析,再到完整的脚本解析。

On the other hand, sending complicated scripts, will mean exposing part your code base. 另一方面,发送复杂的脚本意味着要公开部分代码库。 Also everyone knows what XML does, and you can expect a non-programmer to use/modify XML files. 而且每个人都知道XML的功能,并且您可以期望非程序员使用/修改XML文件。 They are comfortable doing it. 他们这样做很自在。 They may not be comfortable even reading what they think of as 'programs' or 'scripts'. 他们甚至可能不习惯阅读他们认为的“程序”或“脚本”。 I've seen this first hand in my company, where the artists were uneasy modify a Lua file. 我在公司里亲眼目睹了这首手,在那儿,艺术家们不安地修改Lua文件。 They were comfortable modifying the same information, if it was a simple text file. 如果它是一个简单的文本文件,则他们很乐意修改相同的信息。 There might some security issues as well, but I'm really not that familiar with it, so I can't comment. 可能还存在一些安全问题,但是我实际上并不那么熟悉,因此我无法发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM