¿Qué formato de archivo de configuración permite la inclusión de otros archivos y la herencia de la configuración?

StackOverflow https://stackoverflow.com/questions/1228545

Pregunta

Estoy escribiendo un juego multijugador basado en C ++.

Necesito un formato de archivo flexible para almacenar información sobre los personajes del juego.

Los personajes del juego a menudo no comparten los mismos atributos, o usan una base

Por ejemplo:

Un formato que me permitiría hacer algo como esto:

#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

He estado buscando un formato preexistente que haga todo esto, o sea similar, pero sin suerte. Estoy ansioso por no reinventar la rueda a menos que sea necesario, por lo que me preguntaba si alguien conoce algún buen formato de archivo de configuración que admita estas características

¿Fue útil?

Solución 4

Después de muchas búsquedas, he encontrado una solución bastante buena usando Lua

Lua que descubrí fue originalmente diseñado como un lenguaje de archivo de configuración, pero luego se convirtió en un lenguaje de programación completo.

Ejemplo

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

Usando la función de impresión en Lua también es fácil probar las configuraciones si son correctas. La sintaxis no es tan buena como me gustaría, pero está tan cerca de lo que quiero que no me importará escribir un poco más.

El uso del código aquí http: // windrealm.com/tutorials/reading-a-lua-configuration-file-from-c.php Puedo leer la configuración en mi programa C ++

Otros consejos

JSON es el formato de archivo más simple, tiene bibliotecas maduras y puede interpretarlo para hacer lo que quiera.

{
    "FastSpaceship" : {
        "Speed" : 10,
        "Rotation" : 5 
    },
    "MotherShip" : {
        "Inherits" : "FastSpaceship",
        "ShieldRecharge" : 4,
        "WeaponA": {
            "Power": 10,
            "Range": 20,
            "style": "fireball"
        }
    },
    "SlowMotherShip": {
        "Inherits": "MotherShip",
        "Speed": 4 
    } 
}

YAML ? Es como JSON sin las comas y las comillas.

Es posible que desee consultar algún tipo de representación basada en marcos , ya que parece eso es exactamente de lo que estás hablando. Esa página de Wikipedia se vincula con algunas implementaciones existentes que tal vez podría usar, o crear las suyas.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top