Jsm.File.Reader.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using OpenVIII.Fields.Scripts.Instructions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace OpenVIII.Fields.Scripts
  6. {
  7. public static partial class Jsm
  8. {
  9. public static partial class File
  10. {
  11. public static List<GameObject> Read(Byte[] data)
  12. {
  13. unsafe
  14. {
  15. fixed (Byte* ptr = data)
  16. {
  17. if (ptr == null) return null;
  18. Header* header = (Header*)ptr;
  19. Group* areas = (Group*)(ptr + sizeof(Header));
  20. Group* doors = areas + header->CountAreas;
  21. Group* modules = doors + header->CountDoors;
  22. Group* objects = modules + header->CountModules;
  23. Group* end = objects + header->CountObjects;
  24. Script* scripts = (Script*)(ptr + header->ScriptsOffset);
  25. Operation* operation = (Operation*)(ptr + header->OperationsOffset);
  26. Int64 groupNumber = end - areas;
  27. Group[] groups = new Group[groupNumber];
  28. for (Group* group = areas; group < end; group++)
  29. groups[--groupNumber] = *group;
  30. List<GameObject> gameObjects = new List<GameObject>(groups.Length);
  31. foreach (Group group in groups.OrderBy(g => g.Label))
  32. {
  33. List<GameScript> objectScripts = new List<GameScript>(group.ScriptsCount + 1);
  34. for (Int32 s = 0; s <= group.ScriptsCount; s++)
  35. {
  36. Int32 scriptLabel = group.Label + s;
  37. UInt16 position = scripts->Position;
  38. scripts++;
  39. UInt16 count = (UInt16)(scripts->Position - position);
  40. Jsm.ExecutableSegment scriptSegment = MakeScript(operation + position, count);
  41. objectScripts.Add(new GameScript(scriptLabel, scriptSegment));
  42. }
  43. gameObjects.Add(new GameObject(group.Label, objectScripts));
  44. }
  45. return gameObjects;
  46. }
  47. }
  48. }
  49. private static unsafe Jsm.ExecutableSegment MakeScript(Operation* operation, UInt16 count)
  50. {
  51. List<JsmInstruction> instructions = new List<JsmInstruction>(count / 2);
  52. LabeledStack stack = new LabeledStack();
  53. LabelBuilder labelBuilder = new LabelBuilder(count);
  54. for (Int32 i = 0; i < count; i++)
  55. {
  56. Jsm.Opcode opcode = operation->Opcode;
  57. Int32 parameter = operation->Parameter;
  58. operation++;
  59. stack.CurrentLabel = i;
  60. IJsmExpression expression = Jsm.Expression.TryMake(opcode, parameter, stack);
  61. if (expression != null)
  62. {
  63. stack.Push(expression);
  64. continue;
  65. }
  66. JsmInstruction instruction = JsmInstruction.TryMake(opcode, parameter, stack);
  67. if (instruction != null)
  68. {
  69. labelBuilder.TraceInstruction(i, stack.CurrentLabel, new IndexedInstruction(instructions.Count, instruction));
  70. instructions.Add(instruction);
  71. continue;
  72. }
  73. throw new NotSupportedException(opcode.ToString());
  74. }
  75. if (stack.Count != 0)
  76. throw new InvalidProgramException("Stack unbalanced.");
  77. if (!(instructions.First() is LBL))
  78. throw new InvalidProgramException("Script must start with a label.");
  79. if (!(instructions.Last() is IRET))
  80. throw new InvalidProgramException("Script must end with a return.");
  81. // Switch from opcodes to instructions
  82. HashSet<Int32> labelIndices = labelBuilder.Commit();
  83. // Merge similar instructions
  84. instructions = InstructionMerger.Merge(instructions, labelIndices);
  85. // Combine instructions to logical blocks
  86. IReadOnlyList<Jsm.IJsmControl> controls = Jsm.Control.Builder.Build(instructions);
  87. // Arrange instructions by segments and return root
  88. return Jsm.Segment.Builder.Build(instructions, controls);
  89. }
  90. }
  91. }
  92. }