public static UndertaleVariable DefineLocal(this IList <UndertaleVariable> list, IList <UndertaleVariable> originalReferencedLocalVars, int localId, string name, IList <UndertaleString> strg, UndertaleData data) { bool bytecode14 = (data?.GeneralInfo?.BytecodeVersion <= 14); if (bytecode14) { UndertaleVariable search = list.Where((x) => x.Name.Content == name).FirstOrDefault(); if (search != null) { return(search); } } // Use existing registered variables. if (originalReferencedLocalVars != null) { UndertaleVariable refvar; if (data?.GMS2_3 == true) { refvar = originalReferencedLocalVars.Where((x) => x.Name.Content == name).FirstOrDefault(); } else { refvar = originalReferencedLocalVars.Where((x) => x.Name.Content == name && x.VarID == localId).FirstOrDefault(); } if (refvar != null) { return(refvar); } } var str = strg.MakeString(name, out int id); if (data?.GMS2_3 == true) { localId = id; } UndertaleVariable vari = new UndertaleVariable() { Name = str, InstanceType = bytecode14 ? UndertaleInstruction.InstanceType.Undefined : UndertaleInstruction.InstanceType.Local, VarID = bytecode14 ? 0 : localId, NameStringID = id }; list.Add(vari); return(vari); }
public static UndertaleVariable EnsureDefined(this IList <UndertaleVariable> list, string name, UndertaleInstruction.InstanceType inst, bool isBuiltin, IList <UndertaleString> strg, UndertaleData data, bool fast = false) { if (inst == UndertaleInstruction.InstanceType.Local) { throw new InvalidOperationException("Use DefineLocal instead"); } bool bytecode14 = (data?.GeneralInfo?.BytecodeVersion <= 14); if (bytecode14) { inst = UndertaleInstruction.InstanceType.Undefined; } UndertaleVariable vari = fast ? null : list.Where((x) => x.Name?.Content == name && x.InstanceType == inst).FirstOrDefault(); if (vari == null) { var str = strg.MakeString(name, out int id); var oldId = data.VarCount1; if (!bytecode14) { if (data.GMS2_3) { // GMS 2.3+ if (!isBuiltin) { data.VarCount1++; data.VarCount2 = data.VarCount1; } oldId = (uint)id; } else if (!data.DifferentVarCounts) { // Bytecode 16+ data.VarCount1++; data.VarCount2++; } else { // Bytecode 15 if (inst == UndertaleInstruction.InstanceType.Self && !isBuiltin) { oldId = data.VarCount2; data.VarCount2++; } else if (inst == UndertaleInstruction.InstanceType.Global) { data.VarCount1++; } } } vari = new UndertaleVariable() { Name = str, InstanceType = inst, VarID = bytecode14 ? 0 : (isBuiltin ? (int)UndertaleInstruction.InstanceType.Builtin : (int)oldId), NameStringID = id }; list.Add(vari); } return(vari); }