All files / src/lib/compiler compileEntityEvents.ts

74.07% Statements 40/54
46.84% Branches 37/79
100% Functions 3/3
73.08% Lines 38/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172  7x               7x   7x 7x                                   7x                                 56x   56x                             56x       66x   66x 74x 74x         74x 72x   72x 72x                                   72x 11x   2x           2x             2x                   56x           10x       56x   56x   56x       56x   56x 56x 56x 56x       56x 33x 33x   23x       56x               7x   7x  
import type { ScriptEvent } from "shared/lib/entities/entitiesTypes";
import ScriptBuilder, {
  ScriptBuilderEntity,
  ScriptBuilderEntityType,
  ScriptBuilderOptions,
  ScriptOutput,
} from "./scriptBuilder";
import { PrecompiledScene } from "./generateGBVMData";
import { ScriptEventHandlers } from "lib/project/loadScriptEventHandlers";
import { LATEST_PROJECT_VERSION } from "lib/project/migration/migrateProjectResources";
 
const STRING_NOT_FOUND = "STRING_NOT_FOUND";
const VARIABLE_NOT_FOUND = "VARIABLE_NOT_FOUND";
 
type CompileEntityEventsOptions = Partial<ScriptBuilderOptions> & {
  scriptEventHandlers: ScriptEventHandlers;
  output: ScriptOutput;
  branch: boolean;
  loop: boolean;
  lock: boolean;
  isFunction: boolean;
  scene: PrecompiledScene;
  sceneIndex: number;
  entity?: ScriptBuilderEntity;
  entityType: string;
  entityIndex: number;
  debugEnabled: boolean;
  warnings: (msg: string) => void;
};
 
const compileEntityEvents = (
  scriptSymbolName: string,
  input: ScriptEvent[] = [],
  options: CompileEntityEventsOptions
) => {
  const {
    output = [],
    branch = false,
    scene,
    sceneIndex,
    entity,
    entityType,
    entityIndex,
    warnings,
    loop,
    lock,
    isFunction,
  } = options;
 
  const location = {
    ...(scene && {
      scene: scene.name || `Scene ${sceneIndex + 1}`,
    }),
    ...(entityType && {
      scriptType: entityType,
    }),
    ...(entityType === "actor" && {
      actor: entity?.name || `Actor ${entityIndex + 1}`,
    }),
    ...(entityType === "trigger" && {
      actor: entity?.name || `Trigger ${entityIndex + 1}`,
    }),
  };
 
  const compileEventsWithScriptBuilder = (
    scriptBuilder: ScriptBuilder,
    subInput: ScriptEvent[] = []
  ) => {
    const scriptEventHandlers = options.scriptEventHandlers;
 
    for (let i = 0; i < subInput.length; i++) {
      const command = subInput[i].command;
      Iif (subInput[i].args?.__comment) {
        // Skip commented events
        // eslint-disable-next-line no-continue
        continue;
      }
      if (scriptEventHandlers[command]) {
        scriptBuilder.addDebugSymbol(scriptSymbolName, subInput[i].id);
 
        try {
          scriptEventHandlers[command]?.compile(
            { ...subInput[i].args, ...subInput[i].children },
            {
              LATEST_PROJECT_VERSION: LATEST_PROJECT_VERSION,
              ...options,
              ...scriptBuilder,
              scriptSymbolName,
              event: subInput[i],
            }
          );
        } catch (e) {
          console.error(e);
          throw new Error(
            `Compiling "${command}" failed with error "${e}". ${JSON.stringify(
              location
            )}`
          );
        }
        if (scriptEventHandlers[command]?.isConditional) {
          scriptBuilder.addDebugEndSymbol(scriptSymbolName, subInput[i].id);
        }
      } else Iif (command === "INTERNAL_SET_CONTEXT") {
        const args = subInput[i].args ?? {};
        scriptBuilder.options.entity = args.entity as ScriptBuilderEntity;
        scriptBuilder.options.entityType =
          args.entityType as ScriptBuilderEntityType;
        scriptBuilder.options.entityScriptKey = String(args.scriptKey);
      } else Iif (command === "INTERNAL_IF_PARAM") {
        const args = subInput[i].args;
        scriptBuilder.ifParamValue(
          args?.parameter as number,
          args?.value as number,
          subInput[i]?.children?.true
        );
      } else Iif (command !== "EVENT_END") {
        warnings(
          `No compiler for command "${command}". Are you missing a plugin? ${JSON.stringify(
            location
          )}`
        );
      }
    }
  };
 
  const helpers = {
    ...options,
    compileEvents: (
      scriptBuilder: ScriptBuilder,
      childInput: ScriptEvent[]
    ) => {
      compileEventsWithScriptBuilder(scriptBuilder, childInput);
    },
  };
 
  const scriptBuilder = new ScriptBuilder(output, helpers);
 
  const loopId = loop ? scriptBuilder.getNextLabel() : "";
 
  Iif (loop && input.length > 0) {
    scriptBuilder._label(loopId);
  }
 
  compileEventsWithScriptBuilder(scriptBuilder, input);
 
  try {
    if (!branch) {
      scriptBuilder._packLocals();
      Iif (loop && input.length > 0 && output.length > 1) {
        scriptBuilder.idle();
        scriptBuilder._jump(loopId);
      }
      if (isFunction) {
        scriptBuilder.unreserveLocals();
        scriptBuilder.returnFar();
      } else {
        scriptBuilder.scriptEnd();
      }
    }
 
    return scriptBuilder.toScriptString(scriptSymbolName, lock);
  } catch (e) {
    throw new Error(
      `Compiling failed with error "${e}". ${JSON.stringify(location)}`
    );
  }
};
 
export default compileEntityEvents;
 
export { STRING_NOT_FOUND, VARIABLE_NOT_FOUND };