/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="key">The unique key for the entry in the data file.</param> /// <param name="value">The entry value to set.</param> public EditDataPatchRecord(TokenString key, TokenisableJToken value) { this.Key = key; this.Value = value; this.ContextualValues = new IContextual[] { key, value }.Where(p => p != null).ToArray(); }
/// <summary>Parse a JSON structure which can contain tokens, and validate that it's valid.</summary> /// <param name="rawJson">The raw JSON structure which may contain tokens.</param> /// <param name="assumeModIds">Mod IDs to assume are installed for purposes of token validation.</param> /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param> /// <param name="parsed">The parsed value, which may be legitimately <c>null</c> even if successful.</param> public bool TryParseJsonTokens(JToken rawJson, InvariantHashSet assumeModIds, out string error, out TokenisableJToken parsed) { if (rawJson == null || rawJson.Type == JTokenType.Null) { error = null; parsed = null; return(true); } // parse parsed = new TokenisableJToken(rawJson, this.Context); if (!this.Migrator.TryMigrate(parsed, out error)) { return(false); } // validate tokens foreach (LexTokenToken lexToken in parsed.GetTokenStrings().SelectMany(p => p.GetTokenPlaceholders(recursive: false))) { if (!this.TryValidateToken(lexToken, assumeModIds, out error)) { return(false); } } // looks OK if (parsed.Value.Type == JTokenType.Null) { parsed = null; } error = null; return(true); }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="entryKey">The unique key for the entry in the data file.</param> /// <param name="fieldKey">The field number to change.</param> /// <param name="value">The entry value to set.</param> public EditDataPatchField(TokenString entryKey, TokenString fieldKey, TokenisableJToken value) { this.EntryKey = entryKey; this.FieldKey = fieldKey; this.Value = value; this.ContextualValues = new IContextual[] { entryKey, fieldKey, value }.Where(p => p != null).ToArray(); }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="key">The unique key for the entry in the data file.</param> /// <param name="value">The entry value to set.</param> public EditDataPatchRecord(ITokenString key, TokenisableJToken value) { this.Key = key; this.Value = value; this.Contextuals = new AggregateContextual() .Add(key) .Add(value); }
/// <summary>Parse a JSON structure which can contain tokens, and validate that it's valid.</summary> /// <param name="rawJson">The raw JSON structure which may contain tokens.</param> /// <param name="tokenContext">The tokens available for this content pack.</param> /// <param name="migrator">The migrator which validates and migrates content pack data.</param> /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param> /// <param name="parsed">The parsed value, which may be legitimately <c>null</c> even if successful.</param> private bool TryParseJsonTokens(JToken rawJson, IContext tokenContext, IMigration migrator, out string error, out TokenisableJToken parsed) { if (rawJson == null) { error = null; parsed = null; return(true); } // parse parsed = new TokenisableJToken(rawJson, tokenContext); if (!migrator.TryMigrate(parsed, out error)) { return(false); } // validate tokens TokenString[] tokenStrings = parsed.GetTokenStrings().ToArray(); if (tokenStrings.Any()) { // validate unknown tokens string[] unknownTokens = tokenStrings.SelectMany(p => p.InvalidTokens).OrderBy(p => p).ToArray(); if (unknownTokens.Any()) { error = $"found unknown tokens ({string.Join(", ", unknownTokens)})"; parsed = null; return(false); } // validate tokens foreach (TokenName tokenName in tokenStrings.SelectMany(p => p.Tokens)) { IToken token = tokenContext.GetToken(tokenName, enforceContext: false); if (token == null) { error = $"{{{{{tokenName}}}}} can't be used as a token because that token could not be found."; // should never happen parsed = null; return(false); } if (token.CanHaveMultipleValues(tokenName)) { error = $"{{{{{tokenName}}}}} can't be used as a token because it can have multiple values."; parsed = null; return(false); } } } // looks OK if (parsed.Value.Type == JTokenType.Null) { parsed = null; } error = null; return(true); }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="entryKey">The unique key for the entry in the data file.</param> /// <param name="fieldKey">The field number to change.</param> /// <param name="value">The entry value to set.</param> public EditDataPatchField(ITokenString entryKey, ITokenString fieldKey, TokenisableJToken value) { this.EntryKey = entryKey; this.FieldKey = fieldKey; this.Value = value; this.Contextuals = new AggregateContextual() .Add(entryKey) .Add(fieldKey) .Add(value); }
/// <summary>Migrate a tokenised JSON structure.</summary> /// <param name="tokenStructure">The tokenised JSON structure to migrate.</param> /// <param name="error">An error message which indicates why migration failed (if any).</param> /// <returns>Returns whether migration succeeded.</returns> public bool TryMigrate(TokenisableJToken tokenStructure, out string error) { foreach (IParsedTokenString str in tokenStructure.GetTokenStrings()) { if (!this.TryMigrate(str, out error)) { return(false); } } error = null; return(true); }
/// <summary>Parse a JSON structure which can contain tokens, and validate that it's valid.</summary> /// <param name="rawJson">The raw JSON structure which may contain tokens.</param> /// <param name="tokenContext">The tokens available for this content pack.</param> /// <param name="migrator">The migrator which validates and migrates content pack data.</param> /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param> /// <param name="parsed">The parsed value, which may be legitimately <c>null</c> even if successful.</param> private bool TryParseJsonTokens(JToken rawJson, IContext tokenContext, IMigration migrator, out string error, out TokenisableJToken parsed) { if (rawJson == null || rawJson.Type == JTokenType.Null) { error = null; parsed = null; return(true); } // parse parsed = new TokenisableJToken(rawJson, tokenContext); if (!migrator.TryMigrate(parsed, out error)) { return(false); } // validate tokens ITokenString[] tokenStrings = parsed.GetTokenStrings().ToArray(); if (tokenStrings.Any()) { // validate unknown tokens string[] unknownTokens = tokenStrings.SelectMany(p => p.GetDiagnosticState().InvalidTokens).OrderBy(p => p).ToArray(); if (unknownTokens.Any()) { error = $"found unknown tokens ({string.Join(", ", unknownTokens)})"; parsed = null; return(false); } // validate tokens foreach (LexTokenToken lexToken in tokenStrings.SelectMany(p => p.GetTokenPlaceholders(recursive: false)).Distinct()) { IToken token = tokenContext.GetToken(lexToken.Name, enforceContext: false); if (token == null) { error = $"'{lexToken}' can't be used as a token because that token could not be found."; // should never happen parsed = null; return(false); } } } // looks OK if (parsed.Value.Type == JTokenType.Null) { parsed = null; } error = null; return(true); }
/// <summary>Migrate a tokenised JSON structure.</summary> /// <param name="tokenStructure">The tokenised JSON structure to migrate.</param> /// <param name="error">An error message which indicates why migration failed (if any).</param> /// <returns>Returns whether migration succeeded.</returns> public bool TryMigrate(TokenisableJToken tokenStructure, out string error) { // apply migrations foreach (IMigration migration in this.Migrations) { if (!migration.TryMigrate(tokenStructure, out error)) { return(false); } } // no issues found error = null; return(true); }