private void CreateInlineItemScriptFile()
{
// Place generated script in cache folder
var outPath = this.GetCachePath(Path.Combine("system", "scripts", "items", "inline.generated.cs")).Replace(".compiled", "");
// Check if db files were updated, if not we don't need to recreate
// the inline script.
var dbNewerThanScript =
(File.GetLastWriteTime(Path.Combine("system", "db", "items.txt")) >= File.GetLastWriteTime(outPath)) ||
(File.GetLastWriteTime(Path.Combine("user", "db", "items.txt")) >= File.GetLastWriteTime(outPath));
if (!dbNewerThanScript)
return;
var sb = new StringBuilder();
// Default usings
sb.AppendLine("// Automatically generated from inline scripts in the item database");
sb.AppendLine();
sb.AppendLine("using Aura.Channel.World.Entities;");
sb.AppendLine("using Aura.Channel.Scripting.Scripts;");
sb.AppendLine();
// Go through all items
foreach (var entry in AuraData.ItemDb.Entries.Values)
{
var scriptsEmpty = (string.IsNullOrWhiteSpace(entry.OnUse) && string.IsNullOrWhiteSpace(entry.OnEquip) && string.IsNullOrWhiteSpace(entry.OnUnequip) && string.IsNullOrWhiteSpace(entry.OnCreation));
if (scriptsEmpty)
continue;
sb.AppendFormat("// {0}: {1}" + Environment.NewLine, entry.Id, entry.Name);
sb.AppendFormat("[ItemScript({0})]" + Environment.NewLine, entry.Id);
sb.AppendFormat("public class ItemScript{0} : ItemScript {{" + Environment.NewLine, entry.Id);
if (!string.IsNullOrWhiteSpace(entry.OnUse))
sb.AppendFormat(" public override void OnUse(Creature cr, Item i, string param) {{ {0} }}" + Environment.NewLine, entry.OnUse.Trim());
if (!string.IsNullOrWhiteSpace(entry.OnEquip))
sb.AppendFormat(" public override void OnEquip(Creature cr, Item i) {{ {0} }}" + Environment.NewLine, entry.OnEquip.Trim());
if (!string.IsNullOrWhiteSpace(entry.OnUnequip))
sb.AppendFormat(" public override void OnUnequip(Creature cr, Item i) {{ {0} }}" + Environment.NewLine, entry.OnUnequip.Trim());
if (!string.IsNullOrWhiteSpace(entry.OnCreation))
sb.AppendFormat(" public override void OnCreation(Item i) {{ {0} }}" + Environment.NewLine, entry.OnCreation.Trim());
sb.AppendFormat("}}" + Environment.NewLine + Environment.NewLine);
}
File.WriteAllText(outPath, sb.ToString());
}