项目作者: Isuroku

项目描述 :
Cascade Language of Descriptions (Cascade)
高级语言: C#
项目地址: git://github.com/Isuroku/Cascade.git
创建时间: 2018-09-13T19:59:46Z
项目社区:https://github.com/Isuroku/Cascade

开源协议:MIT License

下载


Cascade

Cascade Language of Descriptions (Cascade)

Cascade is compatible with .Net, Mono and Unity3d library for writing/reading to/from text files, streams and other configuration data, object descriptions and settings.

It allows to convert text description to C# object and vice versa (like a Newtonsoft JSON Library). Also, it is possible to get data in hierarchical tree format (Keys, Values).

Text format is easy to read and understand and has minimum of syntactic garbage.

Text object description.

At the beginning JSON was used, which is convenient in general. However, with increasing number of objects and their complexity it was necessary to reuse a lot of the same or almost the same descriptions.

For instance, there is description in text file of NPC behaviour parameters like its actions, dialogues, default goals. This is large and complex file, which is edited and changed not by programmers, but by game designers.
There is certain number of NPC with almost the same description as the default one, but with minor differences. In such cases default description “inheritance” for each NPC with unique parameters changes can be applicable.

Simple example: NPCs with the movement behaviour.

  1. Name: Swordman
  2. MovingParams:
  3. Walk: 10
  4. Run: 15
  5. AngleSpeed: 25
  6. Weapon: Sword
  7. --
  8. Name: Axeman
  9. MovingParams:
  10. Walk: 10
  11. Run: 15
  12. AngleSpeed: 25
  13. Weapon: Axe
  14. --
  15. Name: Spearman
  16. MovingParams:
  17. Walk: 10
  18. Run: 15
  19. AngleSpeed: 25
  20. Weapon: Spear

This is obvious that copying of parameters will lead to well-known problems: a lot of text, difficult to change parameter value for each NPC.

However, it is possible to highlight the movement parameters
in a separate description and insert it into the description of each NPC.

File MoveDescr:

  1. Walkman:
  2. Walk: 10
  3. Run: 15
  4. AngleSpeed: 25

File NPCDescr:

  1. Name: Swordman
  2. MovingParams:
  3. #insert file:MoverDescrs.cscdt key:StandartMan
  4. Weapon: Sword
  5. --
  6. Name: Axeman
  7. MovingParams:
  8. #insert file:MoverDescrs.cscdt key:StandartMan
  9. Weapon: Axe
  10. --
  11. Name: Spearman
  12. MovingParams:
  13. #insert file:MoverDescrs.cscdt key:StandartMan
  14. Weapon: Spear

How to read this description in code(C#).

Describe the structure of NPc and its movement:

  1. struct SDescrMove
  2. {
  3. public float Walk { get; private set; }
  4. public float Run { get; private set; }
  5. public float AngleSpeed { get; private set; }
  6. }
  7. struct SDescrNPC
  8. {
  9. public string Name { get; private set; }
  10. public SDescrMove MovingParams { get; private set; }
  11. public string Weapon { get; private set; }
  12. }

Create our own upload manager, which includes the Cascade serializer:

  1. using System;
  2. using CascadeParser;
  3. using CascadeSerializer;
  4. using System.Collections.Generic;
  5. public class CStaticDataManager : IParserOwner, ILogPrinter
  6. {
  7. protected CCascadeSerializer _serializer;
  8. public CStaticDataManager()
  9. {
  10. _serializer = new CCascadeSerializer(this);
  11. }
  12. //IParserOwner requests this method
  13. public string GetTextFromFile(string inFileName, object inContextData)
  14. {
  15. string path = Path.Combine(Application.StartupPath, "Data", inFileName);
  16. if (!File.Exists(path))
  17. return string.Empty;
  18. return File.ReadAllText(path);
  19. }
  20. #region ILogPrinter
  21. public void LogError(string inText) { Console.WriteLine("Error: {0}", inText); }
  22. public void LogWarning(string inText) { Console.WriteLine("Warning: {0}", inText); }
  23. public void Trace(string inText) { Console.WriteLine(inText); }
  24. #endregion ILogPrinter
  25. }

A few words about the interfaces that it needs to implement in order to create the Cascade serializer.

  • ILogPrinter - serializer will show errors using this interface when reading and parsing text.
  • IParserOwner - description of the NPC says that we want to read MovingParams from MoverDescrs.cscdt file and the StandartMan key.

The serializer itself does not read the files, but asks the host to do this using the method: string GetTextFromFile (string inFileName, object inContextData).
In our case, in the argument of the inFileName method will be “MoverDescrs.cscdt”.
And we must read the content of this file as text and return it as the result of a method.
As shown in the example below. About the argument “inContextData” I will write further

Now read the data (description of the NPC) from the file.

  1. //using из примера выше
  2. public class CStaticDataManager : IParserOwner, ILogPrinter
  3. {
  4. //здесь все из предыдущего примера
  5. List<SDescrNPC> _npc_list = new List<SDescrNPC>;
  6. void LoadNPC()
  7. {
  8. //здесь можно создать объект, который будет передаваться в метод
  9. //string GetTextFromFile(string inFileName, object inContextData)
  10. //как inContextData
  11. object context_data = null;
  12. string text = GetTextFromFile("NPC.cscd", context_data);
  13. _npc_list.AddRange(_serializer.Deserialize<SDescrNPC[]>(text, this));
  14. }
  15. }

Done!