From 36176b10dc33e070fc471fe3e5c873d20d3c0ce6 Mon Sep 17 00:00:00 2001 From: Argent77 <4519923+Argent77@users.noreply.github.com> Date: Sat, 29 Jul 2023 13:36:18 +0200 Subject: [PATCH] Improve "Check for invalid opcodes" to include opcode definitions in effect parameters (e.g. op101, ...) Created new specialized datatype for opcode list representation. --- .../check/EffectValidationChecker.java | 6 ++- src/org/infinity/datatype/EffectBitmap.java | 42 +++++++++++++++++++ .../infinity/resource/effects/Opcode101.java | 4 +- .../infinity/resource/effects/Opcode198.java | 6 +-- .../infinity/resource/effects/Opcode261.java | 6 +-- .../infinity/resource/effects/Opcode276.java | 5 +-- .../infinity/resource/effects/Opcode337.java | 5 +-- 7 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 src/org/infinity/datatype/EffectBitmap.java diff --git a/src/org/infinity/check/EffectValidationChecker.java b/src/org/infinity/check/EffectValidationChecker.java index 31598914c..9cb0261ac 100644 --- a/src/org/infinity/check/EffectValidationChecker.java +++ b/src/org/infinity/check/EffectValidationChecker.java @@ -8,7 +8,9 @@ import java.util.List; import org.infinity.NearInfinity; +import org.infinity.datatype.EffectBitmap; import org.infinity.datatype.EffectType; +import org.infinity.datatype.IsNumeric; import org.infinity.resource.AbstractStruct; import org.infinity.resource.Profile; import org.infinity.resource.Resource; @@ -53,8 +55,8 @@ protected Runnable newWorker(ResourceEntry entry) { private void search(ResourceEntry entry, AbstractStruct struct) { for (final StructEntry field : struct.getFlatFields()) { - if (field instanceof EffectType) { - int value = ((EffectType) field).getValue(); + if (field instanceof EffectType || field instanceof EffectBitmap) { + int value = ((IsNumeric) field).getValue(); final BaseOpcode opcode = BaseOpcode.getOpcode(value); if (opcode instanceof DefaultOpcode) { synchronized (hitFrame) { diff --git a/src/org/infinity/datatype/EffectBitmap.java b/src/org/infinity/datatype/EffectBitmap.java new file mode 100644 index 000000000..bdf4a483c --- /dev/null +++ b/src/org/infinity/datatype/EffectBitmap.java @@ -0,0 +1,42 @@ +// Near Infinity - An Infinity Engine Browser and Editor +// Copyright (C) 2001 Jon Olav Hauglid +// See LICENSE.txt for license information + +package org.infinity.datatype; + +import java.nio.ByteBuffer; + +import org.infinity.resource.effects.BaseOpcode; + +/** + * Specialized {@link Bitmap} type that represents a list of available effect opcodes. + */ +public class EffectBitmap extends Bitmap { + public static final String EFFECT_FX = "Effect"; + + public EffectBitmap(ByteBuffer buffer, int offset, int length) { + this(buffer, offset, length, EFFECT_FX); + } + + public EffectBitmap(ByteBuffer buffer, int offset, int length, String name) { + super(buffer, offset, length, name, BaseOpcode.getEffectNames()); + } + + public EffectBitmap(ByteBuffer buffer, int offset, int length, boolean signed) { + this(buffer, offset, length, EFFECT_FX, signed); + } + + public EffectBitmap(ByteBuffer buffer, int offset, int length, String name, boolean signed) { + super(buffer, offset, length, name, BaseOpcode.getEffectNames(), signed); + } + + public EffectBitmap(ByteBuffer buffer, int offset, int length, boolean signed, + boolean showAsHex) { + this(buffer, offset, length, EFFECT_FX, signed, showAsHex); + } + + public EffectBitmap(ByteBuffer buffer, int offset, int length, String name, boolean signed, + boolean showAsHex) { + super(buffer, offset, length, name, BaseOpcode.getEffectNames(), signed, showAsHex); + } +} diff --git a/src/org/infinity/resource/effects/Opcode101.java b/src/org/infinity/resource/effects/Opcode101.java index 309f44bbe..eff538639 100644 --- a/src/org/infinity/resource/effects/Opcode101.java +++ b/src/org/infinity/resource/effects/Opcode101.java @@ -7,9 +7,9 @@ import java.nio.ByteBuffer; import java.util.List; -import org.infinity.datatype.Bitmap; import org.infinity.datatype.Datatype; import org.infinity.datatype.DecNumber; +import org.infinity.datatype.EffectBitmap; import org.infinity.resource.AbstractStruct; import org.infinity.resource.StructEntry; @@ -30,7 +30,7 @@ public Opcode101() { protected String makeEffectParamsGeneric(Datatype parent, ByteBuffer buffer, int offset, List list, boolean isVersion1) { list.add(new DecNumber(buffer, offset, 4, AbstractStruct.COMMON_UNUSED)); - list.add(new Bitmap(buffer, offset + 4, 4, "Effect", getEffectNames())); + list.add(new EffectBitmap(buffer, offset + 4, 4)); return null; } } diff --git a/src/org/infinity/resource/effects/Opcode198.java b/src/org/infinity/resource/effects/Opcode198.java index 64270b478..939ece295 100644 --- a/src/org/infinity/resource/effects/Opcode198.java +++ b/src/org/infinity/resource/effects/Opcode198.java @@ -7,9 +7,9 @@ import java.nio.ByteBuffer; import java.util.List; -import org.infinity.datatype.Bitmap; import org.infinity.datatype.Datatype; import org.infinity.datatype.DecNumber; +import org.infinity.datatype.EffectBitmap; import org.infinity.resource.AbstractStruct; import org.infinity.resource.Profile; import org.infinity.resource.StructEntry; @@ -18,8 +18,6 @@ * Implemention of opcode 198. */ public class Opcode198 extends BaseOpcode { - private static final String EFFECT_FX = "Effect"; - /** Returns the opcode name for the current game variant. */ private static String getOpcodeName() { switch (Profile.getEngine()) { @@ -41,7 +39,7 @@ public Opcode198() { protected String makeEffectParamsGeneric(Datatype parent, ByteBuffer buffer, int offset, List list, boolean isVersion1) { list.add(new DecNumber(buffer, offset, 4, AbstractStruct.COMMON_UNUSED)); - list.add(new Bitmap(buffer, offset + 4, 4, EFFECT_FX, getEffectNames())); + list.add(new EffectBitmap(buffer, offset + 4, 4)); return null; } diff --git a/src/org/infinity/resource/effects/Opcode261.java b/src/org/infinity/resource/effects/Opcode261.java index c16312936..a6412045c 100644 --- a/src/org/infinity/resource/effects/Opcode261.java +++ b/src/org/infinity/resource/effects/Opcode261.java @@ -10,6 +10,7 @@ import org.infinity.datatype.Bitmap; import org.infinity.datatype.Datatype; import org.infinity.datatype.DecNumber; +import org.infinity.datatype.EffectBitmap; import org.infinity.resource.AbstractStruct; import org.infinity.resource.Profile; import org.infinity.resource.StructEntry; @@ -20,7 +21,6 @@ public class Opcode261 extends BaseOpcode { private static final String EFFECT_SPELL_LEVEL = "Spell level"; private static final String EFFECT_SPELL_CLASS = "Spell class"; - private static final String EFFECT_FX = "Effect"; private static final String RES_TYPE_IWD2 = "SPL"; @@ -63,7 +63,7 @@ protected String makeEffectParamsBG1(Datatype parent, ByteBuffer buffer, int off protected String makeEffectParamsIWD(Datatype parent, ByteBuffer buffer, int offset, List list, boolean isVersion1) { list.add(new DecNumber(buffer, offset, 4, AbstractStruct.COMMON_UNUSED)); - list.add(new Bitmap(buffer, offset + 4, 4, EFFECT_FX, getEffectNames())); + list.add(new EffectBitmap(buffer, offset + 4, 4)); return null; } @@ -71,7 +71,7 @@ protected String makeEffectParamsIWD(Datatype parent, ByteBuffer buffer, int off protected String makeEffectParamsIWD2(Datatype parent, ByteBuffer buffer, int offset, List list, boolean isVersion1) { list.add(new DecNumber(buffer, offset, 4, AbstractStruct.COMMON_UNUSED)); - list.add(new Bitmap(buffer, offset + 4, 4, EFFECT_FX, getEffectNames())); + list.add(new EffectBitmap(buffer, offset + 4, 4)); return RES_TYPE_IWD2; } diff --git a/src/org/infinity/resource/effects/Opcode276.java b/src/org/infinity/resource/effects/Opcode276.java index 4c5b69baf..c300ddde6 100644 --- a/src/org/infinity/resource/effects/Opcode276.java +++ b/src/org/infinity/resource/effects/Opcode276.java @@ -10,6 +10,7 @@ import org.infinity.datatype.Bitmap; import org.infinity.datatype.Datatype; import org.infinity.datatype.DecNumber; +import org.infinity.datatype.EffectBitmap; import org.infinity.resource.AbstractStruct; import org.infinity.resource.Profile; import org.infinity.resource.StructEntry; @@ -18,8 +19,6 @@ * Implemention of opcode 276. */ public class Opcode276 extends BaseOpcode { - private static final String EFFECT_FX = "Effect"; - /** Returns the opcode name for the current game variant. */ private static String getOpcodeName() { switch (Profile.getEngine()) { @@ -56,7 +55,7 @@ protected String makeEffectParamsBG1(Datatype parent, ByteBuffer buffer, int off protected String makeEffectParamsIWD(Datatype parent, ByteBuffer buffer, int offset, List list, boolean isVersion1) { list.add(new DecNumber(buffer, offset, 4, AbstractStruct.COMMON_UNUSED)); - list.add(new Bitmap(buffer, offset + 4, 4, EFFECT_FX, getEffectNames())); + list.add(new EffectBitmap(buffer, offset + 4, 4)); return null; } diff --git a/src/org/infinity/resource/effects/Opcode337.java b/src/org/infinity/resource/effects/Opcode337.java index c45f2f69c..78a5016a1 100644 --- a/src/org/infinity/resource/effects/Opcode337.java +++ b/src/org/infinity/resource/effects/Opcode337.java @@ -7,9 +7,9 @@ import java.nio.ByteBuffer; import java.util.List; -import org.infinity.datatype.Bitmap; import org.infinity.datatype.Datatype; import org.infinity.datatype.DecNumber; +import org.infinity.datatype.EffectBitmap; import org.infinity.resource.Profile; import org.infinity.resource.StructEntry; @@ -18,7 +18,6 @@ */ public class Opcode337 extends BaseOpcode { private static final String EFFECT_MATCH_P2_VALUE = "Match 'Parameter 2' value"; - private static final String EFFECT_FX = "Effect"; /** Returns the opcode name for the current game variant. */ private static String getOpcodeName() { @@ -38,7 +37,7 @@ public Opcode337() { protected String makeEffectParamsEE(Datatype parent, ByteBuffer buffer, int offset, List list, boolean isVersion1) { list.add(new DecNumber(buffer, offset, 4, EFFECT_MATCH_P2_VALUE)); - list.add(new Bitmap(buffer, offset + 4, 4, EFFECT_FX, getEffectNames())); + list.add(new EffectBitmap(buffer, offset + 4, 4)); return null; } }