using DSPRE;
using DSPRE.Resources;
using DSPRE.ROMFiles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml.Linq;
using static DSPRE.RomInfo;
public static class ScriptDatabaseJsonLoader
{
///
/// Call this once at startup (before any script‐lookups).
///
public static void InitializeFromJson(string jsonPath, GameVersions gameVersion)
{
string expandedPath = Environment.ExpandEnvironmentVariables(jsonPath);
string text = File.ReadAllText(expandedPath);
JsonDocument doc = JsonDocument.Parse(text);
try
{
JsonElement root = doc.RootElement;
ScriptDatabase.movementsDictIDName = root
.GetProperty("movements")
.EnumerateObject()
.ToDictionary(
prop => Convert.ToUInt16(prop.Name.Substring(2), 16),
prop => prop.Value.GetString()
);
ScriptDatabase.comparisonOperatorsDict = root
.GetProperty("comparisonOperators")
.EnumerateObject()
.ToDictionary(
prop => Convert.ToUInt16(prop.Name.Substring(2), 16),
prop => prop.Value.GetString()
);
ScriptDatabase.specialOverworlds = root
.GetProperty("specialOverworlds")
.EnumerateObject()
.ToDictionary(
prop => Convert.ToUInt16(prop.Name.Substring(2), 16),
prop => prop.Value.GetString()
);
ScriptDatabase.overworldDirections = root
.GetProperty("overworldDirections")
.EnumerateObject()
.ToDictionary(
prop => (byte)Convert.ToUInt16(prop.Name.Substring(2), 16),
prop => prop.Value.GetString()
);
Dictionary namesDict;
Dictionary paramsDict;
switch (gameVersion)
{
case GameVersions.Platinum:
namesDict = ScriptDatabase.PlatScrCmdNames;
paramsDict = ScriptDatabase.PlatScrCmdParameters;
break;
case GameVersions.Diamond:
case GameVersions.Pearl:
namesDict = ScriptDatabase.DPScrCmdNames;
paramsDict = ScriptDatabase.DPScrCmdParameters;
break;
case GameVersions.HeartGold:
case GameVersions.SoulSilver:
namesDict = ScriptDatabase.HGSSScrCmdNames;
paramsDict = ScriptDatabase.HGSSScrCmdParameters;
break;
default:
throw new ArgumentOutOfRangeException(nameof(gameVersion), gameVersion, "Unsupported game");
}
JsonElement scrRoot;
if (!root.TryGetProperty("scrcmd", out scrRoot))
throw new InvalidOperationException("JSON is missing the \"scrcmd\" key");
Console.WriteLine("About to load scrcmd entries:");
foreach (JsonProperty prop in scrRoot.EnumerateObject())
{
ushort code = Convert.ToUInt16(prop.Name.Substring(2), 16);
JsonElement entry = prop.Value;
string name = entry.GetProperty("name").GetString();
namesDict[code] = name;
var arr = entry.GetProperty("parameters");
byte[] bytes = arr
.EnumerateArray()
.Select(x => (byte)x.GetInt32())
.ToArray();
paramsDict[code] = bytes;
}
}
finally
{
doc.Dispose();
}
}
}
namespace DSPRE.Resources {
public static class ScriptDatabase {
public static Dictionary comparisonOperatorsDict = new Dictionary() {
// Will be populated at runtime from json
};
public static Dictionary comparisonOperatorsGenVappendix = new Dictionary() {
/* GEN V ONLY */
[6] = "OR",
[7] = "AND",
[0xFF] = "TRUEUP"
};
public static Dictionary specialOverworlds = new Dictionary() {
// will be populated at runtime from json
};
public static Dictionary overworldDirections = new Dictionary() {
// Will be populated at runtime from json
};
public static Dictionary pokemonNames = new Dictionary()
{
// Will be populated at runtime from text archive
};
public static void InitializePokemonNames()
{
string[] names = RomInfo.GetPokemonNames();
pokemonNames = names.Select((name, index) => new { name, index })
.ToDictionary(x => (ushort)x.index, x => x.name);
}
public static Dictionary itemNames = new Dictionary()
{
// Will be populated at runtime from text archive
};
public static void InitializeItemNames()
{
string[] names = RomInfo.GetItemNames();
itemNames = names.Select((name, index) => new { name, index })
.ToDictionary(x => (ushort)x.index, x => x.name);
}
public static Dictionary moveNames = new Dictionary()
{
// Will be populated at runtime from text archive
};
public static void InitializeMoveNames()
{
string[] names = RomInfo.GetAttackNames();
moveNames = names.Select((name, index) => new { name, index })
.ToDictionary(x => (ushort)x.index, x => x.name);
}
public static Dictionary commandsWithRelativeJump = new Dictionary() {
//commandID, ID of parameter With Jump Address
[0x0016] = 0, //Jump
[0x0017] = 1, //Call
[0x0018] = 1, //Call
[0x0019] = 1, //Call
[0x001A] = 0, //Call
[0x001C] = 1, //JumpIf
[0x001D] = 1, //CallIf
[0x005E] = 1, //Movement
};
public static HashSet endCodes = new HashSet() {
0x2,
0x16,
0x1B
};
public static Dictionary movementsDictIDName = new Dictionary() {
// Will be populated at runtime from json
};
public static HashSet movementEndCodes = new HashSet() {
0x00FE,
};
public static Dictionary DPScrCmdNames = new Dictionary() {
// will be populated at runtime from json
};
public static Dictionary DPScrCmdParameters = new Dictionary() {
// will be populated at runtime from json
};
public static Dictionary PlatScrCmdNames = new Dictionary() {
// will be populated at runtime from json
};
public static Dictionary PlatScrCmdParameters = new Dictionary() {
// will be populated at runtime from json
};
public static Dictionary HGSSScrCmdNames = new Dictionary() {
// will be populated from json at runtime
};
public static Dictionary HGSSScrCmdParameters = new Dictionary() {
// will be populated from json at runtime
};
}
}