什么配置文件格式允许包含其他文件和继承设置? 我正在编写一个基于多人游戏的C++游戏。
我需要一个灵活的文件格式来存储有关游戏角色的信息 游戏角色通常不会共享相同的属性,或者使用basew 例如: 一种允许我这样做的格式:什么配置文件格式允许包含其他文件和继承设置? 我正在编写一个基于多人游戏的C++游戏。,c++,configuration-files,file-format,C++,Configuration Files,File Format,我需要一个灵活的文件格式来存储有关游戏角色的信息 游戏角色通常不会共享相同的属性,或者使用basew 例如: 一种允许我这样做的格式: #include "standardsettings.config" //include other files which this file //then changes FastSpaceship: Speed: 10 //pixels/sec Rotation: 5 //deg/sec MotherShip : FastSp
#include "standardsettings.config"
//include other files which this file
//then changes
FastSpaceship:
Speed: 10 //pixels/sec
Rotation: 5 //deg/sec
MotherShip : FastSpaceship //inherits all the settings of the Spaceship ship
ShieldRecharge: 4
WeaponA [ power:10,
range:20,
style:fireball]
SlowMotherShip : MotherShip //inherits all the settings of the monther ship
Speed: 4 // override speed
我一直在寻找一种已经存在的格式来完成所有这些,或者是类似的,但是运气不好。除非必须,否则我不想重新发明轮子,所以我想知道是否有人知道支持这些功能的好的配置文件格式您可能想查看某种表示,因为这似乎正是您所说的。wikipedia页面链接到一些现有的实现,您可以使用这些实现,也可以创建自己的实现。JSON是最简单的文件格式,具有成熟的库,您可以将其解释为您想要的任何操作
{
"FastSpaceship" : {
"Speed" : 10,
"Rotation" : 5
},
"MotherShip" : {
"Inherits" : "FastSpaceship",
"ShieldRecharge" : 4,
"WeaponA": {
"Power": 10,
"Range": 20,
"style": "fireball"
}
},
"SlowMotherShip": {
"Inherits": "MotherShip",
"Speed": 4
}
}
??就像没有逗号和引号的JSON一样。经过大量搜索,我找到了一个非常好的解决方案,使用 我发现Lua最初设计为一种配置文件语言,但后来演变成一种完整的编程语言 范例 util.lua
-- helper function needed for inheritance
function inherit(t) -- return a deep copy (incudes all subtables) of the table t
local new = {} -- create a new table
local i, v = next(t, nil) -- i is an index of t, v = t[i]
while i do
if type(v)=="table" then v=inherit(v) end -- deep copy
new[i] = v
i, v = next(t, i) -- get next index
end
return new
end
globalsettings.lua
require "util"
SpaceShip = {
speed = 1,
rotation =1
}
myspaceship.lua
require "globalsettings" -- include file
FastSpaceship = inherits(SpaceShip)
FastSpaceship.Speed = 10
FastSpaceship.Rotation = 5
MotherShip = inherits(FastSpaceship)
MotherShip.ShieldRecharge = 4
ShieldRecharge.WeaponA = {
Power = 10,
Range = 20,
Style = "fireball"
SlowMotherShip = inherits(MotherShip)
SlowMotherShip.Speed = 4
使用Lua中的打印功能还可以轻松测试设置是否正确。语法并没有我想要的那么好,但它非常接近我想要的,我不介意再写一点
使用这里的代码,我可以把设置读到我的C++程序中,这将如何与其他文件一起工作。我想我想要一些类似CSS的东西。我真的不想在用户代码中计算出所有包含继承的代码(我希望库能够完成这项工作),因此我可以键入以下内容:;旋转=查找(“SlowMotherShip.rotation”);旋转值是5,我想我没有一个好的答案。我不知道有哪个文件格式库知道对象之间的层次关系。这并不是说它不存在(开源世界比我的经验要大得多)。我以前写过类似的东西。格式是简单的JSON,解析器只知道如何处理一些“关键字”,比如“inherits”(IIRC,我们使用了关键字“super”)。啊,我明白了,一切似乎都接近我想要的,但还不够接近。我想我将不得不编写自己的库,或者修改现有的库;如果要这样做,可以将Python、Ruby、Javascript或任何其他解释器包含到程序中。请注意安全性,如果该语言有一个execute()函数,您不希望配置文件可以格式化c:没错,考虑到这一点,脚本语言开始比静态库更有意义,因为它正用于级别设计。安全问题是我没有想到的,我听说它可以在沙箱模式下运行,我将对此进行研究。