Skip to content

Commit

Permalink
Fix duplicate name in EO2 lib & add library validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tge-was-taken committed Dec 8, 2024
1 parent 14d1383 commit d96887b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
69 changes: 68 additions & 1 deletion Source/AtlusScriptLibrary/Common/Libraries/LibraryLookup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text.Json;
Expand Down Expand Up @@ -58,10 +59,76 @@ public static Library GetLibrary(string name)
return null;
}

private static bool ValidateLibrary(Library library, out List<string> errors)
{
errors = new List<string>();

// Validate the library's own name and short name
if (string.IsNullOrWhiteSpace(library.Name))
errors.Add("Library name cannot be null or empty.");

if (string.IsNullOrWhiteSpace(library.ShortName))
errors.Add("Library short name cannot be null or empty.");

// Track unique FlowScriptModule names
var flowModuleNameSet = new HashSet<string>();
if (library.FlowScriptModules != null)
{
foreach (var module in library.FlowScriptModules)
{
if (!flowModuleNameSet.Add(module.Name))
errors.Add($"Duplicate FlowScriptModule name found: {module.Name}");

// Validate enum members within each FlowScriptModule, if applicable
foreach (var item in module.Enums)
{
var enumMemberNameSet = new HashSet<string>();
foreach (var enumMember in item.Members)
{
if (!enumMemberNameSet.Add(enumMember.Name))
errors.Add($"Duplicate enum member name '{enumMember.Name}' in FlowScriptModule '{module.Name}'.");
}
}
}
}

// Track unique MessageScriptLibrary names and indices
var messageLibraryNameSet = new HashSet<string>();
var messageLibraryIndexSet = new HashSet<int>();
if (library.MessageScriptLibraries != null)
{
foreach (var msgLibrary in library.MessageScriptLibraries)
{
if (!string.IsNullOrWhiteSpace(msgLibrary.Name) && !messageLibraryNameSet.Add(msgLibrary.Name))
errors.Add($"Duplicate MessageScriptLibrary name found: {msgLibrary.Name}");

if (!messageLibraryIndexSet.Add(msgLibrary.Index))
errors.Add($"Duplicate MessageScriptLibrary index found: {msgLibrary.Index}");

// Validate functions within each MessageScriptLibrary
var functionNameSet = new HashSet<string>();
var functionIndexSet = new HashSet<int>();
foreach (var function in msgLibrary.Functions ?? Enumerable.Empty<MessageScriptLibraryFunction>())
{
if (!string.IsNullOrWhiteSpace(function.Name) && !functionNameSet.Add(function.Name))
errors.Add($"Duplicate function name '{function.Name}' in MessageScriptLibrary '{msgLibrary.Name}'.");

if (!functionIndexSet.Add(function.Index))
errors.Add($"Duplicate function index '{function.Index}' in MessageScriptLibrary '{msgLibrary.Name}'.");
}
}
}

return errors.Count == 0;
}

private static Library ParseLibrary(string path)
{
EnsureInitialized();
string jsonText = File.ReadAllText(path);
return JsonSerializer.Deserialize<Library>(jsonText);
var lib = JsonSerializer.Deserialize<Library>(jsonText);
if (!ValidateLibrary(lib, out var errors))
throw new Exception($"Failed to load library {path}:\n{string.Join("\n", errors)}");
return lib;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override List<T> Read(ref Utf8JsonReader reader, Type typeToConvert, Json
{
var path = reader.GetString();
if (string.IsNullOrEmpty(path))
return null;
return new List<T>();

var fullPath = Path.Combine(LibraryLookup.LibraryBaseDirectoryPath, path);
var jsonString = File.ReadAllText(fullPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@
{
"Index": "0x0041",
"ReturnType": "int",
"Name": "AI_CHK_MYHP",
"Name": "AI_CHK_MYHP_2",
"Description": "",
"Parameters": [
{
Expand Down

0 comments on commit d96887b

Please sign in to comment.