public void ParseObjectModelTest() { string commandJson = File.ReadAllText(TestContext.CurrentContext.TestDirectory + "\\test_commands_1.json"); JsonConvert.DefaultSettings = () => { var settings = new JsonSerializerSettings(); return(settings); }; var commands = JsonConvert.DeserializeObject <CommandSet>(commandJson /*, new ParameterConverter()*/); Assert.AreEqual(72, commands.Count); Assert.AreEqual("difficulty", commands.First(pair => pair.Key == "difficulty").Key); Command command = commands["difficulty"]; Assert.NotNull(command); Version version = command.Versions[0]; Assert.AreEqual("commands.difficulty.usage", version.Description); Assert.AreEqual(2, version.Overloads.Count); Overload overload = version.Overloads["byName"]; Assert.NotNull(overload); // Input Input input = overload.Input; Assert.NotNull(input); Assert.NotNull(input.Parameters); Assert.AreEqual(1, input.Parameters.Length); // Input parameters Parameter inputParameter = input.Parameters[0]; Assert.NotNull(inputParameter); Assert.AreEqual("difficulty", inputParameter.Name); Assert.AreEqual("stringenum", inputParameter.Type); //Assert.AreEqual("commandName", inputParameter.EnumType); Assert.IsFalse(inputParameter.Optional); // Output Output output = overload.Output; Assert.NotNull(output.FormatStrings); Assert.AreEqual(1, output.FormatStrings.Length); Assert.AreEqual("commands.difficulty.success", output.FormatStrings[0].Format); Assert.NotNull(output.Parameters); Assert.AreEqual(1, output.Parameters.Length); Parameter outputParameter = output.Parameters[0]; Assert.AreEqual("difficulty", outputParameter.Name); Assert.AreEqual("string", outputParameter.Type); Assert.That(outputParameter.EnumType, Is.Null.Or.Empty); Assert.IsFalse(outputParameter.Optional); }
public void ParseObjectModelTest() { string commandJson = File.ReadAllText("test_commands_1.json"); JsonConvert.DefaultSettings = () => { var settings = new JsonSerializerSettings(); return(settings); }; var commands = JsonConvert.DeserializeObject <CommandSet>(commandJson /*, new ParameterConverter()*/); Assert.AreEqual(56, commands.Count); Assert.AreEqual("ability", commands.First().Key); Command command = commands["help"]; Assert.NotNull(command); Version version = command.Versions[0]; Assert.AreEqual("commands.help.description", version.Description); Assert.AreEqual(2, version.Overloads.Count); Overload overload = version.Overloads["byName"]; Assert.NotNull(overload); // Input Input input = overload.Input; Assert.NotNull(input); Assert.NotNull(input.Parameters); Assert.AreEqual(1, input.Parameters.Length); // Input parameters Parameter inputParameter = input.Parameters[0]; Assert.NotNull(inputParameter); Assert.AreEqual("command", inputParameter.Name); Assert.AreEqual("stringenum", inputParameter.Type); Assert.AreEqual("commandName", inputParameter.EnumType); Assert.IsFalse(inputParameter.Optional); // Output Output output = overload.Output; Assert.NotNull(output.FormatStrings); Assert.AreEqual(1, output.FormatStrings.Length); Assert.AreEqual("{0}", output.FormatStrings[0]); Assert.NotNull(output.Parameters); Assert.AreEqual(1, output.Parameters.Length); Parameter outputParameter = output.Parameters[0]; Assert.AreEqual("body", outputParameter.Name); Assert.AreEqual("string", outputParameter.Type); Assert.IsNullOrEmpty(outputParameter.EnumType); Assert.IsFalse(outputParameter.Optional); }
partial void AfterDecode() { CommandSet = new CommandSet(); var stringValues = new List <string>(); { uint count = ReadUnsignedVarInt(); Log.Debug($"String values {count}"); for (int i = 0; i < count; i++) { string str = ReadString(); Log.Debug($"{i} - {str}"); stringValues.Add(str); } } int stringValuesCount = stringValues.Count(); { uint count = ReadUnsignedVarInt(); Log.Debug($"Postfix values {count}"); for (int i = 0; i < count; i++) { string s = ReadString(); Log.Debug(s); } } EnumData[] enums; { uint count = ReadUnsignedVarInt(); enums = new EnumData[count]; Log.Debug($"Enum indexes {count}"); string last = null; string clazzType = null; for (int i = 0; i < count; i++) { string enumName = ReadString(); uint enumValueCount = ReadUnsignedVarInt(); string[] enumValues = new string[enumValueCount]; Log.Debug($"{i} - {enumName}:{enumValueCount}"); for (int j = 0; j < enumValueCount; j++) { int idx; if (stringValuesCount <= byte.MaxValue) { idx = ReadByte(); } else if (stringValuesCount <= short.MaxValue) { idx = ReadShort(); } else { idx = ReadInt(); } enumValues[j] = stringValues[idx]; Log.Debug($"{enumName}, {idx} - {stringValues[idx]}"); } enums[i] = new EnumData(enumName, enumValues); } } { uint count = ReadUnsignedVarInt(); Log.Debug($"Commands definitions {count}"); for (int i = 0; i < count; i++) { Command command = new Command(); command.Versions = new Version[1]; string commandName = ReadString(); string description = ReadString(); int flags = ReadShort(); int permissions = ReadByte(); command.Name = commandName; Version version = new Version(); version.Description = description; int aliasEnumIndex = ReadInt(); uint overloadCount = ReadUnsignedVarInt(); version.Overloads = new Dictionary <string, Overload>(); for (int j = 0; j < overloadCount; j++) { Overload overload = new Overload(); overload.Input = new Input(); uint parameterCount = ReadUnsignedVarInt(); overload.Input.Parameters = new Parameter[parameterCount]; Log.Debug($"{commandName}, {description}, flags={flags}, {((CommandPermission) permissions)}, alias={aliasEnumIndex}, overloads={overloadCount}, params={parameterCount}"); for (int k = 0; k < parameterCount; k++) { string commandParamName = ReadString(); var paramType = ReadInt(); var optional = ReadBool(); var paramFlags = ReadByte(); /*int tmp = ReadShort(); * int tmp1 = ReadShort(); * bool isEnum = (tmp1 & 0x30) == 0x30; * bool isSoftEnum = (tmp1 & 0x0410) == 0x0410; * int commandParamType = -1; * int commandParamEnumIndex = -1; * int commandParamSoftEnumIndex = -1; * int commandParamPostfixIndex = -1; * if ((tmp1 & 0x0030) == 0x0030) * { * commandParamEnumIndex = tmp & 0xffff; * } * else if ((tmp1 & 0x0410) == 0x0410) * { * commandParamType = tmp & 0xffff; * commandParamSoftEnumIndex = tmp & 0xffff; * } * else if ((tmp1 & 0x100) == 0x100) * { * commandParamPostfixIndex = tmp & 0xffff; * } * else if ((tmp1 & 0x10) == 0x10) * { * commandParamType = tmp & 0xffff; * } * else * { * Log.Warn("No parameter style read (enum, valid, postfix)"); * }*/ //bool optional = ReadBool(); //byte unknown = ReadByte(); Parameter parameter = new Parameter() { Name = commandParamName, Optional = optional, Type = GetParameterTypeName((paramType & 0xffff)) }; overload.Input.Parameters[k] = parameter; if ((paramType & 0x200000) != 0) //Enum { var paramEnum = enums[paramType & 0xffff]; parameter.EnumValues = paramEnum.Values; parameter.EnumType = paramEnum.Name; parameter.Type = "stringenum"; } else if ((paramType & 0x1000000) != 0) //Postfix { var paramEnum = enums[paramType & 0xffff]; parameter.EnumValues = paramEnum.Values; parameter.EnumType = paramEnum.Name; parameter.Type = "stringenum"; } //Log.Debug($"\t{commandParamName}, 0x{tmp:X4}, 0x{tmp1:X4}, {isEnum}, {isSoftEnum}, {(GetParameterTypeName(commandParamType))}, {commandParamEnumIndex}, {commandParamSoftEnumIndex}, {commandParamPostfixIndex}, {optional}, {unknown}"); } version.Overloads.Add(j.ToString(), overload); } command.Versions[0] = version; CommandSet.Add(commandName, command); } } { // Soft enums? uint count = ReadUnsignedVarInt(); Log.Debug($"Soft enums {count}"); for (int i = 0; i < count; i++) { string enumName = ReadString(); Log.Debug($"Soft Enum {enumName}"); uint valCount = ReadUnsignedVarInt(); for (int j = 0; j < valCount; j++) { Log.Debug($"\t{enumName} value:{ReadString()}"); } } } { // constraints uint count = ReadUnsignedVarInt(); Log.Debug($"Constraints {count}"); for (int i = 0; i < count; i++) { Log.Debug($"Constraint: {ReadInt()} _ {ReadInt()}"); uint someCount = ReadUnsignedVarInt(); for (int j = 0; j < someCount; j++) { Log.Debug($"\tUnknown byte: {ReadByte()}"); } } } }