/// <summary>Pre-Adapt helper for save targets.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The relevant entry ID.</param> /// <param name="canPreExist">Whether the variable is allowed to already exist.</param> /// <param name="tagType">The required type.</param> /// <param name="required">Whether a save name *must* be specified by the user.</param> /// <param name="defaultName">The default name (or null if none).</param> public void PreAdaptSaveMode(CILAdaptationValues values, int entry, bool canPreExist, TagType tagType, bool required, string defaultName = null) { CommandEntry cent = values.CommandAt(entry); string saveName = cent.GetSaveNameNoParse(defaultName); if (saveName == null) { if (!required) { return; } throw new ErrorInducedException($"Command '{Meta.Name}' requires a save name, but none was given."); } int preVarLoc = values.LocalVariableLocation(saveName, out TagReturnType preVarType); if (preVarLoc >= 0) { if (!canPreExist) { throw new ErrorInducedException($"Already have a save target var (labeled '{TextStyle.SeparateVal(saveName)}')?!"); } if (preVarType.Type != tagType) { throw new ErrorInducedException($"Already have a save target var (labeled '{TextStyle.SeparateVal(saveName)}', with type " + $"'{TextStyle.SeparateVal(preVarType.Type.TypeName)}') of wrong type (expected '{TextStyle.SeparateVal(tagType.TypeName)}')."); } cent.SaveLoc = preVarLoc; } else { cent.SaveLoc = values.AddVariable(saveName, tagType); } }
/// <summary> /// Gets the save variable location (used in Adapt, after using <see cref="PreAdaptSaveMode(CILAdaptationValues, int, bool, TagType, bool, string)"/> in PreAdapt). /// </summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The relevant entry ID.</param> /// <param name="defaultName">The default name (or null if none).</param> public static int GetSaveLoc(CILAdaptationValues values, int entry, string defaultName = null) { CommandEntry cent = values.CommandAt(entry); string sn = values.Entry.Entries[cent.BlockStart - 1].GetSaveNameNoParse(defaultName); return(cent.VarLoc(sn)); }
/// <summary>Adapts a command entry to CIL.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The present entry ID.</param> public override void AdaptToCIL(CILAdaptationValues values, int entry) { CommandEntry cent = values.CommandAt(entry); bool debug = cent.DBMode <= DebugMode.FULL; MapTag mt = MapTag.For(cent.Arguments[0].ToString()); if (mt.Internal.Count == 0) { throw new ErrorInducedException("Empty map input to require!"); } foreach (string varn in mt.Internal.Keys) { values.LoadQueue(); values.LoadEntry(entry); values.LoadLocalVariable(cent.VarLoc(varn)); values.ILGen.Emit(OpCodes.Ldstr, varn); values.ILGen.Emit(OpCodes.Call, REQUIRECOMMAND_CHECKFORVALIDITY); } if (debug) { values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, REQUIRECOMMAND_OUTPUTSUCCESS); } }
/// <summary>Adapts a command entry to CIL.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The relevant entry ID.</param> public override void AdaptToCIL(CILAdaptationValues values, int entry) { CommandEntry cent = values.CommandAt(entry); if (cent.DBMode == DebugMode.FULL) { values.MarkCommand(entry); values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, Method_DebugOutput); } }
/// <summary>Adapts a command entry to CIL.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The present entry ID.</param> public override void AdaptToCIL(CILAdaptationValues values, int entry) { CommandEntry cent = values.CommandAt(entry); bool db = cent.DBMode <= DebugMode.FULL; if (db) { values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, DebugStopMethod); } values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[^ 1]);
/// <summary>Prepares to adapt a command entry to CIL.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The relevant entry ID.</param> public virtual void PreAdaptToCIL(CILAdaptationValues values, int entry) { if (SaveMode != CommandSaveMode.NO_SAVE) { CommandEntry cent = values.CommandAt(entry); TagType saveTagType = cent.System.TagTypes.TypeForName(SaveType); if (saveTagType == null) { throw new ErrorInducedException($"Command '{TextStyle.SeparateVal(Meta.Name)}' specifies a non-existent save tag type '{TextStyle.SeparateVal(SaveType)}'."); } PreAdaptSaveMode(values, entry, true, saveTagType, SaveMode != CommandSaveMode.WHEN_NAME_SPECIFIED, DefaultSaveName); } }
/// <summary>Adapts a command entry to CIL.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The relevant entry ID.</param> public override void AdaptToCIL(CILAdaptationValues values, int entry) { CommandEntry cent = values.CommandAt(entry); bool debug = cent.DBMode.ShouldShow(DebugMode.FULL); values.MarkCommand(entry); TagReturnType returnType = ArgumentCompiler.ReturnType(cent.Arguments[2], values); string lvarname = cent.Arguments[0].ToString().ToLowerFast(); SingleCILVariable locVar = cent.VarLookup[lvarname]; // This method: // runnable.Var = TYPE.CREATE_FOR(tagdata, runnable.entry.arg2()); // or: // runnable.Var = runnable.entry.arg2(); values.LoadRunnable(); values.LoadArgumentObject(entry, 2); if (cent.Arguments.Length > 4) { string type_name = cent.Arguments[4].ToString().ToLowerFast(); TagType specifiedType = cent.System.TagSystem.Types.RegisteredTypes[type_name]; TagReturnType specifiedReturnType = new(specifiedType, specifiedType.Meta.RawInternal); values.EnsureType(returnType, specifiedReturnType); // Ensure the correct object type. returnType = specifiedReturnType; } else { TagReturnType specifiedReturnType = new(returnType.Type, returnType.Type.Meta.RawInternal); values.EnsureType(returnType, specifiedReturnType); // Ensure the correct object type. returnType = specifiedReturnType; } values.ILGen.Emit(OpCodes.Stfld, locVar.Field); // Push the result into the local var if (debug) // If in debug mode... { values.LoadLocalVariable(locVar.Index); // Load variable. if (returnType.IsRaw) { values.ILGen.Emit(OpCodes.Newobj, returnType.Type.RawInternalConstructor); // Handle raw translation if needed. } values.ILGen.Emit(OpCodes.Ldstr, lvarname); // Load the variable name as a string. values.ILGen.Emit(OpCodes.Ldstr, returnType.Type.TypeName); // Load the variable type name as a string. values.LoadQueue(); // Load the queue values.LoadEntry(entry); // Load the entry values.ILGen.Emit(OpCodes.Call, Method_DebugHelper); // Call the debug method } }
/// <summary>Adapts a command entry to CIL.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The present entry ID.</param> public override void AdaptToCIL(CILAdaptationValues values, int entry) { CommandEntry cent = values.CommandAt(entry); bool db = cent.DBMode <= DebugMode.FULL; if (cent.IsCallback) { int lvar_ind_loc = GetSaveLoc(values, entry); values.LoadQueue(); if (db) { values.LoadEntry(entry); } else { values.ILGen.Emit(OpCodes.Ldc_I4, cent.BlockStart - 1); } values.LoadLocalVariable(lvar_ind_loc); values.ILGen.Emit(OpCodes.Call, db ? TryForeachCILMethod : TryForeachCILMethodNoDebug); values.ILGen.Emit(OpCodes.Brtrue, values.Entry.AdaptedILPoints[cent.BlockStart]); return; } string arg = cent.Arguments[0].ToString(); if (arg == "stop") { for (int i = entry - 1; i >= 0; i--) { CommandEntry nextEntry = values.Entry.Entries[i]; if (nextEntry.Command is not ForeachCommand || nextEntry.IsCallback) { continue; } string a0 = nextEntry.Arguments[0].ToString(); if (a0 == "start" && nextEntry.InnerCommandBlock != null) { if (db) { values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, DebugStopMethod); } values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 2]); return; } } throw new ErrorInducedException("Invalid 'foreach stop' command: not inside a foreach block!"); } else if (arg == "next") { for (int i = entry - 1; i >= 0; i--) { CommandEntry nextEntry = values.Entry.Entries[i]; if (nextEntry.Command is not ForeachCommand || nextEntry.IsCallback) { continue; } string a0 = nextEntry.Arguments[0].ToString(); if (a0 == "start" && nextEntry.InnerCommandBlock != null) { if (db) { values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, DebugNextMethod); } values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 1]); return; } } throw new ErrorInducedException("Invalid 'foreach next' command: not inside a foreach block!"); } else if (arg == "start") { SingleCILVariable locVar = cent.VarLookup[cent.GetSaveNameNoParse("foreach_value")]; values.LoadQueue(); values.LoadEntry(entry); values.LoadRunnable(); values.ILGen.Emit(OpCodes.Call, CreateListItemMethod); values.ILGen.Emit(OpCodes.Stfld, locVar.Field); values.LoadLocalVariable(locVar.Index); values.ILGen.Emit(OpCodes.Call, db ? TryForeachNumberedCILMethod : TryForeachNumberedCIL_NoDebugMethod); values.ILGen.Emit(OpCodes.Brfalse, values.Entry.AdaptedILPoints[cent.BlockEnd + 2]); } else { throw new ErrorInducedException("Invalid 'foreach' command: unknown argument: " + arg); } }
/// <summary>Adapts a command entry to CIL.</summary> /// <param name="values">The adaptation-relevant values.</param> /// <param name="entry">The present entry ID.</param> public override void AdaptToCIL(CILAdaptationValues values, int entry) { CommandEntry cent = values.CommandAt(entry); bool db = cent.DBMode <= DebugMode.FULL; if (cent.IsCallback) { string sn = values.Entry.Entries[cent.BlockStart - 1].GetSaveNameNoParse("while_index"); int lvar_ind_loc = cent.VarLoc(sn); values.LoadQueue(); if (db) { values.LoadEntry(entry); } else { values.ILGen.Emit(OpCodes.Ldc_I4, cent.BlockStart - 1); } values.LoadLocalVariable(lvar_ind_loc); values.ILGen.Emit(OpCodes.Call, db ? TryWhileCILMethod : TryWhileCILMethodNoDebug); values.ILGen.Emit(OpCodes.Brtrue, values.Entry.AdaptedILPoints[cent.BlockStart]); return; } string arg = cent.Arguments[0].ToString(); if (arg == "stop") { for (int i = entry - 1; i >= 0; i--) { CommandEntry nextEntry = values.Entry.Entries[i]; if (nextEntry.Command is not WhileCommand || nextEntry.IsCallback) { continue; } string a0 = nextEntry.Arguments[0].ToString(); if (a0 != "stop" && a0 != "next" && nextEntry.InnerCommandBlock != null) { if (db) { values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, DebugStopMethod); } values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 2]); return; } } throw new ErrorInducedException("Invalid 'while stop' command: not inside a while block!"); } else if (arg == "next") { for (int i = entry - 1; i >= 0; i--) { CommandEntry nextEntry = values.Entry.Entries[i]; if (nextEntry.Command is not WhileCommand || nextEntry.IsCallback) { continue; } string a0 = nextEntry.ToString(); if (a0 != "stop" && a0 != "next" && nextEntry.InnerCommandBlock != null) { if (db) { values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, DebugNextMethod); } values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 1]); return; } } throw new ErrorInducedException("Invalid 'while next' command: not inside a while block!"); } else { values.LoadQueue(); values.LoadEntry(entry); values.ILGen.Emit(OpCodes.Call, CreateIndexObjectMethod); values.ILGen.Emit(OpCodes.Stfld, cent.VarLookup[cent.GetSaveNameNoParse("while_index")].Field); values.ILGen.Emit(OpCodes.Call, db ? TryWhileNumberedCILMethod : TryWhileNumberedCIL_NoDebugMethod); values.ILGen.Emit(OpCodes.Brfalse, values.Entry.AdaptedILPoints[cent.BlockEnd + 2]); } }