Skip to content

Commit

Permalink
Add script rocket ship, SCRIPT00 deletion prevention, and new preview…
Browse files Browse the repository at this point in the history
… error image (#140)
  • Loading branch information
jonko0493 authored Apr 10, 2023
1 parent 05cf0b9 commit 367cf75
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/SerialLoops.Lib/Items/BackgroundItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public BackgroundItem(string name, int id, BgTableEntry entry, Project project)
PopulateScriptUses(project.Evt);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
PopulateScriptUses(project.Evt);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SerialLoops.Lib/Items/BackgroundMusicItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public BackgroundMusicItem(string bgmFile, int index, Project project) : base(Pa
PopulateScriptUses(project);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
PopulateScriptUses(project);
}
Expand Down
3 changes: 2 additions & 1 deletion src/SerialLoops.Lib/Items/CharacterSpriteItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HaruhiChokuretsuLib.Archive;
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Util;
using SkiaSharp;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -20,7 +21,7 @@ public CharacterSpriteItem(CharacterSprite sprite, CharacterDataFile chrdata, Pr
PopulateScriptUses(project.Evt);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
PopulateScriptUses(project.Evt);
}
Expand Down
3 changes: 2 additions & 1 deletion src/SerialLoops.Lib/Items/ChibiItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Archive.Graphics;
using HaruhiChokuretsuLib.Util;
using SkiaSharp;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -42,7 +43,7 @@ public ChibiItem(Chibi chibi, Project project) : base($"CHIBI{chibi.ChibiEntries
return frames.Zip(timings);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
GetChibiAnimation(Name, project.Grp);
PopulateScriptUses(project.Evt);
Expand Down
3 changes: 2 additions & 1 deletion src/SerialLoops.Lib/Items/DialogueConfigItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Util;

namespace SerialLoops.Lib.Items
{
Expand All @@ -11,7 +12,7 @@ public DialogueConfigItem(MessageInfo messageInfo) : base($"DIALOGUE_{messageInf
DialogueConfig = messageInfo;
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops.Lib/Items/GroupSelectionItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Util;

namespace SerialLoops.Lib.Items
{
Expand All @@ -11,10 +12,9 @@ public GroupSelectionItem(ScenarioSelectionStruct selection, int index, Project
{
Selection = selection;
Index = index;
Refresh(project);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/SerialLoops.Lib/Items/Item.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SerialLoops.Lib.Items
using HaruhiChokuretsuLib.Util;

namespace SerialLoops.Lib.Items
{
public abstract class Item : ItemDescription
{
Expand All @@ -7,7 +9,7 @@ public Item(string name, ItemType type, string displayName = "") : base(name, ty
{
}

public abstract void Refresh(Project project);
public abstract void Refresh(Project project, ILogger log);
}

public class NoneItem : Item
Expand All @@ -18,7 +20,7 @@ public NoneItem(ItemType type) : base("NONE", type)
{
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/SerialLoops.Lib/Items/MapItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Archive.Graphics;
using HaruhiChokuretsuLib.Util;
using SkiaSharp;
using System.Linq;

Expand Down Expand Up @@ -138,7 +139,7 @@ private static SKBitmap GetMapIcon(string name, int size)
return icon;
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
PopulateScriptUses(project.Evt);
}
Expand Down
3 changes: 2 additions & 1 deletion src/SerialLoops.Lib/Items/PlaceItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SkiaSharp;
using System.Linq;
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Util;

namespace SerialLoops.Lib.Items
{
Expand All @@ -20,7 +21,7 @@ public PlaceItem(int index, GraphicsFile placeGrp, Project project) : base(place
PopulateScriptUses(project.Evt);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
PopulateScriptUses(project.Evt);
}
Expand Down
7 changes: 4 additions & 3 deletions src/SerialLoops.Lib/Items/PuzzleItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Graphics;
using HaruhiChokuretsuLib.Util;
using SkiaSharp;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,13 +12,13 @@ public class PuzzleItem : Item
public PuzzleFile Puzzle { get; set; }
public SKBitmap SingularityImage { get; set; }

public PuzzleItem(PuzzleFile puzzleFile, Project project) : base(puzzleFile.Name[0..^1], ItemType.Puzzle)
public PuzzleItem(PuzzleFile puzzleFile, Project project, ILogger log) : base(puzzleFile.Name[0..^1], ItemType.Puzzle)
{
Puzzle = puzzleFile;
Refresh(project);
Refresh(project, log);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
GraphicsFile singularityLayout = project.Grp.Files.First(f => f.Index == Puzzle.Settings.SingularityLayout);
GraphicsFile singularityTexture = project.Grp.Files.First(f => f.Index == Puzzle.Settings.SingularityTexture);
Expand Down
7 changes: 4 additions & 3 deletions src/SerialLoops.Lib/Items/ScenarioItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Util;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -9,13 +10,13 @@ public class ScenarioItem : Item
public ScenarioStruct Scenario { get; set; }
public List<(ScenarioCommand.ScenarioVerb Command, string Parameter)> ScenarioCommands { get; set; } = new();

public ScenarioItem(ScenarioStruct scenario, Project project) : base("Scenario", ItemType.Scenario)
public ScenarioItem(ScenarioStruct scenario, Project project, ILogger log) : base("Scenario", ItemType.Scenario)
{
Scenario = scenario;
Refresh(project);
Refresh(project, log);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
IEnumerable<PuzzleItem> puzzleItems = project.Items.Where(i => i.Type == ItemType.Puzzle).Cast<PuzzleItem>().ToList();
IEnumerable<ScriptItem> scriptItems = project.Items.Where(i => i.Type == ItemType.Script).Cast<ScriptItem>().ToList();
Expand Down
28 changes: 21 additions & 7 deletions src/SerialLoops.Lib/Items/ScriptItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public ScriptItem(EventFile evt, ILogger log) : base(evt.Name[0..^1], ItemType.S
}
}

public Dictionary<ScriptSection, List<ScriptItemCommand>> GetScriptCommandTree(Project project)
public Dictionary<ScriptSection, List<ScriptItemCommand>> GetScriptCommandTree(Project project, ILogger log)
{
Dictionary<ScriptSection, List<ScriptItemCommand>> commands = new();
foreach (ScriptSection section in Event.ScriptSections)
{
commands.Add(section, new());
foreach (ScriptCommandInvocation command in section.Objects)
{
commands[section].Add(ScriptItemCommand.FromInvocation(command, section, commands[section].Count, Event, project));
commands[section].Add(ScriptItemCommand.FromInvocation(command, section, commands[section].Count, Event, project, log));
}
}
return commands;
}

public void CalculateGraphEdges(Dictionary<ScriptSection, List<ScriptItemCommand>> commandTree)
public void CalculateGraphEdges(Dictionary<ScriptSection, List<ScriptItemCommand>> commandTree, ILogger log)
{
foreach (ScriptSection section in commandTree.Keys)
{
Expand All @@ -75,12 +75,26 @@ public void CalculateGraphEdges(Dictionary<ScriptSection, List<ScriptItemCommand
}
else if (command.Verb == CommandVerb.GOTO)
{
Graph.AddEdge(new() { Source = section, Target = ((ScriptSectionScriptParameter)command.Parameters[0]).Section });
try
{
Graph.AddEdge(new() { Source = section, Target = ((ScriptSectionScriptParameter)command.Parameters[0]).Section });
}
catch (ArgumentOutOfRangeException)
{
log.LogWarning("Failed to add graph edge for GOTO command as script section parameter was out of range.");
}
@continue = true;
}
else if (command.Verb == CommandVerb.VGOTO)
{
Graph.AddEdge(new() { Source = section, Target = ((ScriptSectionScriptParameter)command.Parameters[1]).Section });
try
{
Graph.AddEdge(new() { Source = section, Target = ((ScriptSectionScriptParameter)command.Parameters[1]).Section });
}
catch (ArgumentOutOfRangeException)
{
log.LogWarning("Failed to add graph edge for VGOTO command as script section parameter was out of range.");
}
}
else if (command.Verb == CommandVerb.CHESS_VGOTO)
{
Expand Down Expand Up @@ -125,11 +139,11 @@ public void CalculateGraphEdges(Dictionary<ScriptSection, List<ScriptItemCommand
}
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
Graph = new();
Graph.AddVertexRange(Event.ScriptSections);
CalculateGraphEdges(GetScriptCommandTree(project));
CalculateGraphEdges(GetScriptCommandTree(project, log), log);
}
}
}
3 changes: 2 additions & 1 deletion src/SerialLoops.Lib/Items/TopicItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Lib.Util;
using System.Linq;

Expand All @@ -16,7 +17,7 @@ public TopicItem(TopicStruct topicStruct, Project project) : base($"{topicStruct
PopulateScriptUses(project);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
PopulateScriptUses(project);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SerialLoops.Lib/Items/VoicedLineItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void Replace(string audioFile, string baseDirectory, string iterativeDire
File.Copy(Path.Combine(baseDirectory, VoiceFile), Path.Combine(iterativeDirectory, VoiceFile), true);
}

public override void Refresh(Project project)
public override void Refresh(Project project, ILogger log)
{
PopulateScriptUses(project);
}
Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops.Lib/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public LoadProjectResult LoadArchives(ILogger log, IProgressTracker tracker)
tracker.Focus("Puzzles", 1);
Items.AddRange(Dat.Files
.Where(d => d.Name.StartsWith("SLG"))
.Select(d => new PuzzleItem(d.CastTo<PuzzleFile>(), this)));
.Select(d => new PuzzleItem(d.CastTo<PuzzleFile>(), this, log)));
tracker.Finished++;

tracker.Focus("Topics", 2);
Expand All @@ -315,7 +315,7 @@ public LoadProjectResult LoadArchives(ILogger log, IProgressTracker tracker)
tracker.Focus("Scenario", 1);
EventFile scenarioFile = Evt.Files.First(f => f.Name == "SCENARIOS");
scenarioFile.InitializeScenarioFile();
Items.Add(new ScenarioItem(scenarioFile.Scenario, this));
Items.Add(new ScenarioItem(scenarioFile.Scenario, this, log));
tracker.Finished++;

tracker.Focus("Group Selections", scenarioFile.Scenario.Selects.Count);
Expand Down
53 changes: 47 additions & 6 deletions src/SerialLoops.Lib/Script/ScriptItemCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HaruhiChokuretsuLib.Archive.Data;
using HaruhiChokuretsuLib.Archive.Event;
using HaruhiChokuretsuLib.Util;
using QuikGraph;
using QuikGraph.Algorithms.Observers;
using QuikGraph.Algorithms.Search;
Expand All @@ -23,13 +24,13 @@ public class ScriptItemCommand
public int Index { get; set; }
public Project Project { get; set; }

public static ScriptItemCommand FromInvocation(ScriptCommandInvocation invocation, ScriptSection section, int index, EventFile eventFile, Project project)
public static ScriptItemCommand FromInvocation(ScriptCommandInvocation invocation, ScriptSection section, int index, EventFile eventFile, Project project, ILogger log)
{
return new()
{
Invocation = invocation,
Verb = (CommandVerb)Enum.Parse(typeof(CommandVerb), invocation.Command.Mnemonic),
Parameters = GetScriptParameters(invocation, eventFile, project),
Parameters = GetScriptParameters(invocation, section, eventFile, project, log),
Section = section,
Index = index,
Script = eventFile,
Expand Down Expand Up @@ -72,7 +73,7 @@ public List<ScriptItemCommand> WalkCommandGraph(Dictionary<ScriptSection, List<S
return commands;
}

private static List<ScriptParameter> GetScriptParameters(ScriptCommandInvocation invocation, EventFile eventFile, Project project)
private static List<ScriptParameter> GetScriptParameters(ScriptCommandInvocation invocation, ScriptSection section, EventFile eventFile, Project project, ILogger log)
{
List<ScriptParameter> parameters = new();
MessageInfoFile messageInfo = project.Dat.Files.First(f => f.Name == "MESSINFOS").CastTo<MessageInfoFile>();
Expand Down Expand Up @@ -329,10 +330,26 @@ private static List<ScriptParameter> GetScriptParameters(ScriptCommandInvocation
parameter = eventFile.LabelsSection.Objects.FirstOrDefault(l => l.Id != 00)?.Id ?? 0;
if (parameter == 0)
{
throw new ArgumentException("Adding GOTO command failed: no section with a label exists");
log.LogError($"Adding GOTO command in section {section.Name} failed: no section with a label exists");
}
}
try
{
parameters.Add(new ScriptSectionScriptParameter("Script Section", eventFile.ScriptSections.First(s => s.Name == eventFile.LabelsSection.Objects.First(l => l.Id == parameter).Name.Replace("/", ""))));
}
catch (InvalidOperationException)
{
log.LogWarning($"Failed to evaluate script section for GOTO command in section {section.Name}: references a non-existent section. Resetting!");
parameter = eventFile.LabelsSection.Objects.FirstOrDefault(l => l.Id != 00)?.Id ?? 0;
if (parameter == 0)
{
log.LogError($"Adding GOTO command in section {section.Name} failed: no section with a label exists!");
}
else
{
parameters.Add(new ScriptSectionScriptParameter("Script Section", eventFile.ScriptSections.First(s => s.Name == eventFile.LabelsSection.Objects.First(l => l.Id == parameter).Name.Replace("/", ""))));
}
}
parameters.Add(new ScriptSectionScriptParameter("Script Section", eventFile.ScriptSections.First(s => s.Name == eventFile.LabelsSection.Objects.First(l => l.Id == parameter).Name.Replace("/", ""))));
}
break;
case CommandVerb.SCENE_GOTO:
Expand All @@ -356,7 +373,31 @@ private static List<ScriptParameter> GetScriptParameters(ScriptCommandInvocation
break;
// 1 is unused
case 2:
parameters.Add(new ScriptSectionScriptParameter("Script Section", eventFile.ScriptSections.First(s => s.Name == eventFile.LabelsSection.Objects.First(l => l.Id == parameter).Name.Replace("/", ""))));
if (parameter == 0)
{
parameter = eventFile.LabelsSection.Objects.FirstOrDefault(l => l.Id != 00)?.Id ?? 0;
if (parameter == 0)
{
log.LogError($"Adding VGOTO command in section {section.Name} failed: no section with a label exists");
}
}
try
{
parameters.Add(new ScriptSectionScriptParameter("Script Section", eventFile.ScriptSections.First(s => s.Name == eventFile.LabelsSection.Objects.First(l => l.Id == parameter).Name.Replace("/", ""))));
}
catch (InvalidOperationException)
{
log.LogWarning($"Failed to evaluate script section for VGOTO command in section {section.Name}: references a non-existent section. Resetting!");
parameter = eventFile.LabelsSection.Objects.FirstOrDefault(l => l.Id != 00)?.Id ?? 0;
if (parameter == 0)
{
log.LogError($"Adding GOTO command in section {section.Name} failed: no section with a label exists!");
}
else
{
parameters.Add(new ScriptSectionScriptParameter("Script Section", eventFile.ScriptSections.First(s => s.Name == eventFile.LabelsSection.Objects.First(l => l.Id == parameter).Name.Replace("/", ""))));
}
}
break;
}
break;
Expand Down
Loading

0 comments on commit 367cf75

Please sign in to comment.